创建在其他两个表之间具有多对一关系的新表-sql server management studio
我有两个表,一个表有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须了解这些表之间的相互关系。必须有一些业务/逻辑规则来执行此操作,这将是原始关联。
然后你可以尝试类似的东西
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
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.