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

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104
