sql中“delete from 表名”表示_数据库学习之SQL语言

v2-ae5e2829e9812cb62d10b5f48ea9b00f_1440w.jpg?source=172ae18b

今年,小编选修了数据库这门课,原本以为能在课堂上学到许多数据库相关知识,结果没想到,老师给人感觉这么水,但是考试又是这么难,所以很是伤心,而且课堂的内容往往让人容易忽视,所以小编想写下这篇文章,来帮助大家开始学习

数据库。这其中包含了我学习数据库时的经验和收获,希望能给大家带来一些启发。

闲话少说,我们开始进入正题吧。

首先,我想先跟大家来讲讲SQL这门关系数据库标准语言。

  • SQL是一门高度非过程化的语言(不像c语言这样,要求必须过程清晰,即SQL只需要你告诉它你要做什么,至于怎么做,早已对你透明了)
  • SQL是一种面向集合的操作方式。
  • SQL中9个关键动词:create,drop,alter,select,insert ,delete,update,grant,revoke(至于这些动词如何使用,之后小编会一一阐述)

其实SQL是由数据定义语言(包括对基本表,视图,索引等的定义)、数据操作语言(分为数据查询,数据更新)、数据控制语言(对基本表和视图的授权,完整性规则的描述,事物控制等语言)、嵌入式SQL语言四部分组成。

v2-c125b66ad2f2b954d0f08002d9364a30_b.jpg

在这四种语言中,数据查询是其中最为重要的。


数据定义语言

数据定义语言中所涉及到的动词有create,drop,alter三个。在这其中,可分为三个模块:基本表、视图、索引。

  1. 定义、修改和删除基本表

(1)定义基本表(即创建一个基本表)

格式:create table <表名>
          (<列名> <数据类型> [列级完整性约束] [,<列名> <数据类型> [列级完整性约束]...]
           [<表级完整性约束>]);

其中常用的完整性约束有:主码约束primary key,唯一性约束unique,非空约束not null,参照完整性约束foreign key references ...,检查约束check(...)

例如:
 /*创建读者信息表*/
create table 读者信息表
( 借书证号 char(15) ,
  姓名 CHAR(10) constraint r1 not null,
  性别 CHAR(2) constraint r2 not null,
  出生日期  char(15),
  借书量 smallint constraint r3 check(借书量 < 100),
  工作单位 char(15),
  电话 char(15),
  电子邮箱 char(15) constraint r4 check((电子邮箱 like '%.cn' or 电子邮箱 like '%.com')) ,
  constraint studentkey primary key(借书证号)
  );

/*创建图书明细表*/
create table 图书明细表
	  ( 类别号 char(20),
	    图书编号 char(20) ,
	    图书名称 char(20) not null ,
	    作者 char(10) not null,
	    出版社 char(20),
	    定价 smallint constraint b4 check(定价>0),
	    购进日期 date,
	    购入数 smallint constraint b1 check(购入数>0),
	    复本数 smallint constraint b2 check(复本数>0),
	    库存数 smallint constraint b3 check(库存数>=0),
	    constraint bookkey primary key(图书编号),
	    foreign key (类别号) references 图书类别表(类别号)
	  );

 /*创建图书借阅明细表*/
create table 图书借阅明细表
	 (图书编号 char(20) ,
	  图书名称 char(20) ,
	  借书证号 char(15) ,
	  借出日期 date not null ,
	  归还日期 date,
	  库存数 smallint constraint bbo1 check ( 库存数 >= 0),
	  constraint bbokey primary key(图书编号,借书证号),
	  foreign key (借书证号) references 读者信息表(借书证号),
	  foreign key (图书编号) references 图书明细表(图书编号),
	  );

(2)修改基本表:一般是对列和约束做修改

格式:1.添加字句
(a)alter table <表名> [add <新列名> <数据类型> [<完整性约束>]];
(b)alter table <表名> [add constraint <约束名> <完整性约束>];
2.删除列,删除约束
(a)alter table <表名> [drop column <列名 >];
(b)alter table <表名> [drop <完整性约束名>];
例如:
/*删除Student表中Sname的唯一性约束*/
alter table Student drop unique(Sname);
alter table doc_exb drop column column_b;
3.修改列的数据类型
alter table <表名> [modify <列名> <数据类型>];
例如:/*修改Student表中的Sage,改为smallint*/
alter table Student modify Sage smallint;

(3)删除基本表

格式:drop table <表名>;

2.建立和删除索引

(1)建立索引

格式:
create index <索引名> on <表名> (<列名> [<次序>] [,<列名> [<次序>]...]);

其中,asc为升序,desc为降序,当次序缺省时,则默认为升序
例如:
create index SCno on SC(Sno,Cno desc);
/*在SC表上建立索引SCno 其中,先以Sno升序,当Sno相同的时候,以Cno降序*/

(2)删除索引

