mysql 外键约束 外键索引吗,外键必须是在MySQL中的索引?

I've just created my first mySQL table on my own (other than using Joomla, Wordpress, etc.) and I am MS SQL developer for years but normally I can easily create a foreign key in MS SQL but I came across a difficulty or lack of knowledge here.

Here is my tables :

users

user_id int primary auto_increment

username varchar(20)

password varchar(20)

posts

post_id in primary auto_increment

title varchar(100)

message text

user_id int

When I try to add a foreign key to users which refers to posts->user_id, I cannot see the posts->user_id option in the option list Relation_view window on mySQL panel.

I am wondering whether I should define posts->user_id as Index or something? If so, why?

解决方案

Short answer: Yes, MySQL forces you to index foreign key.

InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值