使用 SQL 更改外键
我有一个这样的表:
ID Name Email Referred_by
-------------------------------------------
1 John [email protected] NULL
2 Sam [email protected] [email protected]
3 Sally [email protected] [email protected]
..... more rows .....
我想将其更改为:
ID Name Email Referred_by
-------------------------------------------
1 John [email protected] NULL
2 Sam [email protected] 1
3 Sally [email protected] 2
..... more rows .....
我可以使用 SQL 进行此更改吗?
I have a table like this:
ID Name Email Referred_by
-------------------------------------------
1 John [email protected] NULL
2 Sam [email protected] [email protected]
3 Sally [email protected] [email protected]
..... more rows .....
And I would like to change it to:
ID Name Email Referred_by
-------------------------------------------
1 John [email protected] NULL
2 Sam [email protected] 1
3 Sally [email protected] 2
..... more rows .....
Can I make this change using SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以解决问题:
This should do the trick:
许多 DBMS 将通过使用 DDL(定义)语句而不是 DML(操作)来实现这一点。假设
id
是整数类型,并且referred_by
是(当前)文本列,您可以执行以下操作:ref2< /code>(可为空)与
id
类型相同。ref2
和id
之间设置外键约束。ref2
列。referred_by
。ref2
重命名为referred_by
。Many DBMS' will allow this by the use of DDL (definition) statements rather than DML (manipulation). Assuming that
id
is an integral type andreferred_by
is (currently) a textual column, you can do something like:ref2
(nullable) of the same type asid
.ref2
andid
.ref2
column for all the rows.referred_by
.ref2
toreferred_by
.