![](/img/trans.png)
[英]how to create a table with two primary keys where one is foreign key in one table and the other on another table?
[英]How can I create a table with five foreign keys and one of them as primary key (which is also a VARCHAR)
我想創建一個像這樣的表,
USE [labblyDabbly3455]
GO
CREATE TABLE [dbo].[daTableBang]
(
TableAID varchar FOREIGN KEY REFERENCES TableA(ID)
TableATitle VarChar FOREIGN KEY REFERENCES TableA(title)
TableAName varchar FOREIGN KEY REFERENCES TableA(Name)
TableASurName varchar FOREIGN KEY REFERENCES TableA(surname)
TableBID int FOREIGN KEY REFERENCES TableB(ID)
)
但得到錯誤,
語法不正確
我需要將TableAID聲明為主鍵, 有人可以幫助我使用語法plz
在我嘗試voo回答后我得到了這個錯誤,
引用表'TableB'中沒有與外鍵'FK__0A537D18'中的引用列列表匹配的主鍵或候選鍵。
在每列之后的CREATE查詢中添加逗號
CREATE TABLE [dbo].[daTableBang]
(
TableAID int FOREIGN KEY REFERENCES TableA(ID),
TableATitle VarChar FOREIGN KEY REFERENCES TableA(title),
TableAName varchar FOREIGN KEY REFERENCES TableA(Name),
TableASurName varchar FOREIGN KEY REFERENCES TableA(surname),
TableBID int FOREIGN KEY REFERENCES TableB(ID)
)
另外,我建議您通過將TableAID和TableBID作為主鍵,並將TableAID作為表A的外鍵,並將TableBID作為表B的外鍵來更改結構。並刪除TableATitle,TableAName,TableASurName of caurse。 並且結果查詢:
CREATE TABLE [dbo].[daTableBang]
(
TableAID int not null FOREIGN KEY REFERENCES TableA(ID),
TableBID int not null FOREIGN KEY REFERENCES TableB(ID),
PRIMARY KEY (TableAID,TableBID)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.