user291701
user291701

Reputation: 39681

table column marked as unique, means it's indexed too, right?

I'm trying to setup a mysql table. Would like it to be the following:

id (integer, auto increment, primary)
username (varchar 32, unique)
email (varchar 32, unique)

I don't want to allow duplicate usernames or email addresses in the table. For example, at startup, I'll test if the supplied username or email already exist in the table - if they do, I'll cancel signup.

If I mark the username and email columns as "unique", does that also mean they're indexed? I'd like them to be indexed so that I can do the already-exists check as efficiently as possible,

Thanks

Upvotes: 0

Views: 362

Answers (2)

RichardTheKiwi
RichardTheKiwi

Reputation: 107706

Yes it will implicitly create an index.

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL.

Upvotes: 0

James Gaunt
James Gaunt

Reputation: 14783

Yes they will be indexed. This is also how the database does the already-exists check as efficiently as possible.

Upvotes: 1

Related Questions