I have a basic 'users' table I want to create in MySQL.
我有一个基本的“用户”表,我想在MySQL中创建。
I do not want duplicate emails or duplicate usernames appearing in the database.
我不希望在数据库中出现重复的电子邮件或重复的用户名。
- What is the best way of preventing this upon table creation?
- 在创建桌子时防止这种情况的最佳方法是什么?
- And what is the difference between the following:
- 以下是有什么区别的:
1. UNIQUE (username), UNIQUE (email),
1. UNIQUE(用户名),UNIQUE(电子邮件),
2. UNIQUE KEY (username), UNIQUE KEY (email),
2. UNIQUE KEY(用户名),UNIQUE KEY(电子邮件),
3. CONSTRAINT ucons_login UNIQUE (username, email),
3. CONSTRAINT ucons_login UNIQUE(用户名,电子邮件),
I assume some of these are synonymous, yet I've been reading conflicting information online and was seeking confirmation.
我假设其中一些是同义词,但我一直在网上阅读相互矛盾的信息,并正在寻求确认。
I hope someone can assist.
我希望有人可以提供帮助。
The SQL:
SQL:
CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL,
pass CHAR(40) NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
registration_date DATETIME NOT NULL,
user_level TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
active CHAR(32),
PRIMARY KEY (user_id),
UNIQUE (username),
UNIQUE (email),
INDEX login (email, pass),
INDEX full_name (last_name, first_name)
) ENGINE=INNODB;
2 个解决方案
#1
14
1 and 2 are identical - both create two unique indexes, one for each key. #3 only creates one unique index across both keys, so no combination of username and email can be duplicated, but for example, a username could be duplicated as long as a different email was used.
1和2是相同的 - 都创建了两个唯一索引,每个索引一个。 #3仅在两个密钥上创建一个唯一索引,因此不能复制用户名和电子邮件的组合,但是,例如,只要使用不同的电子邮件,就可以复制用户名。
Sounds like you probably want either of the first two. UNIQUE and UNIQUE KEY are equivalent.
听起来你可能想要前两个中的任何一个。 UNIQUE和UNIQUE KEY是等价的。
#2
1
They are all synonymous as evidenced by syntax documentation:
它们都是同义词,如语法文档所示:
[CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option]
[CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] [index_type](index_col_name,...)[index_option]
[]
in this notation (Wirth's notation) denote optional elements
[]符号(Wirth的符号)表示可选元素
#1
14
1 and 2 are identical - both create two unique indexes, one for each key. #3 only creates one unique index across both keys, so no combination of username and email can be duplicated, but for example, a username could be duplicated as long as a different email was used.
1和2是相同的 - 都创建了两个唯一索引,每个索引一个。 #3仅在两个密钥上创建一个唯一索引,因此不能复制用户名和电子邮件的组合,但是,例如,只要使用不同的电子邮件,就可以复制用户名。
Sounds like you probably want either of the first two. UNIQUE and UNIQUE KEY are equivalent.
听起来你可能想要前两个中的任何一个。 UNIQUE和UNIQUE KEY是等价的。
#2
1
They are all synonymous as evidenced by syntax documentation:
它们都是同义词,如语法文档所示:
[CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option]
[CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] [index_type](index_col_name,...)[index_option]
[]
in this notation (Wirth's notation) denote optional elements
[]符号(Wirth的符号)表示可选元素