MySQL学习笔记(一):添加外键

来源:互联网 发布:ppt mac版 编辑:程序博客网 时间:2024/06/08 17:50

1、添加外键

语句:alter table 表名 add constraint 外键约束名 foreign key(列名) references 引用外键表(列名) eg:alter table clothsize add constraint clothId foreign key (clothId) references cloth(Id)
2、删除外键
语法:alter table 表名 drop constraint 外键约束名 如: alter table clothsize drop constraint clothId 

添加外键的注意事项:

    * 两个表必须是InnoDB表类型。   
    * 使用在外键关系的域(字段)必须为索引型(Index)。所以要为设置外键的字段建立index。
    * 使用在外键关系的两个域(字段)数据类型要相同。 


0 0
原创粉丝点击