Foreign Key relationship error in MySQL. - mysql

I am a total beginner in MySQL
Whenever I try to add a foreign key to a field it produces this error:
ERROR 1064 (42000): 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 'ADD FOREIGN KEY ID
Here's the code to add foreign key:
ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_FKName'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);
Please try to help me

you can try without constraint name :
ALTER TABLE 'table_name'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);

I think your syntax is a little wrong.
Try this:
ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_Name' FOREIGN KEY ('coloumn_name')
REFERENCES 'table_name'('coloumn_name');

The syntax is like , please update as
ALTER TABLE employee
ADD CONSTRAINT fk_department
FOREIGN KEY (departmentID)
references department (departmentID);

Related

Add Foreign Key in SQL by My-SQL Workbench gives Error 1064

I want to add a Foreign Key to a table called "address".
ALTER TABLE `students1`.`address`
ADD CONSTRAINT `id_country`
FOREIGN KEY ()
REFERENCES `students1`.`country` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION;
This request was automatically generated by MySQLWorkbench.
But get this error
ERROR 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 `students1`.`country` ()
ON DELETE NO ACTION
ON UPDATE NO ACT' at line 3
How fix this?
i don't know if your both tables have the same column id_country.
But the syntax in that case is:
ALTER TABLE `students1`.`address`
ADD CONSTRAINT
FOREIGN KEY (`id_country`)
REFERENCES `students1`.`country`(`id_country`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Error while adding a new foreign key column in MySql

ALTER TABLE cart
ADD COLUMN name varchar NOT NULL AFTER cartID
FOREIGN KEY (name) REFERENCES products(p_name);
error - #1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'AFTER cartID
FOREIGN KEY (name) REFERENCES products(p_name)' at line 2
Try this
ALTER TABLE cart
ADD COLUMN name varchar(10) NOT NULL;
ALTER TABLE cart
ADD FOREIGN KEY (name) REFERENCES products1(p_name);
Check this http://rextester.com/IJFDC23276
Try this
ALTER TABLE cart
ADD name VARCHAR NOT NULL,
ADD CONSTRAINT FOREIGN KEY(name) REFERENCES products(p_name);

what is Correct syntax for executing sql syntax?

String ag ="ALTER TABLE QUESTION"+
"(ADD FOREIGN KEY (a_status) REFERENCES ANSWER(a_status))";
stmt.executeUpdate(ag);
Error message:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'FOREIGN KEY
(a_status) REFERENCES ANSWER(a_status))' at line 1
This should be correct. See the offical documentation:
String ag ="ALTER TABLE QUESTION ADD FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_fk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
Also you can have more condition specific to add:
ON DELETE SET NULL/CASCADE
ON UPDATE SET NULL
String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_ibfk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
In general
ALTER TABLE Child_tblName
ADD CONSTRAINT 'give any name to the foreign key generally I do Child_tblName_Parent_tblName_ibfk_number' FOREIGN KEY ('Child_tblName's Column') REFERENCES Parent_tblName('Parent_tblName's Column')

I get an MYSQL error #1064 when add foreign key constraint

I keep getting this sql error
"#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 'Option (OptionId)' at line 1"
when I try and add a foreign key to the OptionId field from the Question Table to the OptionId(pk) field in the Option field. I don't get wy I keep getting the error because I don't see what is wrong with it.
Below is the foreign key constraint using ALTER TABLE:
ALTER TABLE Question ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId) REFERENCES Option (OptionId)
Table names and syntax are correct, I made sure by double checking.
Why is it not working?
option is a reserved word in MySQL and must be surrounded by backticks.
ALTER TABLE Question
ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId)
REFERENCES `Option` (OptionId)

MySql Alter Syntax error with mulitple FK

If i do the first one i have no problem. When i do addition i get a syntax error. What is wrong with the syntax? The error says syntax error near [entire 2nd line]
alter table `ban_Status` add FOREIGN KEY (`banned_user`) REFERENCES `user_data`(`id`)
alter table `ban_Status` add FOREIGN KEY (`banned_user`) REFERENCES `user_data`(`id`),
FOREIGN KEY (`banning_user`) REFERENCES `user_data`(`id`),
FOREIGN KEY (`unban_user`) REFERENCES `user_data`(`id`)
I think you need "add" before lines 2 and 3. That or you need to name your constraints.