由於 這次 項目 做了 數據庫 遷移(從 mysql 轉到oracle 用的是navicat) 的工具 所以導致很多主鍵都丟失了
導致數據庫很多 數據的id重復 導致系統修改一條數據的時候 出現很多值相同 郁悶了大半天
然后 打算在plsql中 給現有的某張表 追加主鍵
教科書形式 如下
/*
1、創建表的同時創建主鍵約束
(1)無命名
create table student ( studentid int primary key not null, studentname varchar(8), age int);
(2)有命名
create table students ( studentid int , studentname varchar(8), age int, constraint yy primary key(studentid));
2、刪除表中已有的主鍵約束
(1)有命名
alter table students drop constraint yy;
(2)無命名
可用 SELECT * from user_cons_columns;
查找表中主鍵名稱得student表中的主鍵名為SYS_C002715
alter table student drop constraint SYS_C002715;
3、向表中添加主鍵約束
alter table student add constraint pk_student primary key(studentid);
*/
實際項目用如下:
alter table SDINTCALENDAR
add constraint SDINTCALENDAR_PRIMARYKEY primary key (ID)
using index
tablespace DEV2_DATA
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
結果報錯 ORA-02437: 無法驗證 (DENGCHAO.TEST) - 違反主鍵
查了網上 一資料 http://www.itpub.net/thread-917613-1-1.html 才恍然大悟
追加主鍵之前 必須 先刪除或修改原有重復id 的記錄 就可以了