CREATE TABLE course (
Cno char(4) NOT NULL,
Cname char(40) DEFAULT NULL,
Cpno char(4) DEFAULT NULL,
Ccredit smallint DEFAULT NULL,
PRIMARY KEY (Cno)
)
--
-- 转存表中的数据 'course'
--
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('1', '数据库', '5', 4);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('2', '数学', NULL, 2);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('3', '信息系统', '1', 4);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('4', '操作系统', '6', 3);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('5', '数据结构', '7', 4);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('6', '数据处理', NULL, 2);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('7', 'PASCAL语言', '6', 4);
INSERT INTO course (Cno, Cname, Cpno, Ccredit)
VALUES ('8', 'DB_DESIGN', '1', 2);
select * from course
--
-- 表的结构 'student'
--
CREATE TABLE student (
Sno char(9) NOT NULL,
Sname char(20) DEFAULT NULL ,
Ssex char(2) DEFAULT NULL,
Sage smallint DEFAULT NULL,
Sdept char(20) DEFAULT NULL,
PRIMARY KEY (Sno)
)
--
-- 转存表中的数据 'student'
--
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('151332087', 'Douzi', '男', 20, 'CS');
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('200215121', '李勇', '男', 20, 'CS');
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('200215122', '刘晨', '女', 19, 'CS');
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('200215123', '王敏', '女', 18, 'MA');
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('200215125', '张立', '男', 19, 'IS');
INSERT INTO student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('200215128', '陈冬', '男', 18, 'IS');
select * from student
--
-- 表的结构 'sc'
--
CREATE TABLE sc (
Sno char(9) NOT NULL DEFAULT '',
Cno char(4) NOT NULL DEFAULT '',
Grade smallint DEFAULT NULL,
PRIMARY KEY (Sno,Cno)
)
--
-- 转存表中的数据 'sc'
--
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('151332087', '1', 99);
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('200215121', '1', 92);
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('200215121', '2', 85);
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('200215121', '3', 88);
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('200215122', '2', 90);
INSERT INTO sc (Sno, Cno, Grade)
VALUES ('200215122', '3', 80);
select * from SC
--修改表属性
Alter Table Course
Add Constraint course_ibfk_1 Foreign key(Cpno) References Course(Cno);
Alter Table sc
Add Constraint sc_ibfk_1 Foreign key(Sno) References student(Sno);
Alter Table sc
Add Constraint sc_ibfk_2 Foreign key(Cno) References course(Cno);
--p143, 表4.4
create login Dou1 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u1 for login Dou1 with default_schema = abo
create login Dou3 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u2 for login Dou3 with default_schema = abo
create login Dou4 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u3 for login Dou4 with default_schema = abo
create login Dou5 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u4 for login Dou5 with default_schema = abo
create login Dou6 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u5 for login Dou6 with default_schema = abo
create login Dou7 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u6 for login Dou7 with default_schema = abo
create login Dou8 with password = '1234', default_database = Prac
--创建数据库用户(为上一条指令中 Prac 数据库创建用户)
create user u7 for login Dou8 with default_schema = abo
--当u1 赋予对表Student的查询权限
Grant Select
On Student
To u1
--当u2 赋予对表student的全部权限
Grant All privileges
On Student
To u2
Grant Update(Sno)
On Student
To U4
Grant Insert
On SC
To U5
with Grant option
Setuser 'U5'
Grant Insert
On SC
To U6
exec sp_helprotect
--收回权限
Revoke Select
On Student
From U1
Revoke All privileges
On Student
From u2
--查询权限
exec sp_helprotect