Database experiment - integrity constraints of the database

1. Data integrity. Use T-SQL statements following completion.

1) The field definition table cno course database stu primary key constraint name cno_pk;
the USE Student
the ALTER TABLE XSKC.course
the ADD CONSTRAINT cno_pk a PRIMARY KEY (cno)
2) constrained to add unique value in the field course cname table;
the USE student
the ALTER tABLE XSKC.course
the ADD CONSTRAINT cname_pk UNIQUE (CNAME)
. 3) of the data table sc sno, cno field is defined as the outer code, so that the master key corresponding to the master key cno sno course of the table and the table with the student, to achieve the following with reference to integrity:
delete recording same in the recording field value sc sno  delete student table recorded in the table at the same time;
when  sno a modified record student table, if the table sc corresponding to the field value with a plurality of records, then reject changes;
l modify the time course table cno field value, the field corresponding value in the sc table should be modified;
l delete course table a record, if the field is present in the sc table, delete the corresponding field record;
when  sc add records to the table, if the value of the field sno record does not exist in the student, the insert is rejected;

the ALTER tABLE XSKC.sc
the ADD CONSTRAINT sc_fk a FOREIGN KEY (Sn o) REFERENCES XSKC.student (sno) on delete cascade on update no action,
KEY a FOREIGN (CNO) the REFERENCES XSKC.course (CNO) ON ON the Delete Cascade Cascade Update
Go
4) definition of check constraints, requires student number must be 9 sno numeric characters, and can not begin with 0, the second three are all 0 ;
the ALTER TABLE XSKC.student
the ADD CONSTRAINT sno_ck
the CHECK (SnO like '[1-9] 00 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] ')
5) is defined in the student database tables stu Students' age values in the range of 25 ~ 16;
the ALTER tABLE XSKC.student
the ADD CONSTRAINT sage_ck
the CHECK (SAGE BETWEEN 30 and 18 is)
. 6) defined stu student database table values the default value is 20 middle School Age ;
the ALTER tABLE XSKC.student
the ADD cONSTRAINT sage_df
the DEFAULT 20 is the FOR SAGE
. 7) to modify the student's age student table values may be constrained in the range of 30 ~ 15;
the ALTER tABLE XSKC.student
the DROP cONSTRAINT sage_ck
Go
the ALTER tABLE XSKC.student
the ADD cONSTRAINT sage_ck
CHECK (sage> = 15 AND sage <= 30)
a unique constraint value 8) Remove the cname fields in table course;
the USE Student
the ALTER TABLE XSKC.course
the DROP CONSTRAINT cname_pk

2. The database is based on experimental data, and write the following trigger test.

1) establish a table INSERT trigger for the course, when the value is not 1 to 6 insert a new row in the course credit (ccredit), you activate the departure device, undo the insert operation and use RAISERROR statement returns an error message.
TRIGGER tri_INSERT_course the ON XSKC.course the CREATE
the FOR the INSERT
the AS
the DECLARE @ccredit tinyint
the SELECT @ ccredit = XSKC.course.ccredit
the FROM XSKC.course, inserted The
the WHERE XSKC.course.ccredit = inserted.ccredit
the IF @ccredit the NOT the AND. 6. 1 the BETWEEN
the BEGIN
ROLLBACK TRANSACTION
RAISERROR ( '~ is not inserted between the course credits. 6', 16,10)
the END
2) and then creates a course of uPDATE trigger table when updating the information about a number of courses of the course, the trigger is activated cascading updates sc-related courses in the table number information, and use the PRINT statement returns a message.
TRIGGER tri_UPDATE_course the ON XSKC.course the CREATE
the FOR the UPDATE
the AS
the IF the UPDATE (CNO)
the BEGIN
the DECLARE @oldcno char (. 3), char newcno @ (. 3)
@ Oldcno = deleted.cno the SELECT, @ newcno = inserted.cno
the FROM deleted, inserted The
the WHERE deleted.cno = inserted.cno
the PRINT 'ready cascading updates the course number information XSKC.sc table ......'
UPDATE XSKC.sc
the SET CNO @ newcno =
the WHERE CNO = @ oldcno
the PRINT 'has cascading updates XSKC.sc table Plains course number' + @ oldcno + + @ newcno ' to'
END
3) create DELECT trigger for student table, a record in the deletion at the same time sc corresponding record in the table are also deleted.
TRIGGER tri_student ON XSKC.student the CREATE
an AFTER the DELETE
the AS
the BEGIN
the DELETE the FROM sc
the WHERE sno = (the SELECT sno the FROM deleted)
the END
. 4) creates INSTEAD OF trigger, when inserted into the recording table sc, check the value of the column student sno whether there is, if you perform an insert operation exists, if it does not exist prompted "the students do not exist."
TRIGGER tri_sc the CREATE
ON XSKC.sc an AFTER INSERT
AS
The BEGIN
the IF (the SELECT COUNT (*) the FROM inserted The inserted.sno the JOIN XSKC.student the ON = student.sno) = 0
the BEGIN
ROLLBACK TRAN
the PRINT 'the student does not exist! '
The END
the END
. 5) comparing execution order constraints and triggers. (Created in a table CHECK constraints and triggers, and then inserted into the table a record does not meet the constraints and triggers view of who should play a role.)
The ALTER TABLE XSKC.course
the ADD CONSTRAINT CK_ccredit
CHECK (ccredit> =. 1 the AND ccredit <= 6)

XSKC.course the INSERT
values ('10 ',' database ',' 5 ', 7)
Here Insert Picture Description
can be seen from the experiment, takes precedence constraints in or after the trigger for it to take effect before the update, the updated value to be rule checking. When checked conflict with existing rules, the system gives an error message, and to cancel the update operation. If the check is no problem, update is executed, and then activate the trigger.

Guess you like

Origin blog.csdn.net/qq_43776450/article/details/95633712