如何将MySQL中的一对一关系更改为一对多关系?

发布于 2024-08-23 08:39:09 字数 397 浏览 7 评论 0原文

我目前有一个用户表,其中包含 Youtube OAuth 令牌的一对一关系。但是,我现在想要支持多个视频网站,并希望将其分解为一对多关系。

我已经设置了新表:

令牌 - cols:id、站点、用户名(用户在 Youtube 上的用户名)、oauth_token、oauth_secret

user_tokens - 列:id、user_id、token_id

有没有办法我可以从当前用户的表中 SELECT INTO 这些表来导入用户名,oauth_token和 oauth_secret 列,同时还使用适当的 id 设置 user_tokens 表?

过去我曾编写过简短的 PHP 脚本来执行此操作,但一直很好奇是否可以直接在 MySQL 中执行此操作。

I currently have a user's table which contains a one-to-one relationship for Youtube OAuth tokens. However, I now want to support multiple video sites and want to break this into a one-to-many relationship.

I have setup the new tables:

tokens - cols: id, site, username (the user's username on Youtube), oauth_token, oauth_secret

user_tokens - cols: id, user_id, token_id

Is there a way I can SELECT from my current user's table INTO these tables to import the username, oauth_token and oauth_secret columns while also setting up the user_tokens table with the appropriate id's?

In the past I have written short PHP scripts to do this, but have always been curious about whether I can do it directly in MySQL.

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

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

发布评论

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

评论(2

穿透光 2024-08-30 08:39:09

您不需要一对多关系的关系表,只需要令牌表中的 user_id 字段。这也使得填充表格变得更容易:(

insert into tokens (site, user_id, username, oauth_token, oauth_secret)
select site, user_id, username, oauth_token, oauth_secret
from users

因为我不确切知道用​​户表中的内容以及字段名称是什么,所以可能需要一些调整。)

You don't need a relation table for a one-to-many relationship, you just need a user_id field in the tokens table. That also makes it easier to poultate the table:

insert into tokens (site, user_id, username, oauth_token, oauth_secret)
select site, user_id, username, oauth_token, oauth_secret
from users

(As I don't know exactly what's in your user table and what the field names are, it might need some adjusting.)

潇烟暮雨 2024-08-30 08:39:09

查看 MySQL 文档。我认为这应该对你有帮助。

Checkout MySQL documentation. I think that should help you.

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