国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

MSSQL 簡單練習回顧

2024-07-21 02:50:35
字體:
供稿:網(wǎng)友
MSSQL 簡單練習回顧
這段時間,報了浦軟培訓的.NET,現(xiàn)在整理回顧下,算是個小小總結(jié)吧 為了便于操作,我沒有在多個數(shù)據(jù)庫間切換數(shù)據(jù)庫實例,以一個總的數(shù)據(jù)庫實例 test_demo為源進行的相關操作,代碼的注釋根據(jù)我的理解,并結(jié)合相關文獻進行的解說,如果哪位前輩覺得注釋的解說不妥,可給予糾正,謝謝。現(xiàn)在不廢話了,上代碼   1 ---練習  2 use master  3 /***********************************************  4   1、創(chuàng)建數(shù)據(jù)庫  5 ************************************************/  6 ---數(shù)據(jù)庫創(chuàng)建前的檢測  7 if exists(select * from sys.sysdatabases where name='test_demo')  8 drop database test_demo  --刪除已有的數(shù)據(jù)庫test_demo  9 create database test_demo  --開始創(chuàng)建新數(shù)據(jù)庫test_demo 10 on PRimary  --默認就屬于primary 主文件組,可省略 ,但必須有on 11 ( 12     name="test_demo",  --主數(shù)據(jù)文件的邏輯名 13     filename="C:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Data/test_demo.mdf", --主數(shù)據(jù)文件的物理名 14     size=10mb,  --主數(shù)據(jù)文件的初始大小 15     filegrowth=10% --主數(shù)據(jù)文件的增長率(可以為百分比,也可為實數(shù)) 16 ) 17 ) 18 log on --以下為日志文件描述,同上 19 ( 20     name="test_name", 21     filename="C:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Data/test_demo_log.ldf", 22     size=1mb, 23     filegrowth=10% 24 ) 25 --檢查數(shù)據(jù)庫是否創(chuàng)建成功 26 select * from sys.sysdatabases where name='test_demo' 27  28  29  30 /*********************************************** 31   2、創(chuàng)建表 32 ************************************************/ 33 use test_demo    34 create table stuInfo   --創(chuàng)建學生信息表 stuInfo 35 ( 36     stuName varchar(20) not null, 37     stuNo char(6) not null,  --學號 38     stuAge int not null, 39     stuId numeric(18,0) null, --身份證號,小數(shù)位為0 40     stuSeat smallint identity(1,1), --座位號,自動遞增     41     stuAddress text 42 ) 43 go 44 create table stuMarks  --創(chuàng)建學生成績表  45 ( 46     examNo char(7) not null, --考號 47     stuNo char(6) not null, --學號 48     writtenExam int not null, --筆記成績 49     labExam int not null --機試成績 50 ) 51 go 52  53 --檢測數(shù)據(jù)庫表stuinfo和stumarks的存在情況 54 use studb 55 select * from sys.sysobjects where name= 'stuinfo'  56 select * from sys.sysobjects where name = 'stumarks' 57  58  59  60 /*********************************************** 61   3、為表 stuInfo 和stuMarks添加約束 62 ************************************************/ 63 /* 64 語法: 65        alter  table tab_name 66        add constraint 約束名 約束類型 具體的約束說明    67 備注: 68      主鍵約束(Primary Key Constraint):要求主鍵列數(shù)據(jù)唯一,并且不允許為空 69      唯一約束(Unique Constraint):要求該列唯一,允許為空,但只能出現(xiàn)一個空值。 70      檢查約束(Check Constraint):某列取值范圍限制、格式限制等,如有關年齡的約束 71      默認約束(Default Constraint):某列的默認值,如我們的男性學員較多,性別默認為“男” 72      外鍵約束(Foreign Key Constraint):用于兩表間建立關系,需要指定引用主表的那列  73 總結(jié):主鍵約束和惟一月雖然都強調(diào)了數(shù)據(jù)的惟一性, 74       但主鍵約束強調(diào)的是數(shù)據(jù)主體,惟一約束則強調(diào)的是某一列 75 */ 76 alter table stuInfo 77 add constraint pk_stuNo primary key(stuNo) --為學生學號添加主鍵約束 78  79 alter table stuInfo 80 add constraint uq_stuId unique(stuId) --為學生身份證添加惟一約束 81  82 alter table stuInfo 83 add constraint df_stuAddress default('地址不詳') for stuAddress --為地址添加默認約束,如果地址不詳。默認為“地址不詳” 84  85 alter table stuInfo 86 add constraint ck_stuAge check(stuAge between 15 and 40) --為學生年齡添加年齡檢查約束,要求年齡在15-40 87  88 alter table stuMarks 89 add constraint fk_stuMarks foreign key(stuNo) references stuInfo(stuNo) --為學生成績添加外鍵約束(主表stuInfo和從表stuMarks建立關系,關鍵字段為stuNo) 90  91 exec sp_helpconstraint stuinfo  --查看當前表stuinf的約束情況 92 exec sp_helpconstraint stumarks --查看當前表stumarks的約束情況 93  94  95 /*********************************************** 96   4、插入相關數(shù)據(jù) 97 ************************************************/ 98 insert into stuinfo values('張三',001,25,420621198906254567,'湖北襄陽') 99 insert into stuinfo values('李四',002,25,420621198906121554,'上海')100 insert into stuinfo values('王五',003,34,420621198003166548,'北京')101 insert into stuinfo values('趙強',004,23,420621199105148756,'湖北武漢')102 insert into stuinfo values('錢海',005,22,420621199208154582,'江蘇蘇州')103 insert into stuinfo values('周國',006,30,420621198409265148,'江蘇南京')104 insert into stuinfo values('孫堅',007,33,420621198304261855,'')105 106 insert into stumarks values(001,1,50.6,70.9)107 insert into stumarks values(022,2,64.5,84.5)108 insert into stumarks values(031,3,46.6,45.9)109 insert into stumarks values(023,4,95.7,51.9)110 insert into stumarks values(043,5,52.5,84.9)111 insert into stumarks values(015,6,94.6,76.9)112 insert into stumarks values(006,7,86.5,84.6)113 114 --查詢數(shù)據(jù)插入情況115 select * from stuinfo116 select * from stuMarks117 118 /***********************************************119   5、為表stuInfo和stuMarks創(chuàng)建視圖120 ************************************************/121 /*122  視圖是一張?zhí)摂M表,可以方便不同用戶的查詢,提高數(shù)據(jù)的安全性,篩選特定的數(shù)據(jù)行。123     視圖的用途:124              篩選表中的行125              防止未經(jīng)許可的用戶訪問敏感數(shù)據(jù)126              降低數(shù)據(jù)庫的復雜程度127              將多個物理數(shù)據(jù)庫抽象為一個邏輯數(shù)據(jù)庫128 */129 if exists(select * from sys.sysobjects where name='view_stuInfo_stuMarks')130 drop view view_stuInfo_stuMarks131 go132 create view view_stuInfo_stuMarks 133 as134 select '姓名'=stuName, '學號'=stuInfo.stuNo,'考試號'= stuMarks.ExamNo,135    '筆記成績'=writtenExam,'機試成績'=labExam,136    '平均分'=(writtenExam +labExam)/2137 from stuInfo  left join stuMarks 138   on stuInfo.stuNo = stuMarks.stuNo139 go140 141 select * from view_stuInfo_stuMarks --使用視圖142 143 144 --使用相關的條件查詢145 --求出平均分數(shù)146 use test_demo147 --獲取每個學員的總成績148 select a.stuName as '學生', a.stuNo as '學號',b.ExamNo as '考號', b.writtenExam as '筆試成績',b.labExam as '機試成績',(b.writtenExam+b.labExam) as '總成績'149  from stuInfo as a inner join stuMarks as b on a.stuNo = b.stuNo150  order by '總成績' desc151 152 /*153   總結(jié):154      當使用group by條件語句進行篩選時,select項的字段中,除使用聚合函數(shù)進行統(tǒng)計的字段外,其他需要select出的字段名必須出現(xiàn)在group by分組條件中155 */156 select a.stuName as '學生', a.stuNo as '學號',b.ExamNo as '考號', b.writtenExam as '筆試成績',b.labExam as '機試成績',sum(b.writtenExam+b.labExam) as '總成績',(b.writtenExam+b.labExam)/2 as '平均成績'157  from stuInfo as a inner join stuMarks as b on a.stuNo = b.stuNo158 group by a.stuName,a.stuNo,b.ExamNo,b.writtenExam,b.labExam159  order by '總成績' desc160 161 162 --根據(jù)學員總的平均成績和每個學員的總成績,判斷是否及格,劃線為150分163 declare @Avg1 float164 --獲取當前學員總的平均成績165 select @Avg1=avg(writtenExam+labexam) from stuInfo as a, stuMarks as b 166   where a.stuNo=b.stuNo 167  168 select  a.stuName as '學生', a.stuNo as '學號',b.ExamNo as '考號', b.writtenExam as '筆試成績',b.labExam as '機試成績',(b.writtenExam+b.labExam) as '總成績', @avg1 as '及格分數(shù)',169 (170 case171   when (b.writtenExam+b.labExam)<@avg1 then '不及格'172         when (b.writtenExam+b.labExam)>=@avg1 then '及格'173 end174 ) as '及格情況'175 from stuInfo as a inner join stuMarks as b on a.stuNo = b.stuNo176 order by '總成績' desc177 178 179 declare @Avg float   --聲明變量@AVG用于存儲學員總的平均成績180 --獲取當前學員總的平均成績181 select @Avg=avg(writtenExam+labexam) from stuInfo as a, stuMarks as b 182   where a.stuNo=b.stuNo 183 184 select  a.stuName as '學生', a.stuNo as '學號',b.ExamNo as '考號', b.writtenExam as '筆試成績',b.labExam as '機試成績',sum(b.writtenExam+b.labExam) as '總成績', @avg as '及格分數(shù)',185 (186 case187   when (b.writtenExam+b.labExam)<@avg then '不及格'188         when (b.writtenExam+b.labExam)>=@avg then '及格'189 end190 ) as '及格情況'191 from stuInfo as a inner join stuMarks as b on a.stuNo = b.stuNo192 group by a.stuName,a.stuNo,b.ExamNo, b.writtenExam ,b.labExam 193 order by '總成績' desc194 195 /***********************************************196   6、觸發(fā)器 triggers197                     觸發(fā)器(trigger)是SQL server 提供給程序員和數(shù)據(jù)分析員來保證數(shù)據(jù)完整性的一種方法,它是與表事件相關的特殊的存儲過程,它的執(zhí)行不是由程序調(diào)用,也不是手工啟動,而是由事件來觸發(fā),比如當對一個表進行操作( insert,delete, update)時就會激活它執(zhí)行。觸發(fā)器經(jīng)常用于加強數(shù)據(jù)的完整性約束和業(yè)務規(guī)則等。 觸發(fā)器可以從 DBA_TRIGGERS ,USER_TRIGGERS 數(shù)據(jù)字典中查到。SQL3的觸發(fā)器是一個能由系統(tǒng)自動執(zhí)行對數(shù)據(jù)庫修改的語句。198 觸發(fā)器與存儲過程的唯一區(qū)別是觸發(fā)器不能執(zhí)行EXECUTE語句調(diào)用,而是在用戶執(zhí)行Transact-SQL語句時自動觸發(fā)執(zhí)行。199 200  說明:模擬銀行銀行存款事件,用戶的取款和存款操作對儲蓄用戶賬目相關信息的更新    201  練習步驟:202              01、創(chuàng)建bank表和transInfo表203                 02、創(chuàng)建觸發(fā)器204 ************************************************/205 use test_demo206 --創(chuàng)建表bank和 transInfo207 create table triggers_bank  --創(chuàng)建賬戶存款表bank208 (209   customerName nvarchar(255) not null,210         cardID varchar(255) not null unique,211         currentMoney money    212 )213 create table triggers_transInfo  --創(chuàng)建賬戶交易表transInfo,用戶記錄賬戶操作事件日志214 (215    transDate datetime not null,216             cardID varchar(255) not null,217          transType nvarchar(50) not null,218             transMoney money 219 )220 221 --向表中插入相關測試數(shù)據(jù)222 insert into triggers_bank values('張三','1001001',1000.000)223 insert into triggers_bank values('李四','1001002',1.000)224 insert into triggers_transinfo values(getDate(),'1001001','支取',100.00)225 --查詢數(shù)據(jù)是否插入成功226 select * f
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汽车| 江油市| 永康市| 同心县| 双城市| 甘孜| 苍溪县| 师宗县| 顺昌县| 台中市| 株洲县| 平遥县| 余干县| 保靖县| 云龙县| 固原市| 英山县| 鄂托克旗| 洞头县| 廊坊市| 石门县| 庆云县| 休宁县| 张家口市| 盐亭县| 祁连县| 汶上县| 惠东县| 广安市| 绥江县| 文水县| 色达县| 承德市| 永德县| 六枝特区| 互助| 芦溪县| 鹤庆县| 湟源县| 盐池县| 仁怀市|