《数据库原理》上机实验报告2017年11月一、实验目的与要求:●熟练使用SQL定义子语言、操纵子语言命令语句●掌握关系模型上的完整性约束机制●掌握一定的数据库管理技术●能完成简单的数据库应用开发二、实验内容1、实验一到实验十七(一)数据定义子语言实验(2学时)实验1:利用SQL语句创建Employee数据库代码如下:create database Employee;运行结果:实验2:利用SQL语句在Employee数据库中创建人员表person、月薪表salary 及部门表dept, 暂不定义外键约束。
要求:按表1、表达、表3中的字段说明创建表1 person表结构字段名数据类型字段长度允许空否字段说明表2 salary表结构表3 dept表结构代码如下:create table person(P_no char(6) not null primary key,P_name varchar(10) not null,Sex char(2) not null,Birthdate datetime null,Prof varchar(10) null,Deptno char(4) not null);create table salary(P_no char(6) not null primary key,Base dec(5) null,Bonus dec(5) null,Fact dec(5) null,Month int not null);create table dept(Deptno char(4) not null primary key,Dname varchar(10) not null);运行结果:(二)数据操纵子语言实验(4学时)实验3:利用SQL语句向表person、salary和dept中插入数据。
要求:按表4、表5、表6中的数据插入。
表4 表person中的数据P_no P_name Sex BirthDate Prof Deptno 000001 王云男1973-4-7 中级0001 000002 谢志文男1975-2-14 中级0001 000003 李浩然男1970-8-25 高级0002代码如下:insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000001','王云','男','1973-4-7','中级','0001')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000002','谢志文','男','1975-2-14','中级','0001')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000003','李浩然','男','1970-8-25','高级','0002')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000004','廖小玲','女','1979-8-6','初级','0002')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000005','梁玉琼','女','1970-8-25','中级','0003')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000006','罗向东','男','1979-5-11','初级','0003')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('000007','尚家庆','男','1963-7-14','高级','0003')运行结果:表5 表salary中的数据P_no Base Bonus Fact S_month 000001 2100 300 1 000002 1800 300 1 000003 2800 280 1 000004 2500 250 1 000005 2300 275 1 000006 1750 130 1 000007 2400 210 1代码如下:insert into salary (P_no,Base,Bonus,Fact,Month) values ('000001',2100,300,2100+300,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000002',1800,300,1800+300,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000003',2800,280,2800+280,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000004',2500,250,2500+500,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000005',2300,275,2300+275,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000006',1750,130,1750+130,1) insert into salary (P_no,Base,Bonus,Fact,Month) values ('000007',2400,210,2400+210,1)运行结果:表6 表dept中的数据Deptno Dname0001 人事部0002 财务部0003 市场部代码如下:insert into dept (Deptno,Dname) values ('0001','人事部')insert into dept (Deptno,Dname) values ('0002','财务部')insert into dept (Deptno,Dname) values ('0003','市场部')运行结果:实验4:(1)利用SQL语句修改表中的数据。
要求:将salary表中工号为000006的员工工资增加为1800元,奖金增加为160元。
代码如下:update salaryset Base=1800,Bonus=160,Fact=1800+160where P_no='000006'运行结果:(2)利用SQL语句删除表中的数据。
要求:删除 salary表中工号为000007的员工数据。
代码如下:deletefrom salarywhere P_no='000007'(3)利用SQL语句查询person表中的所有数据。
代码如下:select *from person运行结果:实验5:(1)创建视图要求:创建员工视图PersonView,包含员工的所有信息,并调用视图代码如下:create view PersonView asselectperson.P_no,P_name,Sex,Birthdate,Prof,person.Deptno,Base,Bonus,Fact,Month,Dna mefrom person,salary,deptwhere person.Deptno=dept.Deptno and salary.P_no=person.P_noselect * from PersonView 运行结果:(2)删除视图要求:将视图PersonView删除代码如下:drop view PersonView运行结果:实验6:条件查询要求:(1)查询person表中所有不重复的职称。
(2)查询person表中职称为中级的所有员工数据。
(3)查询person表中具有高级职称的男员工信息。
(4)查询person表中姓名为王云、谢志文、罗向东的员工数据。
代码及运行结果如下:(1)select distinct Prof from person(2)select * from person where Prof='中级'(3)select * from person where Prof='高级' and Sex='男'(4)select * from person where P_name in ('王云','谢志文','罗向东')实验7:使用ORDER BY排序要求:利用SQL语句将工号在000003和000006之间的员工的月收入按实发工资升序排序。
代码如下:select * from salary where P_no between '000003' and '000006' order by Fact asc 运行结果:实验8:利用SQL语句查询各部门的实发工资总数。
代码如下:select dept.Dname,sum(Fact) as "部门实发工资总数"from person,salary,deptwhere person.Deptno=dept.Deptno and salary.P_no=person.P_nogroup by dept.Dname运行结果:实验9:利用SQL语句查询人事部所有员工信息。