簡體   English   中英

在列中添加FOREIGN KEY約束

[英]Adding FOREIGN KEY Constraint in column

如何在Referrer_id(與配置文件ID相同)列中添加FOREIGN KEY約束? 由於列名包含括號,因此拋出錯誤

ALTER table Referrals ADD Constraint fk_referrer_ID 
       FOREIGN KEY(referrer_id(same as profile id)) REFERENCES Profiles(profile_id)

錯誤:“ referrer_id(與配置文件ID相同)”附近的語法不正確。

在下面的查詢中使用,列名用方括號([])分隔:

ALTER table Referrals ADD Constraint fk_referrer_ID FOREIGN KEY([referrer_id(same as profile id)]) REFERENCES Profiles(profile_id) 

我相信referrer_id是您的列名

ALTER table Referrals ADD Constraint fk_referrer_ID 
        FOREIGN KEY(referrer_id) REFERENCES Profiles(profile_id) 

您不必在外鍵中指定它與配置文件ID相同 ,因此REFERENCES Profiles(profile_id)會將消息傳達給編譯器。

或者,如果您確實有一個難看的列名( referrer_id(same as profile id) ),則需要使用方括號將列名中出現的特殊字符轉義

ALTER table Referrals ADD Constraint fk_referrer_ID 
         FOREIGN KEY([referrer_id(same as profile id)]) REFERENCES Profiles(profile_id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM