SQL Server数据库表格操作
表格的创建
代码操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | use StudentTwo
go
if exists( select * from sysobjects where name = 'nunber' )
drop table number
go
create table XueShengTable(
StudentId int identity(1000,1),
StudentName varchar (20) not null ,
Gender char (2) not null ,
)
注意: 列后面不加 not null ,加 null 是当前列可为空,加 not null 是当前列不可为空
|
界面操作
点击数据库 --> 点开使用的数据看 --> 右键击表 --> 新建表
增删改查
增
解释:
-- 插入、增加学生
-- insert into 表名(列名) values(值)
-- 向哪个表中插入那一列对应值是什么
-- 注意: 列名之间使用逗号隔开,值和列名一一对应,类型也得匹配
写法1:
1 | insert into XueShengTable(StudentName,Gender,Binrthday,Age,Phone,StudentAddress,ClassId,StudentCard) values ( '迪迦' , '男' , '2000-09-18' ,14, '15377300008' , '河南省南阳市邓州市' ,2341,12345678910987)
|
写法2:
-- 也可以将列名省略
1 | insert into XueShengTable values ( '凹凸曼' , '男' , '2000-09-18' ,12345678910987,23, '15377300008' , '河南省南阳市邓州市' ,1)
|
删
-- 语法
delete from 表名 where 条件
方法1delete:
-- 把学号1001数据删除
delete from XueShengTable where StudentId = 1001
-- 第二种删除方案 truncate:
truncate table XueShengTable -- 删除整个表格
-- 小提示: delete删除的时候比truncate删除的更快 -- delete删除的时候 不允许这条记录被外键引用,可以先把外键的关系删除掉,再进行删除这条数据 -- truncate 要求删除的表不能有外键的约束
改
-- 语法:
updaste 表名 set 列名 = 值, 列名 = 值 where 条件
--修改学号为1000学生的姓名改为李白,出生年月
1 | update XueShengTable set StudentName = '李白' ,Binrthday= '1975-01-01' where StudentId=1000
|
查
-- 查询所有信息 类似于数组遍历
1 | select * from XueShengTable
|
-- 查询具体列的数据查询姓名这一列 select 列名, from 表名 select StudentName,Gender from XueShengTable
-- 条件查询 : 查询年龄等于23的 select 列名, 列名 from 表名 where Age = 23
1 | select StudentName from XueShengTable where Age = 23
|
-- 查询满足条件的所有列
1 | select * from XueShengTable where Age <= 23
|
--逻辑运算符号 and 并且关系 相当于&&,or或者条件 相当于||;
1 | select * from XueShengTable where Age>14 and Age<23
|