日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

SQL基本语法总结(含SQL代码)

發(fā)布時(shí)間:2023/12/9 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL基本语法总结(含SQL代码) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)庫(kù)好比人的大腦的記憶系統(tǒng),沒(méi)有了數(shù)據(jù)庫(kù)就沒(méi)有了記憶系統(tǒng)。而SQL語(yǔ)言作為數(shù)據(jù)庫(kù)的王牌語(yǔ)言,肯定是重中之重。

最近在玩的無(wú)論是JAVA的JDBC,還是SSM的mybatis配置,都需要用到SQL代碼,所以下定決心重新整理了一遍,把之前敲過(guò)的代碼重新復(fù)習(xí)了一遍,在博客上做個(gè)備份,方便以后查閱。

下面是大三第一學(xué)期《數(shù)據(jù)庫(kù)原理》實(shí)驗(yàn)的目錄,沒(méi)必要把所有的實(shí)驗(yàn)報(bào)告都理出來(lái),我把SQL基礎(chǔ)的代碼放在這里,另外高級(jí)部分(存儲(chǔ)過(guò)程、觸發(fā)器、游標(biāo))額外拿了出來(lái),另外開(kāi)辟了若干文。


SQL基礎(chǔ)? ?本文(數(shù)據(jù)庫(kù)、表、數(shù)據(jù)的增刪改查、視圖相關(guān),以及所有實(shí)驗(yàn)報(bào)告源代碼)

游標(biāo) (類(lèi)似C++ 的 指針)

存儲(chǔ)過(guò)程(類(lèi)似 C++ 的自定義函數(shù))

觸發(fā)器 (類(lèi)似 自定義的陷阱,或者說(shuō)是監(jiān)聽(tīng)器,滿(mǎn)足某個(gè)條件了執(zhí)行某個(gè)方法)

用戶(hù)權(quán)限及權(quán)限管理 (類(lèi)似Windows的多用戶(hù)管理)

并發(fā)控制 (了解多個(gè)用戶(hù)同時(shí)對(duì)數(shù)據(jù)造成錯(cuò)誤的情況 和 解決方法)

數(shù)據(jù)恢復(fù)(當(dāng)數(shù)據(jù)庫(kù)數(shù)據(jù)丟失,相應(yīng)的解決方法)


創(chuàng)建數(shù)據(jù)庫(kù)

更新/刪除數(shù)據(jù)庫(kù)

創(chuàng)建表

更新/刪除表

?插入數(shù)據(jù)

單表查詢(xún)數(shù)據(jù)

多表查詢(xún)

復(fù)合查詢(xún)

視圖查詢(xún)

游標(biāo)

存儲(chǔ)過(guò)程

觸發(fā)器

用戶(hù)權(quán)限

并發(fā)控制

數(shù)據(jù)恢復(fù)


創(chuàng)建數(shù)據(jù)庫(kù)

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ù)庫(kù)

//添加文件 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ù)庫(kù) drop database zwz

創(chuàng)建表

use 學(xué)生管理系統(tǒng) create table 院系 (編號(hào) smallint primary key,名稱(chēng) 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 ('男','女')),職稱(chēng) char(6) check (職稱(chēng) in ('教授','副教授','講師','助教')),專(zhuān)業(yè) char(10) ) create table 課程 (課程編號(hào) char(8) primary key,課程名稱(chēng) char(20) not null,負(fù)責(zé)教師 char(8) references 教師(教師編號(hào)),學(xué)時(shí) smallint not null,課程性質(zhì) char(10) check (課程性質(zhì) in ('公共基礎(chǔ)','專(zhuān)業(yè)基礎(chǔ)','專(zhuān)業(yè)選修','任意選修')) ) create table 選課 (學(xué)號(hào) char(8) references 學(xué)生(學(xué)號(hào)),課程編號(hào) char(8) references 課程(課程編號(hào)),成績(jī) smallint default '' check (成績(jī) between 0 and 100)primary key(學(xué)號(hào),課程編號(hào)) )

更新/刪除表

