• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

[SQL] Assign FOREIGN KEY to an existing column

kito2

www.masteria.net
Joined
Mar 9, 2009
Messages
3,764
Solutions
1
Reaction score
227
Location
Chile, Santiago
Hello,

I have been digging on the web to assign a foreign key to an existing column.

The example is: Having banned_id at account_bans, which should refers to id at players.

Code:
ALTER TABLE account_bans
ADD FOREIGN KEY (banned_id) REFERENCES Players(id);

But I can't make it work on the server, just works on my localhost:

269aca57a4444e579de5c064a630b9a1.png

a12587c82db4404aa43b2544a34789a2.png
It is giving that error.

Can someone help?

Thanks!
 
Solution
That one worked:

Code:
ALTER TABLE account_bans ADD CONSTRAINT account_bans_banned_id_FK FOREIGN KEY ( banned_id ) REFERENCES players(id)
From that site, exactly that one, managed to create the specific query and worked on my localhost, but on main server didnt. I expect it may be a user problem, maybe permission problem as far as I have read on other sites.
 
From that site, exactly that one, managed to create the specific query and worked on my localhost, but on main server didnt. I expect it may be a user problem, maybe permission problem as far as I have read on other sites.
It could be your mysql version, I remember sharing code with someone and having to place the column names in ` ` so the query would execute.
You might want to compare the mysql/phpmyadmin versions, they might look the same but they could work completely differently.
 
Back
Top