在MySQL中刪除主鍵需要兩步.

創(chuàng)新互聯(lián)公司主營松溪網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),松溪h5微信平臺小程序開發(fā)搭建,松溪網(wǎng)站營銷推廣歡迎松溪等地區(qū)企業(yè)咨詢
1、如果有auto_increment,先刪除之;
alter table products change pid pid int;
2、刪除主鍵約束 primary key.
alter table products drop primary key;
主鍵是一個(gè)特殊的索引,使用刪除索引的語句可以刪除,例如:
Alter table tbn drop primary key
sql中刪除主鍵約束方法:
在服務(wù)器資源管理器中,選擇包含該主鍵的表,再從“數(shù)據(jù)”菜單中單擊“打開表定義”。該表在“表設(shè)計(jì)器”中打開。
在表網(wǎng)格中右擊包含該主鍵的行,然后選擇“移除主鍵”以將該設(shè)置從啟用切換到禁用。
說明:若要撤消此操作,請關(guān)閉該表而不保存更改。 刪除主鍵的操作無法在不丟失對該表作出的其他所有更改的情況下撤消。當(dāng)保存表或關(guān)系圖時(shí),將從數(shù)據(jù)庫中刪除約束。
刪除主鍵時(shí)是否會(huì)刪除索引?
答案取決于索引是創(chuàng)建主鍵時(shí)自動(dòng)創(chuàng)建的,還是創(chuàng)建主鍵前手工創(chuàng)建的。
測試如下:--建表create
table
hqy_test(id
integer)
;--建索引create
(unique)index
idx_hqy_id
on
hqy_test(id)
;--加主鍵alter
table
hqy_test
add
constraint
pk_hqy_id
primary
key
(id);
select
index_name
from
user_indexes
where
index_name='IDX_HQY_ID';IDX_HQY_ID
---刪除主鍵
alter
table
hqy_test
drop
constraint
pk_hqy_id;或者:alter
table
hqy_test
drop
primary
key;
也是行的。
select
index_name
from
user_indexes
where
index_name='IDX_HQY_ID';
IDX_HQY_ID
==沒有刪除索引
--刪除索引,增加主鍵并自動(dòng)創(chuàng)建索引
drop
index
idx_hqy_id;
alter
talbe
hqy_test
add
constraint
pk_hqy_id
primary
key(id)
using
index;
select
index_name
from
user_indexes
where
index_name='PK_HQY_ID';
PK_HQY_ID
==自動(dòng)創(chuàng)建了索引
--刪除主鍵約束
alter
table
hqy_test
drop
primary
key;
select
index_name
from
user_indexes
where
index_name='PK_HQY_ID';
無
==索引被刪除了
如果刪除主鍵時(shí),希望同時(shí)刪掉索引,則應(yīng)該增加drop
index選項(xiàng),從而不管索引是否是創(chuàng)建主鍵時(shí)自動(dòng)創(chuàng)建的,即:alter
table
hqy_test
drop
primary
key
drop
index;
百度了一下,如果是自增列要兩句:
ALTER?TABLE?tabname??MODIFY?col?INT?NOT?NULL;
ALTER?TABLE?tabname?DROP?PRIMARY?KEY;
你再這樣試下ALTER TABLE 表名 drop CONSTRAINT PK_表名
最后的得用約束名稱 默認(rèn)的都這樣 還不對的話 你查看下你的主鍵約束
sp_helpconstraint 表名
查的第2個(gè)表就是約束表
新聞名稱:mysql表怎么刪除主鍵 mysql如何刪除主鍵
轉(zhuǎn)載源于:http://chinadenli.net/article42/hjehec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、Google、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)