一、约束
??????? CHECK(检查)约束限定允许插入某列的值。她和WHERE子句使用相同的条件表达式。如果插入的值无法通过CHECK条件,那么会出现错误信息。(MySQL中无法使用check,可以用触发机制(trigger,满足特定条件才执行)来实现)。
CREATE TABLE piggy_bank ( id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, coin CHAR(1) CHECK (coin IN('P','N','D','Q')), coin_year CHAR(4) )
CREATE TABLE mystery ( column1 INT(4) CHECK (column1>200), column2 CHAR(1) CHECK (column2 NOT IN('x','y','z')), column3 VARCHAR(3) CHECK ('A'=SUBSTRING(column3,1,1)), column4 VARCHAR(3) CHECK ('A'=SUBSTRING(column4,1,1) AND 'A'=SUBSTRING(column4,2,1)) )
ALTER TABLE my_contacts ADD CONSTRAINT CHECK gender IN('M','F');
?
?二、视图
CRETE VIEW web_designers AS SELECT mc.name,mc.phone,mc.email FROM my_contacts mc NATURAL JOIN job_desired jd #此处也可以改为INNER JOIN job_desired jd ON mc.id=jd.id WHERE jd.title='Web Designer';?
SELECT * FROM web_designers;???????? ?这个视图与查询等价于?
SELECT * FROM CRETE VIEW web_designers AS SELECT mc.name,mc.phone,mc.email FROM my_contacts mc NATURAL JOIN job_desired jd WHERE jd.title='Web Designer';???????? 视图是一个查询中使用VIEW是才存在的表,它被视为虚拟表。视图把查询简化成一条命令。即使一直改变数据库结构也不会破坏依赖表的应用程序。创建视图可以隐藏读者需看到的信息。
CRETE VIEW job_raises AS SELECT mc.name,jc.id,jc.salary,jd.salary_low,jd.salary_low-jc.salary AS raise FROM my_contacts mc INNER JOIN job_desired jd INNER JOIN job_current jc WHERE mc.contact_id=jd.contact_id AND jd.contact_id=jc.contact_id;??????? 这个查询是庞大的,且经常使用时,可以将其创建为一个视图,这样就只需要输入SELECT * FROM job_raises;就可以查到需要的信息。