Realizing the Static Integrity of Database Using SQL Language

Note: All underscore + italic statements are optional statements


Static constraints:

  • Data set (O): column or table
  • Predicate Condition (P): needs to be defined
  • Trigger condition (A): Check on update (default)
  • Response Action (R): Reject (default)


Col_constr column constraint/table_constr table constraint:

Add constraints when creating a table: Create table table name (column name 1 not null 

unique or primary key or check (expression) or references table name (column name) on delete cascade or set null )

Column name 2 constraints...)

  • not null : the column value cannot be null
  • unique : Whether the column value is unique
  • primary key : Whether the column value is the primary key
  • check (expression) : The conditions that the column value must meet, such as "age" cannot exceed 100 years old and cannot be negative
  • references table name (column name) on delete cascade or set null : cascade update , refer to the value of the column colname of another table tablename, if there is an on delete cascade or on delete set null  statement, delete a column value of the referenced table When v, delete the record whose column value is v in this table or update the column value to null; the default is no operation

example:

Create a student table, the gender can only be checked to be "male" or "female", the age to check whether it is in the range of [1, 149], Dt is used as the foreign key of the table Dept, if Dt in Dept is expelled from the school, then the All tuples related to Dt in the student table are deleted together

Create Table Stpudent(St char(8) not null unique, Sname char(10),
Ssex char(2), check(Ssex='男' or Ssex='女'),
Sage integer check (Sage>=1 and Sage<150),
Dt char(2) references Dept(Dt) on delete cascade,
Sclass char(6));

Add constraints after creating a table: Alter table table name add constraint constraint name constraint...

Note that if the constraint added after the table is created is a cascading update, the foreign key must be declared with " foreign key(st) " before the statement "references infer(st) ..."

Drop the constraint after creating the table: Alter table table name drop constraint constraint name constraint...

Note that Mysql may not support the above statement, and Mysql does not support check(), but it can run

Similarly, Mysql can only delete foreign key constraints and primary key constraints

example:

alter table sc
add constraint yueshu1 unique(st);
alter table sc
add constraint yueshu2 check(ct='001' or ct='002' or ct='003' or ct='004' or ct='005');
alter table infer
add constraint yueshu3 unique(st);
alter table infer
add constraint yueshu4 check(ct='001' or ct='002' or ct='003' or ct='004' or ct='005');
alter table sc
add constraint yueshu5 foreign key(st) references infer(st) on delete cascade;

Notice:

  • If the original tuple does not satisfy the added constraint, adding the constraint will fail (Mysql: Error1062)
  • Adding constraints also fails if the added constraints are duplicates (Mysql: Error1061)

The condition in check can be any statement after Where in Select-From-Where, including subqueries, for example:

The courses in the course selection table must be in the course table, and the students must be in the student ID list

create Table Sc(St char(8) check(St in (select St from student)),
Ct char(3) check( Ct in (select Ct from course)),
Score float(1), constraint ctscore check (Score>=0.0 and
Score<=100.0))


Affirmation:

  • An assertion is a predicate expression that expresses a condition that you want the database to always satisfy
  • Table constraints and column constraints are special assertions
  • SQL also provides assertions for complex conditional expressions, the syntax of which is: create assertion assertion name check  predicate
  • When an assertion is created, the system will check its validity and test whether the update violates the assertion on each update
  • Assertion testing increases the burden of database maintenance, be careful to use complex assertions, and almost no one uses assertions


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325831018&siteId=291194637