MySQLWorkbench Syntax Error - mysql

I'm trying to execute the following SQL command in MySQLWorkbench but it's giving me an error.
Command:
ALTER TABLE `ABC`.`GroupMembers`
ADD CONSTRAINT `FK_PROFILES`
FOREIGN KEY ()
REFERENCES `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Error:
Operation failed: There was an error while applying the SQL script to the database.
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 `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION' at line 3
SQL Statement:
ALTER TABLE `ABC`.`GroupMembers`
ADD CONSTRAINT `FK_PROFILES`
FOREIGN KEY ()
REFERENCES `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION
Not sure what's up. This script was generated by MySQLWorkbench

There has to be a column (or columns) in both lists. Those can't be empty.
For example:
FOREIGN KEY (profile_id)
-- ^^^^^^^^^^
REFERENCES `ABC`.`profiles` (id)
-- ^^
The datatypes of the columns much match exhactly. And values stored in the foreign key column must match the value in a row in the referenced table. (In this example, all values in profile_id must match the value of the id column in the profiles table.

MYSQL is saying its an syntax issue, and as I can see, there is a couple of things missing in you code.
Please check this link and learn more about the sintaxis in mysql regarding constraints.
Hope it helps.
Edit:
Ok, so just to enforce spencer7593's answer (which should be marked as answer if it solves your issue):
...
/profile_id would be the name you will set to the foreign key
FOREIGN KEY (profile_id)
...
/The references should be of the same value.
/`table_name`.`column_name` is the referenced column
/ id is the column on the table which will hold foreign key
REFERENCES `ABC`.`profiles` (id)

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;

MySQL Error 1064 when creating foreign key

I created this using MySQL WorkBench
ALTER TABLE `android_marketplace`.`ban_utilizator`
DROP INDEX ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC);
ALTER TABLE `android_marketplace`.`ban_utilizator`
ADD CONSTRAINT `ban_utilizator_id_utilizator`
FOREIGN KEY (`id_utilizator`)
REFERENCES `android_marketplace`.`utilizatori` (`id_utilizator`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
I am getting this :
Operation failed: There was an error while applying the SQL script to the
database.
Executing:
ALTER TABLE `android_marketplace`.`ban_utilizator`
DROP INDEX ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC);
ALTER TABLE `android_marketplace`.`ban_utilizator`
ADD CONSTRAINT `ban_utilizator_id_utilizator`
FOREIGN KEY (`id_utilizator`)
REFERENCES `android_marketplace`.`utilizatori` (`id_utilizator`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
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 '
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC)' at line 2
SQL Statement:
ALTER TABLE `android_marketplace`.`ban_utilizator`
DROP INDEX ,
ADD INDEX `ban_utilizator_id_utilizator_idx` (`id_utilizator` ASC)
The error has nothing to do with a foreign key.
You must name the index you want to drop.
Syntax error messages include the context of the problem. If the syntax error says:
check ... for the right syntax to use near '
ADD INDEX ban_utilizator_id_utilizator_idx (id_utilizator ASC)'
This means the syntax was expecting to find something else at the point where you provided ADD INDEX.
In this case, it was expecting a name for the index you tried to drop in the clause immediately preceding ADD INDEX.

Error 1064 in MySQL Workbench when synchronising a model to a server

I am new to MySQL and have only used Microsoft Access before for SQL. When I try to add the Model to the server ([Database]->[Synchronize Model]), the auto-generated SQL code throws this error
Executing SQL script in server
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 `medicineproblems`.`SleepTimes` ()
ON DELETE NO ACTION
' at line 7
SQL Code:
CREATE TABLE IF NOT EXISTS `medicineproblems`.`Records` (
`Date` DATE NOT NULL,
`SleepDuration` DECIMAL NOT NULL DEFAULT 0,
`MoodAverage` DECIMAL NOT NULL DEFAULT 0,
PRIMARY KEY (`Date`),
CONSTRAINT `SleepDuration`
FOREIGN KEY ()
REFERENCES `medicineproblems`.`SleepTimes` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `MoodAverage`
FOREIGN KEY ()
REFERENCES `medicineproblems`.`Mood` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
SQL script execution finished: statements: 5 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I want to be able to know what went wrong. So in your answers, can you please tell me possible causes of the error so I can aim to avoid it?
You are missing the column name to reference on that table as well as the column which will be referring to and thus the error is pointing about. The below line
FOREIGN KEY ()
REFERENCES `medicineproblems`.`SleepTimes` ()
Should be
FOREIGN KEY(some_column_name)
REFERENCES `medicineproblems`.`SleepTimes`(some_column_name)
Similarly you should correct this in the below part of your CREATE TABLE syntax
CONSTRAINT `MoodAverage`
FOREIGN KEY ()
REFERENCES `medicineproblems`.`Mood` ()
See MySQL Documentation for more information. Correct syntax is:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

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

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)