oracle删除主键约束的问题m

来源:互联网 发布:国外英语交流软件 编辑:程序博客网 时间:2024/05/01 12:35

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);再次添加成功


原创粉丝点击