mysql - 试图理解这个 MySQL 表的含义

标签 mysql

我有这张表:

create table user_roles
(
user_id INT UNSIGNED NOT NULL,
role_id INT UNSIGNED NOT NULL,
UNIQUE (user_id, role_id),
INDEX(id)
);

user_idrole_id是其他表的主键,即userrolesUNIQUE (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.

http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

关于mysql - 试图理解这个 MySQL 表的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882129/

相关文章:

php - codeigniter 无法访问用户的 MySQL,但使用 root 登录正常

mysql - 使用内部连接从单个表中删除行

mysql - 如何将MySQL数据合并到一张表中

mysql - 多对多 mysql 搜索寻找我不在的团队

php - 如何使用php按日期对数据进行分组并显示在html表格中

php - 从数据库中检索数据导致 0 个结果

optimization - 优化MySQL查询: Is it always possible to optimize a query so that it doesn't use "ALL"

php - 单表 INTERSECT 替代方案

php - mysql_fetch_array/row 只取1条数据

mysql - 当查询没有生成数据时,默认值为 'grouping by'