mysql> insert into test4(a,b)values(1,6); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
可以看到,如果某个字段被设置为主键,那么插入重复的值就会报出违反主键约束的提示语。
(b) 多字段联合主键 此时的主键是由多个字段联合组成,多个字段之间使用逗号进行分隔,语法规则为:
1
PRIMARY KEY [字段1,字段2,...,字段n]
例子6:多字段联合主键的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> drop table if exists test5; Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> create table test5( -> a int not null comment '字段a', -> b int not null comment '字段b', -> primary key(a,b) -> ); Query OK, 0 rows affected (0.38 sec)
mysql> insert into test9(a) values(1); ERROR 1062 (23000): Duplicate entry '1' for key 'a'
(3)多字段设置唯一键,此时的唯一键有多个字段,多个字段之间使用逗号进行分隔,语法规则为:
1
UNIQUE KEY [字段1,字段2,...,字段n]
例子10:多字段设置唯一键的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> drop table if exists test10; Query OK, 0 rows affected, 1 warning (0.11 sec)
mysql> create table test10( -> a int not null comment '字段a', -> b int not null comment '字段b', -> unique key(a,b) -> ); Query OK, 0 rows affected (0.30 sec)
mysql> drop table if exists test11; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table test11( -> a int not null auto_increment primary key comment '字段a', -> b int not null comment '字段b' -> ); Query OK, 0 rows affected (0.35 sec)
C:\WINDOWS\system32>net stop mysql MySQL 服务正在停止.. MySQL 服务已成功停止。
C:\WINDOWS\system32>net start mysql MySQL 服务正在启动 ... MySQL 服务已经启动成功。
C:\WINDOWS\system32>mysql -uroot -proot mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> describe test10; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | a | int(11) | NO | PRI | NULL | | | b | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.13 sec)
mysql> desc test11; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | a | int(11) | NO | PRI | NULL | auto_increment | | b | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
查看表详细结构语句show create table
show create table语句可以用来显示创建表时的create table语句,相对应的语法格式为:
mysql> desc test11; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | a | int(11) | NO | PRI | NULL | auto_increment | | b | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
mysql> show create table test11; +--------+---------------------------------- | Table | Create Table +--------+------------------------------- | test11 | CREATE TABLE `test11` ( `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a', `b` int(11) NOT NULL COMMENT '字段b', PRIMARY KEY (`a`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COMMENT='测试第11号表' | +--------+------------------------------- 1 row in set (0.00 sec)
mysql> desc test11; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | a | int(11) | NO | PRI | NULL | auto_increment | | b | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
mysql> desc test13; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | a | int(11) | NO | | 0 | | | b | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> drop table if exists test14; Query OK, 0 rows affected (0.25 sec)
mysql> create table test14( -> a int not null auto_increment primary key comment '字段a', -> b int not null comment '字段b' -> ); Query OK, 0 rows affected (0.24 sec)
mysql> alter table test14 add column c int not null default 0 comment '字段c'; Query OK, 0 rows affected (0.69 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test14; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | a | int(11) | NO | PRI | NULL | auto_increment | | b | int(11) | NO | | NULL | | | c | int(11) | NO | | 0 | | +-------+---------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
(b) 在第一列的前面插入“新列”。
例子18:下面的例子将演示,如何在第一列的前面插入“新列”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
mysql> alter table test14 add column d int not null default 100 comment '字段d' first; Query OK, 0 rows affected (0.69 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test14; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | d | int(11) | NO | | 100 | | | a | int(11) | NO | PRI | NULL | auto_increment | | b | int(11) | NO | | NULL | | | c | int(11) | NO | | 0 | | +-------+---------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
(c) 在表的指定列之后插入“新列”。
例子19:下面的例子将演示,如何在a列后面插入“新列”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> alter table test14 add column e int not null comment '字段e' after a; Query OK, 0 rows affected (0.57 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test14; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | d | int(11) | NO | | 100 | | | a | int(11) | NO | PRI | NULL | auto_increment | | e | int(11) | NO | | NULL | | | b | int(11) | NO | | NULL | | | c | int(11) | NO | | 0 | | +-------+---------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)