4

i want to know how to drop a constraint only if it exists. is there any single line statement present in mysql server which will allow me to do this.

i have tried the following command but unable to get the desire output

alter table airlines 
drop foreign key if exits FK_airlines;

any help to this really help me to go forward in mysql

peterm
  • 91,357
  • 15
  • 148
  • 157

2 Answers2

1

I do not believe this is possible in a single line, unless you are willing to detect the error and move on (not a bad thing).

The INFORMATION_SCHEMA database contains the info you need to tell if the foreign key exists, so you could implement it in a 2 step process.

http://dev.mysql.com/doc/refman/5.1/en/table-constraints-table.html

gahooa
  • 131,293
  • 12
  • 98
  • 101
  • I'm willing to 'detect the error and move on' OR to use a '2 step process', but unfortunately the link you offer is not enough to get me there - could you elaborate please? – ilasno Nov 07 '13 at 00:46
0

yep not possible the if exist is available only for database table and view : http://dev.mysql.com/doc/refman/5.0/en/replication-features-drop-if-exists.html

yep 2 step process is a good way like gahooa said