格式:
drop index <索引名>;

3.建立、删除和查询视图

视图是提供给用户以多种角度观察数据库中数据的重要机制。

数据库系统只会将视图定义存入数据字典中,并不执行其中的select语句,只有当用户查询视图时,按其定义从基本表中将数据查出

(1)定义视图

create view <视图名> [ (<列名> [ , <列名>...] )]

as <子查询>;

(2)删除视图

drop view <视图名>;

注意:由于视图可以由基本表或者视图导出,所以,当视图被删除后,由这个视图所导出时其他的视图将无法使用。

(3)查询视图

只需将表名换成视图名即可。

在本文后面会有讲解查询方式


数据操作语言之数据查询

数据查询是SQL中最为核心的语句,所以掌握数据插叙是十分必要的。

而SQL的数据查询则分为单表查询、连接查询、嵌套查询和集合查询。

而查询的格式则为:

select [All |Distinct] <目标列表达式> [, <目标列表达式> ...]
from <表名或视图名> [,<表名或视图名>...]
[where <条件表达式>]
[group by <列名1> [having <条件表达式>] ]
[order by <列名2> [asc | desc]];

当然了,对于刚刚开始学SQL的人来说,短时间内想记住这些还会有些困难,可以先对这些格式有个大致的印象,其中,记住最为核心的则是select ... from ... where...

(一)单表查询

  1. 选择表中若干列
  • 查询指定列或全部列
例如:
select sno ,sname from Student;          /*从Student表中,选出sno列和sname列这两部分*/
select * from Student;                   /*从Student表选出所有列的信息*/
  • 查询经过计算的值
例如:
 select Sname , 2015-Sage from Student;
/*其中在查询结果中,2015-Sage列则是表达式计算过后的值*/

2.选择若干元组(即从表中选出若干行)

例如:
select distinct Cno ,grade from SC where grade > 90;
/* 选择SC表中成绩大于90的Cno(课程号)和grade(成绩),其中去掉重复的元组*/

(1)确定大小

/*从Student表中选出Sage<20的元组,* 表示所有列都要显示*/
select * from Student where Sage < 20;
select * from Student where not Sage >= 20;

(2)确定范围:between ... and ...

/*从Student表中选出年龄在20到30的元组*/
select * from Student where Sage between 20 and 30;
select * from Student where Sage not between 20 and 30 ;

(3)确定集合:in , not in

/*从Student表中选出Sdept为'IS'或者'MA'的元组*/
select * from Student where Sdept in ('IS','MA');
select * from Student where Sdept not in ('IS','MA');

(4)字符串匹配:like ,not like

  • 通配符:%,代表任意长度
例如:/*从Student表中选出Sname以刘开头的元组*/
select * from Student where Sname like '刘%';
  • 通配符:_:代表任意单个字符
/*从Student表中选出Sname列中第二个字为阳的元组*/
select * from Student where Sname like '_阳%';
  • 通配符:换码符
/*从Scourse表中选出Cname列中以DB_开头,且倒数第三个字符为i的元组*/
select * from Scourse where Cname like 'DB_%i_ _' esacpe '';

(5)涉及空值的查询:is null , is not null (is null 不能用 = null代替)

select * from SC where grade is null;

(6)多重条件查询:用and、or连接,其中and的优先级比or高

3.对查询结果排序:升序asc,降序为desc,其中缺省时默认为升序

select * from Student order by Sdept , Sage desc;
/*对Student的所有元组进行排序,先以Sdept升序,如果Sdept相等,则按Sage降序排序*/
/*其中,空值代表无穷大,故asc时放最后,desc时当最前*/

4.使用集函数:

有count(计数)、sum(求和)、avg、max、min等

如:count([distinct | all] *) ,其中distinct代表重复元组不计数,all代表重复元组要计数,缺省时默认为all

/*统计SC表中,Sno列中不同的元组的个数*/
select count (distinct Sno ) from SC;

5.对查询结果分组:使用group by 字句后,select 字句的列名中只能出现group by后的分组属性或者集函数

/*将SC表按Cno分组,并且显示每一组的Cno列和对每一组中Sno进行统计*/
select Cno ,count(Sno) from SC group by Cno;

其中可使用 having <条件>对分后的组进行筛选

/*查询有三门以上的课程是90分以上的学生的学号及课程数*/
select Sno ,count(*) from SC where grade >= 90 
group by Sno having count(*) >= 3 ;

(二)连接查询

1.广义笛卡尔积

select Student.* , SC.* from Student,SC;

2.等值连接

/*将Student表和SC表合并,其中保证两个表连接时,对应的Sno相等*/
select Student.* , SC.* from Student,SC
where Student.Sno = SC.Sno;

3.自身连接

/*查询每一门课的间接先修课(即先修课的先修课)*/
Select first.Cno , second.Cpno 
from Scourse first , Scourse second
where first.Cpno = second.Cno;

