西南交通大学数据库原理实验课程设计.docx
- 文档编号:28556781
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:17
- 大小:21.17KB
西南交通大学数据库原理实验课程设计.docx
《西南交通大学数据库原理实验课程设计.docx》由会员分享,可在线阅读,更多相关《西南交通大学数据库原理实验课程设计.docx(17页珍藏版)》请在冰豆网上搜索。
西南交通大学数据库原理实验课程设计
2014-2015学年第一学期
《数据库原理》
课程实验报告
学号:
学生姓名:
徐玉松
班级:
软件工程2012
教师:
陶宏才
辅导老师:
王泽洲赵红芳
2014年12月
实验一:
表及约束的创建
实验目的与内容
目的:
创建数据表、添加和删除列、实现所创建表的完整性约束。
内容:
11-2、11-26~33。
注:
实验内容编号均取自《数据库原理及设计(第2版)》第11章的实验!
即:
实验内容以第2版教材为准!
报告:
以11-31作为实验一的报告。
实验代码及结果
1.2.1实验代码
createtableperson--创建新表
(
P_nochar(6)primarykey,
P_namevarchar(10)notnull,
Sexchar
(2)notnull,
Birthdatedatetimenull,
Date_hireddatetimenotnull,
Deptnamevarchar(10)notnullDEFAULT'培训部',
P_bosschar(6)null,
constraintbirth_hire_check--为约束创建一个名称
check(Birthdate ) createtablecustomer ( Cust_nochar(6)primarykey, Cust_namevarchar(10)notnull, Sexchar (2)notnull, BirthDatedatetimenull, Cityvarchar(10)null, DiscountDec(4,2)notnull, constraintDiscount_check--检查约束的名称 check(Discount>=andDiscount<=1)--检查约束 ) --createruledas@statebetween0and1--创建规则 --sp_bindruled,''--这种方法也可以给Discount约束绑定规则 createtableorderdetail ( Order_nochar(6)primarykey, constraintOrder_no_constraint CHeck(Order_noLIKE'[A-Z][A-Z][0-9][0-9][0-9][0-9]'), Cust_nochar(6)notnull, P_nochar(6)notnull, Order_totalintnotnull, Order_datedatetimenotnull, constraintperson_contr FOREIGNKEY(P_no)--定义外键为P_no REFERENCESperson(P_no)--外键参照主表person中的P_no ondeleteNOAction--参照定义为不许删除 onupdatecascade,--定义为可随着主表跟新 constraintcusrtomer_contr foreignkey(Cust_no) REFERENCEScustomer(Cust_no)--参考 ondeleteNOAction onupdatecascade ) createtablesalary ( P_noChar(6)primarykey, BaseDec(8,2)notnull, BonusDec(8,2)notnull, FactASBase+Bonus, constraintperson2_contr FOREIGNKEY(P_no) REFERENCESperson(P_no) ondeleteNOAction onupdatecascade )--建表完成 1.2.2实验结果 注: 仅附有实际意义的结果。 运行代码得到结果后拷屏,用Windows画图工具切下有意义的部分,然后粘贴到此处。 Person表的创建 Customer表的创建 Orderdetail表的创建 Salary表的创建 实验二: SQL更新语句 实验目的与内容 目的: update、delete、insert语句的练习。 内容: 11-6~8。 报告: 以11-7、11-8作为实验二的报告。 实验代码及结果 2.2.1实验代码 实验数据准备: insertintoperson--插入person表的数据 values('000001','林峰','男','1975-04-07','2003-08-03','销售部','000007') insertintoperson values('000002','谢志文','男','1975-02-14','2003-12-07','培训部','000005') insertintoperson values('000003','李浩然','男','1970-08-25','2000-05-16','销售部','000007') insertintoperson values('000004','廖小玲','女','1979-08-06','2004-05-06','培训部','000005') insertintoperson values('000005','梁玉琼','女','1970-08-25','2001-03-13','培训部','NULL') insertintoperson values('000006','罗向东','男','1979-05-11','2000-07-09','销售部','000007') insertintoperson values('000007','肖佳庆','男','1963-07-14','1988-06-06','销售部','NULL') insertintoperson values('000008','李浩然','男','1975-01-30','2002-04-12','培训部','000005') insertintoperson values('000009','赵文龙','男','1969-01-20','1996-08-12','培训部','000007') INSERTINTOcustomer--为customer表插入数据 VALUES('000001','王云','男','1972-01-30','成都','') INSERTINTOcustomer VALUES('000002','林国平','男','1985-08-14','成都','') INSERTINTOcustomer VALUES('000003','郑洋','女','1973-04-07','成都','') INSERTINTOcustomer VALUES('000004','张雨洁','女','1983-09-06','北京','') INSERTINTOcustomer VALUES('000005','刘菁','女','1971-08-20','北京','') INSERTINTOcustomer VALUES('000006','李宇中','男','1979-08-06','上海','') INSERTINTOcustomer VALUES('000007','顾培铭','男','1973-07-23','上海','') INSERTINTOorderdetail--为orderdetail表插入数据 VALUES('AS0058','000006','000002','150000','2006-04-05') INSERTINTOorderdetail VALUES('AS0043','000005','000005','90000','2006-03-25') INSERTINTOorderdetail VALUES('AS0030','000003','000001','70000','2006-02-14') INSERTINTOorderdetail VALUES('AS0012','000002','000005','85000','2005-11-11') INSERTINTOorderdetail VALUES('AS0011','000007','000009','130000','2005-08-13') INSERTINTOorderdetail VALUES('AS0008','000001','000007','43000','2006-06-06') INSERTINTOorderdetail VALUES('AS0005','000001','000007','72000','2006-05-12') INSERTINTOorderdetail VALUES('BU0067','000007','000003','110000','2006-03-08') INSERTINTOorderdetail VALUES('BU0043','000004','000008','70000','2006-12-25') INSERTINTOorderdetail VALUES('BU0039','000002','000005','90000','2006-10-12') INSERTINTOorderdetail VALUES('BU0032','000006','000002','32000','2006-08-08') INSERTINTOorderdetail VALUES('BU0021','000004','000006','66000','2006-04-01') INSERTINTOorderdetail VALUES('CX0044','000007','000009','80000','2006-12-12') INSERTINTOorderdetail VALUES('CX0032','000003','000001','35000','2006-09-18') INSERTINTOorderdetail VALUES('CX0025','000002','000003','90000','2006-05-02') INSERTINTOorderdetail VALUES('CX0022','000001','000007','66000','2006-12-04') insertintosalary--为salary表插入数据 values('000001','2100','300') insertintosalary values('000002','1800','300') insertintosalary values('000003','2800','280') insertintosalary values('000004','2500','250') insertintosalary values('000005','2300','275') insertintosalary values('000006','1750','130') insertintosalary values('000007','2400','210') insertintosalary values('000008','1800','235') insertintosalary values('000009','2150','210')--数据插入均完成 2.2.1.111-7实验代码 updatesalarysetBase=1800whereP_no=000006--跟新号员工的工资和奖金 updatesalarysetBonus='160'whereP_no=000006 updatesalary--将两年内没有订单的员工奖金下调25% setBonus=Bonus*.75 wherenotexists(select*fromorderdetail where =andOrder_date>=GETDATE()-365*2) --getdate获取当前日期 2.2.1.211-8实验代码 deletefrompersonwherep_no=000010--删除号员工信息 2.2.2实验结果 2.2.2.111-7实验结果 将salary表中共哈维000006的员工工资增加为1800,奖金增加为160 下调成功 将两年内没有签订单的员工奖金下调25% 2.2.2.211-8实验结果 由于person表中没有000010号员工,故有0行受到影响 实验三: SQL查询语句 实验目的与内容 目的: select语句中各种查询条件的实验。 内容: 11-12~18。 报告: 以11-13、11-14作为实验三的报告。 实验代码及结果 3.2.1实验代码 3.2.1.111-13实验代码 selectdistinctDeptnamefromperson--查询person表中的不同部门 select*frompersonwhereSex='女'andP_boss='null'--isnull--查询女经理的数据 select*frompersonwhereP_namein('林峰','谢志文','罗向东')--查询姓名为林峰谢志文罗向东的信息 select*fromsalarywhereP_nobetween'000003'and'000008'orderbyFactasc--把员工号为000003到0000008的员工按薪水排序 selectP_no工号,2*Base+*Bonus收入fromsalarywhereP_no='000002'--查询工号为000002的员工的基本工资加倍,奖金加.5倍后的实际收入 3.2.1.211-14实验代码 selectDeptname部门,avg(Bonus)平均奖金fromsalaryajoinpersonbon= groupbyDeptnamehavingavg(Bonus)>200orderbyavg(Bonus)desc--查询平均奖金在元以上的部门并排序join的用法 selectcount(*)订单数,sum(Order_total)订单总额 fromorderdetail,customer where=andCity='上海'--查询上海顾客的订单数和订单总额 3.2.2实验结果 3.2.2.111-13实验结果 Person表中确实只有培训部和销售部 女经理确实只有梁玉琼一位,上述输出正确。 查询姓名为林峰谢志文罗向东的信息 查询结果正确。 把员工号为000003到0000008的员工按薪水排序。 从结果看出排序正确。 查询工号为000002的员工的基本工资加倍,奖金加.5倍后的实际收入 经计算上述输出正确 3.2.2.211-14实验结果 查询平均奖金在元以上的部门并排序 因为没有奖金平均数大于200元的部门存在,故没有输出。 查询上海顾客的订单数和订单总额 上海顾客的总订单数和订单金额均正确 实验四: 视图及索引的建立和维护 实验目的与内容 目的: 创建表的视图,修改和删除表的视图,并利用视图完成表的查询,创建表的索引、修改和删除表的索引。 内容: 11-3~5、11-9~11。 报告: 以11-3、11-4、11-9作为实验四的报告。 实验代码及结果 4.2.1实验代码 4.2.1.111-3实验代码 createviewCustomerViewasselectCust_no,Cust_name,Sex,DiscountfromcustomerwhereCity='北京'--建立北京顾客的视图 createviewTrainingViewasselect,P_name,Sex,Deptname,SUM(Order_total)asAchievement fromperson,orderdetail where=andDeptname='培训部'andP_boss! ='null'andOrder_date>=getdate()-365 groupby,P_name,Sex,Deptname --建立培训员工的视图 4.2.1.211-4实验代码 createindexname_sortonperson(P_name) --在人员表上的姓名列上创建一个单列索引name_port createindexbirth_nameonperson(Birthdate,P_name) --在人员表上创建一个组合索引birth_name createuniqueindexu_name_sortonperson(P_name) --在人员表“姓名列上创建一个唯一索引”u_name_sort createclusteredindexfact_idxonsalary(FactDESC) --在月薪表上创建一个聚簇缩影fact—_idx,并使其按降序索引 4.2.1.311-9实验代码 updateCustomerView setDiscount= whereCust_name='王云' --将视图CustomerView中的姓名为“王云”的顾客的购买折扣改为.85 4.2.2实验结果 4.2.2.111-3实验结果 建立北京顾客视图成功 创建培训员工视图成功 4.2.2.211-4实验结果 在人员表上的姓名列上创建一个单列索引name_port 指令: select*frompersonwhereP_name='林峰' 创建成功后我们能通过索引P_name查找数据了。 在人员表上创建一个组合索引birth_name 指令: select*frompersonwhereBirthdate='1975-02-14'andP_name='谢志文' 创建成功后我们能通过组合索引查询到我们想要的信息了。 在人员表“姓名列上创建一个唯一索引”u_name_sort 创建索引失败,因为创建的是一个唯一索引,而在姓名列中有两个“林浩然”的存在,故创建唯一索引失败。 在月薪表上创建一个聚簇缩影fact—_idx,并使其按降序索引 创建索引失败,因为在salary表中我们已经定义了一个主键,主键将自动默认为一个聚簇索引,而一个表中只能有一个聚簇索引故会出现错误。 4.2.2.311-9实验结果 将视图CustomerView中的姓名为“王云”的顾客的购买折扣改为.85 因为在视图CustomerView中不存在“王云”顾客故受到影响的行数为0行 实验五: 存储过程的建立和维护 实验目的与内容 目的: 创建用户的存储过程,修改和删除存储过程、执行存储过程。 内容: 11-22~24。 报告: 以11-24作为实验五的报告。 实验代码及结果 5.2.1实验代码 createprocproc_addbonus (@P_nochar(6),@adddec(5,1)output) as declare@Order_totalint declarecur_addbonus_checkscursorfor selectOrder_total fromorderdetail whereP_no=@P_no select@add=0 opencur_addbonus_checks fetchcur_addbonus_checksinto@Order_total if(@@fetch_status<>0) begin Closecur_addbonus_checks deallocatecur_addbonus_checks return end setnocounton while(@@fetch_status=0) begin if@Order_total<=100000 set@add=@add+20 elseset@add=@add+@Order_total/100000*30 fetchcur_addbonus_checksinto@Order_total end closecur_addbonus_checks return /* --根据各员工在噢仁derdetail表中的销售业绩计算其总的奖金金额,员工每签定一份小雨的订单其奖金加若订单高于,则奖金 加@add=@add+@Order_total/100000*30 */ 5.2.2实验结果 根据各员工在噢仁derdetail表中的销售业绩计算其总的奖金金额,员工每签定一份小雨的订单其奖金加若订单高于,则奖金加@add=@add+@Order_total/100000*30 存储过程建立成功 我们能成功的查询到工号为000002的员工的奖金总额为。 实验六: 触发器的建立和维护 实验目的与内容 目的: 创建触发器,修改和删除触发器,测试触发器的效果。 内容: 11-34。 报告: 以11-34作为实验六的报告。 实验代码及结果 6.2.1实验代码 createtriggerdelect_p_pno onperson afterdelete as if@@rowcount=0return deletesalary fromtitlest,deletedd where= return --构建一个person上的能级联删除salary表中员工信息的触发器 createtriggerupdate_s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 西南交通大学 数据库 原理 实验 课程设计