1、实验二 通过SQL语句创建与管理数据表 一、实验目的 (1)掌握查询分析器的使用。 (2)掌握通过SQL语句创建表的方法。 (3)掌握通过SQL语句修改表结构的方法。 (4)掌握通过SQL语句添加、修改、删除表数据的方法。 二、实验内容 1、通过SQL语句删除表 用SQL语句在数据库StudentStudent_info中删除实验一创建的studentStudent表、courseCourse表、SC表。 drop table SC_20103322 drop table Student_20103322 drop table Course_20103322
2、 2、通过SQL语句创建表 用SQL语句在数据库StudentStudent_info中创建实验一中的studentStudent表、courseCourse表、SC表,结构如实验一中表2、表3、表4所示 表2: create table Student_20103322 ( Sno char(8) not null primary key, --学号 Sname varchar(8) not null, --学生姓名 Sex char(2) not null default '男', --性别 Birth smalldatetime not null, --出生年月
3、Classno char(3) not null, --班级号 Entrance_date smalldatetime not null, --入学时间 Homeaddr varchar(40) not null, --家庭住址 ) 表3: create table Course_20103322 ( Cno char(3) not null primary key, --课程号 Cname varchar(20) not null, --课程名称 Total_perior smallint, --总学时 Credit tinyint, --学分 check (T
4、otal_perior>3 and Credit>0 and Credit<=6) ) 表4: create table SC_20103322 ( primary key(Sno,Cno), Sno char(8) not null foreign key references Student_20103322(Sno), --学号 Cno char(3) not null foreign key references Course_20103322(Cno), --课程号 Grade tinyint, --成绩 check(Grade>=0 and Grade<=
5、100) ) 3、通过SQL语句管理表结构 (1)添加和删除列 a. 给studentStudent表增加身高(以米单位)statureStature列 ,类型为numeric(4,2),允许为空值,且身高值需小于3.0米。 alter table Student_20103322 add Stature numeric(4,2), constraint ck_Stature check(Stature<3.0) b. 给studentStudent表增加所在系Sdept列,字符型,长度2,不允许为空值。 alter table Student_20103
6、322 add Sdept char(8) not null c. 给studentStudent表增加邮政篇码Postcode列,字符型,长度为6,可以为空,若不为空时,则要求其值只能出现数字,不能是其它字符。 alter table Student_20103322 add Postcode char(6), constraint ck_ps check(Postcode like '[0-9][0-9][0-9][0-9][0-9][0-9]') d.删除studentStudent表中身高statureStature列。 alter table Student_2
7、0103322
drop ck_Stature
alter table Student_20103322
drop column Stature
(2)添加和删除约束
a.在studentStudent表添加约束:入学时间必须在出生年月之后。
alter table Student_20103322
add constraint ck_data check(Birth 8、fault(0) for Grade
c.删除gradeGrade列的默认值约束
alter table SC_20103322
drop ck_grade
4、 通过SQL语句添加、修改、删除表中数据
(1)插入数据
a.studentStudent表、courseCourse表、SC表的记录见实验一的表5、表6、表7,其它数据可自行添加。要求studentStudent表和SC表中数据包括了每位同学自己的学号。
insert
into Student_20103322
values ('20110001','张虹','男','1992-09-11', 9、'051','2011-09-01','南京','计算机系','200413')
insert
into Student_20103322
values ('20110002','林红','女','1991-11-12','051','2011-09-01','北京','计算机系','100010')
insert
into Student_20103322
values ('20110103','赵青','男','1993-05-11','061','2011-09-01','上海','软件工程','200013')
insert
into Course_2010332 10、2
values ('001','高数','96','6')
insert
into Course_20103322
values ('002','C语言程序设计','80','5')
insert
into Course_20103322
values ('003','JAVA语言程序设计','48','3')
insert
into Course_20103322
values ('004','Visual Basic','48','4')
insert
into SC_20103322
values ('20110001','001','89')
in 11、sert
into SC_20103322
values ('20110001','002','78')
insert
into SC_20103322
values ('20110001','003','89')
insert
into SC_20103322
values ('20110002','002','60')
insert
into SC_20103322
values ('20110103','001','80')
b.执行如下语句:insert into studentStudent(snoSno,snameSname,sexSex) 12、values(‘20101101’,’赵青’,’男’),该语句能成功执行吗?为什么?
不能执行。因为有不允许为Null的列存在。
c. 执行如下语句:insert into sc values(‘20110103’,’005’,80),该语句能成功执行吗?为什么?
不能执行。因为Course_20103322表中的课号只出现了001、002和003,又因为SC_20103322表中Cno是外键,只能出现Course_20103322中Cno中的值,所以不能执行。
(2)修改数据
a.使用T-SQL语句,将courseCourse表中的课程号为’002’的学分改为4, 13、总学时改为64。
update Course_20103322
set Credit=4,Total_perior=64
where Cno='002'
b.使用T-SQL语句,将SC表中的选修了‘002’课程的同学的成绩*80%。
update SC_20103322
set Grade=Grade*0.8
where Cno='002'
(3)删除数据
a. 使用T-SQL语句,删除选修了“C语言程序设计”的学生的选课记录。
delete
from SC_20103322
where Cno='002'
b. 使用T-SQL语句,删除所有的学生选课记录。
delete
from SC_20103322
说明:删除后,请重新插入SC表中的记录。






