mysql>createtable t1 (id int(11) primary key, name varchar(32), phone char(11)); Query OK, 0rows affected (0.19 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) |NO| PRI |NULL|| | name |varchar(32) | YES ||NULL|| | phone |char(11) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.01 sec)
也可以先定时字段后指定主键,例:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> create table t1 (id int(11), name varchar(32), phone char(11), primary key (id)); Query OK, 0 rows affected (0.18 sec)
mysql> desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(32) | YES | | NULL | | | phone | char(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
1.2.2. 设置联合主键
primary key (字段名1, 字段名2, ……)
例,创建数据表t1,把id、name联合起来作为主键。
1 2 3 4 5 6 7 8 9 10 11 12
mysql>createtable t1 (id int(11), name varchar(32), phone char(11), primary key (id, name)); Query OK, 0rows affected (0.18 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) |NO| PRI |NULL|| | name |varchar(32) |NO| PRI |NULL|| | phone |char(11) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 3rowsinset (0.01 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+----------------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+----------------+ | id |int(11) |NO| PRI |NULL| auto_increment | | name |varchar(24) |NO||NULL|| +-------+-------------+------+-----+---------+----------------+ 2rowsinset (0.01 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+----------------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+----------------+ | id |int(11) |NO| PRI |NULL| auto_increment | | name |varchar(24) |NO||NULL|| +-------+-------------+------+-----+---------+----------------+ 2rowsinset (0.01 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) |NO| PRI |NULL|| | name |varchar(22) | YES | UNI |NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.00 sec)
3.2.2. 删除唯一约束
alter table 表名 drop index 唯一约束名;
例,删除t1表的唯一约束name。
1 2 3 4 5 6 7 8 9 10 11 12
mysql>altertable t1 drop index name; Query OK, 0rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) |NO| PRI |NULL|| | name |varchar(22) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.00 sec)
mysql>desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) |NO| PRI |NULL|| | name |varchar(22) | YES | UNI |NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.01 sec)