码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server之增删改操作

时间:2017-10-05 21:25:57      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:写法   server   tab   add   ram   exist   重复   insert   --   

-------添加约束、增删改

技术分享
 1 use StudentDB2
 2 go
 3 --------创建学生表---------
 4 create table StudentInfo(
 5 --studentId int primary key not null identity(1,1),---设置为主键,并自增长
 6 studentId int not null identity(1,1),
 7 studentNum varchar(16) not null,
 8 studentName nvarchar(16) not null,
 9 studentMobile int null,
10 classNo int null
11 )
12 --------添加约束----------
13 --主键约束
14 alter table StudentInfo
15     add constraint PK_StudentInfo_StudentId primary key (studentId)
16 go
17 --唯一约束
18 alter table StudentInfo
19     add constraint UQ_StudentInfo_studentNum unique (studentNum)
20 go
21 --默认约束
22 alter table StudentInfo
23     add constraint DF_StudentInfo_studentMobile default 号码不详 for studentMobile
24 go
25 --检查约束
26 alter table studentInfo
27 --check (len(studentMobile)=11):检查电话号码的长度是否为11位
28 --check后的括号中的表达式可以是and或者or连接的多个简单逻辑表达式组成的复合型逻辑表达式
29     add constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)
30 go
31 --======为了避免重复书写的麻烦,可使用如下方式添加约束
32 alter table studentInfo
33 add constraint DF_StudentInfo_studentMobile default 号码不详 for studentMobile,
34     constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)
35 go
36 --删除约束
37 alter table studentInfo
38     drop constraint DF_StudentInfo_studentMobile  --constraint关键字是可选的,可写可不写
39 go
40 ------------修改数据表-----------
41 --添加列
42 alter table StudentInfo
43 add remark1 varchar(20) null,
44     remark2 varchar(20) null
45 go
46 --删除列
47 alter table StudentInfo
48 drop column ramark1
49 go
50 --------修改列
51 alter table classNo
52 --修改了数据类型、长度和可空性
53 alter column classId varchar(20) not null
54 go
55 --修改列名
56 exec sp_rename studentInfo.classNo,classNum
57 go
58 --------------删除数据表--------------------
59 --再删除之前要先进行判断数据表是否存在,否则会发生错误
60 --判断方法1
61 if exists (select * from sys.sysobjects where [name]=studentinfo)
62 drop table StudentInfo
63 go
64 --判断方法2
65 if OBJECT_ID(studentinfo) is not null
66 drop table studentinfo
67 go
View Code

-------外键约束

 1 Use StudentDB2
 2 go
 3 --创建表
 4 create table Score(
 5 studentId int not null identity(1,1),
 6 score int 
 7 )
 8 --添加外键约束
 9 alter table score
10     add constraint FK_score_studentinfo_stuId foreign key (studentId) references studentinfo(studentId)
11 go

--------插入、更新、删除

Use StudentDB2
go
--全部列均插入数据,提供所有列的数据值,并按照表中各列的顺序列出这些值,故不必指定列名
insert into StudentInfo values(
1,‘000001‘,‘大壮‘,124565689,10086,‘01‘,‘001‘
);
go
--按照顺序提供了所有的列,并且相应的给出了所有的值(推荐写法)
insert into StudentInfo(studentId,studentNum,studentName,classNo,remark1,remark2)
values (1,‘000001‘,‘大壮‘,124565689,10086,‘01‘,‘001‘);
go 
--也可以不按照表中的顺序来插入数据(但是提供了所有的列)
insert into StudentInfo(studentId,classNo,studentNum,remark1,studentName,remark2)
values(1,2,‘000002‘,‘02‘,‘二狗‘,‘003‘);
go
--插入部分列值(插入值的个数小于列的个数),必须要声明是哪一列
insert into StudentInfo(studentId,classNo,studentNum)
values(3,03,‘000003‘);
go
---------------更新数据--------------
--简单的update语句
update studentinfo set remark1=000;
go
--带where条件的update语句
update studentinfo set studentnum=0101,studentname=‘王二麻子‘ where classno=222;
go
-----------删除数据-------------
--删除整个表格
delete from studentinfo;
go
--删除某一行
delete from studentinfo where studentid=001;
go

  

SQL Server之增删改操作

标签:写法   server   tab   add   ram   exist   重复   insert   --   

原文地址:http://www.cnblogs.com/pang951189/p/7629993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!