How to delete foreign key in mysql? - 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

Related

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)

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.

drop primary key returned error 1064 in my case

my table
http://i.imgur.com/ifJik0T.jpg
my query
ALTER TABLE tasks DROP PRIMARY KEY task_name;
I got this 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 'task_name' at line 1'
Column name is not required, try this:
ALTER TABLE tasks DROP PRIMARY KEY
Reference: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
Looking at your picture, it looks like task_name is column that you set up to have a foreign key relationship. If that is the case, you can't drop a FK by deleting the column. You have to instead use the name of the FK relationship you gave when you created the FK.
If you want to get rid of the task_name column you have to use the DROP COLUMN syntax instead.
Remember, You should remove the autoincrement property before dropping the key. If you have any foreign key reference, first remove them and drop your primary key.
ALTER TABLE tasks DROP PRIMARY KEY;
You are trying to delete column task_name which is not primary key. it can be easily drop by this query
ALTER TABLE tasks DROP task_name

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)