Constraint and Trigger

2016-03-20  本文已影响0人  SkyDavid

Constraint

Impose restrictions on allowable data, beyond those imposed by structure and types

create table Student(sID int primary key, 
                     sName text unique, 
                     GPA real NOT NULL, 
                     sizeHS int check(sizeHS < 2000 AND sizeHS > 1000));
create table Apply(sID int, cName text, major text,
                    check(cName <> 'Stanford' or major <> 'CS'));

Referential Integrity(foreign key) constraint

外键的指向必须是存在的(以指针作比喻的话就是不存在野指针)

比如 R.A 指向 S.B, A 称作外键,B 往往要求是 primary key 或至少是 unique 的,
如下操作可能会违反 Referential Integrity

create table Apply(sID int references Student(sID) on delete set null,
                   cName text references College(cName) on update cascade,
                   major text);

Trigger

"Event-Condition-Action Rule"
when event occurs, check conditon, if true, do action

Move logic from app to db
Enfore constraint

具体待补充

上一篇下一篇

猜你喜欢

热点阅读