4.外连接:

在等值连接中,如果其中的一个表的连接属性为空,则会被舍弃,如上面等值连接的例子,如果存在有学生没有选课,则在最后的查询结果中就不会有该学生的记录。

如果想要最终查询结果中有该学生的记录,只是某些对应部分为空,则采用外连接

/*左外连接,则左边关系(即Student表)出现所有元组*/
select Student.Sno,Sname,Sex,Sage,Sdept,Cno,Grade
from Student left outer join SC on(Studnet.Sno = SC.Sno);
/* ...right outer join...on(条件),则为右外连接,则右边关系(即SC表)出现所有选组*/

5.复合条件连接

/*查询每个学生每一门选修课的记录的学号,学生姓名,课程名和成绩*/
select Student.Sno,Sname,Canme,Grade
from Student ,SC,Course
where Student.Sno = SC.Sno and SC.Cno =  Course.Cno;

(三)嵌套查询

将一个select..from..where...语句模块嵌套在另一个查询块的where字句或者having短语条件中的查询,称为嵌套查询

其中外层查询块称为父查询,内层查询称为子查询,子查询中不能用order b字句,因为此时内层查询结果看做为一个集合。

1.嵌套查询的分类

(1)不相关子查询:子查询的查询条件不依赖父查询,由里向外逐层处理

(2)相关子查询:子查询的查询条件依赖于父查询。

首先去外层查询中的第一个元组,根据它与内层查询相关的属性值处理内层查询,若where字句为真,则去元组放入结果表中;然后再取外层表的下一个元组。重复这一过程,直至外层表全部检查完为止。

2.引出子查询的谓词:in,比较运算符,带any或all,exists

(1)带有in的谓词:

select Sno,Sname from Student
where Sno in (select  Sno from SC
              where Cno in(select Cno from Course
                           where Cname = '信息系统'));

(2)带有比较运算符的子查询:当能确定内层查询返回单值时,可用比较运算符

select * from Student 
where Sdept = ( select Sdept from Student where Sname = '刘晨');

错误表达:
select * from Student 
where ( select Sdept from Student where Sname = '刘晨') = Sdept;
/*即内层查询需在运算符右边*/

(3)带有all或者any谓词的子查询:一般而言这类均可用集函数max或者min来等效表达,故小编在这里就不做展开

(4)带有exists谓词的子查询:相当于

  • 带有exists谓词的子查询不返回任何数据,只产生逻辑真假。由exists引出的子查询的目标表达式通常用*,因为带exists的子查询只返回逻辑真值,给出列名无意义
/*查询选修了1号课程的学生名字*/
select Sname from Student 
where exists (select * from SC where Sno = Student.Sno and Cno = '1');

/*查询没有选修1号课程的学生姓名*/
select Sname from Student 
where not exists (select * from SC where Sno = Student.Sno and Cno = '1');
  • 用(not)exists实现全程量词
/*查询选修了全部课程的学生姓名     ,小编本人这样理解:不存在这样的课程,这个学生没选
而在实际写的时候,可以先写存在的代码 ,然后再在 ȋ   exists    前 加上not   */
select Sname from Student 
where not exists (select * from Course where  not exists
                 (select * from SC where SC.Sno = Studnet.Sno and SC.Cno = Course.Cno));
  • 用(not)exists实现逻辑蕴含
/*查询至少选修了学生95002选修的全部课程的学生代码,即不存在这样的一门课,学生95002选了,而这个学生没选*/
select distinct Sno from SC scx
where  not exists  ( select * from SC scy where  scy.Sno = '95002' and
                   not exists (select * from SC scz   
                                where  scz.Cno = scx.Sno and scz.Cno = scy.Cno));

(四)集合查询:并、交、差操作

在SQL中,仅有并操作的方法,对于交、差操作并没有具体的方法,但是可用之前所讲的查询方法表示

而并操作格式:<查询块> union <查询块>

select Sname from Student 
union
select Tname  from Teacher;
  • union操作必须保证每个查询块的列数相同,对应项的数据类型相同
  • 对集合操作结果的排序:order by 字句只能用于对最终结果排序,不能对中间结果排序,任何情况下order by 字句只能放在最后
select * from Student 
union
select * from Teacher order by 1;
/*对最后的查询结果进行排序,其中按第
一列的升序排序*/

数据操作语言之数据更新

这部分主要涉及对元组,即行的操作,主要涉及动词为insert,delete,update

1.插入数据

(1)插入单个元组,格式:

insert into <表名> [ (<属性列1> [ , <属性列2>...] ) ]

values ( <常量1> [ , <常量2>...] );

insert into Student
values ('19520','陈乐','男','IS','18');

(2)插入子查询结果

insert into <表名> [ (<属性列1> [ , <属性列2>...] ) ]

子查询;

insert into Deptage(Sdept , Avrage)
select Sdept ,AVG(Sage) from Student group by Sdept;

