数据库实验-电子科技大学.docx
电占种域丈学计算机专业类课程实验报生课程名称:数据库系统及应用学院:计算机科学与工程专业:计算机科学与技术学生姓名:李鹤鸣指导教师:郑莉华日期:2016年4月24日龟孑科拉大孽实验报告实验一一、实验名称:创立,备份与恢复数据库二、实验学时:2三、实验内容和目的:本实验要求学生掌握创立数据库的方法及相关操作,向数据库中添加样本数据,学习SQLSERVER数据库的恢复和备份。创立数据库,命名为db+学号,如:Dbooooooooi该数据库包括5个表:建议:在定义表中字段时,尽量使用意义的英文单词。,系别代码表表名tdep'教师表,表名:teacher,学生表,表名:StUd,课程表,表名:COUrSe、选课表,表名*SC为每个表准备大约5-10条记录,使用Insert语句将这些数据插入到相应表中数据录入完成后,将数据库备份到磁盘上,在以后的的试验中备用。四、实验原理:使用数据库管理系统DB、DDL创立数据库及数据库对象。五、实验器材(设备、元器件)计算机,MicrosoftSQLsever2014六、实验步骤:启动SQLSERVER创立数据库:STUD创立表,系别代码表表名:dep其结构如下:(该表的主键为'系代码')字段名称字段类型字段大小/格式是否可为空系代码depidvarchar8否PK系名depnamevarchar20否Notnull、教师表'表名:teacher其结构如下:(该表的主键为教师号')字段名称字段类型字段大小/格式是否可为空教师号tidVarchar8否PK教师名tnameVarchar8否Notnull职称titleVarchar10是所属院系编号depidVarchar20是,学生表,表名:student其结构如下:(该表的主键为,学生号,)字段名称字段类型字段大小/格式是否可为空学号SidVarchar11否PK学生名snameVarchar8否Notnull性别sexChar2行院系编号depidVarchar20是出生年月birthdDate是邮箱semailVarchar20是家庭地址homcaddrVarchar40是,课程表'表名:ColIrSe其结构如下:(该表的主键是课程号)字段名称字段类型字段大小/格式是否可为空课程号Cidvarchar8否PK课程名cnamevarchar30否Notnull先修课程号cid_prevarchar8是学分creditsnumeric3(小数位数1)否Notnull,选课表,表名:SC其结构如下:(该表的主键是课程号)字段名称字段类型字段大小/格式是否可为空学号Sidvarchar8否Notnull,PK课程号Cidvarchar8否Notnull,PK教师号tidvarchar8否成绩scoreinteger是一、备份数据库二、恢复数据库PK:主键,NOtnUl1:不能取空值七、实验数据及结果分析:Createtabledep(depidvarchar(8)primarykey,depnamevarchar(20)notnull);Createtableteacher(tidvarchar(8)primarykey,tnamevarchar(8)notnull,titlevarchar(10),depidvarchar(20);Createtablestudent(sidvarchar(8)primarykey,snamevarchar(8)notnull,sexchar(2)notnull,depidvarchar(20),birthddate,semailvarchar(20),homeaddrvarchar(40);Createtablecourse(cidvarchar(8)primarykey,cnamevarchar(30)notnull,cid_prevarchar(8),creditsnumeric(3,l)notnull);Createtablesc(sidvarchar(8)notnull,cidvarchar(8)notnullprimarykey,tidvarchar(8)notnull,scoreinteger);insertintodep(depid,depname)VaIUeS('01','计算机');insertintodep(depid,depname)values('02',"fB');insertintodep(depid,depname)ValUeS('03','通信');insertintodep(depid,depname)values('04'X,);insertintodep(depid,depname)VaIUeS('05;自动化');insertintoteacher(tid,mame,title,depid)values('200601?文IJ琦?讲师','03');insertintoteacher(tid,tname,title,depid)vaIues('200602;林闲?讲师','02');insertintoteacher(tid,t11ame,title,depid)values('200605','ii"JS',0);insertintoteacher(tid,tname,title,depid)values('200604;曹薇?讲师','04');insertintoteacher(tid,mame,title,depid)values('200608;孙峰?讲师','05');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('201407张澄','男,O,1994.10.15',l3456289163','江苏省南京市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('201402'明r云V男,0271996.1.11,1465187163','河北省邯郸市');insertintostudent(sid,sname,sex,depid,birthd,semail,hmeaddr)values(,201403,;孔玲珑7女,103,71995.6.25'97644163163','福建省泉州市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('2014047雷破男,'04','l994.2.14'4941647163,东省深圳市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('201405,;李-II男,05V1994.5.26V8794164163','湖北省武汉市');insertintoCoUrSe(Cid,cname,cid_pre,CreditS)ValUeS('00101:离散数学','10001',2);insertintoCoUrSe(Cid,cname,cicLPre,credits)values('00102','大学物理下','OOlOO',2);insertintoCOUrSe(Cid,cname,cid_pre,CreditS)ValUeS('00103','数据结构','1OOOO','3');insertintocourse(cid,cname,cid_pre,credits)values('00104,'M½'10002,4');insertintocourse(cid,cname,cid_pre,credits)vaIues('00105','i+MM½','00010'1');insertintosc(sid,cid,tid,score)values('201401','OO101','200601,80');insertintosc(sid,cid,tid,score)values('201402,00102','200604','76');insertintosc(sid,cid,tid,score)values('201403'00103','200605','80');insertintosc(sid,cid,tid,score)values('201404','00104','200608,841);insertintosc(sid,cid,tid,score)values('201405,00105','200602',86');JDB2014060101011JRp<xtSrSSQLE×PRESSUReportS<<wSSQLEXPRESSJh三Hg*.MtJ“速MicrsoftSQLServerMan*9fnntStudio216)UlDesktoplasrikosqlexpReSS0WS!78aKDB201406010101.iPLASKTKCr,ijL.100%日口表田二(FiIeTabIes国口dbo.course口dbo.dep国3dbo.scS)Zdbo.student国Hdbo.teacherCDEifil®QDB2014060101011囹口数据库关系图IS=dbo.course®3dbo.dep田口dbo.scSdbo.studentGBndbo.teacher)口视图I口同义词ICi可编程性二dbo.course口dbo.dep口dbo.sc口dbo.student口dbo.teacher飕同义词可编程性国3dbo.course(±)Hdbo.depE)口dbo.scE23dbo.studentGE)2dbo.teacher囹口视图同义词raBTiS倒CD_JUMM.K.VUI5C3dbo.dep国3dbo.sc田口dbo.studentS)二dbo.teacher鹿同义词1CidIOOiOiICname离散数学cidj>re10001credits2200102大学物理下OOlOO23103数据结构l003400104瞬论l0245105计算机导论0101口结果IL消息结果J消息depiddepname1Soi计算机202信软303通信404电工505自幼化口结果为消息sidCidtidscore1!201401I10120060180220140200102200604763201403103200605804201404001042006088452014050010520060286I口结果4消息sidznmttsxdpidbirthdyhomddr1201401张潢里011994-10-1513456289<8163,com江苏省南京市2201402明青云里021996-01-11465187可河北省邯郸市3201403孑於珑女031995-06-2597644163(福建省泉州市4201404雷破男M1994-02-144941647(8163.co.广东省深圳市5201405李米里051994-05-2687941G4(8163.cm湖北省武汉市IlI口结果上消息tidtnmetitledepid156茁6丁;刘埼讲师032200602林闲讲师023200604曹薇讲师044200605李迁教授01l5200608油蟋itt三05八、实验结论、心得体会和改良建议:按步骤进行试验,本实验相对简单。龟孑科拉大孽实验二三、实验名称:数据库的完整性四、实验学时:4五、实验内容和目的:通过设置表的检查约束、外键约束体会数据库完整性的含义,约束条件下数据修改操作的限制,以及实现修改操作的技巧。表结构中的完整性约束条件如下:PK:主键,FK:外键,其它:约束条件,系别代码表,表名:dep其结构如下:(该表的主键为,系代码,)字段名称字段类型字段大小/格式是否可为空约束条件系代码depidvarchar8否PK系名depnamevarchar20否Notnull、教师表'表名:teacher其结构如下:(该表的主键为,教师号,)字段名称字段类型字段大小/格式是否可为空约束条件教师号tidVarchar8否PK教师名tnameVarchar8否Notnull职称titleVarchar10是所属院系编号depidVarchar20是,学生表,表名:student其结构如下:(该表的主键为,学生号,)字段名称字段类型字段大小/格式是否可为空约束条件学号SidVarchar11否PK学生名snameVarchar8否Notnull性别sexChar2否院系编号depidVarchar20是FK出生年月birthdDale是邮箱semailVarchar2()是家庭地址homeaddrVarchar40是,课程表'表名:COiIrSe其结构如下:(该表的主键是课程号)字段名称字段类型字段大小/格式是否可为空约束条件课程号Cidvarchar8否PK课程名cnamevarchar30否Notnull先修课程号cid_prevarchar8是学分creditsnumeric3(小数位数1)否Notnull,选课表'表名:SC其结构如下:(该表的主键是课程号)字段名称字段类型字段大小/格式是否可为空约束条件学号Sidvarchar8否Notnull,PK,FK课程号Cidvarchar8否Notnull,PK,FK教师号tidvarchar8否Notnull,FK成绩scoreinteger是0<score<100(1)设置选课表的三个外键约束(学号,课程号,教师号)(2)设置教师表,学生表中的院系字段(depid)的外键约束(3)设置学生表中姓名字段为非空字段(必须有数据,不能是空值)(4)设置选课表中成绩字段的取值范围是0到100(5)设置学生表中性别字段的取值为男,或,女(6)设置学生表电子邮件字段的取值必须包含符号(8)向'系别代码表添加数据,数据如下:向教师表'(10)向学生表(11)向'课程表'(12)向,选课表,添加数据,2条添加数据,5条以上添加数据,3条以上添加数据,10条六、实验原理:数据库库的完整性、约束条件、结构化查询语言。七、实验器材(设备、元器件)计算机,MicrosoftSQLsever2014.八、实验步骤:恢复数据库,将数据库备份StUd恢复到SQLSERVER中执行SQL命令完成实验内容备份数据库九、实验数据及结果分析:Createtabledep(depidvarchar(20)primarykey,depnamevarchar(20)notnul1);Createtableteacher(tidvarchar(8)primarykey,tnamevarchar(8)notnull,titlevarchar(10),depidvarchar(20),foreignkey(depid)referencesdep(depid);Createtablestudent(sidvarchar(8)primarykey,snamevarchar(8)notnull,sexchar(2)notnull>depidvarchar(20),birthddate»semai1varchar(20),homeaddrvarchar(40)>check(sexin('男','女'),check(semai1like,%0%,);Createtablecourse(cidvarchar(8)primarykey,cnamevarchar(30)notnul1,cid_prevarchar(8),creditsnumeric(3,1)notnul1);Createtablesc(sidvarchar(8)notnul1,cidvarchar(8)notnull,tidvarchar(8)notnull,scoreinteger,check(scorebetween0and100),foreignkey(sid)referencesstudent(sid),foreignkey(cid)referencescourse(cid),foreignkey(tid)referencesteacher(tid);insertintoinsertintoinsertintoinsertintoinsertintodep(depid,depname)dep(depid,depname)dep(depid,depname)dep(depid,depname)dep(depid,depname)VaIUeS('01','计算机');VaIUeS('02','信软');values(,03,通信');values(,04,电工');values(,05,自动化');insertintoinsertintoteacher(tid,tname,title,depid)values(,200605','李迁','教授,'0T);teacher(tid,tname,title,depid)values(,200604,曹薇,讲师,02');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr)valuesC20140T,'张澄','男01'1994.10.15'13456289Q163','江苏省南京市');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr),02,1996.1.11,465187163','河北省邯郸市');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr)',03','1995.6.25','97644163163','福建省泉州市');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr),04,/1994.2.14','4941647163','广东省深圳市');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr),05','1994.5.26','8794164163',湖北省武汉市');insertintostudent(sid,sname,sex,depid,birthd,semai1,homeaddr),0/1995.5.28','8745156163','湖北省孝感市');values(,201402','明青云','男valuesC201403',孔玲珑','女valuesC201404','雷破','男values(,201405','李肃',男values(,201406',林清',女insertintocourseinsertintocourseinsertintocourseinsertintocourseinsertintocourse(cid,cname,cid_pre,credits)(cid,cname,cid_pre,credits)(cid,cname,cid_pre,credits)(cid,cname,cid_pre,credits)(cid,cname,cid_pre,credits)valuesC00101,t,离散数学',T000T,'2');valuesC00102,t,大学物理下','00100','2');valuesC00103,/数据结构','10000','3');VaIUeS('00104'概率论','10002'4');ValUeS(Oolo5'计算机导论00010',T');insertintoinsertintoinsertinto(sid,cid,tid,score)(sid,cid,tid,score)(sid,cid,tid,score)insertintosc(sid,cid>tid,score)insertintoinsertintoinsertintoinsertintoinsertintoinsertinto(sid,cid,tid,score)(sid,cid,tid,score)(sid,cid,tid,score)(sid,cid>tid,score)(sid,cid,tid,score)(sid,cid,tid,score)valuesC201401,00101,/200605',80')valuesC201402','OOIO2','200604',76')valuesC201403,00103,/200605',80')values(,201404,00104,/200604','84')valuesC201405','OOlO5','200604',86')valuesC201401','OOIO2','200604',69')values('201402,00101,/200605,85,)values('201403,t,00104,/200604',80')valuesC201404','OOlO3','200605',75')valuesC201405','OOlO1','200605',94')还原计MicrosoftSQLServerManagementStudio,DB2014060101011w.还原TOP-LASFGE)口dbo.course3dbo.dep口dbo.sc田"3dbo.student口dbo.teacherJ视图口同义词Cj可编程性E3dbo.depEdbo.sc(S口dbo.studentE)口dbo.teacher口视图1.J同义词消息S结果3CidcnameCid_preeredits1IOOlOlI:离散数学100012.0200102大学物理下001002.0300103数据结构100003.0400104概率论100024.0500105计算机导论000101.0depiddepname1IOi计算机202信软303通信404电工505自动化一J阳禾一口洎思B2014060101011J3三库关系图J表3a系统表3dFiIeTabIesDmdbo.course0Z3dbo.depDmdbo.sc3mdbo.studentDndbo.teacherB视图a局。沟sidcidHdscoreI!201401!001012006058022014020010220060476320140300103200605804201404001042006048452014050010526048662014010010220060469720140200101200605858201403001042006048092014040010320060575102014050010120060594iFiIeTabIes口dbo.course3dbo.dep3dbo.sc3dbo.student3dbo.teacher见图I;CJtudenteacher1sidSname张澄sexSdepidEirthdSemail13456289Komeaddr江苏省南京市201401三011994-10-152201402明青云男021996-01-11465187河北省邯郸市3201403孔玲珑女031995-06-2597644163福建省泉州市4201404雷破男041994-02-14广东省深圳市5201405李肃男051994-05-268794164湖北省武汉市I16201406栋清tid200604I;200605男tname曹薇李迁011995-05-titiedepid讲师02教授01湖北省孝感市十、实验结论、心得体会和改良建议:注意Sql语句语法,认真编写代码根本就可以顺利完成实验。孑科放大孽实验三一实验名称:数据的修改二实验学时:2三实验内容和目的:练习UPDATE、DELETE命令的使用,实现对数据的修改和删除。(1)将院系中,原院系名'IS,改为'Information,(2)在选课表中,删除计算机科学与工程系学生选修2号课程的记录(3)在选课表中,删除软件工程系学生选课1号课程的纪录记录(4)学号为2406010103的同学由原来的计算机科学与工程系转入信息平安系,学号更改为2406030102,在数据库中做出相应修改。四实验原理:使用结构化查询语言,在满足约束条件的情况下完成数据修改五实验器材(设备、元器件)计算机,MicrosoftSQLsever六实验步骤:1 .恢复数据库,将数据库备份StUd恢复到SQLSERVER中2 .执行SQL命令完成实验内容3 .备份数据库七实验数据及结果分析:Createtabledep(depidvarchar(20)Primarykey,depnamevarchar(20)notnull);Createtableteacher(tidvarchar(8)primarykey,tnamevarchar(8)notnull,titlevarchar(10),depidvarchar(20),foreignkey(depid)referencesdep(depid);Createtablestudent(sidvarchar(20)primarykey,snamevarchar(8)notnull,sexchar(2)notnull,depidvarchar(20),birthddate,semailvarchar(20),homeaddrvarchar(40),check(sexin('男?女'),check(semaillike,%');Createtablecourse(cidvarchar(8)primarykey,cnamevarchar(30)notnull,cid-prevarchar(8),creditsnumeric(3J)notnull);Createtablesc(sidvarchar(20)notnull,cidvarchar(8)notnull,tidvarchar(8)notnull,scoreinteger,check(scorebetween0and100),foreignkey(sid)referencesstudent(sid),foreignkey(cid)referencescourse(cid),foreignkey(tid)referencesteacher(tid);insertintodep(depid,depname)VaIUeS('01','计算机');insertintodep(depid,depnamc)VaIUeS('02','软件工程);insertintodep(depid,depname)VaIUeS('03','信息平安');insertintodep(depid,depname)values('04'ZE,);insertintodep(depid,depname)VallIeS('05;自动化');insertintodep(depid,depname)ValUeS('06','通信');insertintoteacher(tid,tname,title,depid)values('200605,'ii'J,0);insertintoteacher(tid,t11ame,title,depid)values('200604'1fVfffi',02,);insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('240601010,1张澄?男,01,1994.10.15':13456289163','江苏省南京市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values(,240602010/明青云,男,02'1996.1.1,465187163','河北省邯郸市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('2406020102','孑L玲珑",女,02,1995.6.25'97644163163福建省泉州市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('240603010/雷破男,03'71994.2.14','4941647163广东省深圳市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('240604010/李肃?男,04','l994.5.26','8794164163;湖北省武汉市上insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('2406010102,/林清女,01,1995.5.28V8745156163湖北省孝感市');insertintostudent(sid,sname,sex,depid,birthd,semail,homeaddr)values('24060101031,'石心远男,O,1996.12.30','45161664163吉林省长春市');insertintoCoUrSe(Cid,cname,cicLPre,credits)values('0001','离散数学','0002',2);insertintoCoUrSe(Cid,cn