当前位置:网站首页>Database Experiment 7 integrity constraints
Database Experiment 7 integrity constraints
2022-06-28 18:21:00 【Askar code】
Experimental topics (1)
1 Data integrity . Based on the experimental database , Please use T-SQL Statement completes the following , And will SQL The sentence is written in the experimental report book :
Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) 1) Will database stu Table of course Of cno Fields are defined as primary keys , The constraint name is cno_pk;
Alter table course add constraint cno_pk primary key (cno)
2) Is a table course In the field cname Add a unique value constraint ;
Alter table course add constraint cname_nq unique (cname)
3) For data sheets sc Of sno、cno Field is defined as foreign code , Make it consistent with the table student The main code of sno And table course The main code of cno Corresponding , Achieve the following referential integrity :
Delete student Delete the records in the table at the same time sc The record in the table sno Records with the same field value ;
modify student Form a record of sno when , if sc There are several records corresponding to the field value in the table , Refuse to modify ;
modify course surface cno When the field value , The field is in sc The corresponding values in the table should also be modified ;
Delete course When a record is shown , If the field is in sc Presence in table , Delete the record corresponding to this field ;
towards sc When adding records to the table , If the record is sno The value of the field is in the student Does not exist in the , Then refuse to insert ;
[ The primary table is affected by the secondary table ( Foreign key table )]
Alter table sc
Add constraint sc_fk
foreign key sno references student(sno)
Delete on cascade
Update on no action,
Foreign key cno references course(cno)
Delete on cascade
Update on cascade
4 ) Definition check constraint , Students are required to have a student number sno It has to be for 9 Four digit characters , And not with 0 start , The second and third are 0;
Alter table student
Add constraint sno_ck
Check(sno like [1-9]00[0-9][0-9][0-9][0-9][0-9][0-9])
5) Definition stu In the database student Table middle school students' age values are 16~25 Within the scope of ;
Alter table student
Add constraint sage_ck
Check(sage between 16 and 25)
6) Definition stu database student The default value of middle school students' age in the table is 20;
Alter table student
Add constraint sage_def default 20 for sage
7) modify student The age constraint of students in the table can be 15~30 Within the scope of ;
Alter table student
Drop constraint sage_ck
Go
Alter table student
Add constraint sage_ck
Check(sage between 15 and 30)
8) Delete table course Middle field cname The unique value constraint of ;
Alter table course
Drop constraint cname_uq
Experimental topics (2)
2. Based on the experimental database , Write the following triggers and test .
Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) 1) Is a table course Build a INSERT trigger , When the new line is inserted, the course credits (ccredit) The value is not 1~6 when , Activate the starter , Undo the insertion , And use RAISERROR Statement returns an error message . Rest
Create trigger ccredit_insert on course Before insert
As
Declare @new_credit int
Select @new_credit = ccredit from inserted
If( @new_credit < 1 and @new_credit > 6 )
Begin
raiserror( “ Credits do not meet the requirements !!”,10,16)
rollback tran
End
2) by course Create another table UPDATE trigger , When the course number information of a course is updated , Activate the trigger cascade update sc Relevant course number information in the table , And use PRINT Statement returns a prompt message .
Create trigger cno_update on course after update
As
Begin
Declare @new_cno char(9),@old_cno char(9)
Selset @new_cno = cno from inserted
Select @old_cno = cno form deleted
Update sc set cno = @new_cno
where cno = old_cno
Print ‘ success !’
End
3) by student Table creation DELECT trigger , While deleting a record in, you will sc The corresponding records in the table are also deleted .
Create trigger sno_delete on student after delete
As
Begin
Delete from sc
Where sno in (select sno from deleted)
End
4) establish INSTEAD OF trigger , Direction sc When a record is inserted in a table , First check sno The value on the column is student Whether there is , If present, perform the insert operation , If not, prompt “ The student does not exist ”.
Create trigger sno_instead_of on sc instead of insert
As
Declare @new_sno
Select @new_sno = sno from inserted
If (sno not in (select sno from student)
Begin
Raiserror(‘ The student does not exist !’,10,16)
Rollback tran
End
5) Compare the execution order of constraints and triggers .( Create... In a table CHECK Constraints and triggers , Then insert a record into the table that does not meet the constraints and triggers , See who works first .)
Topic four : Created trigger insert
Topic 5 : Create constraints
alter table sc
add constraint grade_ck
check (grade between 0 and 100)
insert sc
values('200515066','01',1000)
insert The constraint is verified first when the statement is executed , At the same time, the constraints themselves have a sequence ( No default constraint )
1 . Verify non null constraints (not null)
2 . Verify the primary key constraint (primary key)
3 . Verify uniqueness constraints (unique)
4. Revalidate relevant Check(check)
5. Finally, execute the trigger , If the trigger must also ensure that it does not violate the relevant constraints and Check
边栏推荐
猜你喜欢

Applet graduation project reservation based on wechat housekeeping service applet graduation project opening report function reference

Small program graduation project based on wechat recruitment small program graduation project opening report function reference

Squid proxy server application (I came from afar to make an appointment with you)

Small program graduation project based on wechat subscription water supply mall small program graduation project opening report function reference

2022年山东省安全员C证考试练习题及模拟考试

使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
![[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto

Redis principle - hash

MCU modifies network hardware driver (PHY chip replacement)

Small program graduation project based on wechat mobile mall small program graduation project opening report function reference
随机推荐
Mycat+ sub database and sub table
【云驻共创】昇腾异构计算架构CANN,助力释放硬件澎湃算力
抓包整理外篇fiddler————了解工具栏[一]
Why can't China produce products like idea?
Analysis of response parsing process of SAP ui5 batch request
第2章 处理文件、摄像头和图形用户界面cameo应用
如何高效优雅地管理接口文档
HackTheBox-baby CachedView
February 15, 2022 learning summary
GCC getting started manual
2022 operation of simulated examination platform of hoisting machinery command examination question bank
July2022 plan (unreal)
Matlb| optimal operation and marketization of power system
工业数字化与新一代数字化系统设计平台----讲座
2022 review questions and answers for safety production management personnel of hazardous chemical production units
China gaobang brand story: standing guard for safety, gaobang pays attention to
Go descending sort takes top n
IDM certification process log embedding point description
2022 recurrent training question bank and online simulation examination for main principals of hazardous chemicals business units
9个优秀的占位图服务