23、序列使用 auto_increment

序列使用 auto_increment

  • MySQL 序列是一组整数:1, 2, 3, ...,
  • 一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现。
  • 使用序列的方法就是使用 AUTO_INCREMENT 来定义列
  • 使用auto_increment定义的字段,必须同时也定义为primary key

实例:

create table test_increment(
id int unsigned not null auto_increment,
name varchar(20) not null,
date date not null,
primary key (id)
);

insert into test_increment values
(1,'张三','2001-09-10'),
(null,'李四','2001-09-10');

mysql> select * from test_increment;
+----+------+------------+
| id | name | date       |
+----+------+------------+
|  1 | 张三 | 2001-09-10 |
|  2 | 李四 | 2001-09-10 |
+----+------+------------+
2 rows in set

(1,'王五','2001-09-10');
1064 - You have an error in your SQL syntax;...
posted @ 2019-10-15 05:07  楠change  阅读(182)  评论(0)    收藏  举报