六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 79|回复: 0

SQL SERVER --STUDY(10)

[复制链接]

升级  1.33%

58

主题

58

主题

58

主题

举人

Rank: 3Rank: 3

积分
204
 楼主| 发表于 2013-2-7 16:06:20 | 显示全部楼层 |阅读模式
转载:
http://www.51testing.com/?uid-182741-action-viewspace-itemid-95001
 
 
设置外键的同时:
 
ALTER TABLE PERSON ADD (CONSTRAINT FK_PERSON1 FOREIGN KEY (PROFESSION)REFERENCES PERSONPROFESSION (PROFESSIONID) ON DELETE CASCADE); 
 
增加外键约束的时候加ON DELETE CASCADE选项目的是可以在子表记录存在的情况下直接删除父表记录,而不用受约束的限制

在delete语句的末尾加上CASCADE,就可以实现两张表的级删除:
 
 
 
delete from PERSONPROFESSION cascade; 

 
 
 
 

 
 
 
 
 
 
以下是ON DELETE的官方解释:
ON DELETE
The ON DELETE clause indicates that when a DELETE is executed on a referenced row in the referenced table, one of the following actions will be executed upon the constrained column, as specified by action:
 
NO ACTION(default)
The NO ACTION clause produces an error if the reference is violated. This is the default if action is not specified.
CASCADE
The CASCADE keyword removes all rows which reference the deleted row. Exercise caution with this action.
SET NULL
The SET NULL clause assigns a NULL value to all referenced column values.
 
转载:
 
http://newsgroups.derkeiler.com/Archive/Comp/comp.databases.theory/2005-10/msg00137.html
 
ALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES
bar(bar_id) ON DELETE RESTRICT

- meaning: If someone tries to remove a row in bar which foo needs (for
referential integrity), the action is denied (in stead of cascaded as a
deletion in the foo table). See you DBMS's documentation for more on this.

RESTRICT can also be part of a command, like: DROP TABLE bar RESTRICT;

- meaning: Drop the "bar" table, unless the table is used as part of
referential integrity constraints in other tablesALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES
bar(bar_id) ON DELETE RESTRICT

- meaning: If someone tries to remove a row in bar which foo needs (for
referential integrity), the action is denied (in stead of cascaded as a
deletion in the foo table). See you DBMS's documentation for more on this.

RESTRICT can also be part of a command, like: DROP TABLE bar RESTRICT;

- meaning: Drop the "bar" table, unless the table is used as part of
referential integrity constraints in other tables
 
——————————————————————————————————————
 
http://www.w3schools.com/SQL/sql_alias.asp
 
SQL 语法网站
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表