mysql中auto_increment的作用是什么

这篇文章主要讲解了“mysql中auto_increment的作用是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql中auto_increment的作用是什么”吧!

说明

1、auto_increment约束条件就是加在主键字段上的,并且在插入数据的时候主键的值无需手动插入了。

2、auto_increment数据列必须有唯一索引,以避免序号重复;必须具备NOT NULL属性。

实例

-- 创建表的完整语法
mysql> create table t5(id int primary key auto_increment, name varchar(4), age int(3), phone varchar(11));
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into t5 (name, age, phone) values ('python', 5, '119'),('java', 10, '120');
Query OK, 2 rows affected, 1 warning (0.15 sec)
Records: 2  Duplicates: 0  Warnings: 1
 
mysql> select * from t5;
+----+------+------+-------+
| id | name | age  | phone |
+----+------+------+-------+
|  1 | pyth |    5 | 119   |
|  2 | java |   10 | 120   |
+----+------+------+-------+
2 rows in set (0.00 sec)
 
mysql> desc t5;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(4)  | YES  |     | NULL    |                |
| age   | int(3)      | YES  |     | NULL    |                |
| phone | varchar(11) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

感谢各位的阅读,以上就是“mysql中auto_increment的作用是什么”的内容了,经过本文的学习后,相信大家对mysql中auto_increment的作用是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!