如何在 sql server management studio 2012 中创建复合外键 [英] How to create composite foreign key in sql server management studio 2012

查看:35
本文介绍了如何在 sql server management studio 2012 中创建复合外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过选择两列(OrderId、CompanyId)并右键单击并设置为主键,在 sql server management studio 2012 中成功创建复合主键.但我不知道如何使用 sql server management studio 2012 在其他表中的两列(OrderId、CompanyId)上创建外键.

I can successfully create composite primary key in sql server management studio 2012 by selecting two columns (OrderId, CompanyId) and right click and set as primary key. But i don't know how to create foreign key on two columns(OrderId, CompanyId) in other table by using sql server management studio 2012.

推荐答案

在对象资源管理器中,转到您的表并选择 Keys >上下文菜单中的新外键:

In Object Explorer, go to your table and select Keys > New Foreign Key from the context menu:

在弹出的对话框中,点击Add按钮来创建一个新的外键:

From the dialog box that pops up, click on the Add button to create a new foreign key:

给它一个有意义的名字,然后点击...按钮打开Tables and Columns Specification对话框:

Give it a meaningful name and then click on the ... button to open the Tables and Columns specification dialog box:

为父表和子表填写必要的列,点击确定大功告成!

Fill in the necessary columns for the parent and the child tables, click OK and you're done!

或者更简单、更高效 - 使用 T-SQL 脚本!

Or much easier and more efficiently - use a T-SQL script!

ALTER TABLE dbo.OtherTable
ADD CONSTRAINT FK_OtherTable_ParentTable
FOREIGN KEY(OrderId, CompanyId) REFERENCES dbo.ParentTable(OrderId, CompanyId)

这篇关于如何在 sql server management studio 2012 中创建复合外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