我正在使用 Laravel 框架的迁移功能在数据库中创建外键。使用代码
Schema::table('posts', function($table){
$table->integer('category_id')->unsigned();
$table->foreign('category_id')
->references('id')
->on('categories');
});
产生 MySQL 输出(来自SHOW CREATE TABLE posts
)
`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 使用KEY
andCONSTRAINT ... FOREIGN KEY ... REFERENCES ...
来创建新索引,而不是在FOREIGN KEY ... REFERENCES ...
引用外键的列上更常见?