我有这张表:
create table user_roles
(
user_id INT UNSIGNED NOT NULL,
role_id INT UNSIGNED NOT NULL,
UNIQUE (user_id, role_id),
INDEX(id)
);
user_id
和role_id
是其他表的主键,即user
和roles
。 UNIQUE (user_id, role_id),
是什么意思?这是否意味着所有 user_id
/role_id
对都必须是唯一的?
INDEX(user_id)
的作用是什么?
最佳答案
UNIQUE
确实确保进入字段的所有值都不同于之前的值。
INDEX(user_id)
将在 user_id 字段上创建一个索引,以便更好地对其进行索引,即可以更快地搜索它。
Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows.
关于mysql - 试图理解这个 MySQL 表的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882129/