如何创建外键 [英] how to create foreign key

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

问题描述

如何创建外键

i创建了两个表组织和管理器都具有ID coloumn如何在SQL studio 2005中继续

解决方案

< blockquote>这样的事情:



  ALTER   TABLE 经理
ADD FOREIGN KEY (Id)
REFERENCES 组织(Id)





在这里查找更多详细信息:



http://www.w3schools.com/sql/sql_foreignkey.asp [ ^ ]



http://msdn.microsoft.com/en-us/library/ms175464(v = SQL.105)。如px [ ^ ]


参考: http:// msdn .microsoft.com / zh-CN / library / ms189049.aspx [ ^ ]

例如:

  CREATE   TABLE 订单

O_Id int NOT NULL PRIMARY KEY
OrderNo int NOT NULL
P_Id int FOREIGN KEY 参考人员(P_Id)



要点:

Persons表中的P_Id列是人物表中的 PRIMARY KEY 。 />
订单表中的P_Id列是 FOREIGN KEY 在订单表中。


http://stackoverflow.com/questions/6340343/how-can-we-make-foreign-key-among-tables-after-inserting-那些数据表 [ ^ ]





< pre lang =sql> ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);


how to create foreign key
i have created two tables organisation and manager both are having ID coloumn how to proceed in SQL studio 2005

解决方案

Something like this:

ALTER TABLE Managers
ADD FOREIGN KEY (Id)
REFERENCES Organization(Id)



Look for more details here:

http://www.w3schools.com/sql/sql_foreignkey.asp[^]

http://msdn.microsoft.com/en-us/library/ms175464(v=sql.105).aspx[^]


Refer: http://msdn.microsoft.com/en-us/library/ms189049.aspx[^]
e.g:

CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)


Key Point:
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.


http://stackoverflow.com/questions/6340343/how-can-we-make-foreign-key-among-tables-after-inserting-data-in-those-tables[^]


ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);


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

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