本篇内容介绍了“MySQL表怎么创建自增字段"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
注:如果使用新的自增互斥方式,对于replication应该避免使用INSERT...ONDUPLICATEKEYUPDATE语句。
设置新自增互斥方式:通过配置选项:innodb_autoinc_lock_mode:调整锁策略:
innodb_autoinc_lock_mode=0("传统"锁定模式:全部使用表锁)
innodb_autoinc_lock_mode=1(默认)("连续"锁定模式:可预判行数时使用新方式,不可时使用表锁)
innodb_autoinc_lock_mode=2("交错"锁定模式:全部使用新方式,不安全,不适合复制)
##创建自增字段
方法1、创建:
MySQL create tablec(iditauto _ increment,namevarchar(20),主键(id));
QueryOK,0rowsaffected(0.52sec)
mysqldescc
- - - - - -
|字段|类型|空|键|默认|额外|
- - - - - -
| id | int(11)| NO | PRI | NULL | auto _ increment |
|名称|varchar(20)|YES||NULL||
- - - - - -
2rowsinset(0.03sec)
方法2、修改:
mysqlcreatetablecc(idint,namevarchar(20));
QueryOK,0rowsaffected(0.42sec)
mysqltertableccchangedidinprimarykeyauto _ increment;
QueryOK,0rowsaffected(1.12sec)
记录33600个重复:个字节
p;0 Warnings: 0
mysql> desc cc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)
mysql> insert into cc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');
mysql> select * from cc;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 5 | d |
+----+------+
4 rows in set (0.00 sec)
注:只有int类型且为primary key 才可以使用auto_increment.
##对存在记录的表的列修改为自增列
mysql> create table ccc (id int,name varchar(20));
Query OK, 0 rows affected (0.27 sec)
mysql> insert into ccc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');
Query OK, 4 rows affected (0.53 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from ccc;
+------+------+
| id | name |
+------+------+
| 1 | a |
| NULL | b |
| NULL | c |
| 5 | d |
+------+------+
4 rows in set (0.00 sec)
mysql> alter table ccc change id id int primary key auto_increment;
Query OK, 4 rows affected (1.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> desc ccc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into ccc(id,name) values(1,'a'),(NULL,'b'),(NULL,'c'),(5,'d');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into ccc(id,name) values(6,'aa'),(NULL,'ab'),(NULL,'ac'),(10,'ad')
;
Query OK, 4 rows affected (0.07 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from ccc;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 5 | d |
| 6 | aa |
| 7 | ab |
| 8 | ac |
| 10 | ad |
+----+------+
8 rows in set (0.00 sec)
mysql>
“MySQL表怎么创建自增字段”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/102052.html