use 學(xué)生管理系統(tǒng) alter table 學(xué)生 add 平均成績(jī) 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),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1101,'信息與電子系','戈素貞','5-211') insert into 院系(編號(hào),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1102,'經(jīng)濟(jì)與管理系','楊文兵','6-411') insert into 院系(編號(hào),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1103,'外語(yǔ)系','況細(xì)林','3-205') insert into 院系(編號(hào),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1104,'人文系','','') insert into 院系(編號(hào),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1105,'生物科學(xué)系','金自學(xué)','6-211') insert into 院系(編號(hào),名稱(chēng),負(fù)責(zé)人,辦公地點(diǎn)) values (1106,'工程技術(shù)系','胡國(guó)軍','5-311') insert into 院系(編號(hào),名稱(chēng)) values (1107,'公共基礎(chǔ)部') insert into 院系(編號(hào),名稱(chēng),負(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,'葉國(guó)燦','男','教授','經(jīng)濟(jì)管理') insert into 教師 values('100002',1105,'金子學(xué)','男','教授','環(huán)境管理') insert into 教師 values('100003',1106,'胡國(guó)軍','男','副教授','工程') insert into 教師 values('100004',1103,'況細(xì)林','男','副教授','英語(yǔ)') insert into 課程 values('1128','高等數(shù)學(xué)',null,6,'公共基礎(chǔ)') insert into 課程 values('1156','英語(yǔ)',100003,6,'公共基礎(chǔ)') insert into 課程 values('1137','管理學(xué)',100001,6,'專(zhuān)業(yè)基礎(chǔ)') insert into 課程 values('1124','數(shù)據(jù)庫(kù)原理',null,4,'專(zhuān)業(yè)基礎(chǔ)') insert into 課程 values('1136','離散數(shù)學(xué)',null,4,'專(zhuān)業(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)

單表查詢(xún)數(shù)據(jù)

select 課程名稱(chēng),學(xué)時(shí) from 課程select distinct 課程名稱(chēng),學(xué)時(shí) from 課程select * from 課程select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4;select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 and 課程名稱(chēng) = '數(shù)據(jù)庫(kù)原理';select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 or 學(xué)時(shí) = 6;select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) = 4 and 課程名稱(chēng) = '數(shù)據(jù)庫(kù)原理'or 學(xué)時(shí) = 6;select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) between 4 and 5;select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 學(xué)時(shí) not between 4 and 5;select 課程名稱(chēng), 學(xué)時(shí) from 課程 where 課程名稱(chēng) like '%學(xué)';select 課程名稱(chēng), 負(fù)責(zé)教師 from 課程 where 負(fù)責(zé)教師 is null;select 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 order by 成績(jī) desc;select 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 order by 成績(jī);select top 3 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 order by 成績(jī);select top 3 with ties 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 order by 成績(jī) desc;

多表查詢(xún)

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 姓名,職稱(chēng),課程名稱(chēng),課程性質(zhì) from 教師 inner join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱(chēng),課程名稱(chēng),課程性質(zhì) from 教師 left join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱(chēng),課程名稱(chēng),課程性質(zhì) from 教師 right join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師select 姓名,職稱(chēng),課程名稱(chēng),課程性質(zhì) from 教師 fulljoin 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師

復(fù)合查詢(xún)

