数据库原理上机实验-代码及截图_第1页
数据库原理上机实验-代码及截图_第2页
数据库原理上机实验-代码及截图_第3页
数据库原理上机实验-代码及截图_第4页
数据库原理上机实验-代码及截图_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上数据库原理上机实验报告2017年11月一、实验目的与要求:l 熟练使用SQL定义子语言、操纵子语言命令语句l 掌握关系模型上的完整性约束机制l 掌握一定的数据库管理技术l 能完成简单的数据库应用开发二、实验内容1、实验一到实验十七(一)数据定义子语言实验(2学时)实验1:利用SQL语句创建Employee数据库代码如下:create database Employee;运行结果:实验2:利用SQL语句在Employee数据库中创建人员表person、月薪表salary及部门表dept, 暂不定义外键约束。要求:按表1、表达、表3中的字段说明创建表1 person表结构

2、字段名数据类型字段长度允许空否字段说明P_noChar6Not Null工号,主键P_nameVarchar10Not Null姓名SexChar2Not Null性别BirthdateDatetime8Null出生日期ProfVarchar10Null职称DeptnoChar4Not Null部门代码,外键(参照dept表)表2 salary表结构字段名数据类型字段长度允许空否字段说明P_noChar6Not Null工号,主键,外键(参照person表)BaseDec5Null基本工资BonusDec5Null奖金,要求>50FactDec5Null实发工资=基本工资+奖金Month

3、Int2Not Null月份表3 dept表结构字段名数据类型字段长度允许空否字段说明DeptnoChar4Not Null部门代码,主键,DnameVarchar10Not Null部门名称代码如下: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

4、( 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_noP_nameS

5、exBirthDateProfDeptno王云男1973-4-7中级0001谢志文男1975-2-14中级0001李浩然男1970-8-25高级0002廖小玲女1979-8-6初级0002梁玉琼女1970-8-25中级0003罗向东男1979-5-11初级0003肖家庆男1963-7-14高级0003肖家庆男1963-7-14高级0003代码如下:insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','王云','男','1973-4-7',

6、9;中级','0001')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','谢志文','男','1975-2-14','中级','0001')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','李浩然','男','1970-8-25'

7、,'高级','0002')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','廖小玲','女','1979-8-6','初级','0002')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','梁玉琼','女','1970-8-25&#

8、39;,'中级','0003')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','罗向东','男','1979-5-11','初级','0003')insert into person (P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','尚家庆','男','1963-7-

9、14','高级','0003') 运行结果:表5 表salary中的数据P_noBaseBonusFactS_month21003001180030012800280125002501230027511750130124002101代码如下:insert into salary (P_no,Base,Bonus,Fact,Month) values ('',2100,300,2100+300,1)insert into salary (P_no,Base,Bonus,Fact,Month) values ('',1800,

10、300,1800+300,1)insert into salary (P_no,Base,Bonus,Fact,Month) values ('',2800,280,2800+280,1)insert into salary (P_no,Base,Bonus,Fact,Month) values ('',2500,250,2500+500,1)insert into salary (P_no,Base,Bonus,Fact,Month) values ('',2300,275,2300+275,1)insert into salary (P_no

11、,Base,Bonus,Fact,Month) values ('',1750,130,1750+130,1)insert into salary (P_no,Base,Bonus,Fact,Month) values ('',2400,210,2400+210,1)运行结果:表6 表dept中的数据DeptnoDname0001人事部0002财务部0003市场部代码如下:insert into dept (Deptno,Dname) values ('0001','人事部')insert into dept (Deptno,Dn

12、ame) values ('0002','财务部')insert into dept (Deptno,Dname) values ('0003','市场部')运行结果:实验4:(1)利用SQL语句修改表中的数据。要求:将salary表中工号为的员工工资增加为1800元,奖金增加为160元。代码如下:update salaryset Base=1800,Bonus=160,Fact=1800+160where P_no=''运行结果:(2)利用SQL语句删除表中的数据。要求:删除 salary表中工号为的员

