Code
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
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