1.使用IN運(yùn)算的簡(jiǎn)單嵌套查詢(xún)select 姓名,院系,職稱(chēng) from 教師 where 院系 in(select 編號(hào) from 院系where 名稱(chēng) = '經(jīng)濟(jì)與管理系' or 名稱(chēng) = '信息與電子系' )2.使用NOT IN運(yùn)算的簡(jiǎn)單嵌套查詢(xún)select 姓名,院系,職稱(chēng) from 教師 where 院系 not in(select 編號(hào) from 院系where 名稱(chēng) = '經(jīng)濟(jì)與管理系' or 名稱(chēng) = '信息與電子系' )3.使用關(guān)系運(yùn)算(如等于)的簡(jiǎn)單嵌套查詢(xún)select 姓名,院系,職稱(chēng) from 教師 where 院系 =(select 編號(hào) from 院系where 名稱(chēng) = '經(jīng)濟(jì)與管理系' )④ 使用ANY或SOME的簡(jiǎn)單嵌套查詢(xún)select 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 where 成績(jī) > ALL(select 成績(jī) from 選課where 學(xué)號(hào) = 2000012 )⑤ 使用ALL的簡(jiǎn)單嵌套查詢(xún) select 學(xué)號(hào),課程編號(hào),成績(jī) from 選課 where 成績(jī) > ANY(select 成績(jī) from 選課where 學(xué)號(hào) = 2000012 )⑥ 查詢(xún)?cè)合得Q(chēng)含“計(jì)算機(jī)”、職稱(chēng)為教授、所負(fù)責(zé)課程為必修課的老師姓名、 職稱(chēng)、課程名稱(chēng)和課程學(xué)時(shí)等信息(分別用嵌套查詢(xún)和連接查詢(xún)完成,分析各自的效率)。select 姓名 from 教師 full join 課程 on 教師.教師編號(hào) = 課程.負(fù)責(zé)教師 full join 院系 on 教師.院系 = 院系.編號(hào) where (專(zhuān)業(yè) = '專(zhuān)業(yè)基礎(chǔ)' or 專(zhuān)業(yè) = '公共基礎(chǔ)' )and 職稱(chēng) = '教授' and 院系.名稱(chēng) like '%計(jì)算機(jī)%'⑦ 設(shè)計(jì)兩個(gè)內(nèi)外層互相關(guān)的嵌套查詢(xún)。 select 姓名 from 教師 where 姓名 = ( select 姓名 from 教師 where 姓名 = '葉國(guó)燦' )⑧ 使用EXISTS的嵌套查詢(xún)。select * from 學(xué)生 where exists (select * from 選課where 選課.學(xué)號(hào) = 學(xué)生.學(xué)號(hào) )⑨ 使用NOT EXISTS 的嵌套查詢(xún)。 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(成績(jī))as 總成績(jī) from 選課select count(學(xué)號(hào))as 計(jì)數(shù) ,sum(成績(jī))as 求和 ,avg(成績(jī)) as 平均 from 選課(select * from 學(xué)生where 學(xué)號(hào) = (select 學(xué)號(hào) from 選課where 成績(jī) = (select max(成績(jī)) from 選課) ) ) intersect ( select * from 學(xué)生 where 學(xué)號(hào) = (select 學(xué)號(hào) from 選課where 成績(jī) > 70and 課程編號(hào) = (select 課程編號(hào) from 課程where 課程名稱(chēng) = '數(shù)據(jù)庫(kù)原理'))) ⑤ 查詢(xún)每個(gè)學(xué)生的平均成績(jī)。select 學(xué)生.姓名 ,avg(選課.成績(jī)) as 平均成績(jī) from 選課,學(xué)生group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名⑥ 查詢(xún)每個(gè)學(xué)生的所有課程的最高成績(jī)、最低成績(jī)、平均成績(jī)和所考課程的門(mén)數(shù)。select 學(xué)號(hào), max(成績(jī)) as 最高成績(jī),min(成績(jī)) as 最低成績(jī) , avg(成績(jī)) as 平均成績(jī),count(課程編號(hào)) as 所考門(mén)數(shù)from 選課 group by 選課.學(xué)號(hào)⑦ 查詢(xún)至少有10門(mén)必修課程考試成績(jī)的每個(gè)學(xué)生的平均成績(jī) select 學(xué)生.姓名 ,avg(選課.成績(jī)) as 平均成績(jī) from 選課,學(xué)生group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名having count(課程編號(hào)) > 10 ⑧ 設(shè)計(jì)2個(gè)使用COMPUTE…BY 和COMPUTE的查詢(xún) select 學(xué)號(hào),成績(jī) from 選課 group by 學(xué)號(hào) compute sum(成績(jī)) by 學(xué)號(hào) //---- select 學(xué)號(hào),成績(jī) from 選課 group by 學(xué)號(hào) compute sum(成績(jī)) //---- (select 學(xué)號(hào),成績(jī) from 選課group by 學(xué)號(hào)compute sum(成績(jī)) by 學(xué)號(hào) ) union (select 學(xué)號(hào),成績(jī) from 選課group by 學(xué)號(hào)compute sum(成績(jī)) ) //---- (select 學(xué)號(hào),成績(jī) from 選課group by 學(xué)號(hào)compute avg(成績(jī)) by 學(xué)號(hào) ) union (select 學(xué)號(hào),成績(jī) from 選課group by 學(xué)號(hào)compute avg(成績(jī)) ) //----