13、工数据。代码如下:delete from salary where P_no=''(3)利用SQL语句查询person表中的所有数据。代码如下:select * from person运行结果:实验5:(1)创建视图要求:创建员工视图PersonView,包含员工的所有信息 ,并调用视图代码如下:create view PersonView as select person.P_no,P_name,Sex,Birthdate,Prof,person.Deptno,Base,Bonus,Fact,Month,Dname from person,salary,dept where

14、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 pe

15、rson(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语句将工号在和之间的员工的月收入按实发工资升序排序。代码如下:select * from salary where P_no between '

16、;' and '' order by Fact asc运行结果:实验8:利用SQL语句查询各部门的实发工资总数。代码如下:select dept.Dname,sum(Fact) as "部门实发工资总数" from person,salary,dept where person.Deptno=dept.Deptno and salary.P_no=person.P_no group by dept.Dname运行结果:实验9:利用SQL语句查询人事部所有员工信息。代码如下:select * from person,salary,dept where

17、person.Deptno=dept.Deptno and salary.P_no=person.P_no and dept.Dname=人事部运行结果:实验10:表的内连接查询:要求:利用SQL语句查询person表中职称为中级的员工信息。代码如下:select * from person where prof='中级'运行结果:实验11:表的外连接查询:要求:利用SQL语句查询每个员工1 月份的工资和奖金代码如下:select person.P_no,person.P_name,salary.Base,salary.Bonusfrom person left outer j

18、oin salary on (person.P_no=salary.P_no)运行结果:实验12:子查询:要求:利用SQL语句查询比工号为的员工实发工资高的所有员工信息。代码如下:select * from person,salarywhere Fact>(select Fact from salary where P_no='') and person.P_no=salary.P_no(三) 数据完整性实验(1学时)实验13:定义外键约束 要求:(1) 创建表时将person表的deptno列定义为外键,并参考dept表的列deptno。代码如下:alter table

19、 person add constraint personkey foreign key (Deptno) references dept(Deptno)(2)将salary表中的P_no设为外键,并使其参照person表中的列P_no。代码如下:alter table salaryadd constraint salarykey foreign key (P_no) references person(P_no)运行结果:实验14:测试对主表进行插入、更新及删除操作时的影响。(请写明原因) 要求:(1) 向表dept中插入一行数据(0004,研发部),测试是否影响从表。代码如下:insert

20、 into dept(Deptno,Dname) values ('0004','研发部')运行结果:不影响从表,插入的数据与外键约束的数据没有联系。(2) 将表dept中的部门号0003改为0006,测试是否影响从表。代码及运行结果如下:update dept set Deptno='0006' where Deptno='0003'分析:影响从表,0006为原本不存在的数据,修改时受外键约束无法实现。(3) 删除表dept中部门号为0001的员工的数据,测试是否影响从表。代码及运行结果如下:delete from dept w

21、here Deptno='0001'分析:影响从表,删除表dept中部门号为0001的员工的数据,修改时受外键约束无法实现。实验15:测试对从表进行插入、更新及删除操作时的影响。(请写明原因)要求:(1) 向表person中插入一行数据(,宋全礼,男、1980-7-17,初级,0005),测试是否违背参照完整性。代码及运行结果如下:insert into person(P_no,P_name,Sex,Birthdate,Prof,Deptno) values ('','宋全礼','男','1980-7-17',&#

22、39;初级','0005');分析:违背参照完整性,更新数据的操作受到约束无法实现。(2) 删除表中工号为的员工数据,测试是否违背参照完整性。代码及运行结果如下:delete from person where P_no=''分析:违背参照完整性,删除数据的操作受到约束无法实现。(四) SQL Server安全设置实验(1学时) 实验 16:创建登录账号(1) 创建SQL Server登录账号 要求:基于SQL Server创建登录账号kmust、student、testdeletesp_addlogin loginame=kmust,passwd='',defdb=N'Employee',deflanguage=N'Simplified Chinese',sid=null,encryptopt=nullsp_addlogin loginame=student,passwd='',defdb=N'Employee',deflanguage=N'Simplified Chines

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论