2.修改数据

update <表名>

set <列名> = <表达式> [ , <列名> = <表达式>...]

[where <条件>];

eg.
1.update Student set Sage = 22 where Sno = '95001';
2.update Student set Sage = Sage+1;
3.update SC set Grade = 0
 where 'CS' = (select Sdept from Student where Student.Sno = SC.Sno);

3.删除数据

delete from <表名> [ where <条件>];

delete from Student where Sno = '95001';
delete from SC;/*删除SC中所有数据*/

数据控制语言

数据控制分为很多,在这里,小编主要讲对于授权的控制

在SQL中,权限是指对视图,基本表等进行数据操纵的权限,而对于用户而言,需要被授权

(1)授权:

grant <权限> [ ,<权限>...] on <对象类型> <对象名>

to <用户> [ ,<用户>...] [with grant option];

/*with grant option代表该用户可以将该权限授予给其他用户*/

1.grant select on table Student to U1;
2.grant all priviliges on table Student to U2,U3;
3.grant update(Sno) ,delete on table Student to U4 with grant option;
/*将delete和对Sno列修改的权限赋予用户U4*/

(2)收回权限

revoke update(Sno) on table Student from U4;

权限的级联回收:U4的权限被回收,则同时自动回收由U4给予别的用户的同样权限。但若别的用户还从其他用户获得了update(Sno)权限,则仍具有该权限,系统只回收从U4得到的权限。


至此,SQL基本的用法已经讲完,但是在这里小编还想再讲两个比较重要的东西:

存储过程和触发器

1.存储过程是一个预先编译好的SQL代码,可以将其约等于c语言的函数,但是其不是函数,因为函数是会返回值,但存储过程是不会返回的,而且在SQL中也有函数。

有人可能会问了,为什么还要设置存储过程。可以这样想,对于一个程序应用的开发人员而言,其可能并没有参与数据库的设计,而数据库的设计则是由另一部分的人来开发,而对于程序应用的开发人员来说,他需要能查询到他想要的信息,那么数据库开发人员就可以写好存储过程来供程序应用的开发人员的使用,而不需要再去了解其中是如何进行的

2.触发器:

触发器是一种特殊的存储过程,一般的存储过程需要由用户显性使用,但是触发器则是一种对表进行插入、更新、删除时会自动执行。

例如当你在Student表中删除了一个学生的信息,那么对Student表设置触发器后,则可以直接自动将SC选课表中该学生的选课信息一并删除,而不再需要用户自己去调用

当然,对于不同的数据库语言,其格式不同,大家可以去百度搜索,小编在这里就不再展开了


到这里,小编基本就讲完了,当然,小编在其中只是列一些非常简单的例子,下来如果想熟悉SQL的话,还是得多多练习,这里小编就把当时自己当时做的练习给大家,以供参考

【实验项目一】数据表, 索引, 视图创建, 修改,删除的设计与完整性约束

1)用SQL的DDL语句创建以下包括读者信息表,借还明细表,图书类别表,图书借阅明细表,图书明细表和工作人员表6个基本表,设置主键,并输入数据。

2)用不同的方法创建基本表的约束,并能够查看和删除约束;能够创建和删除默认规则,在试验中要求掌握主键约束的特点和用法;掌握惟一性约束的用法;掌握默认约束和默认规则的用法;掌握CHECK约束的用法;掌握利用主键与外键约束实现参照完整性的方法。

3)修改基本表,包括增加一个字段;删除一个字段;增加一个约束;修改字段的数据类型。

4)创建与删除索引

5) 创建与删除视图

SQL语句:

create database sb;

use sb;

/*创建读者信息表*/

CREATE TABLE 读者信息表

( 借书证号 char(15) ,

姓名 CHAR(10) constraint r1 not null,

性别 CHAR(2) constraint r2 not null,

出生日期 date,

借书量 smallint constraint r3 check(借书量 < 100),

工作单位 char(15),

电话 char(15),

电子邮箱 char(15) constraint r4 check((电子邮箱 like '%.cn' or 电子邮箱 like '%.com')) ,

constraint studentkey primary key(借书证号)

);

insert into 读者信息表 values('29307142','张晓露','女','1989-02-1',2,'管理信息系','8586126','zxl@163.com');

insert into 读者信息表 values('36405216','李阳','男 ','1988-12-26 ',1,'航海系','85860729','ly@sina.com.cn');

insert into 读者信息表 values('28308208','王新全','男 ','1988-04-25 ',1,'人文艺术系 ','85860618','wxq@yahoo.cn');

insert into 读者信息表 values('16406236','张继刚','男 ','1989-08-18 ',1,'轮机工程系 ','85860913','zjg@163.com');

insert into 读者信息表 values('16406247','顾一帆','男 ','1981-12-30 ',null,'轮机工程系 ','85860916','gyf@yahoo.cn');

