创建在其他两个表之间具有多对一关系的新表-sql server management studio

发布于 2024-08-29 06:58:55 字数 110 浏览 6 评论 0原文

我有两个表,一个表有 rowguid 列,另一个表有自动递增列,但两个表都没有主键或外键。我想创建第三个表来存储上述两个表之间的关系。有人能给我正确且最简单的方法吗?我是新使用sql server。请回复我

I have two tables, one table has rowguid column and other has auto incremented column but both tables dont have primary key or foreign key. I want to create third table that store the relationship between above two tables. can somebody give me the correct and easiest way to do that. i m new using sql server. please reply me

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

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

发布评论

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

评论(2

°如果伤别离去 2024-09-05 06:58:55

首先,您必须了解这些表之间的相互关系。必须有一些业务/逻辑规则来执行此操作,这将是原始关联。

然后你可以尝试类似的东西

DECLARE @TableGuid TABLE(
        GuidID UNIQUEIDENTIFIER DEFAULT NEWID(),
        Val VARCHAR(10)
)

DECLARE @TableID TABLE(
        ID INT IDENTITY(1,1),
        Val VARCHAR(10)
)

INSERT @TableGuid (Val) SELECT 'A'
INSERT @TableID (Val) SELECT 'A'

SELECT * FROM @TableGuid
SELECT * FROM @TableID

DECLARE @TableManyMany TABLE(
        GuidID UNIQUEIDENTIFIER,
        ID INT,
        PRIMARY KEY(GuidID, ID)
)

INSERT INTO @TableManyMany
SELECT  GuidID,
        ID
FROM    @TableGuid g INNER JOIN
        @TableID i ON g.Val = i.Val

SELECT  *
FROM    @TableManyMany

Firstly, you will have to make a call on how these tables relate to each other. There will have to be some business/logic rule to do this, and that would be the original association.

Then you can try something like

DECLARE @TableGuid TABLE(
        GuidID UNIQUEIDENTIFIER DEFAULT NEWID(),
        Val VARCHAR(10)
)

DECLARE @TableID TABLE(
        ID INT IDENTITY(1,1),
        Val VARCHAR(10)
)

INSERT @TableGuid (Val) SELECT 'A'
INSERT @TableID (Val) SELECT 'A'

SELECT * FROM @TableGuid
SELECT * FROM @TableID

DECLARE @TableManyMany TABLE(
        GuidID UNIQUEIDENTIFIER,
        ID INT,
        PRIMARY KEY(GuidID, ID)
)

INSERT INTO @TableManyMany
SELECT  GuidID,
        ID
FROM    @TableGuid g INNER JOIN
        @TableID i ON g.Val = i.Val

SELECT  *
FROM    @TableManyMany
漆黑的白昼 2024-09-05 06:58:55

Amby,除非通过脚本,否则切勿对数据库执行任何操作。使用 GUI 界面是一种非常糟糕的做法,您现在需要改掉这个习惯。您需要脚本中的内容,以便可以将它们放入源代码管理中并使用它们部署到其他服务器。

永远不要,我的意思是永远不要有一个没有主键的表。这是数据库设计的第一天。如果没有主键,至少有一个唯一索引吗?如果没有,您可能会得到重复的值,这很糟糕。任何无法唯一标识行的表都会被破坏。在继续之前解决这个问题。

您还需要某种方法来确定两个表之间的关系。你如何知道tablea中的哪条记录与tableb中的哪条记录匹配?由于您有两种不同的数据类型,因此这不是关系,因此您需要在两个表中具有相同的其他字段(或字段组合)来构建关系。如果连这个都没有,那么这段关系的基础是什么?请在两个表中显示示例数据,并在桥接表中显示您想要的数据。我相信您还没有根据您发布的内容充分考虑表格设计。

Amby, never do anything to a database except through a script. It is a very poor practice to use the GUI interface and you need to break the habit right now. YOu need things in scripts so that you can put them in source control and use them to deploy to other servers.

Never and, I do mean never, have a table without a primary key. That's database design day one. If you don't have a primary key, do you at least have a unique index? If not, you can get duplicate values which is bad. Any table without a way to uniquely identify a row is broken. Fix that before you go any farther.

What you also need is some way to determine what the relationship is between the two tables. How do you know which record in tablea is to match to which record in tableb? Since you have two different datatypes, that is not the relationship so you need to have some other field (or combination of fields) which is the same in both tables to build the relationship on. If you don't have that, what is the basis of the relationship. Please show sample data in both tables and show which data you want in the bridge table. I believe you have not yet thought out the table design enough based on what you posted.

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