SQL Server之增删改操作
生活随笔
收集整理的這篇文章主要介紹了
SQL Server之增删改操作
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
-------添加約束、增刪改
1 use StudentDB2 2 go 3 --------創(chuàng)建學(xué)生表--------- 4 create table StudentInfo( 5 --studentId int primary key not null identity(1,1),---設(shè)置為主鍵,并自增長(zhǎng) 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 --默認(rèn)約束 22 alter table StudentInfo 23 add constraint DF_StudentInfo_studentMobile default '號(hào)碼不詳' for studentMobile 24 go 25 --檢查約束 26 alter table studentInfo 27 --check (len(studentMobile)=11):檢查電話號(hào)碼的長(zhǎng)度是否為11位 28 --check后的括號(hào)中的表達(dá)式可以是and或者or連接的多個(gè)簡(jiǎn)單邏輯表達(dá)式組成的復(fù)合型邏輯表達(dá)式 29 add constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11) 30 go 31 --======為了避免重復(fù)書(shū)寫(xiě)的麻煩,可使用如下方式添加約束 32 alter table studentInfo 33 add constraint DF_StudentInfo_studentMobile default '號(hào)碼不詳' 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關(guān)鍵字是可選的,可寫(xiě)可不寫(xiě) 39 go 40 ------------修改數(shù)據(jù)表----------- 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 --修改了數(shù)據(jù)類型、長(zhǎng)度和可空性 53 alter column classId varchar(20) not null 54 go 55 --修改列名 56 exec sp_rename 'studentInfo.classNo','classNum' 57 go 58 --------------刪除數(shù)據(jù)表-------------------- 59 --再刪除之前要先進(jìn)行判斷數(shù)據(jù)表是否存在,否則會(huì)發(fā)生錯(cuò)誤 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 --創(chuàng)建表 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 --全部列均插入數(shù)據(jù),提供所有列的數(shù)據(jù)值,并按照表中各列的順序列出這些值,故不必指定列名 insert into StudentInfo values( 1,'000001','大壯',124565689,10086,'01','001' ); go --按照順序提供了所有的列,并且相應(yīng)的給出了所有的值(推薦寫(xiě)法) insert into StudentInfo(studentId,studentNum,studentName,classNo,remark1,remark2) values (1,'000001','大壯',124565689,10086,'01','001'); go --也可以不按照表中的順序來(lái)插入數(shù)據(jù)(但是提供了所有的列) insert into StudentInfo(studentId,classNo,studentNum,remark1,studentName,remark2) values(1,2,'000002','02','二狗','003'); go --插入部分列值(插入值的個(gè)數(shù)小于列的個(gè)數(shù)),必須要聲明是哪一列 insert into StudentInfo(studentId,classNo,studentNum) values(3,03,'000003'); go ---------------更新數(shù)據(jù)-------------- --簡(jiǎn)單的update語(yǔ)句 update studentinfo set remark1=000; go --帶where條件的update語(yǔ)句 update studentinfo set studentnum=0101,studentname='王二麻子' where classno=222; go -----------刪除數(shù)據(jù)------------- --刪除整個(gè)表格 delete from studentinfo; go --刪除某一行 delete from studentinfo where studentid=001; go
轉(zhuǎn)載于:https://www.cnblogs.com/pang951189/p/7629993.html
總結(jié)
以上是生活随笔為你收集整理的SQL Server之增删改操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Uva 12093】Protectin
- 下一篇: Django内置的用户认证