create table 图书类别表

(类别号 char(20) primary key,

图书类别 char(20) constraint s1 unique

);

insert into 图书类别表 values('H31','英语');

insert into 图书类别表 values('I267','当代作品');

insert into 图书类别表 values('TP312','程序语言');

insert into 图书类别表 values('TP393','计算机网络');

insert into 图书类别表 values('U66','船舶工程');

/*创建图书明细表*/

create table 图书明细表

( 类别号 char(20),

图书编号 char(20) ,

图书名称 char(20) not null ,

作者 char(10) not null,

出版社 char(20),

定价 smallint constraint b4 check(定价>0),

购进日期 date,

购入数 smallint constraint b1 check(购入数>0),

复本数 smallint constraint b2 check(复本数>0),

库存数 smallint constraint b3 check(库存数>=0),

constraint bookkey primary key(图书编号),

foreign key (类别号) references 图书类别表(类别号)

);

insert into 图书明细表 values('I267','99011818','文化苦旅','余秋雨','知识出版社',16,'2000-03-19',8,15 ,14);

insert into 图书明细表 values('TP312','00000476','Delphi高级开发指南','坎图','电子工业出版社',80,'2000-03-19',15,15 ,15);

insert into 图书明细表 values('U66','01058589','船舶制造基础','杨敏','国防工业出版社',19,'2001-07-15',20,20,20);

insert into 图书明细表 values('I267','07410139','艺海潮音','李叔','江苏文艺出版社',19,'2007-04-12',15,20,18);

insert into 图书明细表 values('TP312','07410298','C++程序设计','成颖','东南大学出版社',38,'2007-05-08',10,15,14);

insert into 图书明细表 values('H31','07410802','航海英语','陈宏权','武汉工业大学出版社',42,'2007-10-20',25,25,24);

insert into 图书明细表 values('H31','07108667','大学英语学习辅导','姜丽蓉','北京理工大学出版社',23.5,'2008-02-06',25,25,25);

insert into 图书明细表 values('TP393','07410810','网络工程实用教程','汪新民','北京大学出版社',34.8,'2008-08-21',10,15,15);

/*创建工作人员表*/

create table 工作人员表

( 工号 char(15) ,

姓名 char(10) not null,

性别 char(5) constraint w1 check (性别 in ('男' , '女')),

出生日期 date,

联系电话 char(15),

电子邮箱 char(15),

constraint workerkey primary key (工号)

);

insert into 工作人员表 values('002016','周学飞','男','1971-05-03','85860715','zxf@163.com');

insert into 工作人员表 values('002017','李晓静','女','1979-09-15','85860716','lj@163.com');

insert into 工作人员表 values('002018','顾彬','男','1972-04-25','85860717','gb@yahoo.cn');

insert into 工作人员表 values('002019','陈欣','女','1968-11-03','85860718','cx@sina.com');

/*创建借还明细表*/

create table 借还明细表

( 借书证号 char(15),

图书编号 char(20),

借还信息 char(5) constraint bo2 check (借还信息 in ('借','还')),

借书日期 date,

还书日期 date,

数量 smallint constraint bo1 check(数量 >0),

工号 char(15),

constraint borrowkey primary key(借书证号),

foreign key (借书证号) references 读者信息表(借书证号),

foreign key (图书编号) references 图书明细表(图书编号),

foreign key (工号) references 工作人员表(工号)

);

insert into 借还明细表 values('29307142','07108667','还','2008-03-28','2008-04-14',1,'002016')

insert into 借还明细表 values('29307142','99011818','借','2008-04-27',null,1,'002016')

insert into 借还明细表 values('36405216','07410802','借','2008-04-27',null , 1,'002018')

insert into 借还明细表 values('29307142','07410298','借','2008-04-28',null,1,'002018')

insert into 借还明细表 values('36405216','00000476','还','2008-04-29','2008-05-09',1,'002016')

insert into 借还明细表 values('28308208','07410139','借','2008-05-10',null,1,'002019')

insert into 借还明细表 values('16406236','07410139','借','2008-05-11',null,1,'002017')

/*创建图书借阅明细表*/

create table 图书借阅明细表

(图书编号 char(20) ,

图书名称 char(20) ,

借书证号 char(15) ,

借出日期 date not null ,

归还日期 date,

库存数 smallint constraint bbo1 check ( 库存数 >= 0),

constraint bbokey primary key(图书编号,借书证号),

foreign key (借书证号) references 读者信息表(借书证号),

foreign key (图书编号) references 图书明细表(图书编号),

);

insert into 图书借阅明细表 values('07410139','艺海潮音','28308208','2008-05-10',null ,18);

insert into 图书借阅明细表 values('07410139','艺海潮音','16406236','2008-05-11',null ,17);insert into 图书借阅明细表 values('99011818','文化苦旅','29307142','2008-04-27',null ,14);

