mysql ID递增修改

mysql ID递增修改,第1张

alter

table

questionlib

modify

id

int(11)

auto_increment

这样应该可以的,修改后从下一条记录开始自动增长。

如果想让原来的自动增长就得复制现有表的结构(无id),添加id并加上auto_increment,然后通过循环,添加n条空记录,然后对应先前表的id,依次插入数据。

如果跟其他表有关联就比较麻烦了

这样可以么?

你的表的列id是否是"auto_increment":

show

create

table

表名

若列id不是auto_increment的话,那肯定不能自增长了,修改其属性为"auto_increment"即可

alter

table

【表名】

modify

[name

varchar(22)]你可以讲name变为id

int(5)

NOT

NULL

auto_increment

PRIMARY

KEY

,试试

:id

int

identity(1,1)

解释:

identity是自动增长参数。

use

[你的数据库]

go

create

trigger

name

on

[table]

after

delete

as

begin

--定义游标,使你逐个往下找个ID,并执行update修改

declare

@flag

int

select

@flag=ID

from

deleted

declare

[cursorname]

cursor

for

select

ID

from

[table]

where

ID>@flag

open

[cursorname]

fetch

next

from

[cursorname]

update

[table]

set

ID=ID+1

where

ID=fetch

next

from

[cursorname]

WHILE

@@FETCH_STATUS

=

0

begin

update

[table]

set

ID=ID+1

where

ID=fetch

next

from

[cursorname]

close

[cursorname]

DEALLOCATE

authors_cursor

end

end


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存