数据库实验三( 数据更新)

3.1 项目信息管理数据库 DDL(Oracle SQL)
alter table Department
drop constraint FK_DEPARTME_管理_TEACHER;
alter table MyProject
drop constraint FK_MYPROJEC_PP_MYPROJEC;
alter table MyProject
drop constraint FK_MYPROJEC_PROJECTMA_TEACHER;
alter table TM
drop constraint FK_TM_TM_TEACHER;
alter table TM
drop constraint FK_TM_TM2_MYPROJEC;
alter table Teacher
drop constraint FK_TEACHER_BELONGTO_DEPARTME;
drop index 管理_FK;
drop table Department cascade constraints;
drop index PP_FK;
drop index ProjectManager_FK;
drop table MyProject cascade constraints;
drop index TM2_FK;
drop index TM_FK;
drop table TM cascade constraints;
drop index BelongTo_FK;
drop table Teacher cascade constraints;
/*==============================================================*/
/* Table: Department */
/*==============================================================*/
create table Department
(
DNO VARCHAR2(10) not null,
TNO VARCHAR2(10),
DName NVARCHAR2(20) not null,
constraint PK_DEPARTMENT primary key (DNO)
);
/*==============================================================*/
/* Index: 管理_FK */
/*==============================================================*/
create index 管理_FK on Department (
TNO ASC
);
/*==============================================================*/
/* Table: MyProject */
/*==============================================================*/
create table MyProject
(
PNO VARCHAR2(10) not null,
ParentPNO VARCHAR2(10),
TNO VARCHAR2(10) not null,
PName NVARCHAR2(30) not null,
PFund NUMBER(20,2),
PBeginDate DATE not null,
PTimeSpan INTEGER not null,
constraint PK_MYPROJECT primary key (PNO)
);
/*==============================================================*/
/* Index: ProjectManager_FK */
/*==============================================================*/
create index ProjectManager_FK on MyProject (
TNO ASC
);
/*==============================================================*/
/* Index: PP_FK */
/*==============================================================*/
create index PP_FK on MyProject (
ParentPNO ASC
);
/*==============================================================*/
/* Table: TM */
/*==============================================================*/
create table TM
(
TNO VARCHAR2(10) not null,
PNO VARCHAR2(10) not null,
WorkDays INTEGER,
constraint PK_TM primary key (TNO, PNO)
);
/*==============================================================*/
/* Index: TM_FK */
/*==============================================================*/
create index TM_FK on TM (
TNO ASC
);
/*==============================================================*/
/* Index: TM2_FK */41
/*==============================================================*/
create index TM2_FK on TM (
PNO ASC
);
/*==============================================================*/
/* Table: Teacher */
/*==============================================================*/
create table Teacher
(
TNO VARCHAR2(10) not null,
DNO VARCHAR2(10) not null,
TName NVARCHAR2(20) not null,
TSex VARCHAR2(3) not null,
TSalary NUMBER(30,2),
TBirthday DATE not null,
constraint PK_TEACHER primary key (TNO)
);
/*==============================================================*/
/* Index: BelongTo_FK */
/*==============================================================*/
create index BelongTo_FK on Teacher (
DNO ASC
);
alter table Department
add constraint FK_DEPARTME_管理_TEACHER foreign key (TNO)
references Teacher (TNO);
alter table MyProject
add constraint FK_MYPROJEC_PP_MYPROJEC foreign key (ParentPNO)
references MyProject (PNO);
alter table MyProject
add constraint FK_MYPROJEC_PROJECTMA_TEACHER foreign key (TNO)
references Teacher (TNO);
alter table TM
add constraint FK_TM_TM_TEACHER foreign key (TNO)
references Teacher (TNO);
alter table TM
add constraint FK_TM_TM2_MYPROJEC foreign key (PNO)
references MyProject (PNO);
alter table Teacher
add constraint FK_TEACHER_BELONGTO_DEPARTME foreign key (DNO)
references Department (DNO);
3.2 项目信息管理数据库初始化数据
--系表数据
insert into department(dno,dname) values('d001', '计算机科学系');
insert into department(dno,dname) values('d002', '网络工程系');
insert into department(dno,dname) values('d003', '软件工程系');
--教师表数据
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t001', '张三', '男', 3000,
To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t002', '李四', '女', 3600,
To_date('21-10-1979', 'DD-mm-yyyy'), 'd001');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t003', '王五', '女', 5600,
To_date('7-7-1981', 'DD-mm-yyyy'), 'd002');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t004', '刘晨', '女', 5800,
To_date('7-7-1985', 'DD-mm-yyyy'), 'd002');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t005', '王二小', '男',
3500, To_date('7-7-1981', 'DD-mm-yyyy'), 'd003');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t006', '李小龙', '男',
5687, To_date('7-12-1990', 'DD-mm-yyyy'), 'd003');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t007', '熊猫', '男', 6000,
To_date('27-11-1980', 'DD-mm-yyyy'), 'd003');
insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t008', '李小小', '女',
5687, To_date('17-10-1985', 'DD-mm-yyyy'), 'd001');