insert into 图书借阅明细表 values('07410802','航海英语','36405216','2008-04-27',null ,24);

insert into 图书借阅明细表 values('07410298','C++程序设计语言','29307142','2008-04-28',null ,14);

运行界面:

图书类别表

v2-d6cad267636457fcebf4a3cbb2fe6a01_b.jpg

图书明细表

v2-62240fddd50b64fcbb0e6d43d1778173_b.jpg

读者信息表

v2-847dd6c48733ea010afadaeb085c73ee_b.jpg

工作人员表

v2-03287a0a4033f7f0d5ee12d650ce3f8d_b.jpg

借还明细表

v2-f65d601d8adce5a2735ef64f470a8ddb_b.jpg

图书借阅明细表

v2-f67d326cefc92952c242626e9b717324_b.jpg
  1. 用不同的方法创建基本表的约束,并能够查看和删除约束;能够创建和删除默认规则,在试验中要求掌握主键约束的特点和用法;掌握惟一性约束的用法;掌握默认约束和默认规则的用法;掌握CHECK约束的用法;掌握利用主键与外键约束实现参照完整性的方法。

SQL语句:

alter table 读者信息表 drop r4;

alter table 读者信息表 add constraint r4 check((电子邮箱 like '%.cn' or 电子邮箱 like '%.com'));

运行界面:

读者信息表

v2-bcd8d92bd6599270482f9aba25753a2a_b.jpg

工作人员表

v2-03b1a191de5db77cdecb47cdb46a9783_b.jpg

图书类别表

v2-44403123e23ffc6a873ec316d8e7f552_b.jpg

图书明细表

v2-b7c220f99aa5fe8479d2a09de5a67d2a_b.jpg

借还明细表

v2-d5e0efe03c099d29c39db52e48451962_b.jpg

图书借阅明细表

v2-17f5fd707509cc663e4edf05027f9f12_b.jpg
  1. 修改基本表,包括增加一个字段;删除一个字段;增加一个约束;修改字段的数据类型。

alter table 读者信息表

add 字段 char(15);

alter table 读者信息表 drop column 字段;

【实验项目二】SQL 语言与视图

请写出下列查询语句并给出结果

  1. 列出student表中所有记录的sname、sex和class列。

SQL语句

select sname ,sex,class

from student ;

运行界面

v2-a0ec6abf385eb42df54c805b5fe81741_b.jpg

2.显示教师所有的单位即不重复的depart列。

SQL语句

select distinct depart

from teacher

运行界面

;

v2-8bf84274b4c8e70f6274327818b4f7fd_b.jpg

3.显示学生表的所有记录。

SQL语句

select*

from student ;

运行界面

v2-bd42a608c453ebbf05dc017846ec6dce_b.jpg
  1. 显示score表中成绩在60到80之间的所有记录。

SQL语句

select *

from score

where degree between 60 and 80;

运行界面

v2-113bef50873cc4634091cf872742ffec_b.jpg
  1. 显示score表中成绩为85,86或88的记录。

SQL语句

select *

from score

where degree = 85 or degree =80 or degree =88;

运行界面

v2-26d0d6aad219077f351c14b3bb228165_b.jpg
  1. 显示student表中“95031”班或性别为“女”的同学记录。

SQL语句

select *

from student

where class like '95031' or sex like '女';

运行界面

v2-ecca2fd6fd4bba47fbe15710d325c78f_b.jpg
  1. 以class降序显示student表的所有记录。

SQL语句

select *

from student

order by class desc;

运行界面

v2-4f4bf22f5d84809a1c71b739a73c3970_b.jpg
  1. 以cno升序、degree降序显示score表的所有记录。

SQL语句

select *

from score

order by cno asc ,degree desc;

运行界面

v2-ea6b2eab8671005a32328c5bfc2e55e3_b.jpg
  1. 显示“98031”班的学生人数。

SQL语句

select count(*)

from student

where class like '98031';

运行界面

v2-8c1a08856772ccc58bf9b18297bf9436_b.jpg
  1. 显示score表中的最高分的学生学号和课程号。

SQL语句

select sno ,cno

from score

where degree =

(select max(degree)

from score)

运行界面

v2-5e9c95ab6c0561e25c8439f30d80c2d6_b.jpg
  1. 显示“3-105”号课程的平均分。

SQL语句

select avg(degree)

from score

where cno like '3-105';

运行界面

v2-8d4f8ddfa6d1ec94b18509f8453c3b33_b.jpg
  1. 显示score表中至少有5名学生选修的并以3开头的课程号的平均分数。

SQL语句

select avg(degree)

from score

where cno like '3%'

and cno in (select cno

from score

group by cno having count(*)>=5);

运行界面

v2-6e90e174a3b58194dc0167d4001f61e7_b.jpg

13.显示最低分大于70,最高分小于90 的sno列。

