看你的表的列id是否是"auto_increment": show create table 表名; 若列id不是auto_increment的話,那肯定不能自增長了,修改其屬性為"auto_increment"即可

成都創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務越城,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108
alter table 【表名】 modify [name varchar(22)];你可以講name變?yōu)閕d int(5) NOT NULL auto_increment PRIMARY KEY ,試試
:id int identity(1,1) 解釋: identity是自動增長參數(shù)。
1問:你有id=8的數(shù)據(jù)嗎?答:沒有!那你為什么where后面找id=8的?
2問:你想修改的是什么?答:id,那你修改數(shù)據(jù)名字干什么呢?改ID啊!
3問:你的ID是自增可修改嗎?答:是!那就修改,不是!那就把數(shù)據(jù)庫的ID列設(shè)置為可修改!
wl:武力,zl:智力,ts:統(tǒng)帥,zz是什么?
use
[你的數(shù)據(jù)庫]
go
create
trigger
name
on
[table]
after
delete
as
begin
--定義游標,使你逐個往下找個ID,并執(zhí)行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
=
begin
update
[table]
set
ID=ID+1
where
ID=fetch
next
from
[cursorname]
close
[cursorname]
DEALLOCATE
authors_cursor
end
end
思路:
1、首先搞清楚所有表的主外鍵關(guān)系
2、取消全部表的主鍵自增標識列,方便你后續(xù)的直接修改ID
例如:
exec?sp_configure?'allow?updates',1
reconfigure?with?override
GO
----取消標識列標記
update?syscolumns?set?colstat?=?0?where?id?=?object_id('表名')?and?colstat?=?1
GO
----恢復標識列標記
update?syscolumns?set?colstat?=?1?where?id?=?object_id('表名')?and?name?=?'標識列名稱'
3、寫個SQL腳本,修改ID,在修改的時候,一并更新全部表里的此ID值
例如:
declare?@old_id?as?int,@new_id?as?int
select?@old_id=12,@new_id=123
update?表名1?set?id=@new_id?where?id=@old_id
update?表名2?set?id=@new_id?where?id=@old_id
update?表名3?set?id=@new_id?where?id=@old_id
.....
update?表名n?set?id=@new_id?where?id=@old_id
創(chuàng)建表時設(shè)置遞增ID:
create table users (pkid int auto_increment primary key,...)
表創(chuàng)建完成后設(shè)置遞增ID:
alter table users add pkid int auto_increment primary key
注意:自增字段,一定要設(shè)置為primary key.
很多時候不希望pkId從1開始,我們可能希望他從10000開始:
alter table users AUTO_INCREMENT=10000;
4
你也可以修改現(xiàn)有的遞增值, 比如大批量刪除數(shù)據(jù)后,想id從654321退回123456開始:
alter table users AUTO_INCREMENT=123456;
本文名稱:mysql怎么改id信息 mysql改名字
地址分享:http://chinadenli.net/article26/hppgcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、ChatGPT、外貿(mào)建站、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)