SQL基本语法总结(含SQL代码)
數(shù)據(jù)庫好比人的大腦的記憶系統(tǒng),沒有了數(shù)據(jù)庫就沒有了記憶系統(tǒng)。而SQL語言作為數(shù)據(jù)庫的王牌語言,肯定是重中之重。
最近在玩的無論是JAVA的JDBC,還是SSM的mybatis配置,都需要用到SQL代碼,所以下定決心重新整理了一遍,把之前敲過的代碼重新復(fù)習(xí)了一遍,在博客上做個(gè)備份,方便以后查閱。
下面是大三第一學(xué)期《數(shù)據(jù)庫原理》實(shí)驗(yàn)的目錄,沒必要把所有的實(shí)驗(yàn)報(bào)告都理出來,我把SQL基礎(chǔ)的代碼放在這里,另外高級(jí)部分(存儲(chǔ)過程、觸發(fā)器、游標(biāo))額外拿了出來,另外開辟了若干文。
SQL基礎(chǔ)? ?本文(數(shù)據(jù)庫、表、數(shù)據(jù)的增刪改查、視圖相關(guān),以及所有實(shí)驗(yàn)報(bào)告源代碼)
游標(biāo) (類似C++ 的 指針)
存儲(chǔ)過程(類似 C++ 的自定義函數(shù))
觸發(fā)器 (類似 自定義的陷阱,或者說是監(jiān)聽器,滿足某個(gè)條件了執(zhí)行某個(gè)方法)
用戶權(quán)限及權(quán)限管理 (類似Windows的多用戶管理)
并發(fā)控制 (了解多個(gè)用戶同時(shí)對數(shù)據(jù)造成錯(cuò)誤的情況 和 解決方法)
數(shù)據(jù)恢復(fù)(當(dāng)數(shù)據(jù)庫數(shù)據(jù)丟失,相應(yīng)的解決方法)
創(chuàng)建數(shù)據(jù)庫
更新/刪除數(shù)據(jù)庫
創(chuàng)建表
更新/刪除表
?插入數(shù)據(jù)
單表查詢數(shù)據(jù)
多表查詢
復(fù)合查詢
視圖查詢
游標(biāo)
存儲(chǔ)過程
觸發(fā)器
用戶權(quán)限
并發(fā)控制
數(shù)據(jù)恢復(fù)
創(chuàng)建數(shù)據(jù)庫
create database 學(xué)生管理系統(tǒng) on (name = stu,filename = 'D:\temp\database\stu.mdf',size = 10,maxsize = 50,filegrowth = 5% ) log on (name = stu_log,filename = 'D:\temp\database\stu.ldf',size = 5,filegrowth = 5%,maxsize = 25 )更新/刪除數(shù)據(jù)庫
//添加文件 stu_data2.mdf alter database 學(xué)生管理系統(tǒng) add file (name = stu_data2,filename = 'd:\temp\database\stu_data2.mdf',size = 5,maxsize = 25,filegrowth = 5 )//修改文件大小為10 alter database 學(xué)生管理系統(tǒng) modify file (name = stu_data2,filename = 'd:\temp\database\stu_data2.mdf',size = 10 )//更新邏輯名 alter database 學(xué)生管理系統(tǒng) modify file (name = stu_data2,newname = stu_data3,filename = 'D:\temp\database\student_data2.mdf' )//更新物理地址 alter database 學(xué)生管理系統(tǒng) modify file (name = stu_data3,filename = 'D:\temp\database\student_data2.mdf' )//刪除新加的文件 alter database 學(xué)生管理系統(tǒng) remove file stu_data3//刪除整個(gè)數(shù)據(jù)庫 drop database zwz創(chuàng)建表
use 學(xué)生管理系統(tǒng) create table 院系 (編號(hào) smallint primary key,名稱 char(20) not null unique,負(fù)責(zé)人 char(10),辦公地點(diǎn) char(20) ) create table 學(xué)生 (學(xué)號(hào) char(8) primary key,院系 smallint references 院系(編號(hào)),姓名 char(10) not null,性別 char(2) check (性別 in ('男','女')),生源 char(6),狀態(tài) char(4) check (狀態(tài) in ('正常','留級(jí)','休學(xué)','退學(xué)')) ) create table 教師 (教師編號(hào) char(8) primary key,院系 smallint references 院系(編號(hào)),姓名 char(10) not null,性別 char(2) check (性別 in ('男','女')),職稱 char(6) check (職稱 in ('教授','副教授','講師','助教')),專業(yè) char(10) ) create table 課程 (課程編號(hào) char(8) primary key,課程名稱 char(20) not null,負(fù)責(zé)教師 char(8) references 教師(教師編號(hào)),學(xué)時(shí) smallint not null,課程性質(zhì) char(10) check (課程性質(zhì) in ('公共基礎(chǔ)','專業(yè)基礎(chǔ)','專業(yè)選修','任意選修')) ) create table 選課 (學(xué)號(hào) char(8) references 學(xué)生(學(xué)號(hào)),課程編號(hào) char(8) references 課程(課程編號(hào)),成績 smallint default '' check (成績 between 0 and 100)primary key(學(xué)號(hào),課程編號(hào)) )更新/刪除表
use 學(xué)生管理系統(tǒng) alter table 學(xué)生 add 平均成績 smallint default ''alter table 課程 add check (學(xué)時(shí) % 8 = 0)alter table 院系 alter column 負(fù)責(zé)人 varchar(30) not nullalter table 教師 add 工資 decimal(5,2)drop table 選課 --刪除表?插入數(shù)據(jù)
use 學(xué)生管理系統(tǒng) insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1101,'信息與電子系','戈素貞','5-211') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1102,'經(jīng)濟(jì)與管理系','楊文兵','6-411') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1103,'外語系','況細(xì)林','3-205') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1104,'人文系','','') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1105,'生物科學(xué)系','金自學(xué)','6-211') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1106,'工程技術(shù)系','胡國軍','5-311') insert into 院系(編號(hào),名稱) values (1107,'公共基礎(chǔ)部') insert into 院系(編號(hào),名稱,負(fù)責(zé)人,辦公地點(diǎn)) values (1108,'網(wǎng)絡(luò)技術(shù)部',default,default) insert into 學(xué)生 values('2000012',1101,'王林','男','浙江','正常') insert into 學(xué)生 values('2000113',1101,'張大民','男','浙江','正常') insert into 學(xué)生 values('2000256',1102,'顧芳','女','浙江','留級(jí)') insert into 學(xué)生 values('2000278',1103,'姜凡','男','浙江','正常') insert into 學(xué)生 values('2000014',1104,'葛波','女','浙江','正常') insert into 教師 values('100001',1102,'葉國燦','男','教授','經(jīng)濟(jì)管理') insert into 教師 values('100002',1105,'金子學(xué)','男','教授','環(huán)境管理') insert into 教師 values('100003',1106,'胡國軍','男','副教授','工程') insert into 教師 values('100004',1103,'況細(xì)林','男','副教授','英語') insert into 課程 values('1128','高等數(shù)學(xué)',null,6,'公共基礎(chǔ)') insert into 課程 values('1156','英語',100003,6,'公共基礎(chǔ)') insert into 課程 values('1137','管理學(xué)',100001,6,'專業(yè)基礎(chǔ)') insert into 課程 values('1124','數(shù)據(jù)庫原理',null,4,'專業(yè)基礎(chǔ)') insert into 課程 values('1136','離散數(shù)學(xué)',null,4,'專業(yè)基礎(chǔ)') insert into 選課 values('2000012','1156',80) insert into 選課 values('2000113','1156',89) insert into 選課 values('2000256','1156',93) insert into 選課 values('2000014','1156',88) insert into 選課 values('2000256','1137',77)單表查詢數(shù)據(jù)
select 課程名稱,學(xué)時(shí) from 課程select distinct 課程名稱,學(xué)時(shí) from 課程select * from 課程select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4;select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 and 課程名稱 = '數(shù)據(jù)庫原理';select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 or 學(xué)時(shí) = 6;select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 and 課程名稱 = '數(shù)據(jù)庫原理'or 學(xué)時(shí) = 6;select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) between 4 and 5;select 課程名稱, 學(xué)時(shí) from 課程 where 學(xué)時(shí) not between 4 and 5;select 課程名稱, 學(xué)時(shí) from 課程 where 課程名稱 like '%學(xué)';select 課程名稱, 負(fù)責(zé)教師 from 課程 where 負(fù)責(zé)教師 is null;select 學(xué)號(hào),課程編號(hào),成績 from 選課 order by 成績 desc;select 學(xué)號(hào),課程編號(hào),成績 from 選課 order by 成績;select top 3 學(xué)號(hào),課程編號(hào),成績 from 選課 order by 成績;select top 3 with ties 學(xué)號(hào),課程編號(hào),成績 from 選課 order by 成績 desc;多表查詢
select * from 學(xué)生,院系 where 學(xué)生.院系 = 院系.編號(hào);select * from 學(xué)生,院系 where 學(xué)生.院系 = 院系.編號(hào) and 性別 = '男';select * from 學(xué)生,院系 where 學(xué)生.院系 = 院系.編號(hào) and 性別 = '男' and 學(xué)號(hào)='2000012';select * from 選課 cross join 學(xué)生select * from 選課 cross join 學(xué)生 where 選課.學(xué)號(hào) = 學(xué)生.學(xué)號(hào)select 姓名,職稱,課程名稱,課程性質(zhì) from 教師 inner join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱,課程名稱,課程性質(zhì) from 教師 left join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱,課程名稱,課程性質(zhì) from 教師 right join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱,課程名稱,課程性質(zhì) from 教師 fulljoin 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師復(fù)合查詢
1.使用IN運(yùn)算的簡單嵌套查詢select 姓名,院系,職稱 from 教師 where 院系 in(select 編號(hào) from 院系where 名稱 = '經(jīng)濟(jì)與管理系' or 名稱 = '信息與電子系' )2.使用NOT IN運(yùn)算的簡單嵌套查詢select 姓名,院系,職稱 from 教師 where 院系 not in(select 編號(hào) from 院系where 名稱 = '經(jīng)濟(jì)與管理系' or 名稱 = '信息與電子系' )3.使用關(guān)系運(yùn)算(如等于)的簡單嵌套查詢select 姓名,院系,職稱 from 教師 where 院系 =(select 編號(hào) from 院系where 名稱 = '經(jīng)濟(jì)與管理系' )④ 使用ANY或SOME的簡單嵌套查詢select 學(xué)號(hào),課程編號(hào),成績 from 選課 where 成績 > ALL(select 成績 from 選課where 學(xué)號(hào) = 2000012 )⑤ 使用ALL的簡單嵌套查詢 select 學(xué)號(hào),課程編號(hào),成績 from 選課 where 成績 > ANY(select 成績 from 選課where 學(xué)號(hào) = 2000012 )⑥ 查詢院系名稱含“計(jì)算機(jī)”、職稱為教授、所負(fù)責(zé)課程為必修課的老師姓名、 職稱、課程名稱和課程學(xué)時(shí)等信息(分別用嵌套查詢和連接查詢完成,分析各自的效率)。select 姓名 from 教師 full join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師 full join 院系 on 教師.院系 = 院系.編號(hào) where (專業(yè) = '專業(yè)基礎(chǔ)' or 專業(yè) = '公共基礎(chǔ)' )and 職稱 = '教授' and 院系.名稱 like '%計(jì)算機(jī)%'⑦ 設(shè)計(jì)兩個(gè)內(nèi)外層互相關(guān)的嵌套查詢。 select 姓名 from 教師 where 姓名 = ( select 姓名 from 教師 where 姓名 = '葉國燦' )⑧ 使用EXISTS的嵌套查詢。select * from 學(xué)生 where exists (select * from 選課where 選課.學(xué)號(hào) = 學(xué)生.學(xué)號(hào) )⑨ 使用NOT EXISTS 的嵌套查詢。 select * from 學(xué)生 where not exists (select * from 選課where 選課.學(xué)號(hào) = 學(xué)生.學(xué)號(hào) )select count(學(xué)號(hào))as 參與選課人數(shù) from 選課select sum(成績)as 總成績 from 選課select count(學(xué)號(hào))as 計(jì)數(shù) ,sum(成績)as 求和 ,avg(成績) as 平均 from 選課(select * from 學(xué)生where 學(xué)號(hào) = (select 學(xué)號(hào) from 選課where 成績 = (select max(成績) from 選課) ) ) intersect ( select * from 學(xué)生 where 學(xué)號(hào) = (select 學(xué)號(hào) from 選課where 成績 > 70and 課程編號(hào) = (select 課程編號(hào) from 課程where 課程名稱 = '數(shù)據(jù)庫原理'))) ⑤ 查詢每個(gè)學(xué)生的平均成績。select 學(xué)生.姓名 ,avg(選課.成績) as 平均成績 from 選課,學(xué)生group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名⑥ 查詢每個(gè)學(xué)生的所有課程的最高成績、最低成績、平均成績和所考課程的門數(shù)。select 學(xué)號(hào), max(成績) as 最高成績,min(成績) as 最低成績 , avg(成績) as 平均成績,count(課程編號(hào)) as 所考門數(shù)from 選課 group by 選課.學(xué)號(hào)⑦ 查詢至少有10門必修課程考試成績的每個(gè)學(xué)生的平均成績 select 學(xué)生.姓名 ,avg(選課.成績) as 平均成績 from 選課,學(xué)生group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名having count(課程編號(hào)) > 10 ⑧ 設(shè)計(jì)2個(gè)使用COMPUTE…BY 和COMPUTE的查詢 select 學(xué)號(hào),成績 from 選課 group by 學(xué)號(hào) compute sum(成績) by 學(xué)號(hào) //---- select 學(xué)號(hào),成績 from 選課 group by 學(xué)號(hào) compute sum(成績) //---- (select 學(xué)號(hào),成績 from 選課group by 學(xué)號(hào)compute sum(成績) by 學(xué)號(hào) ) union (select 學(xué)號(hào),成績 from 選課group by 學(xué)號(hào)compute sum(成績) ) //---- (select 學(xué)號(hào),成績 from 選課group by 學(xué)號(hào)compute avg(成績) by 學(xué)號(hào) ) union (select 學(xué)號(hào),成績 from 選課group by 學(xué)號(hào)compute avg(成績) ) //----視圖查詢
create view v1 asselect 學(xué)號(hào),姓名,性別 from 學(xué)生create view v2 asselect * from 學(xué)生where 性別 = '男'create view v3 asselect 學(xué)號(hào),姓名,性別 from 學(xué)生where 性別 = '男'create view v4 asselect 學(xué)號(hào),姓名,性別,名稱from 學(xué)生,院系where 學(xué)生.院系 = 院系.編號(hào)create view v5 asselect *from 學(xué)生where 院系 not in(select 編號(hào)from 院系where 名稱 = '外語系')create view v6 asselect b.學(xué)號(hào),b.姓名,b.院系from 學(xué)生 as a , 學(xué)生 as bwhere a.姓名 = '王林' and a.院系 = b.院系select * from v1select v1.學(xué)號(hào),v1.姓名 from v1 join v2 on v1.學(xué)號(hào) = v2.學(xué)號(hào)insert into v6 values('200000','張三',1102)update 學(xué)生set 學(xué)生.狀態(tài) = '不對'where 學(xué)生.姓名 = '王林'update 學(xué)生set 學(xué)生.狀態(tài) = '留級(jí)'where 學(xué)生.姓名 = '王林'delete 學(xué)生where 學(xué)生.姓名 = '張三'游標(biāo)
/*1.聲明游標(biāo)*/ declare youbiao cursor scroll for select 學(xué)生.學(xué)號(hào),姓名,院系.名稱,課程.課程名稱,選課.成績 from 學(xué)生,院系,選課,課程 where 學(xué)生.院系 = 院系.編號(hào) and 選課.學(xué)號(hào) = 學(xué)生.學(xué)號(hào) and 課程.課程編號(hào) = 選課.課程編號(hào) order by 學(xué)生.學(xué)號(hào);/*2.打開游標(biāo)*/ open youbiao/*3.聲明游標(biāo)提取數(shù)據(jù)所要存放的變量*/declare @numId char(8),@nameId char(10),@yuanxiname char(20) ,@classname char(20),@grade char(20)/*4.定位游標(biāo)到哪一行*/ fetch First from youbiao into @numId, @nameId, @yuanxiname,@classname,@grade while @@fetch_status = 0beginprint char(23)print '學(xué) 號(hào): ' + @numId +'姓 名: ' + @nameId+ '院系名稱: '+ @yuanxiname + '課程名稱: ' + @classname+ '成 績: ' + @gradefetch next from youbiao into @numId, @nameId, @yuanxiname,@classname,@gradeend /*關(guān)閉游標(biāo) 釋放*/ close youbiao deallocate youbiao存儲(chǔ)過程
//分割線1.1 create procedure aaa @s smallint, @e smallint asselect 學(xué)生.學(xué)號(hào),學(xué)生.姓名,院系.名稱 as 院系名稱,avg(選課.成績) as 平均成績from 學(xué)生,選課,院系where 學(xué)生.學(xué)號(hào) = 選課.學(xué)號(hào) and 學(xué)生.院系 = 院系.編號(hào)group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名,院系.名稱having avg(選課.成績) between @s and @e; goexec aaa 80,88;drop procedure aaa//分割線1.2create procedure new_data @sno char(8), @cno char(8), @grade smallint asif(@sno is not null and @cno is not null)beginupdate 選課 set 成績 = @gradewhere 選課.學(xué)號(hào) = @sno and 選課.課程編號(hào) = @cno;select 選課.學(xué)號(hào),avg(選課.成績) as 平均成績from 選課where 選課.學(xué)號(hào) = @snogroup by 選課.學(xué)號(hào);end goexec new_data '2000278','1156',99;drop procedure new_data//分割線2.1create procedure new_find @sno char(8) asif(@sno is not null)beginselect 學(xué)生.學(xué)號(hào),學(xué)生.姓名,學(xué)生.性別,學(xué)生.生源,院系.名稱,學(xué)生.狀態(tài)from 學(xué)生,院系where 學(xué)生.院系 = 院系.編號(hào) and 學(xué)生.學(xué)號(hào) = @snoend goexec new_find '2000012'drop procedure new_find //分割線2.2 create procedure new_class @cno char(8), @cname char(20), @ctea char(8), @time smallint, @xz char(10) asif(@cno is not null)begininsert 課程 values('1000','不知名的高級(jí)課程','100001',2,'專業(yè)基礎(chǔ)');end goexec new_class '1000','不知名的高級(jí)課程','100001',2,'專業(yè)基礎(chǔ)'drop procedure new_class//分割線3.1 select 學(xué)生.學(xué)號(hào),學(xué)生.姓名,學(xué)生.性別,學(xué)生.生源,院系.名稱,學(xué)生.狀態(tài) from 學(xué)生,院系 where 學(xué)生.院系 = 院系.編號(hào) and 學(xué)生.學(xué)號(hào) = '2000012'//分割線以下是課外部分 create procedure aaa asselect *from 課程; goexec aaa drop procedure aaa//分割線create procedure aaa @a int, @b int asset @a = @a + @b;return @a; go declare @ans int;exec @ans = aaa 1,2;print @ans; drop procedure aaa//分割線create procedure aaaasselect * from 課程; goexec aaa;drop procedure aaa//分割線觸發(fā)器
---------------------------------------------------------------------------------------------------------------------- --第一題create trigger zwzdecfq on 選課 for insert as declare @sum int, @id char(10) select @id = 學(xué)號(hào) from inserted if @id is not null beginselect @sum=(select count(*)from 選課 where 成績 <60 and 學(xué)號(hào)= @id)if @sum>=5beginraiserror('不及格達(dá)到5門',16,10)endend--drop trigger zwzdecfqinsert into 選課 values('2000012','1124','50')insert into 選課 values('2000012','1128','50')insert into 選課 values('2000012','1136','50')insert into 選課 values('2000012','1137','50')insert into 選課 values('2000012','1188','50')insert into 選課 values('2000012','1189','50')------------------------------------------------------------------------------------------------------------------------第一部分 插入--2.1.1先插入一行教師數(shù)據(jù) insert into 教師 values('100005',1102,'zzz','男','講師','視聽說')--2.1.2創(chuàng)建觸發(fā)器 create trigger zwzdecfq02 on 課程 for insert as declare @id char(8),@xingzhi char(10),@zc char(6),@jiaoshi char(8) select @id = 課程編號(hào),@jiaoshi = 負(fù)責(zé)教師,@xingzhi = 課程性質(zhì) from insertedif @id is not null beginselect @zc = 職稱 from 教師 where 教師編號(hào) = @jiaoshiif @zc !='教授 'and @zc !='副教授 ' and @xingzhi = '專業(yè)基礎(chǔ)'begin raiserror('專業(yè)基礎(chǔ)課的教師必須為教授或副教授',11,11)rollback transactionend end--2.1.3 嘗試插入數(shù)據(jù)insert into 課程 values('1191','未知課程04','100005',4,'任意選修') insert into 課程 values('1190','未知課程03','100005',4,'專業(yè)基礎(chǔ)')--2.1.4備用代碼 delete 課程 where 課程編號(hào) = '1191' delete 課程 where 課程編號(hào) = '1190' drop trigger zwzdecfq02 ---------------------------------------------------------------------------------------------------------------------- 第二部分 更新create trigger zwzdecfq03 on 課程 for update as declare @id char(8),@xingzhi char(10),@zc char(6),@jiaoshi char(8) select @id = 課程編號(hào),@jiaoshi = 負(fù)責(zé)教師,@xingzhi = 課程性質(zhì) from insertedif @id is not null beginselect @zc = 職稱 from 教師 where 教師編號(hào) = @jiaoshiif @zc !='教授 'and @zc !='副教授 ' and @xingzhi = '專業(yè)基礎(chǔ)'begin raiserror('專業(yè)基礎(chǔ)課的教師必須為教授或副教授',11,11)rollback transactionend end update 課程 set 課程性質(zhì) = '專業(yè)基礎(chǔ)' where 負(fù)責(zé)教師 = '100005'----------------------------------------------------------------------------------------------------------------------第3.1題:create trigger zwzdecfq04 on 學(xué)生 for insert as declare @id char(8),@home char(6) select @id = 學(xué)號(hào) , @home = 生源 from inserted if @id is not null begin if @home != '浙江'beginraiserror('您不是浙江人',16,10)rollback transactionend end --drop trigger zwzdecfq04 insert into 學(xué)生 values('2000013',1102,'張三','男','河南','正常')---------------------------------------------------------------------------------------------------------------------- 3.2題create trigger zwzdecfq05 on 院系 for insert as declare @id smallint,@name char(20) select @id = 編號(hào) , @name = 名稱 from inserted if @id is not null begin if @name not like '%系' and @name not like '%部'beginraiserror('院系名稱輸入錯(cuò)誤',16,10)rollback transactionend end --drop trigger zwzdecfq05 insert into 院系 values('1109','吾展書院','馬云','88-888') insert into 院系 values('1111','萬達(dá)系','王健林','99-999') ---------------------------------------------------------------------------------------------------------------------- 3.3題 create trigger zwzdecfq07 on 學(xué)生 for insert as declare @id char(8),@zt char(4) select @id = 學(xué)號(hào) , @zt = 狀態(tài) from inserted if @id is not null begin if @zt != '正常'beginraiserror('您不正常',16,10)rollback transactionend end insert into 學(xué)生 values('2000112',1102,'王五','男','浙江','正常') insert into 學(xué)生 values('2000111',1102,'李四','男','浙江','留級(jí)')用戶權(quán)限
--1.1 創(chuàng)建登入用戶 exec sp_addlogin @loginame = 'zwz01',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' --服務(wù)器主體 zwz01 exec sp_addlogin @loginame = 'zwz02',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' --服務(wù)器主體 zwz02 exec sp_addlogin @loginame = 'zwz03',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' --服務(wù)器主體 zwz03 --exec sp_droplogin @loginame = 'zwz01' --exec sp_droplogin @loginame = 'zwz02' --exec sp_droplogin @loginame = 'zwz03'--1.3 建立一個(gè)權(quán)限為管理員的登入用戶 即將zwz01用戶加入 sysadmin 服務(wù)器角色exec sp_addsrvrolemember @loginame = 'zwz01',@rolename = 'sysadmin'--2.1 根據(jù)已有的注冊用戶建立幾個(gè)當(dāng)前數(shù)據(jù)庫的用戶 exec sp_addrole @rolename = 'zwzdatabase1',@ownername = 'db_datareader' --上方代碼為 建立只讀數(shù)據(jù)庫角色 zwzdatabase1 exec sp_addrole @rolename = 'zwzdatabase2',@ownername = 'db_datareader' --上方代碼為 建立只讀數(shù)據(jù)庫角色 zwzdatabase2--exec sp_droprole 'zwzdatabase1' --exec sp_droprole 'zwzdatabase2'create user zwz01 --創(chuàng)建數(shù)據(jù)庫用戶zwz01 create user zwz02 --創(chuàng)建數(shù)據(jù)庫用戶zwz02--drop user zwz01 --drop user zwz02exec sp_addrolemember @rolename = 'zwzdatabase1',@membername = 'zwz01' --上方代碼為 把數(shù)據(jù)庫角色 zwzdatabase1 添加數(shù)據(jù)庫用戶 zwz01 exec sp_addrolemember @rolename = 'zwzdatabase2',@membername = 'zwz02' --上方代碼為 把數(shù)據(jù)庫角色 zwzdatabase2 添加數(shù)據(jù)庫用戶 zwz02--exec sp_droprolemember @rolename = 'zwzdatabase1',@membername = 'zwz01' --exec sp_droprolemember @rolename = 'zwzdatabase2',@membername = 'zwz02'--2.4 授權(quán)zwz01可以創(chuàng)建表 grant create table to zwz01--2.5 查看其他用戶 exec sp_helprolemember @rolename = 'zwzdatabase1'--上方代碼為 查看數(shù)據(jù)庫角色 zwzdatabase1的用戶列表grant select,update,insert,delete on 學(xué)生 to zwz01 with grant option --上方代碼為 授予zwz01用戶 學(xué)生表的查詢增刪改和轉(zhuǎn)授 權(quán)限--3.1 每個(gè)用戶有建立對像的權(quán)限,各自建立自已的對象exec sp_addlogin @loginame = 'user01',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' exec sp_addlogin @loginame = 'user02',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' exec sp_addlogin @loginame = 'user03',@passwd = '123456',@defdb = '學(xué)生管理系統(tǒng)' create user user01 --創(chuàng)建數(shù)據(jù)庫用戶user01 create user user02 --創(chuàng)建數(shù)據(jù)庫用戶user02 create user user03 --創(chuàng)建數(shù)據(jù)庫用戶user03grant select,update,insert,delete on user01table to user01 with grant option grant select,update,insert,delete on user02table to user02 with grant option grant select,update,insert,delete on user03table to user03 with grant optionexec sp_addrole @rolename = 'user',@ownername = 'db_ddladmin' --建立能操作對象的數(shù)據(jù)庫角色user exec sp_addrolemember @rolename = 'user',@membername = 'user01' --把數(shù)據(jù)庫角色 user 添加數(shù)據(jù)庫用戶user01 即只能操作對象 exec sp_addrolemember @rolename = 'user',@membername = 'user02' --把數(shù)據(jù)庫角色 user 添加數(shù)據(jù)庫用戶user02 即只能操作對象 exec sp_addrolemember @rolename = 'user',@membername = 'user03' --把數(shù)據(jù)庫角色 user 添加數(shù)據(jù)庫用戶user03 即只能操作對象create table user01table (aa int primary key,bb int,cc int ) create table user02table (aa int primary key,bb int,cc int ) create table user03table (aa int primary key,bb int,cc int ) --user01 insert user01table values(1,2,3)select * from user01tablegrant select,update,insert,delete on user01table to user02 with grant option--user02 select * from user01tableselect * from user02tableselect * from user03table--user01 revoke grant option for select on user01table from user02 cascade并發(fā)控制
臟讀窗口1: begin transaction zwz1 update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128' waitfor delay '00:00:20' rollback tran zwz1 select * from 課程 where 課程編號(hào) = '1128' 窗口2: select * from 課程 with (nolock) where 課程編號(hào) = '1128' waitfor delay '00:00:20' select * from 課程 where 課程編號(hào) = '1128'封鎖:set transaction isolation level read committed /******************************************************************************/不可重復(fù)讀:窗口1:begin transaction zwz2 select 學(xué)時(shí) from 課程 where 課程編號(hào) = '1128' waitfor delay '00:00:05' select 學(xué)時(shí) from 課程 where 課程編號(hào) = '1128' commit transaction zwz2窗口2:begin transaction zwz3 update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128' commit transaction zwz3封鎖:set transaction isolation level repeatable read /******************************************************************************/丟失更新:查詢1:begin transaction zwz4 update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128' waitfor delay '00:00:05' select 學(xué)時(shí) from 課程 where 課程編號(hào) = '1128' commit transaction zwz4查詢2:begin transaction zwz5 update 課程 set 學(xué)時(shí) = 10 where 課程編號(hào) = '1128' waitfor delay '00:00:05' select 學(xué)時(shí) from 課程 where 課程編號(hào) = '1128' commit transaction zwz5封鎖:set transaction isolation level repeatable read/******************************************************************************/死鎖:查詢1:begin transaction zwz6update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1136' waitfor delay '00:00:05' update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128'commit transaction zwz6查詢2: begin transaction zwz7update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128' waitfor delay '00:00:05' update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1136'commit transaction zwz7調(diào)換后(相同順序法)查詢1:begin transaction zwz6update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1136' waitfor delay '00:00:05' update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128'commit transaction zwz6查詢2:begin transaction zwz7update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1136' waitfor delay '00:00:05' update 課程 set 學(xué)時(shí) = 8 where 課程編號(hào) = '1128'commit transaction zwz7數(shù)據(jù)恢復(fù)
--完整備份(包括數(shù)據(jù)data和日志log) Backup Database 學(xué)生管理系統(tǒng) To disk='D:\temp\SQL Server\zwz.bak'--文件夾一定要存在--差異備份(包含數(shù)據(jù)data和日志log) Backup Database 學(xué)生管理系統(tǒng)To disk='D:\temp\SQL Server\zwz.bak' with Differential--還原數(shù)據(jù)庫 RESTORE DATABASE 學(xué)生管理系統(tǒng) --所被恢復(fù)的數(shù)據(jù)庫名稱FROM disk = 'D:\temp\SQL Server\zwz.bak'--本地硬盤路徑--先把原來的數(shù)據(jù)庫刪除,在執(zhí)行該條語句?
總結(jié)
以上是生活随笔為你收集整理的SQL基本语法总结(含SQL代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Swift5.1 语言参考(十) 语法汇
- 下一篇: SQL server增删改查