实验一 数据定义和数据更新

实验目的:

1. 熟悉数据库的交互式SQL工具

2. 通过本实验能够熟练应用sql语言进行基本表和索引的定义,能够对表的结构进行修改和删除,并通过数据更新命令输入相应的数据.

所以下载sqldeveloper文件夹下的sqldeveloper,右键单击文件夹,点击“复制”命令;


 

实验内容:

(一) 数据定义

一、基本表操作

Tabs:用户创建的表

user_constraints 自定义约束


1.建立基本表

创建教材中的学生表(student)、学生选课表(SC)、课程表(course)

1)·学生表:Student (Sno, Sname,Sage,Ssex,Sdept)其中学号Sno主码

 

2)·课程表:Course (Cno, Cname, Cpno, Ccredit)其中课程号Cno主码;先行课为外码参照Course表中Cno字段。

create table c
(
cno char(4) PRIMARY key,
cname VARCHAR2(10),
cpno CHAR,
ccredit int,
foreign key(cpno)REFERENCES c(cno)
 
);

3)·学生选课表:SC(Sno, Cno, Grade)其中学号Sno、课程号Cno为主码;Sno为外码参照Student表中sno字段;Cno为外码参照Course表中cno字段。

create table sc
(
sno char(10),
cno char(4) ,
grade int,
primary KEY(sno,cno),
FOREIGN key(sno)REFERENCES s(sno),
FOREIGN key (cno)REFERENCES c(cno)
);

2.修改基本表

1)在Student表中加入属性BloodType(char(2)型)。

alter table s

add BloodType char(2)

2)修改表student中的Sdept属性的数据类型为varchar2(40),注意和定义表的时候类型不同

alter table s

modify ssdept varchar2(40)

3)给表student的sage列添加一个自定义约束sage必须大于15且小于30。

alter table s

add check(ssage>15 and ssage<30)

4)删除3)中新添加的约束。

select * from user_constraints where table_name='S';

alter table s

drop CONSTRAINT sys_c0010658

5)删除表student中的字段BloodType。

alter table s

drop (BloodType);

3.删除基本表

1) 删除基本表Student

drop table s cascade CONSTRAINTS

2)删除基本表SC

drop table sc

二、索引操作

1.建立索引

1)在Student表上建立关于Sname的唯一索引stusnam+学号后四位

CREATE UNIQUE INDEX stusnam ON student(sname);

2)在SC表上建立关于Sno升序、Cno降序的唯一索引i_sc+学号后四位

    CREATE UNIQUE INDEX i_sc ON sc(sno asc, cno desc);

2.删除索引

1)删除Student表上的索引stusnam+学号后四位

drop index stusnam

2)删除Course表上的索引i_sc+学号后四位

    drop index i_sc

(二)数据操作

一、数据更新

1.插入数据

1)向Student表中插入数据

insert into student VALUES('200215121','李勇','20','M','CS');
    insert into student VALUES('200215122','刘晨','19','F','CS');
insert into student VALUES('200215123','王敏','18','F','MA');
insert into student VALUES('200215125','张立','19','M','IS');

2)向Course表中插入数据

insert into course VALUES('2','数学','2',null);
      insert into course VALUES('6','数据处理','2',null);
 insert into course VALUES('7','PASCAL语言','4','6');
 insert into course VALUES('5','数据结构','4','7');
  insert into course VALUES('1','数据库','4','5');
 insert into course VALUES('4','操作系统','3','6');
  insert into course VALUES('3','信息系统','4','1');

3)向SC表中插入数据

insert into sc values('1','200215121','92'),
insert into sc values('2','200215121','85'),
insert into sc values('3','200215121','88'),
insert into sc values('4','200215122','90'),
insert into sc values('3','200215122','80');

可参考如下数据,也可不参考。

Student

学号Sno

姓名 Sname

性别 Ssex

年龄 Sage

所在系 Sdept

200215121

李勇

20

CS

200215122

刘晨

19

CS

200215123

王敏

18

MA

200215125

张立

19

IS

Course

课程号 Cno

课程名 Cname

先行课 Cpno

学分Ccredit

1

数据库

5

4

2

数学

 

2

3

信息系统

1

4

4

操作系统

6

3

5

数据结构

7

4

6

数据处理

 

2

7

PASCAL语言

6

4

SC

学号 Sno

课程号Cno

成绩Grade

200215121

1

92

200215121

2

85

200215121

3

88

200215122

4

90

200215122

3

80

2.修改数据

1)将王敏的同学的年龄改为20。

update student set sage='20' where sname='王敏';

2)将全部同学的年龄加1。

    update student set sage=sage+1;

3)将’CS’系同学的选课信息中的成绩置0。

    update sc set grade='0';

3.删除数据

1)删除和’ 刘晨’在同一个系的学生的信息。

Alter Table SC
Drop Constraint sc_fksn
delete from STUDENT where SDEPT=(select sdept FROM student where sname='刘晨');

2)删除’CS’系同学的选课信息。

     delete from sc where cno=(select cno FROM course where cname='CS');

(三)思考题:

1)一个列上有外码约束如何实现。

2)删除表时,表中某一列是另外一个表的外键,此表如何删除。

3)对表中某一列的数据类型进行修改时,要修改的列是否必须为空列。