Drop foreign key using subquery to get its name - mysql

I need to drop a foreign key, but I don't know its name.
I don't want to drop all indexes from table.
I'm trying to do this with following subquery
ALTER TABLE `onboarding_requests`
DROP FOREIGN KEY (SELECT `CONSTRAINT_NAME` FROM `INFORMATION_SCHEMA.KEY_COLUMN_USAGE`
WHERE `TABLE_NAME` = 'onboarding_requests' AND `COLUMN_NAME` = 'partner_responsible');
but it returns:
SQL 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 '(SELECT CONSTRAINT_NAME FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE `TABL' at line 2

You cannot use variable substitution in data definition statements. In other words, you must give the literal name of the foreign key in a DROP FOREIGN KEY statement.
How can you work around this inherent limitation of SQL? All SQL, by the way, not just MySQL, prohibits this. Write a small program in your favorite language (python? php>) to retrieve the names of the data entities from the information schema, then use string concatenation to create the data definition statements you need.

Related

Why Drop constraint if exists return an error MySQL?

Why when I try to run this query in MySQL, it gives me a syntax error.
I'd like to drop a foreign key only if exists
ALTER TABLE
`messaggi`
DROP CONSTRAINT IF EXISTS
`messaggi_ibfk_1`,
DROP CONSTRAINT IF EXISTS
`messaggi_ibfk_2`;
When in doubt, refer to the reference documentation on syntax:
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
| DROP {CHECK | CONSTRAINT} symbol
It only supports the symbol, i.e. the constraint name, directly after DROP CONSTRAINT. It does not support an optional IF EXISTS clause after DROP CONSTRAINT.
That's just how the SQL parser code for MySQL is implemented. It does not support any syntax you think of, it only supports syntax they implemented. Lots of software works this way. :-)
This enhancement to SQL syntax was requested in 2004, but declined: https://bugs.mysql.com/bug.php?id=5746

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

Table named 'Index'

I have a table named 'Index'. I realize that this is a keyword in MySQL, and was wondering how I can reference this table in queries?
My 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 ''Index' (ID) )' at line 9
Use backticks:
references `Index` (ID)
You actually already did this when you created the Index table (and you must have used backticks or else you could not have created the table):
CREATE TABLE `Index`
You should avoid naming databases, tables, and columns using MySQL keywords.
you can use backticks instead of quotes
CONSTRAINT grants_fk_index
FOREIGN KEY (Index_fk)
references `Index` (ID)

MySQL not dropping index for unique constraint

I created a table and assigned a UNIQUE CONSTRAINT to a specific column.
... column_name INT UNSIGNED NOT NULL UNQUE
Now I no longer want the column to have the unique constraint, and so I tried modifying that:
ALTER TABLE mytable DROP INDEX column_name
like I saw here.
The above query executes successfully the first time. But when I try inserting duplicate values in the column_name column, I still get the error
#1062 Duplicate entry '10' for key column_name_2
Which, I presume, means the Constraint still remains on the table. (Also funny how _2 gets appended to the column name). But if I repeat the above ALTER statement, I get
#1091 - Can't DROP 'column_name'; check that column/key exists
I also tried
ALTER TABLE mytable DROP INDEX UNIQUE
and it gives me the 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 'UNIQUE' at line 1
Don't know if I'm missing something, but how do I remove the UNIQUE constraint from this column?
The issue came up because I had earlier ran the following query, in trying to remove the UNIQUE constraint.:
ALTER TABLE mytable CHANGE column_name column_name INT UNSIGNED NOT NULL
This somehow created a new column called column_name_2 and assigned it the UNIQUE constraint. So when I ran SHOW CREATE TABLE like #Paolof76 suggested, I found among the columns:
UNIQUE KEY `column_name_2` (`column_name`)
which i presume got created due to the ALTER statement above.
So I ran
ALTER TABLE mytable DROP INDEX column_name_2
which eliminated the unique key and solved my problem.
Thanks for the heads-up #Paolof76!

add foreign key in mysql

I am upgrading my database from mssql to mysql, I am struct on creating foreign keys
In MSSQL I was using
alter table ac_master add constraint 'ac_master_table_conf' foreign key (ac_code) references table_conf (ac_code)
MySql is giving error on this
#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 ''ac_master_table_conf' foreign key (ac_code) references
table_conf(ac_code)' at line 1
Remove the quotes from around your constraint name e.g.
ALTER TABLE ac_master ADD CONSTRAINT ac_master_table_conf FOREIGN KEY (ac_code) REFERENCES table_conf (ac_code)
As far as I can tell, neither SQL Server nor MySQL use single quote syntax for identifiers. You probably want ac_master_table_conf rather than 'ac_master_table_conf'.
Other than that, the ALTER TABLE syntax is documented, you don't need to guess.
no quotes are involved in mysql if it is a name of a field or a table.
the only need of quotes is to specify a string as below
select * from abc where column = 'string';
so be careful next time.