当前位置:网站首页>数据库上机实验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 transaction (transaction) (this is enough..)
- Pytorch教程Introduction中的神经网络实现示例
- <urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
- MYSQL一站式学习,看完即学完
- Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
- 剑指offer基础版 ---- 第27天
- 基于web3.0使用钱包Metamask的三方登陆
- 剑指offer基础版--- 第23天
- 剑指offer专项突击版 ---- 第 6 天
- 剑指offer专项突击版 ---- 第2天
猜你喜欢

面试官,不要再问我三次握手和四次挥手

Temporal介绍

分布式事务处理方案大 PK!

Numpy中np.meshgrid的简单用法示例

Interviewer, don't ask me to shake hands three times and wave four times again

MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细

12个MySQL慢查询的原因分析

剑指offer基础版 ---- 第27天

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
![[MQ I can speak for an hour]](/img/ef/863c994ac3a7de157bd39545218558.jpg)
[MQ I can speak for an hour]
随机推荐
剑指offer专项突击版 ---- 第2天
mysql存储过程
centos7安装mysql5.7步骤(图解版)
质量小议12 -- 以测代评
Three oj questions on leetcode
面试官,不要再问我三次握手和四次挥手
MySQL transaction isolation level, rounding
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Workflow番外篇
剑指offer基础版 ----- 第25天
剑指offer专项突击版 ---- 第1天
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
SQL statement to range query time field
1. Get data - requests.get()
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
SQL row-column conversion
MYSQL一站式学习,看完即学完
如何将项目部署到服务器上(全套教程)
[mysql improves query efficiency] Mysql database query is slow to solve the problem
MySQL forgot password