Syntax error in MySQL 5.7 for use CONSTRAINT - mysql

I try to fix some error in Jira as mention in this page but when I try to run an SQL query, I got the below 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 'CONSTRAINT
fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id' at line 1
My Query is:
ALTER TABLE AO_8542F1_IFJ_OBJ_ATTR_VAL
DROP CONSTRAINT `fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id`
How I can fix this Query? I use MySQL version 5.7

Drop MySQL foreign key constraints
To drop a foreign key constraint, you use the ALTER TABLE statement:
ALTER TABLE table_name
DROP FOREIGN KEY constraint_name;

Judging by the name of the constraint, I believe it's a foreign key and MySQL has a different approach to drop foreign keys:
ALTER TABLE AO_8542F1_IFJ_OBJ_ATTR_VAL
DROP FOREIGN KEY `fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id`

Related

How to fix error 1064 while trying to add foreign key

I am trying to set up a DataBase for a library and after I created the table and trying to create foreign keys I get this error (1064) and I relly dont know what to do. I am using MySQL 8
I searched and I couldnt find anything similar. All I found was problem with reserved words , something I dont think is the problem in this case.
ALTER TABLE `project`.`book`
ADD INDEX `pubName_fk_idx` (`pubName` ASC) VISIBLE;
;
ALTER TABLE `project`.`book`
ADD CONSTRAINT `pubName_fk`
FOREIGN KEY (`pubName`)
REFERENCES `project`.`publisher` (`pubName`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Error message:
Operation failed: There was an error while applying the SQL script to
the database. Executing:
ALTER TABLE `project`.`book`
ADD INDEX `pubName_fk_idx` (`pubName` ASC) VISIBLE;
;
ALTER TABLE `project`.`book`
ADD CONSTRAINT `pubName_fk`
FOREIGN KEY (`pubName`)
REFERENCES `project`.`publisher` (`pubName`)
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 '' at line 2
SQL Statement:
ALTER TABLE `project`.`book`
ADD INDEX `pubName_fk_idx` (`pubName` ASC) VISIBLE
If you are using MariaDB or MySQL under 8.0, they have not inplemented the VISIBLE or INVISIBLE index, so you need to change your query:
ALTER TABLE `project`.`book`
ADD INDEX `pubName_fk_idx` (`pubName` ASC)

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.

How to delete foreign key in mysql?

I have tried both the syntaxes:
Alter Table bc DROP FOREIGN KEY STUD_ID;
It is giving the error: Can't DROP 'STUD_ID'; check that column/key exists
Alter Table bc DROP CONSTRAINT STUD_ID;
It is giving the 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 'CONSTRAINT STUD_ID' at line 1
Suggest me the possible ways.
ALTER TABLE TableName DROP FOREIGN KEY ForeignKeyConstraintName;
hope this helps :)
Your first query works. It is telling you that there is no such key to drop. This means your key has another name. It has not the same name as the column it indexes. Run
show index from bc
to show all key names and then run your query again with the correct name
Alter Table bc DROP FOREIGN KEY <STUD_ID_index_name>
alter table bc drop foreign key forconstraintname

#1064 - You have an error in your SQL syntax; while adding database

Error
SQL query:
ADD CONSTRAINT `ospos_sales_ibfk_3`
FOREIGN KEY (`vehicle_id`) REFERENCES `ospos_vehicles` (`person_id`);
MySQL said: Documentation
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 'ADD CONSTRAINT `ospos_sales_ibfk_3` FOREIGN KEY (`vehicle_id`) REFERENCES `ospos' at line 1
add ALTER TABLE in your mysql query and also remove quotes. try this
ALTER TABLE table_name ADD CONSTRAINT ospos_sales_ibfk_3
FOREIGN KEY (vehicle_id) REFERENCES ospos_vehicles(person_id);
i think it should work without errors, you can check here http://www.w3schools.com/sql/sql_foreignkey.asp it is correct way.

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)