MySQL Error 1064 when creating foreign key - mysql

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.

Related

MySQL There was an error while applying the SQL script to the database (foreign keys)

SQL Statement:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `ceramica`.`productos`
ADD INDEX `id_categoria_idx` (`id_categoria` ASC) VISIBLE;
;
ALTER TABLE `ceramica`.`productos`
ADD CONSTRAINT `id_categoria`
FOREIGN KEY (`id_categoria`)
REFERENCES `ceramica`.`categorias` (`id`)
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 `ceramica`.`productos`
ADD INDEX `id_categoria_idx` (`id_categoria` ASC) VISIBLE

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;

Syntax error in MySQL 5.7 for use CONSTRAINT

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`

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)

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