1. 程式人生 > >oracle刪除主鍵約束的問題m

oracle刪除主鍵約束的問題m

oracle“刪除”主鍵約束的方法有兩個<write by SnowShana / qq:449394683>

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);再次新增成功