Database Principle and Application Experiment Report - Experiment 9 - User Defined Integrity

Database Principle and Application Experiment Report

 Experiment topic Experiment 9 user-defined integrity  

    1. Purpose

Learn user-defined constraints, practice user-defined integrity, and use SQL language to use phrases NOT NULL, UNIQUE, and CHECK to ensure user-defined integrity.

    1. Experimental content ( it is recommended to change the table to Teachers, and the corresponding attribute is named starting with T, such as Tname ) to create a Teacher table.

Create a table Teacher, pay attention to distinguish it from the Teacher created earlier,

        1. Create Worker and Teacher tables, and customize two constraints U1 and U2, where U1 stipulates that the Wname and Tname fields are unique, and U2 stipulates that the upper limit of Wage and  Tage  (level) fields is 28.
        2. Insert a legal record in the Worker,  Teacher table.
        3. Demonstrate an example of inserting a violation of the U2 constraint. U2 stipulates that the value of the Wage and Tage attributes of the tuple must be <=28.
        4. Remove the U2 constraint.
        5. Re-insert the data you want to insert in (3). Since the U2 constraint is removed, the insertion is successful.
        6. Create a rule Rule_sex, stipulate that the value inserted or updated can only be M or F, and bind it to the Wsex Tsex field of Worker Teacher .
        7. Demonstrates an insert operation that violates Rule_sex.
    1. Experimental procedure

Log in to SSMS as the system administrator or sa account, enter the following command in the new query window , run it and observe the result.

(1) Enter the following SQL statement in the new query window:

USE University_Mis
CREATE TABLE Teacher(
Tno CHAR(5),
Tname CHAR(8) CONSTRAINT U1 UNIQUE,
Tsex CHAR(1),
Tage INT CONSTRAINT U2 CHECK (Tage<=28),
Tdept CHAR(20),
CONSTRAINT PK_Teacher PRIMARY KEY(Tno))

The result is shown in Figure 1 below, the table was successfully created

 Figure 1 Create a table

(2) Enter the following SQL statement in the new query window

USE University_Mis
INSERT INTO Teacher (Tno, Tname,Tsex, Tage,Tdept) 
VALUES(‘T01’,’李用’,’M’,14,’后勤部’)
SELECT * FROM Teacher

The result is shown in Figure 2 below

 Figure 2 Insert the required data

 (3) Enter the following SQL statement in the new query window

USE University_Mis
INSERT INTO Teacher (Tno, Tname,Tsex, Tage,Tdept) 
Values(‘T02’,’王勇’,’M’,38,’ 后勤部’)
SELECT * FROM Teacher

The result is shown in Figure 3 below, the insertion error.

 

Figure 3 error

Error message:

Msg 547, Level 16, State 0, Line 1

INSERT statement conflicts with CHECK constraint 'U2'. The conflict occurred in database "Xuni_University_Mis", table "dbo.Teacher", column 'Tage'.

Statement terminated.

As shown in Figure 3, because U2 constrains Tage<=28 , the Tage=38 of the inserted data does not meet the constraint conditions.

(4) Enter the following SQL statement in the new query window

USE University_Mis

ALTER TABLE teacher DROP U2

The result is shown in Figure 4

 

Figure 4 drop constraint U2

(5) Enter the following SQL statement in the new query window 

USE University_Mis
INSERT INTO Teacher (Tno, Tname,Tsex, Tage,Tdept) 
VALUES(‘T02’,’王勇’,’M’,38,’ 后勤部’)
SELECT * FROM Teacher

The result is shown in Figure 5 below. Because the constraint is deleted, the tuple can be inserted normally.

 

Figure 5 Insert successfully

(6) Enter the following SQL statement in the new query window 

USE University_Mis
Go
CREATE RULE Rule_sex AS @Value IN (‘F’,’M’)
Go
EXEC SP_bindrule Rule_sex, ‘Teacher.[Tsex]’;

The result is shown in Figure 6 below

 Figure 6 Rule binding

(7) Enter the following SQL statement in the new query window

USE University_Mis
INSERT INTO Teacher VALUES(‘T03’,’黄号’,’1’,’25’,’ 后勤部’)

The result is shown in Figure 7 below

 Figure 7 Insertion error

An error is reported because the value of Tsex must be F, M

Msg 513, Level 16, State 0, Line 1

The insertion or update of a column violates a rule specified by a previous CREATE RULE statement. The statement has been terminated. The conflict occurred in database 'Xuni_University_Mis', table 'dbo.Teacher', column 'Tsex'.

Statement terminated.

    1. Experimental requirements

(1) Before the experiment, please read the general requirements and instruction manual of the experiment carefully

(2) In the SSMS environment of SQL Server 2005 or 2008 or 2014, complete all SQL data definition operations in steps (1)-(7) of the above experiment, and paste the operation windows of the odd steps into the experiment report.

9.5 Experiment experience _

1) Experiment reflection

  none

2) Experimental Harvest

Learned the relevant content of user-defined integrity, and deepened the understanding of this part of knowledge points

appendix:

none

Guess you like

Origin blog.csdn.net/cangzhexingxing/article/details/125563667