mysql中如何通过写sql 来开启和关闭id的自动增长(表已建好)

mysql中如何通过写sql 来开启和关闭id的自动增长(表已建好),第1张

开启:ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型

NOT NULL AUTO_INCREMENT

关闭:ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 NOT NULL

你的ID号这么有规律 从1开始的话

那可以 增加数据

先 select count(*) from test 获取记录条数 和最大 ID比 如果不相等

那说明 有删除字段

那就

1 取消ID为自增字段

2 查询最小未使用的ID号 然后插入数据

3 再设置ID为自增字段

实际上 步骤2的sql语句有点麻烦。 效率也很低

我的建议是如下:

create table test (

id int auto_increment primary key,

deleteflag int ,

user char(30) not null,

pass varchar(30) not null)

增加一个deleteflag 标记 正常数据deleteflag 为 0

删除数据的时候 只是将deleteflag 标志设置为1

那样的话 增加数据时 只要update就可以了

读取select min(id) from test where deleteflag=1

update test deleteflag=0, 。。。。。。。。where deleteflag=1 and id=minID

MySQL主键的限制,每一个分区表中的公式中的列,必须在主键/unique key 中包括

MYSQL的官方文档里是这么说明的

18.5.1. Partitioning Keys, Primary Keys, and Unique Keys

This section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have.

In other words,every unique key on the table must use every columnin the table's partitioning expression. (This also includes the table's primary key, since it is by definition a unique key. This particular case is discussed later in this section.) For example, each of the following table creation statements is invalid:

解决办法是创建复合主键,将id和你的分区字段创建复合主键..


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6156945.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存