数据库
首页 > 数据库> > mysql-添加外键时CONSTRAINT关键字的用途是什么?

mysql-添加外键时CONSTRAINT关键字的用途是什么?

作者:互联网

我不知道这两者之间有什么区别.

CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES ..

FOREIGN KEY (P_Id) REFERENCES ..

它只是命名,还是其他?

解决方法:

根据MySQL关于foreign keys indicates的手册,约束语法的CONSTRAINT symbol_name部分是可选的:

[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION

区别在于外键的命名.如上述链接文档所述:

Otherwise, MySQL implicitly creates a foreign key index that is named according to the following rules:

• If defined, the CONSTRAINT symbol value is used. Otherwise, the
FOREIGN KEY index_name value is used.

• If neither a CONSTRAINT symbol or FOREIGN KEY index_name is defined,
the foreign key index name is generated using the name of the
referencing foreign key column.

标签:constraints,foreign-keys,mysql
来源: https://codeday.me/bug/20191118/2028443.html