SQL语句

select sno

from score

group by sno having (min(degree)>70 and max(degree)<90);

运行界面

v2-b1da652ce70d92bf4582e3579ac79cdd_b.jpg

14.显示所有学生的 sname、 cno和degree列。

SQL语句

select sname,cno,degree

from student,score

where student.sno = score.sno;

select sname,cno,degree

from student left join score

on

student.sno = score.sno;

运行界面

v2-55126f7b7c9834ee026094a011e58f26_b.jpg

v2-ddcb9d4c0b1e1c10f50264e67ab7fba1_b.jpg

15..显示所有学生的 sname、 cname和degree列。

SQL语句

select sname,cname,degree

from student,score,course

where student.sno = score.sno

and course.cno = score.cno;

运行界面

v2-052622a6de5a7ee5feb72da8efb3cd65_b.jpg

16.列出“95033”班所选课程的平均分。

SQL语句

SELECT score.cno , avg (degree)

from score ,student

where score.sno = student.sno and class like '95033'

group by cno;

运行界面

v2-90842348f6098fa44acdfe59d06249df_b.jpg

17.显示选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

SQL语句

select*

from student

where sno in (select sno

from score

where cno like '3-105'

and degree > ( select degree

from score

where sno like '109'

and cno like '3-105'));

运行界面

v2-6e39f29db317bb9927afe33fce947fe3_b.jpg

18.显示score中选修多门课程的同学中分数为非最高分成绩的记录。

SQL语句

select*

from score x

where degree <(

select max (degree)

from score y

where y.sno in (select sno

from score

group by sno having count(*) > 1)

and x.sno = y.sno

group by sno);

运行界面

v2-447f6182c6615b95eeef0073bb4aa9fa_b.jpg

19.显示成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

SQL语句

select*

from score

where cno like '3-105'

and degree > (select degree

from score

where sno like '109'

and cno like '3-105')

运行界面

v2-1ddb2f6b6e03825ea66d1fbf6d6ab877_b.jpg

20.显示出和学号为“108”的同学同年出生的所有学生的sno、sname和 birthday列。

SQL语句

select sno , sname , birthday

from student

where year(birthday) = (select year(birthday)

from student

where sno like '108');

运行界面

v2-39d316f2b573af7de0435a9abe0ba00f_b.jpg

21.显示“张旭”老师任课的学生成绩。

SQL语句

select degree

from score,teacher,course

where course.tno = teacher.tno

and score .cno = course.cno

and teacher.tname like '李旭';

运行界面

v2-ab2c5414fafc9096888a3428c405cf91_b.jpg

22.显示选修某课程的同学人数多于5人的老师姓名。

SQL语句

select tname

from teacher

where tno in (

select teacher.tno

from score ,teacher,course

where score.cno = course.cno

and teacher.tno = course.tno

group by teacher.tno having count(*)>=5);

运行界面

v2-f70edb23e79802a3c35549fdbe355601_b.jpg

23.显示“95033”班和“95031”班全体学生的记录。

SQL语句

select *

from student

where class in ('95033','95031');

运行界面

v2-685cd0c35fc7a823ef7db1d62dbf8f9d_b.jpg

24.显示存在有85分以上成绩的课程cno。

SQL语句

select cno

from score

group by cno having max(degree) > 85;

运行界面

v2-bf05f4a6d987b4d670e5e1c43536244a_b.jpg

25.显示“计算机系”老师所教课程的成绩表。

SQL语句

select *

from score

where cno in (select cno

from course , teacher

where course.tno = teacher.tno

and depart like '计算机系');

运行界面

v2-e5bcbee96486a4157874909611d0b3ac_b.jpg

26.显示“计算机系”和“电子工程系”不同职称的老师的tname和prof。

SQL语句

select tname , prof

from teacher x

where depart like '计算机系'

and not exists

(select *

from teacher y

where x.prof = y.prof

and depart like '计算机系'

and x.tno ! = y.tno)

union

select tname , prof

from teacher x

where depart like '电子工程系'

and not exists

(select *

from teacher y

where x.prof = y.prof

and depart like '电子工程系'

and x.tno ! = y.tno)

select tname , prof

from teacher x

where not exists

(select *

from teacher y

where x.prof = y.prof

and x.tno ! = y.tno);

运行界面

v2-6d548b9327cd4e4b2aa124df617e27e3_b.jpg

v2-fa9d0ebf65730b717aa54cc1a3c7438c_b.jpg

27.显示选修编号为“3-105”课程且成绩至少高于“3-245”课程的同学的cno、sno和degree,并按degree从高到低次序排列。

SQL语句

select cno , sno , degree

from score

where cno like '3-105'

and degree >= (select min(degree)

from score

where cno like'3-245')

order by degree desc;

select cno , sno , degree

from score

where cno like '3-105'

and degree >= (select max(degree)

from score

where cno like'3-245');

