MySql Alter Syntax error with mulitple FK - mysql

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.

Related

Alter Table Foreign Key

I'm trying to add barista_grade_id to the baristas table as a foreign key and I've looked every where on what syntax to use and it seems to be unanimous that this is the correct way.
ALTER TABLE baristas
ADD FOREIGN KEY barista_grade_id REFERENCES barista_grade(barista_grade_id)
I did it before this and it worked. but because of some mistakes I deleted it and redo it again, but for whatever reason it says that it's a syntax error.
The format is like this:
ALTER TABLE baristas
ADD CONSTRAINT FOREIGN KEY (barista_grade_id) REFERENCES barista_grade (barista_grade_id);

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')

Foreign Key relationship error in 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);

How to add foreign key to MySQL table?

I use MySQL with InnoDB engine. I double-checked type of columns. But always have:
Error Code: 1215. Cannot add foreign key constraint
I tried:
ALTER TABLE `mail`.`boxes`
ADD CONSTRAINT FK_id
FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
and
ALTER TABLE `mail`.`boxes`
ADD FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
Nothing works(((
Please, help, what I am doing wrong (except choosing MySQL :-) )?
If table contains data then you are not able to add foreign key you drop table object and recreate
use below reference for the same
Basics of Foreign Keys in MySQL?
To check what exactly the problem is, use:
SHOW ENGINE INNODB STATUS\G
There is section "last foreign key error". Look at: http://dev.mysql.com/doc/refman/5.0/en/innodb-monitors.html
My guess is that data type od mail.boxes (id) and mail.users (id) is not the same. (E.g. smallint in one table and integer in second one).
Data in table on which you're trying to create FK could possibly also be problem (are your mailbox ids the same as id of existing users?)

Error inserting in mysql (constraint)

when I try to insert in the database I get the following error:
DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails (`vym`.`vendedor`, CONSTRAINT `fk_vendedor_division` FOREIGN KEY (`codigo_empresa`, `codigo_division`) REFERENCES `division` (`codigo_empresa`, `codigo`)) at vendedores_aes_insert_85 line 53
I know I have a constraint but I don't know how to interpret the message. What is the constraint and why?
It looks like the division column is violating it. I would check the definition of the `fk_vendedor_division constraint.
Basically, the error is saying that you are trying to use a division in the vendedor table that doesnt exist in the other one.
The constraint is the foreign key on table vym.vendedor; columns codigo_empresa, codigo_division) are referencing table division columns (codigo_empresa, codigo).