您好,欢迎访问一九零五行业门户网

将 MySQL 列更改为 AUTO_INCRMENT?

假设我们有一个表,现在需要在列名上添加 auto_incrment。为此,请使用 modify 命令。
在这里,我们首先创建一个演示表。
mysql> create table addingautoincrement -> ( -> id int, -> name varchar(200), -> primary key(id) -> );query ok, 0 rows affected (0.47 sec)
我们在上面创建了一个表,现在让我们更改该表以在列名“id”上添加 auto_incrment。语法如下 -
alter table yourtablenamet modify yourcolumnname int auto_increment;
应用上述语法添加 auto_incrment。查询如下。
mysql> alter table addingautoincrement modify id int auto_increment;query ok, 0 rows affected (1.19 sec)records: 0 duplicates: 0 warnings: 0
上面,我们在列名“id”上添加了“auto_incrment”。让我们在 desc 命令的帮助下检查一下。查询如下 -
mysql> desc addingautoincrement;
示例输出。
+-------+--------------+------+-----+---------+----------------+| field | type | null | key | default | extra |+-------+--------------+------+-----+---------+----------------+| id | int(11) | no | pri | null | auto_increment || name | varchar(200) | yes | | null | |+-------+--------------+------+-----+---------+----------------+2 rows in set (0.00 sec)
查看上面的输出和列名称“extra”。在列名“extra”中,有一个关键字auto_increment。这本身就说明我们已经成功添加了关键字。
现在,我将插入记录并检查该行是否加一。查询如下 -
mysql> insert into addingautoincrement(name) values('john');query ok, 1 row affected (0.20 sec)mysql> insert into addingautoincrement(name) values('smith');query ok, 1 row affected (0.12 sec)mysql> insert into addingautoincrement(name) values('bob');query ok, 1 row affected (0.10 sec)
借助 select 语句显示所有记录。
mysql> select *from addingautoincrement;
以下是输出。
+----+-------+| id | name |+----+-------+| 1 | john || 2 | smith || 3 | bob |+----+-------+3 rows in set (0.00 sec)
正如您在上面的输出中看到的,行增加了 1。
以上就是将 mysql 列更改为 auto_incrment?的详细内容。
其它类似信息

推荐信息