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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值