視圖查詢(xún)

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),姓名,性別,名稱(chēng)from 學(xué)生,院系where 學(xué)生.院系 = 院系.編號(hào)create view v5 asselect *from 學(xué)生where 院系 not in(select 編號(hào)from 院系where 名稱(chēng) = '外語(yǔ)系')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) = '不對(duì)'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),姓名,院系.名稱(chēng),課程.課程名稱(chēng),選課.成績(jī) 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.打開(kāi)游標(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+ '院系名稱(chēng): '+ @yuanxiname + '課程名稱(chēng): ' + @classname+ '成 績(jī): ' + @gradefetch next from youbiao into @numId, @nameId, @yuanxiname,@classname,@gradeend /*關(guān)閉游標(biāo) 釋放*/ close youbiao deallocate youbiao

存儲(chǔ)過(guò)程

//分割線(xiàn)1.1 create procedure aaa @s smallint, @e smallint asselect 學(xué)生.學(xué)號(hào),學(xué)生.姓名,院系.名稱(chēng) as 院系名稱(chēng),avg(選課.成績(jī)) as 平均成績(jī)from 學(xué)生,選課,院系where 學(xué)生.學(xué)號(hào) = 選課.學(xué)號(hào) and 學(xué)生.院系 = 院系.編號(hào)group by 學(xué)生.學(xué)號(hào),學(xué)生.姓名,院系.名稱(chēng)having avg(選課.成績(jī)) between @s and @e; goexec aaa 80,88;drop procedure aaa//分割線(xiàn)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 成績(jī) = @gradewhere 選課.學(xué)號(hào) = @sno and 選課.課程編號(hào) = @cno;select 選課.學(xué)號(hào),avg(選課.成績(jī)) as 平均成績(jī)from 選課where 選課.學(xué)號(hào) = @snogroup by 選課.學(xué)號(hào);end goexec new_data '2000278','1156',99;drop procedure new_data//分割線(xiàn)2.1create procedure new_find @sno char(8) asif(@sno is not null)beginselect 學(xué)生.學(xué)號(hào),學(xué)生.姓名,學(xué)生.性別,學(xué)生.生源,院系.名稱(chēng),學(xué)生.狀態(tài)from 學(xué)生,院系where 學(xué)生.院系 = 院系.編號(hào) and 學(xué)生.學(xué)號(hào) = @snoend goexec new_find '2000012'drop procedure new_find //分割線(xiàn)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,'專(zhuān)業(yè)基礎(chǔ)');end goexec new_class '1000','不知名的高級(jí)課程','100001',2,'專(zhuān)業(yè)基礎(chǔ)'drop procedure new_class//分割線(xiàn)3.1 select 學(xué)生.學(xué)號(hào),學(xué)生.姓名,學(xué)生.性別,學(xué)生.生源,院系.名稱(chēng),學(xué)生.狀態(tài) from 學(xué)生,院系 where 學(xué)生.院系 = 院系.編號(hào) and 學(xué)生.學(xué)號(hào) = '2000012'//分割線(xiàn)以下是課外部分 create procedure aaa asselect *from 課程; goexec aaa drop procedure aaa//分割線(xiàn)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//分割線(xiàn)create procedure aaaasselect * from 課程; goexec aaa;drop procedure aaa//分割線(xiàn)

觸發(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 成績(jī) <60 and 學(xué)號(hào)= @id)if @sum>=5beginraiserror('不及格達(dá)到5門(mén)',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','男','講師','視聽(tīng)說(shuō)')--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 = 職稱(chēng) from 教師 where 教師編號(hào) = @jiaoshiif @zc !='教授 'and @zc !='副教授 ' and @xingzhi = '專(zhuān)業(yè)基礎(chǔ)'begin raiserror('專(zhuān)業(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,'專(zhuān)業(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 = 職稱(chēng) from 教師 where 教師編號(hào) = @jiaoshiif @zc !='教授 'and @zc !='副教授 ' and @xingzhi = '專(zhuān)業(yè)基礎(chǔ)'begin raiserror('專(zhuān)業(yè)基礎(chǔ)課的教師必須為教授或副教授',11,11)rollback transactionend end update 課程 set 課程性質(zhì) = '專(zhuān)業(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 = 名稱(chēng) from inserted if @id is not null begin if @name not like '%系' and @name not like '%部'beginraiserror('院系名稱(chēng)輸入錯(cuò)誤',16,10)rollback transactionend end --drop trigger zwzdecfq05 insert into 院系 values('1109','吾展書(shū)院','馬云','88-888') insert into 院系 values('1111','萬(wàn)達(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í)')

用戶(hù)權(quán)限

--1.1 創(chuàng)建登入用戶(hù) 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)限為管理員的登入用戶(hù) 即將zwz01用戶(hù)加入 sysadmin 服務(wù)器角色exec sp_addsrvrolemember @loginame = 'zwz01',@rolename = 'sysadmin'--2.1 根據(jù)已有的注冊(cè)用戶(hù)建立幾個(gè)當(dāng)前數(shù)據(jù)庫(kù)的用戶(hù) exec sp_addrole @rolename = 'zwzdatabase1',@ownername = 'db_datareader' --上方代碼為 建立只讀數(shù)據(jù)庫(kù)角色 zwzdatabase1 exec sp_addrole @rolename = 'zwzdatabase2',@ownername = 'db_datareader' --上方代碼為 建立只讀數(shù)據(jù)庫(kù)角色 zwzdatabase2--exec sp_droprole 'zwzdatabase1' --exec sp_droprole 'zwzdatabase2'create user zwz01 --創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)zwz01 create user zwz02 --創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)zwz02--drop user zwz01 --drop user zwz02exec sp_addrolemember @rolename = 'zwzdatabase1',@membername = 'zwz01' --上方代碼為 把數(shù)據(jù)庫(kù)角色 zwzdatabase1 添加數(shù)據(jù)庫(kù)用戶(hù) zwz01 exec sp_addrolemember @rolename = 'zwzdatabase2',@membername = 'zwz02' --上方代碼為 把數(shù)據(jù)庫(kù)角色 zwzdatabase2 添加數(shù)據(jù)庫(kù)用戶(hù) 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 查看其他用戶(hù) exec sp_helprolemember @rolename = 'zwzdatabase1'--上方代碼為 查看數(shù)據(jù)庫(kù)角色 zwzdatabase1的用戶(hù)列表grant select,update,insert,delete on 學(xué)生 to zwz01 with grant option --上方代碼為 授予zwz01用戶(hù) 學(xué)生表的查詢(xún)?cè)鰟h改和轉(zhuǎn)授 權(quán)限--3.1 每個(gè)用戶(hù)有建立對(duì)像的權(quán)限,各自建立自已的對(duì)象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ù)庫(kù)用戶(hù)user01 create user user02 --創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)user02 create user user03 --創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)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' --建立能操作對(duì)象的數(shù)據(jù)庫(kù)角色user exec sp_addrolemember @rolename = 'user',@membername = 'user01' --把數(shù)據(jù)庫(kù)角色 user 添加數(shù)據(jù)庫(kù)用戶(hù)user01 即只能操作對(duì)象 exec sp_addrolemember @rolename = 'user',@membername = 'user02' --把數(shù)據(jù)庫(kù)角色 user 添加數(shù)據(jù)庫(kù)用戶(hù)user02 即只能操作對(duì)象 exec sp_addrolemember @rolename = 'user',@membername = 'user03' --把數(shù)據(jù)庫(kù)角色 user 添加數(shù)據(jù)庫(kù)用戶(hù)user03 即只能操作對(duì)象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 /******************************************************************************/丟失更新:查詢(xún)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查詢(xún)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/******************************************************************************/死鎖:查詢(xún)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查詢(xún)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)換后(相同順序法)查詢(xún)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查詢(xún)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ù)庫(kù) RESTORE DATABASE 學(xué)生管理系統(tǒng) --所被恢復(fù)的數(shù)據(jù)庫(kù)名稱(chēng)FROM disk = 'D:\temp\SQL Server\zwz.bak'--本地硬盤(pán)路徑--先把原來(lái)的數(shù)據(jù)庫(kù)刪除,在執(zhí)行該條語(yǔ)句

?

總結(jié)

以上是生活随笔為你收集整理的SQL基本语法总结(含SQL代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。