--myproject 数据
insert into myproject(PNO, pname, pfund, pbegindate, ptimespan, parentpno, tno)
values('p0001', '信息安全技术研究', 30, To_date('7-12-2012', 'DD-mm-yyyy'), 3, null,'t001');
insert into myproject(PNO, pname, pfund, pbegindate, ptimespan, parentpno, tno)
values('p0002', '云计算研究', 40, To_date('7-12-2008', 'DD-mm-yyyy'), 4, null, 't003');
insert into myproject(PNO, pname, pfund, pbegindate, ptimespan, parentpno, tno)
values('p0003', '信息中心网络研究', 60, To_date('7-12-2012', 'DD-mm-yyyy'), 6, null,
't006');
insert into myproject(PNO, pname, pfund, pbegindate, ptimespan, parentpno, tno)
values('p0004', '对等网络研究', 30, To_date('7-12-2010', 'DD-mm-yyyy'), 3, 'p0002',
't006');

--tm 数据
insert into tm(tno, pno, workdays) values('t001', 'p0001', 180);
insert into tm(tno, pno, workdays) values('t001', 'p0002', 180);
insert into tm(tno, pno, workdays) values('t001', 'p0004', 360);
insert into tm(tno, pno, workdays) values('t001', 'p0003', 540);
insert into tm(tno, pno, workdays) values('t002', 'p0001', 720);
insert into tm(tno, pno, workdays) values('t002', 'p0002', 90);
insert into tm(tno, pno, workdays) values('t002', 'p0004', 90);
insert into tm(tno, pno, workdays) values('t003', 'p0001', 180);
insert into tm(tno, pno, workdays) values('t003', 'p0002', 360);
insert into tm(tno, pno, workdays) values('t003', 'p0003', 360);
insert into tm(tno, pno, workdays) values('t003', 'p0004', 360);
insert into tm(tno, pno, workdays) values('t004', 'p0001', 180);
insert into tm(tno, pno, workdays) values('t004', 'p0002', 180);
insert into tm(tno, pno, workdays) values('t004', 'p0003', 360);
insert into tm(tno, pno, workdays) values('t005', 'p0004', 180);
insert into tm(tno, pno, workdays) values('t005', 'p0002', 720);
insert into tm(tno, pno, workdays) values('t005', 'p0003', 180);
insert into tm(tno, pno, workdays) values('t006', 'p0004', 360);
insert into tm(tno, pno, workdays) values('t006', 'p0002', 180);
insert into tm(tno, pno, workdays) values('t006', 'p0003', 720);
update department set tno='t006' where dno='d003';
commit;




1. 列出 Teacher 表的所有约束,并说明每个约束的具体含义及其对表列取值的影响; 
select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER';

2. 书写 SQL 语句,在 Teacher 表中插入 2 条记录,记录内容任意设置,要求能取空值的 列均设置为空(提示:如果插入失败,则查看是否满足基本表的约束条件); 
 
insert into teacher(tno, tname, tsex, tbirthday, dno) values('t111', '张八', '男',
To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');
insert into teacher(tno, tname, tsex, tbirthday, dno) values('t222', '张二十', '男',
To_date('7-7-1977', 'DD-mm-yyyy'), 'd001');

3. 利用以下 CREATE TABLE 语句创建表 teacher2,并列出 Teacher2 表的所有约束,比 较 Teacher2 表与 Teacher 表的约束差异; 
create table teacher2 as select * from teacher; 
select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER';
select table_name,constraint_name,constraint_type from user_constraints where table_name='TEACHER2';


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


insert into teacher(tno, tname, tsex, tsalary, tbirthday, dno) values('t008', '李小小', '女',
5687, To_date('17-10-1985', 'DD-mm-yyyy'), 'd001');

错误违反唯一约束条件 实体完整性

insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t008', '李小小', '女',
5687, To_date('17-10-1985', 'DD-mm-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 like '男';

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

alter table teacher add check(tsex='男' or tsex='女');

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

delete from teacher2 where tsalary=6000;

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

delete from teacher2
where dno in(select dnofrom department where dname='计算机科学系');

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

delete from teacher2;

10. 修改 teacher2 表,使列 tno 为主码; 

alter table teacher2 add primary key(tno);

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

alter table teacher2 add unique(tname);

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

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

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

insert into department(dno,dname) values('xyz','xxx');
insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t001', 'Saber', '女', 5000, To_date('8-8月-1987', 'DD-mon-yyyy'), 'xyz'); 
insert into teacher2(tno, tname, tsex, tsalary, tbirthday, dno) values('t002', 'Archer', '男', 6600, To_date('1-10月-1989', 'DD-mon-yyyy'), 'xyz'); 

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

delete from department where dno='d001';
delete from department where dno='xyz';

第一个删除失败,违反了参照完整性约束。

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

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

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

update teacher set tsalary=tsalary+100;

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

update teacher set tsalary=tsalary+100 where dno in(select dno from department where dname='计算机工程系');


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 vt(tno, tname, tsex, tsalary, tbirthday, dno) values('t010', '冷冷', '女', 10000, To_date('21-1月-1992', 'DD-mon-yyyy'), 'd001'); 
insert into vt2(tno, tname, tsex, tsalary, tbirthday, dno) values('t010', '冷冷', '女', 10000, To_date('21-1月-1992', 'DD-mon-yyyy'), 'd001'); 


第一个可以,第二个插入不成功。
with check option 的作用是在对视图进行操作时,会先判断是否符合视图的选择条件,如果不符合拒绝这个操作。
 

你可能感兴趣的:(SQL,数据库实验)