oracle刪除主鍵約束的問題m

2021-06-19 02:56:17 字數 2594 閱讀 6768

1:alter table 表名 drop primary key;

這個是把主鍵從表中去除,而不是真正的刪除主鍵

例子:建立表:create table test_table_students (student_id number not null,student_name varchar(20) not null,student_telephone long not null);

建立主鍵:alter table test_table_students add constraint test_key_students primary key (student_id,student_name);

第一次插入資料:insert into test_table_students (student_id,student_name,student_telephone) values (1,'alice',136133);

第二次插入資料:insert into test_table_students (student_id,student_name,student_telephone) values (1,'peter',136133); 提示主鍵約束

第三次插入資料:insert into test_table_students (student_id,student_name,student_telephone) values (2,'alice',136133); 提示主鍵約束

刪除主鍵約束:alter table test_table_students drop primary key;

第四次插入資料:insert into test_table_students (student_id,student_name,student_telephone) values (1,'peter',136134); 插入成功

第五次插入資料:insert into test_table_students (student_id,student_name,student_telephone) values (2,'alice',136135); 插入成功

刪除剛才兩行資料:delete from test_table_students where student_telephone=136134;delete from test_table_students where student_telephone=136135;

第二次新增主鍵約束:alter table test_table_students add constraint test_key_students primary key (student_id,student_name);約束名被占用

2:alter table 表名 drop constraint 約束名;

這個是把主鍵刪除,可以再次新增同名主鍵

例子:建立表:create table new_table_students (student_id number not null,student_name varchar(20) not null,student_telephone long not null);

建立主鍵:alter table new_table_students add constraint new_key_students primary key (student_id,student_name);

第一次插入資料:insert into new_table_students (student_id,student_name,student_telephone) values (1,'alice',136133);

第二次插入資料:insert into new_table_students (student_id,student_name,student_telephone) values (1,'peter',136133); 提示主鍵約束

第三次插入資料:insert into new_table_students (student_id,student_name,student_telephone) values (2,'alice',136133); 提示主鍵約束

刪除主鍵約束:alter table new_table_students drop constraint new_key_students;

第四次插入資料:insert into new_table_students (student_id,student_name,student_telephone) values (1,'peter',136134); 插入成功

第五次插入資料:insert into new_table_students (student_id,student_name,student_telephone) values (2,'alice',136135); 插入成功

刪除剛才兩行資料:delete from new_table_students where student_telephone=136134;delete from new_table_students where student_telephone=136135;

第二次新增主鍵約束:alter table new_table_students add constraint new_key_students primary key (student_id,student_name);再次新增成功

Oracle如何刪除主鍵約束的同時也刪除索引

一 現象 在oracle10g中刪除主鍵約束後,在插入重複資料時候仍然報 ora 00001 錯誤。二 原因 oracle在的10g版本中對內部函式 atbdui 進行了調整,導致在刪除約束的時候無法刪除使用者建立的索引。這個現象被oracle分類到了 problem 三 方法 在刪除約束的時候需要...

oracle的主鍵約束的新增和刪除

oracle的主鍵約束新增刪除 1 建立表的同時建立主鍵約束 一 無命名 create table accounts accounts number number primary key,accounts balance number 二 有命名 create table accounts acco...

oracle 約束之主鍵約束

1 主鍵約束作用 確保表中每一行資料是唯一的,要求非空且唯一 2 一張表中只能設定乙個主鍵約束 主鍵約束可以由多個字段構成 聯合主鍵或復合主鍵 1 在建立表時設定主鍵約束 建立聯合主鍵 通過user constraints資料字典查詢表中主鍵的名稱 2 修改表時新增主鍵約束 alter table ...