gpt4 book ai didi

php - 使用 KEY/CONSTRAINT 与 FOREIGN KEY 之间的区别?

转载 作者:行者123 更新时间:2023-11-30 01:20:48 24 4
gpt4 key购买 nike

我正在使用 Laravel 框架的迁移功能在数据库中创建外键。使用代码

Schema::table('posts', function($table){
$table->integer('category_id')->unsigned();
$table->foreign('category_id')
->references('id')
->on('categories');
});

生成 MySQL 输出(来自 SHOW CREATE TABLE 帖子)

`category_id` int(10) unsigned NOT NULL,
KEY `posts_category_id_foreign` (`category_id`),
CONSTRAINT `posts_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)

为什么 Laravel 使用 KEYCONSTRAINT ... FOREIGN KEY ... REFERENCES ... 来创建新索引而不是更常见的 FOREIGN KEY ... REFERENCES ... 在引用外键的列上?

最佳答案

因为 MySql 简单地忽略内联引用规范,所以他们在这里写到:http://dev.mysql.com/doc/refman/5.6/en/create-table.html

MySQL does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.

关于php - 使用 KEY/CONSTRAINT 与 FOREIGN KEY 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18562398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com