运行界面

v2-a1afa5f143d192436bbace8f7fd589a6_b.jpg

v2-016b6b594832147147db78c60fad0fe0_b.jpg

28.显示选修编号为“3-105”课程且成绩高于“3-245”课程的同学的cno、sno和degree。

SQL语句

select *

from score x

where x.cno like '3-105'

and degree > (select degree

from score y

where y.cno like '3-245'

and x.sno = y.sno);

运行界面

v2-31bf7431572b2c3d3c98b0b979bd197f_b.jpg

29.列出所有任课老师的tname和depart。

SQL语句

select tname , depart

from teacher ,course

where teacher.tno = course.tno;

运行界面

v2-18c631dc754ca5460e2c8a2d297dbf0e_b.jpg

SQL语句

select tname, depart

from teacher

where tno in (select tno

from course

group by tno);

运行界面

v2-107cf204daa429ca6e409c74ead37b08_b.jpg

30.列出所有未讲课老师的tname和depart。

SQL语句

select tname

from teacher

where tno not in (select tno

from course

group by tno);

运行界面

v2-b24de292e586b695c5a0ff32ce519116_b.jpg

31.列出所有老师和同学的 姓名、性别和生日。

SQL语句

select tname , sex , birthday

from teacher

union

select sname,sex,birthday

from student

运行界面

v2-cfb312b0eaba6c0f87eca4705d548246_b.jpg

32.检索所学课程包含学生“103”所学课程的学生学号。

SQL语句

select sno

from score x

where not exists

(select *

from score y

where sno like '103'

and not exists

(select *

from score z

where y.cno = z.cno

and x.sno = z.sno

));

运行界面

v2-7eb01747fe1f51549d8b1cc6a19a2495_b.jpg
  1. 检索选修所有课程的学生姓名。

SQL语句

select sname

from student

where not exists

(select *

from course

where not exists

(select *

from score

where course.cno = score.cno

and student.sno = score.sno));

运行界面

v2-6bc71055cf36a9cef85a7058ccf6a9d5_b.jpg

【实验项目三】存储过程和触发器

  1. 创建一个查询图书库存量的存储过程CX_TSKCL_PROC,输出的内容包含类别号、图书编号、图书名称、库存数等数据内容

SQL语句

go

create proc CX_TSKCL_PROC

as

select * from 图书明细表;

go

go

execute CX_TSKCL_PROC;

Go

运行界面

v2-b1fa615c71faf7d66c26d643d9b80bcd_b.jpg
  1. .创建一个名为TS_CX_PROC的存储过程,它带有一个输入参数,用于接受图书编号,显示该图书的名称、作者、出版和复本数。

SQL语句

go

create proc TS_CX_PROC @图书编号 char(20)

as

select 图书名称,作者,出版社,复本数 from 图书明细表

where 图书编号 like @图书编号;

go

go

execute TS_CX_PROC'01058589';

go

运行界面

v2-970429d7517ffff37ee56ad30ad88c9a_b.jpg
  1. 修改TS_CX_PROC存储过程,使之能按图书名称查询图书的相关信息。执行修改后的TS_CX_PROC存储过程,分别查询“航海英语”、“艺海潮音”等图书的信息。

SQL语句

go

alter proc TS_CX_PROC @图书编号 char(20)

as

select * from 图书明细表

where 图书名称 like @图书编号;

go

go

execute TS_CX_PROC '航海英语';

execute TS_CX_PROC '艺海潮音';

go

运行界面

v2-c47a228c38e5a1f1da228cfb5eac5aba_b.jpg
  1. 删除创建的存储过程

SQL语句

go

drop proc CX_TSKCL_PROC,TS_CX_PROC;

go

  1. 在图书类别表上创建一个名为tslb_insert_trigger的触发器,当执行INSERT操作时,该触发器被触发,禁止插入记录。

SQL语句

go

create trigger tslb_insert_trigger on 图书类别表

after insert

as

delete from 图书类别表

where 类别号 like(select 类别号 from inserted);

  1. 在图书明细表上创建一个名为ts_delete_trigger的触发器,当执行DELETE操作时,该触发器被触发,禁止删除记录。

SQL语句

go

create trigger ts_delete_trigger on 图书明细表

after delete

as

insert into 图书明细表

select * from deleted;

  1. 在读者信息表上创建一个名为dzxx_insert_trigger的触发器,当在读者信息表中插入记录时,将该记录中的借书证号自动插入借还明细表中。

SQL语句

go

create trigger dzxx_insert_trigger on 读者信息表

after insert

as

declare @no char(20);

select @no = 借书证号 from inserted;

insert into 借还明细表(借书证号,图书编号 ,工号) values(@no,'00000476','002016');

  1. 删除上述触发器

SQL语句

go

drop trigger tslb_insert_trigger,ts_delete_trigger,dzxx_insert_trigger ;

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值