当前位置:网站首页>数据库上机实验6 数据库完整性
数据库上机实验6 数据库完整性
2022-07-31 05:09:00 【Meteor.792】
一、实验目的
1、熟练掌握实体完整性、参照完整性的定义。
2、掌握用户定义完整性的定义。
3、了解触发器的定义和使用。
二、实验内容
使用SQL命令完成如下题目:
1、删除学生作业表的主键约束和外键约束。
alter table score
drop constraint PK__score__8497E91307705DC5,
FK__score__cno__15502E78,FK__score__Sno__164452B12、在学生作业表中添加学号和课程号的主键约束。
alter table score
add constraint
pk_sno_cno primary key(sno,cno)3、在学生作业表中添加课程号的外键约束。
alter table score
add constraint
fk_score_cno foreign key(cno) references course(cno)4、创建触发器tr1,实现当修改学生表中的数据时,显示提示信息“学生表信息被修改了。”
创建触发器语句:
create trigger tr1 on student after update
as
print '学生表信息被修改了。'
执行触发器语句:
select * from student where sno='0433'
update student set sname='张艳艳' where sno='0433'
select * from student where sno='0433'5、使用触发器tr2,实现当修改学生表中某个学生的学号时,对应学生作业表中的学号也要修改。
创建触发器语句:
create trigger tr2 on student after update
as
update score set sno=(select sno from inserted)
where sno=(select sno from deleted)
执行触发器语句:
select * from student where sname='张艳艳'
select * from score where sname='张艳艳'
update student set sno='0434' where sname='张艳艳'
select * from student where sname='张艳艳'
select * from score where sname='张艳艳'6、查看学生表中已创建的触发器。
exec sp_helptrigger 'student'
7、查看已创建的触发器tr1的内容。
exec sp_helptext 'tr1'
8、删除学生表上的触发器tr1。
drop trigger tr1
三、实验指导
1、启动SQL Server2012软件。
2、通过分离附加的方法,将实验1所创建的作业管理数据库恢复到该软件中。
3、SQL Server中,程序不区别大小写,特别要注意程序中的标点符号,一定要在英文半角状态下输入,否则会出错。
4、使用ALTER TABLE命令来创建和删除主键约束和外键约束,如果在创建的过程中,出现已经存在该约束的错误,就需要先删除再创建。
5、若在创建约束的时候没有给约束指定名字,系统会自动给约束分配名字。
6、触发器的触发事件有INSERT、UPDATE和DELETE。
7、创建触发器后,要使用相应语句来触发该触发器执行。
边栏推荐
- MySQL-Explain详解
- MySQL transaction isolation level, rounding
- Three handshakes and four waves
- tf.keras.utils.pad_sequences()
- Temporal客户端模型
- Lock wait timeout exceeded解决方案
- Simple read operation of EasyExcel
- DVWA installation tutorial (understand what you don't understand · in detail)
- C语言教程(二)-printf及c自带的数据类型
- Swordsman Offer Special Assault Edition --- Day 3
猜你喜欢

限流的原理

MySQL optimization: from ten seconds to three hundred milliseconds

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

The monitoring of Doris study notes

Unity Framework Design Series: How Unity Designs Network Frameworks

DVWA installation tutorial (understand what you don't understand · in detail)

MySQL optimization slow log query

Centos7 install mysql5.7

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?

mysql5.7.35安装配置教程【超级详细安装教程】
随机推荐
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
STM32——DMA
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
a different object with the same identifier value was already associated with the session
MySQL transaction isolation level, rounding
关于superset集成到自己的项目中
Temporal线上部署
限流的原理
12 reasons for MySQL slow query
mysql stored procedure
With MVC, why DDD?
Apache DButils使用注意事项--with modifiers “public“
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
剑指offer基础版 --- 第22天
.NET-9. A mess of theoretical notes (concepts, ideas)
MySQL transaction (transaction) (this is enough..)
.NET-6.WinForm2.NanUI learning and summary
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
对list集合进行分页,并将数据显示在页面中