標籤:寫法 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 null11 )12 --------添加約束----------13 --主鍵約束14 alter table StudentInfo15 add constraint PK_StudentInfo_StudentId primary key (studentId)16 go17 --唯一約束18 alter table StudentInfo19 add constraint UQ_StudentInfo_studentNum unique (studentNum)20 go21 --預設約束22 alter table StudentInfo23 add constraint DF_StudentInfo_studentMobile default ‘號碼不詳‘ for studentMobile24 go25 --檢查約束26 alter table studentInfo27 --check (len(studentMobile)=11):檢查電話號碼的長度是否為11位28 --check後的括弧中的運算式可以是and或者or串連的多個簡單邏輯運算式組成的複合型邏輯運算式29 add constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)30 go31 --======為了避免重複書寫的麻煩,可使用如下方式添加約束32 alter table studentInfo33 add constraint DF_StudentInfo_studentMobile default ‘號碼不詳‘ for studentMobile,34 constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)35 go36 --刪除約束37 alter table studentInfo38 drop constraint DF_StudentInfo_studentMobile --constraint關鍵字是可選的,可寫可不寫39 go40 ------------修改資料表-----------41 --添加列42 alter table StudentInfo43 add remark1 varchar(20) null,44 remark2 varchar(20) null45 go46 --刪除列47 alter table StudentInfo48 drop column ramark149 go50 --------修改列51 alter table classNo52 --修改了資料類型、長度和可空性53 alter column classId varchar(20) not null54 go55 --修改列名56 exec sp_rename ‘studentInfo.classNo‘,‘classNum‘57 go58 --------------刪除資料表--------------------59 --再刪除之前要先進行判斷資料表是否存在,否則會發生錯誤60 --判斷方法161 if exists (select * from sys.sysobjects where [name]=‘studentinfo‘)62 drop table StudentInfo63 go64 --判斷方法265 if OBJECT_ID(‘studentinfo‘) is not null66 drop table studentinfo67 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 score10 add constraint FK_score_studentinfo_stuId foreign key (studentId) references studentinfo(studentId)11 go
--------插入、更新、刪除
Use StudentDB2go--全部列均插入資料,提供所有列的資料值,並按照表中各列的順序列出這些值,故不必指定列名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之增刪改操作