如何使用 ALTER TABLE 删除 Oracle-SQL 中的内联外键?

发布于 2024-12-22 18:41:42 字数 207 浏览 2 评论 0原文

create table supplier(  
.  
.  
.  
city varchar2(16) references city(city_name)  
);  

正确的查询是什么?

alter table suppliers modify city varchar2(16);  
create table supplier(  
.  
.  
.  
city varchar2(16) references city(city_name)  
);  

What's the correct query?

alter table suppliers modify city varchar2(16);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-12-29 18:41:42

您遇到的问题是您创建了外键而没有为约束指定名称。这是不好的做法,因为它使操作约束变得更加困难,因为几乎所有 Oracle DDL 都需要对象名称。

当我们没有明确命名约束时,Oracle 会生成一个默认约束。这些都非常相似,并且无法判断约束实际上是做什么的。例如,如果 SUPPLIER 有三个外键约束,则需要连接 USER_CONS_COLUMNS 视图,以便查看哪个约束实际上对 CITY 列强制执行规则。

因此,为了将来的参考,

city varchar2(16) constraint city_fk references city(city_name)

无论如何,现在您需要找到外键约束的默认名称,这样您就可以删除它。我们假设您对 CITY 表也同样马虎,所以首先我们需要找到它的主键(如果您确实知道名称,则可以跳过此阶段)。

 select constraint_name 
 from user_constraints
 where table_name = 'CITY' 
 and constraint_type = 'P'

接下来,将该名称输入到此查询中:

 select constraint_name 
 from user_constraints
 where table_name = 'SUPPLIER' 
 and constraint_type = 'R'
 and r_constraint_name = '&CITY_PK'

最后,删除约束:

alter table supplier drop constraint city_fk

The problem you have is that you have created a foreign key without giving a name to the constraint. This is bad practice, because it makes it harder to manipulate the constraint, as pretty much all Oracle DDL requires the object name.

When we don't explicitly name the constraints Oracle generates a default one. These are all horribly similar and there is no way of telling what the constraint actually does. For instance, if you had three foreign key constraints on SUPPLIER you would need to join with the USER_CONS_COLUMNS view in order to see which constraint actually enforce a rule on the CITY column.

So, for future reference,

city varchar2(16) constraint city_fk references city(city_name)

Anyway, right now you need to find the defaulted name of the foreign key constraint, so you can drop it. We'll assume you were equally sloppy with the CITY table, so first we need to find its primary key (you can skip this stage if you actually know the name).

 select constraint_name 
 from user_constraints
 where table_name = 'CITY' 
 and constraint_type = 'P'

Next, feed that name into this query:

 select constraint_name 
 from user_constraints
 where table_name = 'SUPPLIER' 
 and constraint_type = 'R'
 and r_constraint_name = '&CITY_PK'

Finally, drop the constraint:

alter table supplier drop constraint city_fk
时光是把杀猪刀 2024-12-29 18:41:42

您想要这样做:

ALTER TABLE supplier
DROP CONSTRAINT constraint_name

如果您没有为约束指定明确的名称,Oracle 会为您分配一个名称,因此您必须先找到它。您可以列出所有表约束,例如:

SELECT *
FROM user_constraints
WHERE TABLE_NAME='SUPPLIER'

You want to do this:

ALTER TABLE supplier
DROP CONSTRAINT constraint_name

If you didn't give the constraint a explicit name, Oracle asigned one for you so you have to find it first. You can list all the table constraints with, e.g.:

SELECT *
FROM user_constraints
WHERE TABLE_NAME='SUPPLIER'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文