Experiment 11 SQL database language - database integrity

Experiment 11 SQL database language - database integrity

One. lab environment:

​ MYSQL WORKBENCH

two. Experimental content and achievement:
5.

Create a table faculty teacher (tno, tname, tage, telphone, tsex, id), the faculty tno number as the primary key, sex default value is "male"

create table teacher
(
	tno CHAR(7) primary key, #设置主键
    tname VARCHAR(10),
    tage INT,
    telphone CHAR(12),
    tsex char(2) default '男',  #性别默认值为"男"
    tid VARCHAR(20)
)engine=InnoDB;
6.

Complete the following table according to faculty teacher:

  1. The default value is set telphnoe 00000000

    ALTER TABLE teacher modify telphone CHAR(12)default '0000000'; 
    

    or

    ALTER TABLE teacher CHANGE COLUMN telphone telphone char(12) DEFAULT '00000000';
    

    an examination:
    Here Insert Picture Description

  2. Set tsex the check is a check constraint: Enter a value can only be "male" or "female"

    ALTER TABLE teacher modify tsex char(2) default '男' check(tsex='男' or tsex='女');
    

    After setting check, inspection, check and found no effect from:

    The main reason is MYSQL does not support check operation, so with enum set here can only be "male" or "female":

    ALTER TABLE teacher CHANGE COLUMN tsex tsex enum('男','女') default '男';
    

    Re-examination:


Set clear success.

  1. Id set a median of 15 or 18, each are numbers

    delimiter //
    create trigger tr_id after insert on teacher  #创建触发器
    for each row
    begin
    declare iresult int default 0;
    if (length(new.tid)!=15 and length(new.tid)!=18)  #如果id长度不为15或18则报错
    then
    SIGNAL SQLSTATE'45000'SET MESSAGE_TEXT = 'The length is not allowed';
    end if;
    select new.tid regexp '^[0-9]*$' into iresult;  #如果id内容存在非数字则报错
    if (iresult=0)
    then 
    SIGNAL SQLSTATE'45000' SET MESSAGE_TEXT ='id must be consisted of nums';
    end if;
    END//
    

    an examination:

    Length of the insertion elements 15 and 18: insert failed

    Here Insert Picture Description

    Inserting a length of 15 and 18 elements, but non-numeric comprises: insert failed

Here Insert Picture Description

15 and the insertion length of the digital data are: successful insert

Here Insert Picture Description

7

Reports subsystem management table provided PAPER database DingBao in the table as follows:

Here Insert Picture Description

On the basis of the integrity of your master database knowledge, integrity rules setting as much as possible in accordance with the table of contents for the table, for the protection of the correctness and completeness of the table.

Binding:

  • Newspapers main code number provided, and the length must be pno 6, while the group numbers are

  • Ppr size constraints must be greater than 0

  • Setting the newspaper name can not be empty and not be the same

Code is as follows (wherein one implementation):

create table paper
(
	pno char(7) primary key ,
    pna char(20) unique not null,
    ppr float not null
)engine=InnoDB;
delimiter //
create trigger trig after insert on paper  #创建触发器
for each row
begin
declare iresult int default 0;
if (length(new.pno)!=6 or ppr<0)
then
SIGNAL SQLSTATE'45000';
end if;
select new.pno regexp '^[0-9]*$' into iresult;  #如果id内容存在非数字则报错
if (iresult=0)
then 
SIGNAL SQLSTATE'45000';
end if;
END//

Published 68 original articles · won praise 36 · views 10000 +

Guess you like

Origin blog.csdn.net/dingdingdodo/article/details/103016963