Trying to understand what this MySQL table means
I have this table:
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
and role_id
are primary keys of other tables, namely user
and roles
. 开发者_Python百科What does UNIQUE (user_id, role_id),
mean? Does it mean that all user_id
/role_id
pairs have to be unique?
And what is INDEX(user_id)
for?
UNIQUE
does indeed ensure that all values that go into the fields differ from the previous.
INDEX(user_id)
will create an index on the user_id field so that it is better indexed, i.e. it can be searched through faster.
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
Yes, UNIQUE(user_id, role_id) means that the combination of those two fields for any particular row cannot exist in the table more than once.
INDEX(user_id) applies an index on the user_id column, making searching on that column very fast (at some small performance expense when inserting/updating to the table)
Yes it means that user_id/role_id pairs are unique.
精彩评论