在MySQL中可以使用AUTO_INCREMENT关键字设置自增字段,也可以通过自定义序列的方式设置自增字段。
如果要使用AUTO_INCREMENT关键字设置自增字段,要求字段的类型必须是int类型,并且一张数据表只能设置一个自增字段。
语法如下:
create table tablename(
字段名 int auto_increment primary key,
...) auto_increment=n;
create table stu(
s_id int primary key auto_increment,
s_name char(20),
birth datetime,
phone char(20)
);
输入数据,并查看结果:
mysql> insert into stu(s_name,birth,phone)
-> values('Zhangs','1999-2-13','15537302158'),
-> ('Wangg','2000-12-3','13837302666');
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+--------+---------------------+-------------+
| s_id | s_name | birth | phone |
+------+--------+---------------------+-------------+
| 1 | Zhangs | 1999-02-13 00:00:00 | 15537302158 |
| 2 | Wangg | 2000-12-03 00:00:00 | 13837302666 |
+------+--------+---------------------+-------------+
2 rows in set (0.00 sec)
create table emp(
e_id int primary key auto_increment,
e_name char(20),
birth datetime,
salary decimal(10,2)
) auto_increment=10001;
输入数据,并查看结果:
mysql> insert into emp(e_name,birth,salary)
-> values('Zhangs','1987-12-15',7500),
-> ('Wangg','1990-2-25',5300);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from emp;
+-------+--------+---------------------+---------+
| e_id | e_name | birth | salary |
+-------+--------+---------------------+---------+
| 10001 | Zhangs | 1987-12-15 00:00:00 | 7500.00 |
| 10002 | Wangg | 1990-02-25 00:00:00 | 5300.00 |
+-------+--------+---------------------+---------+
2 rows in set (0.00 sec)
create table book(
b_number char(30) not null,
b_name char(20) not null,
publishing_house char(50),
price decimal(10,2)
);
为book表添加一个字段b_id,int类型,自增字段并且设置为主键。
mysql> alter table book add b_id int primary key auto_increment first;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> show create table book\G
*************************** 1. row ***************************
Table: book
Create Table: CREATE TABLE `book` (
`b_id` int(11) NOT NULL AUTO_INCREMENT,
`b_number` char(30) NOT NULL,
`b_name` char(20) NOT NULL,
`publishing_house` char(50) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`b_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
然后更改自增字段的起始值,代码如下:
mysql> alter table book auto_increment=100001;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table book\G
*************************** 1. row ***************************
Table: book
Create Table: CREATE TABLE `book` (
`b_id` int(11) NOT NULL AUTO_INCREMENT,
`b_number` char(30) NOT NULL,
`b_name` char(20) NOT NULL,
`publishing_house` char(50) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`b_id`)
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
使用auto_increment定义自增字段,一张表只能定义一个自增字段。如果希望在一张表中创建多个自增字段,则可以使用下面的方法自定义序列(注:在Oracle数据库中可以使用create sequence命令直接定义序列)。
sequence表的结构如下:
create table sequences(
seq_id int primary key auto_increment,
seq_name char(50) not null,
current_value int not null,
step_length int not null default 1
);
delimiter //
create function seq_curval(name char(50))
returns int reads sql data
begin
declare value int;
set value:=0;
select current_value into value
from sequences
where seq_name=name;
return value;
end //
delimiter ;
delimiter //
create function seq_nextval(name char(50))
returns int deterministic contains sql
begin
update sequences
SET current_value=current_value+step_length
where seq_name=name;
return seq_curval(name);
end //
delimiter ;
delimiter //
create function seq_setval(name char(50),value int)
returns int deterministic contains sql
begin
update sequences
SET current_value=value
where seq_name=name;
return seq_curval(name);
end //
delimiter ;
mysql> insert into sequences(seq_name,current_value,step_length)
-> values('seq_goods',0,1),('seq_user',1000,1);
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from sequences;
+--------+-----------+---------------+-------------+
| seq_id | seq_name | current_value | step_length |
+--------+-----------+---------------+-------------+
| 1 | seq_goods | 0 | 1 |
| 2 | seq_user | 1000 | 1 |
+--------+-----------+---------------+-------------+
2 rows in set (0.00 sec)
create table goods(
g_id int primary key,
g_name char(20)
);
create table user(
user_id int primary key,
user_name char(20)
);
mysql> insert into goods(g_id,g_name)
-> values(seq_nextval('seq_goods'),'张平');
Query OK, 1 row affected (0.03 sec)
mysql> insert into goods(g_id,g_name)
-> values(seq_nextval('seq_goods'),'李大鹏');
Query OK, 1 row affected (0.03 sec)
mysql> select * from goods;
+------+-----------+
| g_id | g_name |
+------+-----------+
| 1 | 张平 |
| 2 | 李大鹏 |
+------+-----------+
2 rows in set (0.00 sec)
mysql> insert into user(user_id,user_name)
-> values(seq_nextval('seq_user'),'李大鹏');
Query OK, 1 row affected (0.01 sec)
mysql> insert into user(user_id,user_name)
-> values(seq_nextval('seq_user'),'张静');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1001 | 李大鹏 |
| 1002 | 张静 |
+---------+-----------+
2 rows in set (0.00 sec)
mysql> select seq_setval('seq_user',2001);
+-----------------------------+
| seq_setval('seq_user',2001) |
+-----------------------------+
| 2001 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> insert into user(user_id,user_name)
-> values(seq_nextval('seq_user'),'刘峰');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user(user_id,user_name)
-> values(seq_nextval('seq_user'),'张小天');
Query OK, 1 row affected (0.03 sec)
mysql> select * from user;
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1001 | 李大鹏 |
| 1002 | 张静 |
| 2002 | 刘峰 |
| 2003 | 张小天 |
+---------+-----------+
4 rows in set (0.00 sec)