mssql的T-SQL教程(从建登陆到建库、表和约束)
??本文引用自:http://www.cnblogs.com/seerlin/archive/2009/02/05/1384901.html
1?
??2--1,創建登錄,可以有兩種方式:
??3--(1)以SQL驗證的方式新建一個新的登錄:
??4????exec?sp_addlogin?'allnen','all123'--創建登錄,登錄名和密碼
??5
??6--(2)以windows驗證的方式將windows中的某個用戶授予登錄數據庫服務的權限:
??7????exec?sp_grantlogin?'PC-200901051757\allnen'
??8--或者
??9????--create?login?[PC-200901051757\allnen]?from?windows
?10
?11--如果要刪除某個登錄,可以用如下方式:
?12????--drop?login?[PC-200901051757\allnen]?
?13
?14--2,現在就可以用allnen這個帳號登錄,但這里先不要登錄,我們先給這個帳號授予創建數據庫和表的權限
?15
?16????exec?sp_grantdbaccess?@loginame='allnen',@name_in_db='allnenDbUser'--將登錄名映射到當前數據庫
?17
?18????--exec?sp_revokedbaccess?'allnen'--從當前數據庫中刪除對應的登錄用戶映射,也就是去除某個用戶訪問此數據庫的權限
?19
?20????grant?create?database,create?table?to?allnen--授予當前用戶創建數據庫的權限
?21--如果要去除某個用戶的權限,用revoke,如果要拒絕某個用戶的相關權限,用deny,revoke和deny的區別,請查閱其他相關文章
?22????--deny?create?database?to?allnenDbUser
?23
?24--3,現在就可以用allnen這個帳號登錄,然后創建數據庫和表了
?25--有時候如果要創建的數據庫已經存在?,那創建數據庫的時候就會出錯,
?26--所以我們一般在創建數據庫前都會先判斷要創建的數據庫是否已經存在,
?27--如果存在,就先將存在的數據庫刪除
?28????IF?EXISTS(SELECT?*?FROM?sysdatabases?WHERE?name='stuDB')
?29????????drop?database?stuDB
?30????GO
?31--當然,也可以用db_id(數據庫名)這個函數來判斷
?32
?33--創建數據庫的時候,有可能數據庫文件已經存在,或者文件目錄不存在等,所以,我們還要先在硬盤中創建對應的文件夾
?34
?35????exec?sp_configure?'show?advanced?options',?1--顯示高級選項,然后才可以配置xp_cmdshell
?36????reconfigure--從新應用配置,讓配置生效
?37????exec?sp_configure?'xp_cmdshell',1--啟用xp_cmdshell功能
?38????reconfigure
?39????exec?xp_cmdshell?'mkdir?D:\project\'--用xp_cmdshell創建目錄
?40????GO
?41--開始創建數據庫
?42????CREATE?DATABASE?stuDB
?43???????ON??PRIMARY??--默認就屬于PRIMARY主文件組,可省略
?44????(
?45?????NAME='stuDB_data',??--主數據文件的邏輯名
?46?????FILENAME='D:\project\stuDB_data.mdf',??--主數據文件的物理名
?47?????SIZE=5mb,??--主數據文件初始大小
?48?????MAXSIZE=100mb,??--主數據文件增長的最大值
?49?????FILEGROWTH=15%???--主數據文件的增長率
?50????)
?51????LOG?ON
?52????(
?53??????NAME='stuDB_log',
?54??????FILENAME='D:\project\stuDB_log.ldf',
?55??????SIZE=2mb,
?56??????FILEGROWTH=1MB
?57????)
?58????GO?
?59--如果要創建多個數據庫文件或者多個日志文件,則可以用這樣的方式
?60????CREATE??DATABASE??employees
?61??????ON?
?62???????(
?63???????/**//*-主數據文件的具體描述-*/
?64???????NAME?=?'employee1',?
?65???????FILENAME?=?'D:\project\employee1_Data.mdf'?,?
?66???????SIZE?=?10,?
?67???????FILEGROWTH?=?10%
?68??????),?
?69??????(
?70???????/**//*-次要數據文件的具體描述-*/
?71???????NAME?=?'employee2',?
?72???????FILENAME?=?'D:\project\employee2_Data.ndf'?,?
?73???????SIZE?=?20,?
?74???????MAXSIZE?=?100,?
?75???????FILEGROWTH?=?1
?76??????)?
?77?????LOG?ON?
?78??????(
?79???????/**//*-日志文件1的具體描述-*/
?80???????NAME?=?'employeelog1',?
?81???????FILENAME?=?'D:\project\employeelog1_Log.ldf'?,?
?82???????SIZE?=?10,?
?83???????FILEGROWTH?=?1
?84???????),?
?85??????(
?86???????/**//*-日志文件2的具體描述-*/
?87???????NAME?=?'employeelog2',?
?88???????FILENAME?=?'D:\project\employeelog2_Log.ldf'?,?
?89???????SIZE?=?10,?
?90???????MAXSIZE?=?50,?
?91???????FILEGROWTH?=?1
?92??????)
?93????GO?
?94
?95
?96--
?97
?98--4,創建完數據庫后,我們就可以開始創建表了
?99????USE?stuDB???--將當前數據庫設置為stuDB?
100????GO
101--同樣,在創建表的時候,表也有可能已經存在,所以我們要先將已存在的表刪除
102????IF?EXISTS(SELECT?*?FROM?sysobjects??WHERE?name='stuInfo')
103????????drop?table?stuInfo
104????GO
105????CREATE??TABLE??stuInfo????/**//*-創建學員信息表-*/
106????(
107?????stuName??VARCHAR(20)??NOT??NULL?,??--姓名,非空(必填)
108?????stuNo???CHAR(6)??NOT??NULL,???--學號,非空(必填)
109?????stuAge??INT??NOT??NULL,??--年齡,INT類型默認為4個字節
110?????stuID??NUMERIC(18,0),?????--身份證號
111?????stuSeat???SMALLINT??IDENTITY?(1,1),???--座位號,自動編號
112?????stuAddress???TEXT???--住址,允許為空,即可選輸入
113????)?
114????GO
115
116????IF?EXISTS(SELECT?*?FROM?sysobjects?WHERE?name='scoreInfo')
117????????DROP?TABLE?scoreInfo
118????GO
119????CREATE?TABLE?scoreInfo
120????(
121????????scoreInfoId?int?identity(1,1)?primary?key,
122????????score?int,
123????????stuNo?CHAR(6)
124????)
125????GO
126
127--5,創建完表之后,要做的事情就算給表建立約束
128????alter?table?stuInfo
129????????add?constraint?PK_stuNo?primary?key(stuNo)--主鍵約束
130????go
131????alter?table?stuInfo
132????????add?constraint?UQ_stuID?unique(stuID)--唯一約束
133????go
134????alter?table?stuInfo
135????????add?constraint?DF_stuAddress?default('地址不詳')?for?stuAddress--默認約束
136????go
137????alter?table?stuInfo
138????????add?constraint?CK_stuAge?check(stuAge?>20?and?stuAge<40)--檢查約束
139????go
140????alter?table?scoreInfo
141????????add?constraint?FK_stuNo?foreign?key(stuNo)?references?stuInfo(stuNo)?--外鍵約束
142????go
143???????
?1?--1,用管理員登錄
?2?--2,用管理員創建新數據庫
?3?--3,用管理員創建新登錄
?4?--4,授權新登錄名訪問新數據庫的權限
?5?use?master
?6?go
?7?exec?sp_configure?'show?advanced?options',1
?8?reconfigure
?9?exec?sp_configure?'xp_cmdshell',1
10?reconfigure
11?exec?xp_cmdshell?'mkdir?d:\Data\'
12?
13?
14?if?exists(select?*?from?sysdatabases?where?name='StuDb')
15?drop?database?StuDb
16?create?database?StuDb?on?primary?
17?(
18?????name='StuDb',
19?????filename='D:\Data\StuDb.mdf',
20?????size=5MB,
21?????filegrowth=15%
22?)
23?log?on
24?(
25?????name='StuDb_log',
26?????filename='D:\Data\StuDb.ldf',
27?????size=3MB,
28?????maxsize=10MB,
29?????filegrowth=10%
30?)
31?go
32?use?StuDb
33?go
34?if?exists(select?*from?sysobjects?where?name='StuInfo')
35?????drop?table?StuInfo
36?go
37?create?table?StuInfo(
38?????StuNo?int?identity(1,1),
39?????StuName?nvarchar(10)
40?)
41?go
42?if?exists(select?*from?sysobjects?where?name='ScoreInfo')
43?????drop?table?ScoreInfo
44?go
45?create?table?ScoreInfo(
46?????ScoreInfoId?int?identity(1,1),
47?????ExamScore?float,
48?????LabScore?float,
49?????StuNo?int
50?)
51?--刪除約束
52?alter?table?ScoreInfo
53?????drop?constraint?CK_ExamScore,CK_LabScore
54?go
55?alter?table?ScoreInfo
56?????alter?column?ExamScore?numeric(5,2)
57?alter?table?ScoreInfo
58?????alter?column?LabScore?numeric(5,2)
59?go
60?--約束
61?alter?table?StuInfo
62?????add?constraint?PK_StuNo?primary?key(StuNo)
63?alter?table?ScoreInfo
64?????add?constraint?CK_ExamScore?check(ExamScore>0?and?ExamScore<100)
65?alter?table?ScoreInfo
66?????add?constraint?CK_LabScore?check(LabScore>0?and?LabScore<100)
67?alter?table?ScoreInfo
68?????add?constraint?FK_StuNo?foreign?key(StuNo)?references?StuInfo(StuNo)
69?go
70?
71?--授權windows用戶訪問數據庫
72?
73?????exec?sp_grantlogin?'lab-04\administrator'--即將過期的方式
74?????create?login?[lab-04\administrator]?from?windows----推薦方式
75?
76?drop?login?[lab-04\administrator]--刪除登錄
77?
78?create?login?t0811?with?password='t0811'--創建新sql登錄
79?
80?--創建新數據庫用戶,以前用sp_grantdbaccess,以后用
81?use?StuDb
82?go
83?create?user?t0811InStuDb?for?login?t0811
84?--授權訪問表
85?grant?select,delete,update,insert?on?StuInfo?to?t0811InStuDb
86?--取消權限
87?revoke?delete?on?StuInfo?to?t0811InStuDb
88?--將t0811這個登錄加入到sysadmin這個服務器級別角色中
89?--exec?sp_addsrvrolemember?'t0811','sysadmin'
90?
91?--將t0811InStuDb這個數據庫用戶加入到db_owner這個數據庫級別角色中
92?exec?sp_addrolemember?'t0811InStuDb','db_owner'
93?--拒絕某個用戶的某個權限
94?deny?delete?on?StuInfo?to?t0811InStuDb
95?
96??
轉載于:https://www.cnblogs.com/johnsmith/archive/2011/09/05/2167504.html
總結
以上是生活随笔為你收集整理的mssql的T-SQL教程(从建登陆到建库、表和约束)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ2230 Watchcow——欧拉
- 下一篇: 可配置循环左右滚动例子