0

I am trying to create a table like,

    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)
    )

but getting error,

Incorrect syntax

I need to declare TableAID as primary key too, can someone help me with syntax plz

After i try voo answer i get this error,

There are no primary or candidate keys in the referenced table 'TableB' that match the referencing column list in the foreign key 'FK__0A537D18'.

4

1 回答 1

2

在每列之后的 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 的外键来更改结构。并删除 TableATTitle、TableAName、TableASurName 的原因。结果查询:

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)
)
于 2013-04-24T14:21:32.470 回答