数据库期末复习---SQL语言

本文详细介绍SQL语言的基础操作,包括表的创建与管理、数据查询、更新及权限设置等。通过具体示例,读者能快速掌握如何进行数据增删改查、创建视图以及管理数据库权限。

一、SQL语言主要组成部分

 cut3

二、DDL

说明:后续的所有操作针对create table 中建的三张表

①create table

student表:

Create Table Student 
(
    sno char(10) primary key(sno) ,
    sname varchar(20)  not null,
    sage smallint,
    ssex char(2),
    sdept char(10)
)

course表:

Create Table Course 
(
    cno char(10) primary key (cno) ,
    cname varchar(20) ,
    credit smallint 
)

sc表:

Create Table SC        
(
    sno char(10) not null ,
    cno char(10) not null ,
    grade smallint,
    primary key (sno, cno) 
)

②drop table

drop table student cascade

cascade:表示强制删除
restrict:有外码、视图时就不能删了

③alter table

例1:向student表增加“入学时间”列,其数据类型为日期型。

alter table student add s_extrance datetime

例2:将年龄的数据类型有字符型改为整型

alter table student alter column sage int

例3:增加课程名称必须取唯一值的约束条件 

alter table course add unique(cname)

例4:删除一列

alter table Student drop  column  guofeng
 

④create index

说明:在最经常查询的列上建立,并且一个表最多只能建立一个聚簇索引。
例1:unique表明此索引的每一个索引值只对应唯一的数据记录。

create unique index x on student(sno)

例2:cluster表明要建立的索引时聚簇索引。

create cluster index x on student(sno)

 

⑤drop index 后直接加索引名

 

三、DML

①select

           例1:查询第一个是‘刘’开头的学生学号

 select Sname from Student where Sname like '刘%'

说明:%是匹配任意字符串,_是匹配一个字符

例2:查询选了课的学生学号

select sno from sc group by sno

另外一种方法:

select distinct sno from sc


例3:列出具有两门(含)以上不及格的学生的学号、不及格的课目数。

Select  sno,count(sno) From SC
Where grade < 60
Group By sno
Having count(sno) >= 2


例4:查询全体学生的姓名及其出生年份

select sname,2009-Sage from Student


例5:查询考试成绩有不及格的学生的学号

select distinct sno from sc where grade < 60

说明:这里用了distinct,若一个学生有多门课不及格则只列出一次。

例6:查询年龄在20-30(包括20、30)岁的人

select sno from student where sage between 20 and 30


例7:查询CS、MA、IS系的学生姓名

select sname from student where sdept in ('CS','MA','IS')


例8:查询CS系年龄在20以下的学生姓名

select sname from student where sdept='CS' and sage < 20


例9:查询学生110119选修课程的总学分数

select sum(ccredit) from sc,course where sno='110119' and sc.cno=course.cno


例10:
查询与郭峰所在系相同的学生姓名

select sno,sname from student
where sdept in 
    (select sdept from student where sname='郭峰')


例11:查询选修了“算法”的学生学号
方法1:

select sno from student 
where sno in
(
    select sno from sc where cno in
                (select cno from course where cname='算法算法')
)


方法二:

select sno from sc,student,course
where student.sno=sc,sno and sc.cno=course.cno and course.cname='算法算法'



例12:查询每个学生超过他选修课程平均成绩的课程号

select sno,cno from sc x
where grade >= (select avg(grade) from sc y where y.sno=x.sno)


例13:查询选修了3门以上课程的学生学号

select sno from sc group by sno having count(*) > 3


例14:查询其他系中比CS系所有学生年龄都小的学生学号、年龄
方法一:

select sname,sage from student
where sage < all(select sage from student where sdept='CS')
  and sdept <> 'CS'

方法二:

select sname,sage from student 
where sage < (select min(sage) from student where sdept='CS')
   and sdept <> 'CS'

②insert

举例:insert into student(…) values('001','朱佳','21','f','C');

③update

举例:将学生110119的年龄改为22岁
update student set sage=22 where sno=’110119’

④delete

举例:删除学号为110119的记录
delete from student where sno=’110119’

四、视图

①概述

建立一个视图:

create view as
select sno,sname,sage
     from student where sdept='CS' with check option

 with check option:保证你只能对CS系操作,否则出错

之后的操作和对表的操作都一样

②视图的作用总结

使用视图,可以简化数据操作。
使用视图,基表中的数据就有了一定的安全性。
可以更清晰的表达查询。

五、DCL

①把查询student表和修改学生学号的权限授权给用户guofeng

grant update(sno),select
on table student
to guofeng(public是所有人)


②把用户对SC表的INSERT权限收回

revoke insert
on table sc
from guofeng cascade(把开始给别人的也要回来)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值