alter 语句

--基本语法:select [字段列表] from [表名] :查询出该表的所有记录
select ClsId, clsName, cl sTeacher, cl sNumber
f rom
TbClass
--只查询若干字段
select Cl sName, clsTeacher from
TbClass
一一
如果就是想查看所有字段的,可以使用1*'代替字段列表
select * from TbClass
--[]的作用:避免字段名与关键字的冲突,但是绝对不要使用关键字当
--字段名或者表名
SELECT [cl sName],[clsTeacher] FROM [ TbClass ]

 --修改表结构和追加约束--

1.删除一-列
  alter  table TbStudent drop  column stuPhone
  --2.添加一一列
  alter table TbStudent add column stuPhone一-

3。修改字段的数据类型
  alter   table TbStudent alter column stuGender nchar (1 )
  --4.添加主键约束
  alter   table TbStudent add constraint PK TbStudent stuId primary key (stuId)
  --5.添加唯一性约束
  alter table TbStudent add constraint UK TbStudent stuName unique (s tuName)--6.添加check约束
 -- 6.添加check约束
  al ter  table  TbStudent add  constraint CK TbStudent stuAgecheck (stuAge>=18 and   stuAge<=35)
  --7.添加非空约束,实际上就是对列的数据类型修改
  alte r  table  TbStudent alter column  stuPhone char (11 )  not nul 1
  --8.添加外键约束
  alter  table  TbStudent add  constraint  FK TbStudent stuClassIdforeign key (stuClassId)  refe rences  TbClass (clsId)
  --9.外键的级联删除/更新
  语法:  on delete  [no  action | cascade ]
  on update [no action | cascade]
  alter  table  TbStudent add  constraint  FK TbStudent stuClassId
  foreign key (stuClassId)  references  TbClass (clsId)  O]n delete cascade
--10.删除约束
  alter table TbStudent drop constraint FK TbStudent stuClassId
  --11.一条语句删除多条约束
  alter table TbStudent drop constraint FK TbStudent stuClassId, CK TbStuden
  --12.一条语句添加多个约束
  alter table TbStudent add
  constraint  FK_ TbStudent_ stuClassId foreign key (stuClassId)  re ferences  Tb

constraint  FK_ TbStudent_ stuClassId foreign key (stuClassId)  re ferences  Tb

猜你喜欢

转载自blog.csdn.net/cc1179877179/article/details/81262276