2008-3-20
1
这是今天做的数据库,感觉都生孰了.技术这东西啊,一定得常用啊.
2
3
4
5
use master
6
7
go
8
/*判断stuDB是否存在,如果存在,则删除,然后再创建数据库*/
9
if exists(select * from sysdatabases where name = 'stuDB')
10
11
drop database stuDB
12
13
create database stuDB
14
15
on primary --主文件组,可省略
16
(
17
name = 'stuDB_data',--主数据库文件的逻辑名
18
filename = 'F:\Sql\stuDB\stuDB_data.mdf',--主数据库文件的物理名
19
size = 5mb,
20
maxsize = 100mb,
21
filegrowth = 15%
22
)
23
log on(
24
25
name = 'stuDB_log',
26
filename = 'F:\Sql\stuDB\stuDB_log.ldf',
27
size = 2mb,
28
filegrowth = 1mb
29
)
30
go
31
32
33
use stuDB
34
35
--创建表stuInfo
36
create table stuInfo
37
(
38
stuName varchar(20) not null, --姓名
39
stuNo char(6) not null,--学号
40
stuAge int not null,--年龄
41
stuID numeric(18,0),--身份证号
42
stuSeat smallint identity(1,1),--座位号,自动标识列
43
stuAddress text --住址
44
45
)
46
go
47
--创建表stuMarks
48
create table stuMarks
49
(
50
ExamNo char(7) not null,--考试号
51
stuNo char(6) not null,--学号
52
labExam int not null--机试成绩
53
)
54
go
55
56
--添加主键约束
57
alter table stuInfo
58
add constraint PK_stuNo primary key(stuNo)
59
60
--添加唯一约束
61
alter table stuInfo
62
add constraint UQ_stuID unique(stuID)
63
64
--添加默认约束(地址为"地址不祥")
65
alter table stuInfo
66
add constraint DF_stuAddress default('地址不祥')for stuAddress
67
68
--添加Check约束(年龄在15到40之间)
69
alter table stuInfo
70
add constraint CK_stuAge check(stuAge between 15 and 40)
71
72
--添加外键约束
73
alter table stuMarks
74
add constraint FK_stuNo
75
foreign key (stuNo) references stuInfo(stuNo)
76
77
--添加ckeck约束
78
alter table stuMarks
79
add constraint CK_labExam check(labExam between 0 and 100)
80
81
go
82
83
--创建登录帐户
84
85
--创建Windows登录帐户(如果创建的域用户,则用'域名\用户名',如果是Window本地用户,则用'计算机名\用户名')
86
exec sp_grantlogin 'HP-Gzp\administrator'
87
88
--创建SQL登录帐户
89
exec sp_addlogin 'gzp','1234'
90
91
--创建数据库用户
92
93
exec sp_grantdbaccess 'HP-Gzp\administrator','gzp'
94
/*'HP-Gzp\administrator':为登录帐户,'gzp'为添加的数据库用户*/
95
96
exec sp_grantdbaccess
97
98
--向数据库用户授权
99
100
--grant 权限 [on 表名] to 数据库用户
101
102
grant select,insert on stuInfo to gzp
103
104
这是今天做的数据库,感觉都生孰了.技术这东西啊,一定得常用啊.2

3
4

5
use master 6

7
go 8
/*判断stuDB是否存在,如果存在,则删除,然后再创建数据库*/9
if exists(select * from sysdatabases where name = 'stuDB')10

11
drop database stuDB12

13
create database stuDB14

15
on primary --主文件组,可省略16
(17
name = 'stuDB_data',--主数据库文件的逻辑名 18
filename = 'F:\Sql\stuDB\stuDB_data.mdf',--主数据库文件的物理名19
size = 5mb,20
maxsize = 100mb,21
filegrowth = 15%22
)23
log on(24

25
name = 'stuDB_log',26
filename = 'F:\Sql\stuDB\stuDB_log.ldf',27
size = 2mb,28
filegrowth = 1mb29
)30
go31

32

33
use stuDB34

35
--创建表stuInfo36
create table stuInfo37
(38
stuName varchar(20) not null, --姓名39
stuNo char(6) not null,--学号40
stuAge int not null,--年龄41
stuID numeric(18,0),--身份证号42
stuSeat smallint identity(1,1),--座位号,自动标识列43
stuAddress text --住址44

45
)46
go47
--创建表stuMarks48
create table stuMarks49
(50
ExamNo char(7) not null,--考试号51
stuNo char(6) not null,--学号52
labExam int not null--机试成绩53
)54
go55

56
--添加主键约束57
alter table stuInfo58
add constraint PK_stuNo primary key(stuNo)59

60
--添加唯一约束61
alter table stuInfo62
add constraint UQ_stuID unique(stuID)63

64
--添加默认约束(地址为"地址不祥")65
alter table stuInfo66
add constraint DF_stuAddress default('地址不祥')for stuAddress67

68
--添加Check约束(年龄在15到40之间)69
alter table stuInfo70
add constraint CK_stuAge check(stuAge between 15 and 40)71

72
--添加外键约束73
alter table stuMarks74
add constraint FK_stuNo75
foreign key (stuNo) references stuInfo(stuNo)76

77
--添加ckeck约束78
alter table stuMarks79
add constraint CK_labExam check(labExam between 0 and 100)80

81
go82

83
--创建登录帐户84

85
--创建Windows登录帐户(如果创建的域用户,则用'域名\用户名',如果是Window本地用户,则用'计算机名\用户名')86
exec sp_grantlogin 'HP-Gzp\administrator'87

88
--创建SQL登录帐户89
exec sp_addlogin 'gzp','1234'90

91
--创建数据库用户92

93
exec sp_grantdbaccess 'HP-Gzp\administrator','gzp'94
/*'HP-Gzp\administrator':为登录帐户,'gzp'为添加的数据库用户*/95

96
exec sp_grantdbaccess 97

98
--向数据库用户授权99

100
--grant 权限 [on 表名] to 数据库用户101

102
grant select,insert on stuInfo to gzp103

104


浙公网安备 33010602011771号