An Alternative Method To Create Primary Keys And Auto Increment
In the example I provide in this guide, I always break out adding Primary Keys and Auto Increment into separate mysql statements.
You can also combine them into your create table statement. Doing it this way as opposed to the examples above are just a matter of style. There is no right or wrong way to do it.
create table addresses (
id int unsigned not null auto_increment primary key,
nameId int unsigned not null,
street varchar(252) null,
city varchar(64) null,
state varchar(2) null,
zip varchar(12) null
) engine=innodb default charset=latin1;
From the above example, you'll see that I only needed to add auto_increment and primary key to the end of the id field.
mysql> describe addresses;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| nameId | int(10) unsigned | NO | | NULL | |
| street | varchar(252) | YES | | NULL | |
| city | varchar(64) | YES | | NULL | |
| state | varchar(2) | YES | | NULL | |
| zip | varchar(12) | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)