jobs1109 Posted September 16, 2011 Share Posted September 16, 2011 Hi everyone, I am trying to add a foreign key (jointable) to an existing table called (Jobs) and I would like it to reference the id in users table. Also I like to have it auto update when records added and deleted. ENGINE=InnoDB When I run the following code I get an error message. ALTER TABLE Jobs ADD FOREIGN KEY (jointable) REFERENCES users (id); here is the error message Error SQL query: ALTERTABLE Jobs ADDFOREIGNKEY ( jointable )REFERENCES users( id ) MySQL said: #1072 - Key column 'jointable' doesn't exist in table What am I doing wrong ? please help Quote Link to comment https://forums.phpfreaks.com/topic/247276-adding-foreign-key-to-existing-table/ Share on other sites More sharing options...
Adam Posted September 16, 2011 Share Posted September 16, 2011 Remove the brackets around "jointable". To have it auto update you need to add to the end: ON DELETE CASCADE ON UPDATE CASCADE Quote Link to comment https://forums.phpfreaks.com/topic/247276-adding-foreign-key-to-existing-table/#findComment-1269925 Share on other sites More sharing options...
jobs1109 Posted September 16, 2011 Author Share Posted September 16, 2011 Hi when I remove the brackets I get the followin message. Here is the code ALTER TABLE Jobs ADD FOREIGN KEY jointable REFERENCES users (id); here is the error message Error SQL query: ALTERTABLE Jobs ADDFOREIGNKEY jointable REFERENCES users( id ) MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES users (id)' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/247276-adding-foreign-key-to-existing-table/#findComment-1269939 Share on other sites More sharing options...
jobs1109 Posted September 16, 2011 Author Share Posted September 16, 2011 both users TABLE and Jobs TABLE =InnoDB Quote Link to comment https://forums.phpfreaks.com/topic/247276-adding-foreign-key-to-existing-table/#findComment-1269944 Share on other sites More sharing options...
Adam Posted September 16, 2011 Share Posted September 16, 2011 Sorry, bit rusty. You need: [...] jointable(id) REFERENCES users(id) [...] Quote Link to comment https://forums.phpfreaks.com/topic/247276-adding-foreign-key-to-existing-table/#findComment-1269978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.