1. 程式人生 > >SQL Server之增刪改操作

SQL Server之增刪改操作

values 數據表 所有 var rop varchar con foreign entity

-------添加約束、增刪改

技術分享
 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之增刪改操作