数据库练习题8--用户管理习题

实验目的
掌握用户的管理、数据库的备份与恢复;
用户管理:
请使用T-SQL 语句实现进行以下操作:
1.创建SQL Server身份验证的登录账户。登录名为:SQL_User1,密码为:123456

create login SQL_User1 with password='123456'

2.SQL_User1登录账户成为students数据库中的用户,并且用户名同登录名。

  use stu
go
create user SQL_User1

3.为用户SQL_User1授予Student表的查询权。

grant select on student to SQL_User1

4.为用户SQL_User1授予SC表的查询和插入权。

grant select,insert on sc to SQL_User1

5.收回用户SQL_User1对Student表的查询权。

revoke select on student to SQL_User1

6.授予SQL_User1具有创建数据库表和视图的权限。

grant create table,create view to SQL_User1

7.将SQL_User1登录名添加到sysadmin角色中。

exec sp_addsrvrolemember 'SQL_User1' ,'sysadmin'

8.删除SQL_User2用户。

use stu
go
create user SQL_User2
drop user SQL_User2

9.删除SQL_User2登录账户。

create login SQL_User2 with password='123456'
drop login SQL_User2
*10. P301 10
grant delete,insert on course to u1
revoke delete on course to u1
deny update on course to u1
grant create table to u1
revoke create table to u1

SQL 练习:
有一个学生数据库STUDENT设计如下,数据库中包括三张表:
学生(学号,姓名,年龄,性别,系部)
课程(课程号,课程名,学分)
选修(学号,课程号,成绩)
1.创建一个数据库STUDENT。

 CREATE DATABASE student

ON
(
 NAME=students_dat,
 FILENAME='D:\test\students_dat.mdf',
 SIZE=6MB,
 FILEGROWTH=1MB
)

LOG ON
(
 NAME=students_log,
 FILENAME='D:\test\students_log.ldf',
 SIZE=2MB,
 FILEGROWTH=10%
)        

2.创建数据表:学生,其中学号为主键,姓名非空,系部非空,年龄在18~25岁之间,年龄的类型为整型,其余属性的数据类型为字符型。

 create table student
(
  Sno     char(10)    primary key,
  Sname   char(6)     NOT NULL,
  Ssex    char(2)    ,
  Sage    int        check(Sage>=18 and Sage<=25),
  Sdept   char(8) 
)          

3.给学生表插入一行数据(‘S201901’,‘王燕’,20, ‘女’,‘计算机系’)

 insert into student(Sno,Sname,Ssex,Sage,Sdept)
VALUES ('S201901','王燕','女','20','计算机系');
select * from student

4.给课程表插入一行数据(‘C001’,‘数据库应用’,3)

 insert into course (Cno,Cname,Ccredit)
values ('C001','数据库应用','3')
select * from course

5.查询课程表中所有课程信息。

 select * from course

6.查询课程编号为‘C001’的课程的最高分、最低分、平均分。

 select MAX(grade),MIN (grade),AVG (grade)
from sc
where Cno='C001'

7.查询同时选修“C001”号课程和“C003”号课程的学生的学号。

 select sno from sc where Cno='C001' and Cno='C003'

8.查询所选课程的平均成绩大于“王燕”的平均成绩的学生学号、姓名及平均成绩。

 select s.Sno,Sname,AVG(grade)
from student s join sc on s.Sno=sc.Sno
group by s.Sno,Sname
having AVG (grade)>(
select AVG(grade)
from student s join sc on s.Sno=sc.Sno
where sname = '王燕'
)

9.更新所有计算机系学生的信息,将年龄增加1岁。

 update student
set Sage= Sage+1
where Sdept='计算机系'
select * from student

10.创建选课综合信息的视图V1,视图的属性列包括学号、姓名、课程号、成绩。

 create view V1 (学号,姓名,课程号,成绩)
as
select s.Sno 学号,Sname 姓名,SC.Cno 课程号,Grade 成绩
from student s join sc on s.Sno = sc.Sno
join course on sc.Cno=course.Cno
select * from V1

11.删除表选修中的所有信息。

Drop table sc

猜你喜欢

转载自blog.csdn.net/ssdssa/article/details/109107651