如何使用 ALTER TABLE 删除 Oracle-SQL 中的内联外键?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的问题是您创建了外键而没有为约束指定名称。这是不好的做法,因为它使操作约束变得更加困难,因为几乎所有 Oracle DDL 都需要对象名称。
当我们没有明确命名约束时,Oracle 会生成一个默认约束。这些都非常相似,并且无法判断约束实际上是做什么的。例如,如果 SUPPLIER 有三个外键约束,则需要连接 USER_CONS_COLUMNS 视图,以便查看哪个约束实际上对 CITY 列强制执行规则。
因此,为了将来的参考,
无论如何,现在您需要找到外键约束的默认名称,这样您就可以删除它。我们假设您对 CITY 表也同样马虎,所以首先我们需要找到它的主键(如果您确实知道名称,则可以跳过此阶段)。
接下来,将该名称输入到此查询中:
最后,删除约束:
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,
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).
Next, feed that name into this query:
Finally, drop the constraint:
您想要这样做:
如果您没有为约束指定明确的名称,Oracle 会为您分配一个名称,因此您必须先找到它。您可以列出所有表约束,例如:
You want to do this:
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.: