当前位置:文档之家› 实验二--通过SQL语句创建与管理数据表

实验二--通过SQL语句创建与管理数据表

实验二--通过SQL语句创建与管理数据表
实验二--通过SQL语句创建与管理数据表

实验二

一、实验目的

(1)掌握查询分析器的使用。

(2)掌握通过SQL语句创建表的方法。

(3)掌握通过SQL语句修改表结构的方法。

(4)掌握通过SQL语句添加、修改、删除表数据的方法。

二、实验容

1、通过SQL语句删除表

用SQL语句在数据库Student_info中删除实验一创建的Student表、Course表、SC表。

1、选择Student_info数据库,在该数据库环境中“新建查询”,然后完成删除操作

2、分别填写如下SQL语言

①、drop table Student

②、drop table Course

③、drop table SC

3、删除操作完成

2、通过SQL语句创建表

用SQL语句在数据库Student_info中创建实验一中的Student表、Course表、SC表,结构如实验一中表2、表3、表4(即创建出空表即可)所示

①、创建Student表

create table Student(

Sno char(8)primary key,

Sname varchar(8)not null,

Sex char(2)not null,

Birth smalldatetime not null,

Classno char(3)not null,

Entrance_date smalldatetime not null,

Home_addr varchar(40)

)

②、创建Course表

create table Course(

Cno char(3)primary key,

Cname varchar(20)not null,

Total_perior smallint check(Total_perior>0),

Credit tinyint check(Credit<=6 and credit>0)

)

③、创建SC表

create table SC(

Sno char(8)not null,

Cno char(3)not null,

Grade tinyint check(Grade>=0 and Grade<=100),

primary key(Sno,Cno),

foreign key(Sno)references Student(Sno),

foreign key(Cno)references Course(Cno)

3、通过SQL语句管理表结构

(1)添加和删除列

a. 给Student表增加身高(以米单位)Stature列,类型为numeric(4,2),允许为空

值,且身高值需小于3.0米。

alter table Student

add Stature numeric(4,2)

check(Stature<=3.0 and Stature>=0)

b. 给Student表增加所在系Sdept列,字符型,长度2,不允许为空值。

alter table Student

add Sdept char(2)not null

c. 给Student表增加邮政篇码Postcode列,字符型,长度为6,可以为空,若不为空时,则要求其值只能出现数字,不能是其它字符。

alter table Student

add Postcode char(6)

check(Postcode Like'[1-9][0-9][0-9][0-9][0-9][0-9]')

d.删除Student表中身高Stature列。

①、添加Stature列时就已知该列存在约束条件,若要删除该列,必须先删除约束条件,则

首先必须先找出约束条件的约束名称。以下有两种方法:

1、写入SQL语句找出

alter table Student

drop column Stature

2、运用企业管理器找出

a、打开Student表

b、选择Stature行,单击右键,选择“CHECK约束”

c、约束名称显而易见

②、其次删除Stature约束

alter table Student

drop constraint CK__Student__Stature__1A14E395

③、最后删除Stature列,完成

alter table Student

drop column Stature

(2)添加和删除约束

a.在Student表添加约束:入学时间必须在出生年月之后alter table Student

add constraint birth1

check(Birth

b.给SC表的成绩Grade列增加默认值约束,默认值为0 alter table SC

add constraint grade1

default('0')for grade

c.删除Grade列的默认值约束

锁定Grade列的约束名即可

alter table SC

drop constraint grade1

(grade1 为约束名)

4、通过SQL语句添加、修改、删除表中数据

(1)插入数据

a. Student表、Course表、SC表的记录见实验一的表5、表6、表7,其它数据可自行添加。要求Student表和SC表中数据包括了每位同学自己的学号。

①、向Student表中添加数据

insert into Student values('20110001','虹','男

','1992-09-01','051','2011-09-01','','CS','200413');

insert into Student values('20110002','林红','女

','1991-11-12','051','2011-09-01','','CS','100010');

insert into Student values('20110103','青','男','1993-05-11','061','2011-09-01','','MS','200013');

注意:Sdept的类型为char(2),添加数据是不能写“计算机系”和“软件工程”,应改为“CS”和“MS”

②、向Course表中添加数据

insert into Course values('001','高数','96','6');

insert into Course values('002','C语言程序设计','80','5');

insert into Course values('003','JAVA语言程序设计','48','3');

insert into Course values('004','Visual Basic','48','4');

③、向SC表中添加数据

insert into SC values('20050001','001',89);

insert into SC values('20110001','001',89);

insert into SC values('20110001','002',78);

insert into SC values('20110001','003',89);

insert into SC values('20110002','002',60);

insert into SC values('20110103','001',80);

上面截图所示的外键约束可不予考虑

b.执行如下语句:insert into Student(Sno, Sname, Sex) values(‘20101101’,’青’,’男’),该语句能成功执行吗?为什么?

答:不能成功执行,因为不能将值NULL 插入列'Sdept'中

c. 执行如下语句:insert into sc values(‘20110103’,’005’,80),该语句能成功执行吗?为什么?

答:不能成功执行,因为SC表中的学号“005”的同学在Course表中不存在。而SC表中的Cno 是作为Course表的外键存在的,所以不能成功执行。

(2)修改数据

a.使用T-SQL语句,将Course表中的课程号为’002’的学分改为4,总学时改为64。

update Course

set Credit=4

where Cno='002'

update Course

set Total_perior=64

where Cno='002'

结果:

b.使用T-SQL语句,将SC表中的选修了‘002’课程的同学的成绩*80%。update SC

set Grade=Grade*0.8

where Cno='002'

结果:

(3)删除数据

a. 使用T-SQL语句,删除选修了“C语言程序设计”的学生的选课记录。

删除时,发现存在外键约束,且约束名为FK__SC__Cno__1920BF5C,则先删除约束

删除约束:

alter table SC

drop constraint FK__SC__Cno__1920BF5C

删除记录:

delete from Course

where Cno in

(select Cno from Course

where Cname='c语言程序设计')

结果:

b. 使用T-SQL语句,删除所有的学生选课记录。delete from SC

结果:

说明:删除后,请重新插入SC表中的记录。

同第4题③所示步骤相同,

insert into SC values('20050001','001',89); insert into SC values('20110001','001',89); insert into SC values('20110001','002',78); insert into SC values('20110001','003',89); insert into SC values('20110002','002',60); insert into SC values('20110103','001',80);

结果:

相关主题
文本预览
相关文档 最新文档