• SQL 外键约束详解,及禁用、启用外键约束


    SQL FOREIGN KEY 约束

    一个表中的 FOREIGN KEY 指向另一个表中的 UNIQUE KEY(唯一约束的键)。

    让我们通过一个实例来解释外键。请看下面两个表:

    “Persons” 表:

    P_Id LastName FirstName Address City
    1 Hansen Ola Timoteivn 10 Sandnes
    2 Svendson Tove Borgvn 23 Sandnes
    3 Pettersen Kari Storgt 20 Stavanger

    “Orders” 表:

    O_Id OrderNo P_Id
    1 77895 3
    2 44678 3
    3 22456 2
    4 24562 1

    请注意,“Orders” 表中的 “P_Id” 列指向 “Persons” 表中的 “P_Id” 列。

    “Persons” 表中的 “P_Id” 列是 “Persons” 表中的 PRIMARY KEY。

    “Orders” 表中的 “P_Id” 列是 “Orders” 表中的 FOREIGN KEY。

    FOREIGN KEY 约束用于预防破坏表之间连接的行为。

    FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。


    CREATE TABLE 时的 SQL FOREIGN KEY 约束

    下面的 SQL 在 “Orders” 表创建时在 “P_Id” 列上创建 FOREIGN KEY 约束:

    MySQL:

    CREATE TABLE Orders
    (
    O_Id int NOT NULL,
    OrderNo int NOT NULL,
    P_Id int,
    PRIMARY KEY (O_Id),
    FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
    )
    

    SQL Server / Oracle / MS Access:

    CREATE TABLE Orders
    (
    O_Id int NOT NULL PRIMARY KEY,
    OrderNo int NOT NULL,
    P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
    )
    

    如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:

    MySQL / SQL Server / Oracle / MS Access:

    CREATE TABLE Orders
    (
    O_Id int NOT NULL,
    OrderNo int NOT NULL,
    P_Id int,
    PRIMARY KEY (O_Id),
    CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
    REFERENCES Persons(P_Id)
    )
    
    
    

    ALTER TABLE 时的 SQL FOREIGN KEY 约束

    当 “Orders” 表已被创建时,如需在 “P_Id” 列创建 FOREIGN KEY 约束,请使用下面的 SQL:

    MySQL / SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    ADD FOREIGN KEY (P_Id)
    REFERENCES Persons(P_Id)
    

    如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:

    MySQL / SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    ADD CONSTRAINT fk_PerOrders
    FOREIGN KEY (P_Id)
    REFERENCES Persons(P_Id)
    

    撤销 FOREIGN KEY 约束

    如需撤销 FOREIGN KEY 约束,请使用下面的 SQL:

    MySQL:

    ALTER TABLE Orders
    DROP FOREIGN KEY fk_PerOrders
    

    SQL Server / Oracle / MS Access:

    ALTER TABLE Orders
    DROP CONSTRAINT fk_PerOrders
    

    禁用、启用外键约束

    作用

    当我们表已经设置了外键关联的时候,想要删除或者表中的数据会造成错误,或者要多次修改,很麻烦。我们可以暂时禁用外键约束来规避掉这种问题。

    SQLServer禁用、启用外键约束

    ---启用or禁用指定表所有外键约束 
    alter table PUB_STRU  NOCHECK constraint all; 
    alter table PUB_STRU  CHECK constraint all; 
       
    ---生成启用or禁用指定表外键约束的sql 
    select 'ALTER TABLE ' + b.name + ' NOCHECK CONSTRAINT ' + a.name +';'  from sysobjects a ,sysobjects b where a.xtype ='f' and a.parent_obj = b.id and b.name='表名'; 
    select 'ALTER TABLE ' + b.name + ' CHECK CONSTRAINT ' + a.name +';'  from sysobjects a ,sysobjects b where a.xtype ='f' and a.parent_obj = b.id and b.name='表名'; 
     
    --生成的sql如下
    ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK1;
    ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK2;
    ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK1;
    ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK2;  
     
     --查看约束状态(查询字典表 sys.foreign_keys,该字典表开始出现于sqlserver2005及以上版本):
    select name , is_disabled from sys.foreign_keys order by name; 
     --其中:name  : 外键约束名称   is_disabled : 是否已禁用
    

    例子

    --删除外键
    alter table AdItem drop constraint AdOrder_AdItem_FK1
     
    --增加外键
    alter table AdItem
    add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo)
     
    --单个表的一个外键
    alter table Student nocheck constraint FK__Student__SchoolN__4222D4EF 
    alter table Student check constraint FK__Student__SchoolN__4222D4EF 
     
    --单个表的所有外键
    alter table Student nocheck constraint all 
    alter table Student check constraint all 
     
    --某个数据库的所有表
    EXEC sp_MSforeachtable @command1='alter table ?  NOCHECK constraint all;
    EXEC sp_MSforeachtable @command1='alter table ?  CHECK constraint all;
    

    MySQL 启动和禁用外键约束

    我们可以使用

    SET FOREIGN_KEY_CHECKS=0;
    

    来禁用外键约束.

    之后再用

        SET FOREIGN_KEY_CHECKS=1;
    

    来启动外键约束.

    来启动外键约束.

    查看当前FOREIGN_KEY_CHECKS的值可用如下命令

        SELECT  @@FOREIGN_KEY_CHECKS;
    
  • 相关阅读:
    智能电视系列(4)-高通,天才与极限
    Intel processor brand names-Xeon,Core,Pentium,Celeron----Xeon
    Intel CPUs
    Configuring IPMI under Linux using ipmitool
    CPUID
    About Intel® Processor Numbers
    ipad iphone 开发的应用,加一个启动预览图片
    mysql 查询随机条记录的sql语句和php计算概率
    参考:iPhone OS 3.0中的字体列表
    UIScrollView 不能滚动的问题
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13307977.html
Copyright © 2020-2023  润新知