实验二 SQL的数据定义和数据更新
[实验目的和要求]
1.掌握SQL Server Management Studio中以SQL 命令方式和以向导方式操作表和数据的操作;
2.掌握SQL 的数据定义命令,包括:定义表、删除表和修改表以及建立索引和删除索引;
3.掌握SQL 的数据更新命令,包括:插入数据、修改数据和删除数据。
[实验内容]
1、在SQL Server Management Studio中建立数据库“学生-课程”数据库MyDb。
2、在SQL Server Management Studio中以SQL 命令方式实现以下要求:
1)创建表Student 、Course 和SC ,并为每个表定义主键约束; Student(Sno,Sname ,Ssex ,Sage ,Sdept) Course (Cno,Cname ,Cpno ,Ccredit) SC (Sno ,Cno ,Grade )
2)分别向三个表中插入以下数据
3)修改数据
将表Student 中所有学生的年龄加2岁。 将表SC 中所有学生的成绩降低10%。
4)删除数据
将表Student 中Sno 为95004的学生信息删除。
3.SQL Server Management Studio中建立数据库“图书读者”数据库。
4.SQL Server Management Studio中以向导方式实现以下要求:
1)创建表图书(book)、读者(person)和借阅(look),并为每个表定义主键约束;
图书【book】(书号book_number (主键),类别classes,出版社press,作者writer,书名title,定价pricing,出版数量number)
读者【person】 (读者编号Sno(主键),姓名name,单位work_ address,性别Ssex,电话phone)
借阅【look】(书号book_number,读者编号Sno,借阅日期date);
2)定义借阅表与图书表之间以及借阅表与读者表之间的键约束;
3)分别向每个表中插入至少5行模拟数据
4)修改借阅表,增加“归还日期”字段;
5)修改借阅表主键。
联合主键删除,然后用alter重新赋联合主键
[实验思考题]
在完成要求4的过程中,分别向图书表、读者表和借阅表中插入数据时,可以随意向任何一张表中添加数据吗?请分析并解释其中的原因。
不可以随意加入,因为有主外键约束
5.附录代码
create database MyDb
create table Student
(Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
create table Course
(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint,
foreign key(Cpno) References Course(cno)
);
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
);
INSERT
INTO Student(Sno,Sname,Ssex,Sage,Sdept)
VALUES('95001','李勇','男',30,'CS');
INSERT
INTO Student
VALUES('95002','刘晨','女','19','IS');
INSERT
INTO Student
VALUES('95003','王敏','女','18','MA');
INSERT
INTO Student
VALUES('95004','张立','男','19','IS');
insert
into Course
values(2,'数学',null,2);
insert
into Course
values(6,'数据处理',null,2);
insert
into Course
values(4,'操作系统',6,3);
insert
into Course
values(7,'C语言',6,4);
insert
into Course
values(5,'数据结构',7,4);
insert
into Course
values(1,'数据库',5,4);
insert
into Course
values(3,'信息系统',1,4);
insert
into SC(Sno,Cno,Grade)
values('95001',1,32);
insert
into SC
values('95001',2,85);
insert
into SC
values('95001',3,88)
insert
into SC
values('95002',2,90)
insert
into SC
values('95002',3,80)
update Student
set Sage = Sage +2;
update SC
set Grade = Grade*0.1;
delete
from Student
where Sno = '95004'
---------------------------------------------------------------------
create database Library
create table book
(
book_number char(20) primary key,
classes char(20),
press char(30),
writer char(20),
title char(30),
pricing money,
number bigint
);
create table person
(
Sno char(11) primary key,
name char(20),
work_address char(30),
Ssex char(2),
phone bigint
);
create table look
(
book_number char(20),
Sno char(11),
date date,
primary key(book_number,Sno),
foreign key(book_number) references book(book_number),
foreign key(Sno) references person(Sno)
);
insert
into person
values('20211104227','闫冠希','计科2102班','男',18335850028);
insert
into person
values('20211104225','小丽','计科2102班','女',12374196301);
insert
into person
values('20211104217','凯子','计科专升本2102班','男',19635741286);
insert
into person
values('20211104221','小刘子','计科2101班','女',15934712486);
insert
into person
values('20211104205','董卓','计科2102班','男',15642893651);
insert
into person
values('20211104233','泽华','计科2102班','男',17634852176);
insert
into book
values('K565.41 24','历史','上海译文出版社','威廉·多伊尔','法国大革命',58.98,200);
insert
into book
values('K231.04 4','历史','上海古籍出版社','刘向','战国策',99.8,20);
insert
into book
values('TP3121036','计算机','机械工业出版社','黑马程序员','Java面向对象程序设计',78.568,60);
insert
into book
values('I216.2 483','文学','上海译文出版社','林徽因','林徽因文学精品选',68.46,400);
insert
into book
values('I210.1 1:16 ','文学','人民文学出版社','鲁迅文学爱好者','鲁迅全集',86.79,500);
insert
into book
values('k565.08 6','小说','清华大学出版社','简·奥斯汀','傲慢与偏见',46.27,80);
insert
into book
values('K242.09 7','历史','新星出版社','陈舜臣','大唐帝国',54.31,150);
insert
into book
values('J222.49 23','文学','中国社会科学文献出版社','再传弟子编纂','论语',125.63,350);
insert
into look
values('K565.41 24',20211104221,'2021-11-10');
insert
into look
values('K231.04 4',20211104227,'2021-10-06');
insert
into look
values('TP3121036',20211104233,'2021-09-25');
insert
into look
values('I216.2 483',20211104233,'2021-10-25');
insert
into look
values('I210.1 1:16',20211104205,'2021-11-16');
insert
into look
values('k565.08 6',20211104217,'2021-11-18');
insert
into look
values('K242.09 7',20211104227,'2021-10-14');
insert
into look
values('J222.49 23',20211104225,'2021-09-25');
alter table look add breturn date;
ALTER TABLE look ADD PRIMARY KEY(book_number,Sno);