03 SQL查询数据 实验报告

                                              03 SQL查询数据 实验报告                                          

                                                                   广州大学学生实验报告

                                开课学院及实验室:计算机科学与工程实验室418B室         2018年05月 17 日

学院

计算机科学与教育软件

年级、专业、班

网络***

姓名

卟咚君

学号

1606100***

实验课程名称

数据库原理实验

成绩

 

实验项目名称

SQL查询数据

指导老师

***

                 

实验目的

插入、删除、修改是数据库中更新数据的基本方法,对数据库进行数据更新操作必须满足数据的完整性

要求。通过本次实验,掌握数据库中插入数据、修改数据、删除数据的基本语法,理解和掌握完整性约

束条件的作用和使用方法。

实验要求

熟悉实验室实验环境,阅读实验预备知识,掌握本实验内容涉及知识点的基本用法,了解实验中故障排

除的基本方法。实验中根据实验步骤进行,根据步骤要求撰写实验报告。

实验环境

Oracle 10g,windows 2003;

实验内容和步骤

1.列出Teacher表的所有约束,并说明每个约束的具体含义及其对表列取值的影响;

select table_name,constraint_name,constraint_type from user_constraints where table_name like 'TEACHER';

约束条件:

table_name constraint_name constraint_type

TEACHER  FK_TEACHER_TSEX    C

TEACHER  SYS_C0011387  C

TEACHER  SYS_C0011388  C

TEACHER  SYS_C0011389  C

TEACHER  SYS_C0011390  C

TEACHER  SYS_C0011391  C

TEACHER  PK_TEACHER  P

TEACHER  FK_TEACHER_BELONGTO_DEPARTME    R

C:check on a table

P:primary key

R:referential AKA foreign key

2.书写SQL语句,在Teacher表中插入2条元组,元组内容任意设置,要求能取空值的列均设置为空(提示

:如果插入失败,则查看是否满足基本表的约束条件);

insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t010', '刘小备', '男', null, To_date('7-7月-1977', 'DD-mon-yyyy'), 'd001');

insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t011', '曹小操', '男',  null, To_date('7-7月-1977', 'DD-mon-yyyy'), 'd001');

3.利用“create table teacher2 as select * from teacher”语句创建表teacher2,并列出Teacher2表的所有约束,比较Teacher2表与Teacher表的约束差异;

create table teacher2 as select * from teacher;

  1. 任取teacher表中的一条元组,把这条元组分别插入到teacher2和Teacher表中,比较两次插入操作的运行结果并分析原因(要求插入失败时必须指出违反了哪类完整性约束条件);

insert into teacher(tno, tname, tsex,  tsalary, tbirthday, dno) values('t002', '李四', '女', 3600, To_date('21-10月-1979', 'DD-mon-yyyy'), 'd001');

 --插入teacher表时会失败,违反了实体完整性(主码取唯一值)

nsert into teacher2(tno, tname, tsex,  tsalary, tbirthday, dno) values('t002', '李四', '女', 3600, To_date('21-10月-1979', 'DD-mon-yyyy'), 'd001');

5.使用带子查询的插入语句把teacher表中的所有男教师插入到teacher2表中;

insert into teacher2(tno, tname, tsex,  tsalary, tbirthday, dno)select tno, tname, tsex,  tsalary, tbirthday, dno from teacher where tsex='男' ;

6.为表Teacher添加check约束,使性别的取值只能为“男”或者“女”;

alter table teacher add constraint fk_teacher_tsex check(tsex in ('男','女') );

7.删除teacher2表中工资等于6000的教师信息;

delete teacher2 where tno in(select tno from teacher2 where tsalary=6000);

8.删除teacher2表中“计算机科学系”的所有教师;

delete teacher2 where tno in(select teacher2.tno from teacher2, department where teacher2.dno=department.dno and department.dname like '计算机科学系');

select *from department;

9.删除teacher2表中的所有教师;

delete teacher2;

10.修改teacher2表,使列tno为主码,主码约束名字为PK_teacher2;

alter table teacher2 add constraint PK_teacher2 primary key(tno);

11.为teacher2表添加唯一约束,使tname的取值不能重复;

alter table teacher2 add constraint un_teacher2_tname unique(tname);

12.修改teacher2表,使列dno成为外码,引用department表的主码dno,当删除department表中的元组时,级联删除Teacher2表中的元组(提示:删除并重新创建外码约束,使用ON DELETE CASCADE选项);

alter table teacher2 add constraint fk_teacher2_to_department foreign key (dno)references department (dno) on delete cascade;

13.在department表中插入一个新系,系号为“xyz”,在Teacher2表中为该新系添加两个教师信息;

insert into department(dno,dname) values('xyz', '网络安全');

insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t012', '张小飞', '男', 3000, To_date('7-7月-1977', 'DD-mon-yyyy'), 'xyz');

insert into teacher2(tno, tname, tsex,  tsalary, tbirthday, dno) values('t013', '关小羽', '男', 3600, To_date('21-10月-1979', 'DD-mon-yyyy'), 'xyz');

14.分别写出删除department表中系号为d001和xyz的记录的SQL语句并执行,比较并分析执行结果(提示:在Teacher表和Teacher2表中的外码定义是不同的);

delete department where dno='d001';

--删除department表中系号为d001会报错,违反完整约束条件

delete department where dno='xyz';

15.在tm中插入一条元组,只设置tno、pno的值;

insert into tm(tno, pno) values('t010', 'p0001');

16.给teacher表中的所有教师的工资增加100;

update teacher set tsalary= tsalary+100;

17.给teacher表中的“计算机科学系”教师的工资增加100;

update teacher set tsalary=tsalary+100 where tno in(select teacher.tno from department,teacher where teacher.dno=department.dno and department.dname like '计算机科学系');

18.创建两个视图VT、VT2,两个视图均为包含所有teacher表的男教师的信息,但视图VT2的定义带有with check option选项,设置一条女教师信息记录,指出通过哪个视图可以成功插入记录,并说明with check option选项的作用;

create view VT as select *from teacher where tsex='男';

create view VT2 as select *from teacher where tsex='男' with check option;

insert into VT2(tno,tname,tsex,tsalary,tbirthday,dno) values('t014','貂小蝉','男',3000,To_date('17-10月-1985', 'DD-mon-yyyy'), 'd001');

--在定义视图的时候加上with check option 子句,这样在视图上插入,删除,修改数据时,RDBMS会检查视图定义的条件,若不满足条件则拒绝执行该操作。

实验总结

总结实验过程中涉及到的知识点、实验过程中遇到的问题及解决方法。

通过本次实验,掌握数据库中插入数据、修改数据、删除数据的基本语法,理解和掌握完整性约束条件的作用和使用方法。在select table_name,constraint_name,constraint_type from user_constraints where table_name like '表名称' 使用语句进行查询的时候,需要注意的是,表名称需要英文大写的字符串,查询的结果会出现的一些约束条件的缩写,其他的比如not null,not quite语句的约束条件不会出现,通过sqldevelpor的查询用户中的表的约束条件时可以很明显的查询出来。

在创建视图的时候,如果创建的时候带有with check option的选项,可以通过对视图的修改来修改基本表的信息。

猜你喜欢

转载自blog.csdn.net/qq_40160605/article/details/81278331