简体   繁体   中英

Adding FOREIGN KEY Constraint in column

How can i add FOREIGN KEY constraint in the referrer_id(same as profile id) column? Since the column name contains parentheses it is throwing error

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

ERROR: Incorrect syntax near '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) 

I believe referrer_id is your column name

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

You don't to specify that it is same as profile id in foreign key, REFERENCES Profiles(profile_id) conveys that message to compiler.

or if you really have a ugly column name( referrer_id(same as profile id) ) then you need to use square brackets to escape the special characters present in your column name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM