create table mydatabase.test
(id int not null auto_increment PRIMARY key,
name varchar(20))auto_increment = 3;
#auto_increment = 3 指定一个自增的初始值
insert into mydatabase.test(name) VALUES
('littlelawson'),
('shakespere');
select * from mydatabase.test
drop table mydatabase.test;
create table a
(id int identity(1,2),
name varchar(10)
);
报错如下:
mysql> create table a
-> (id int identity(1,2),
-> name varchar(10));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identity(1,2),
name varchar(10))' at line 2
错误原因:MySQL不支持identity关键字。但是mysql具有相同的特性,使用的关键字是auto_increment。
当插入记录时,如果为AUTO_INCREMENT数据列明确指定了一个数值,则会出现两种情况
AUTO_INCREMENT
数据列的值必须是唯一的;AUTO_INCREMENT
是数据列的一种属性,只适用于整数类型数据列AUTO_INCREMENT
数据列必须具备NOT NULL属性。AUTO_INCREMENT=n
选项来指定一个自增的初始值alter table table_name AUTO_INCREMENT=n
命令来重设自增的起始值,默认的起始值是1mysql> create table testAutoIncre(id int auto_increment primary key,name varchar(20));
Query OK, 0 rows affected (0.19 sec)
mysql> insert into testAutoIncre values(1,'Lawson'),(2,'Ting');
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from testAutoIncre;
+----+--------+
| id | name |
+----+--------+
| 1 | Lawson |
| 2 | Ting |
+----+--------+
2 rows in set (0.00 sec)
mysql> delete from testAutoIncre where id = 2;
Query OK, 1 row affected (0.04 sec)
mysql> insert into testAutoIncre(name) values('hadoop');
Query OK, 1 row affected (0.06 sec)
mysql> select * from testAutoIncre;
+----+--------+
| id | name |
+----+--------+
| 1 | Lawson |
| 3 | hadoop |
+----+--------+
2 rows in set (0.00 sec)
可以看到这条name= hadoop
记录的id值为3,而不是2。
在mysql 5.7中,将字段设置成auto_increment之后,是需要将其设置成主键/或者是主键的一部分,否则是不会通过的。
如下sql:
mysql> alter table orders modify id int auto_increment;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
应该将其修改成如下sql:
mysql> alter table orders modify id int auto_increment primary key;
Query OK, 0 rows affected (0.95 sec)
Records: 0 Duplicates: 0 Warnings: 0
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`address` varchar(20) DEFAULT NULL,
`score` varchar(10) DEFAULT NULL,
`rank` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| score | varchar(10) | YES | | NULL | |
| rank | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> select * from student;
+----+--------+---------------+-------+------+
| id | name | address | score | rank |
+----+--------+---------------+-------+------+
| 1 | lawson | anhui_jinzhai | 98.2 | 1% |
| 2 | ting | anhui_suzhou | NULL | NULL |
| 3 | spark | anhui_hefei | NULL | NULL |
| 4 | hadoop | anhui_sanyuan | NULL | NULL |
+----+--------+---------------+-------+------+
4 rows in set (0.00 sec)
mysql> select auto_increment from information_schema.tables where table_name = 'student' and table_schema = 'insidemysql';
+----------------+
| auto_increment |
+----------------+
| 5 |
+----------------+
1 row in set (0.00 sec)