I'm just beginning with mysql. I'm trying to design a table which has columns containing an id which is auto-incremented, a unique username (userid), and a password.
Initially I thought the id should be the primary key so that I could easily update the data referring to the number, as is done in some forum softwares.
So I created the table with:
CREATE TABLE IF NOT EXISTS auth (
-> id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> userid VARCHAR(30) NOT NULL,
-> password VARCHAR(30) NOT NULL,
-> role VARCHAR(20) NOT NULL,
-> email VARCHAR(50)
-> );
However I found that if I did a replace command, it would duplicate rows.
mysql> SELECT * FROM `auth`;
+----+--------+-----------------+---------------+-------+
| id | userid | password | role | email |
+----+--------+-----------------+---------------+-------+
| 1 | jody | pw | administrator | NULL |
+----+--------+-----------------+---------------+-------+
1 row in set (0.00 sec)
mysql> REPLACE INTO `auth` SET `userid` = "jody", `password` = "pw", `role` = "administrator";
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM `auth`;
+----+--------+-----------------+---------------+-------+
| id | userid | password | role | email |
+----+--------+-----------------+---------------+-------+
| 1 | jody | pw | administrator | NULL |
| 2 | jody | pw | administrator | NULL |
+----+--------+-----------------+---------------+-------+
2 rows in set (0.00 sec)
I then tried to change the primary key to the username, but ran into this:
mysql> alter table `auth` drop primary key, add primary key (`userid`);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
So it seems something's very wrong with my understanding of primary keys. What's the problem here?