ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

When deleting a table in a MySQL database, I get the following error message

MariaDB [thrkhztbpt]> drop table users;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
MariaDB [thrkhztbpt]>

To fix the error, run

SET FOREIGN_KEY_CHECKS=0;

Now the drop table SQL command will work. After you dropped the table, re-enable foreign key check with

Advertisement

SET FOREIGN_KEY_CHECKS=1;

Example

MariaDB [thrkhztbpt]> drop table users;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
MariaDB [thrkhztbpt]> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)

MariaDB [thrkhztbpt]> drop table users;
Query OK, 0 rows affected (0.00 sec)

MariaDB [thrkhztbpt]>
MariaDB [thrkhztbpt]> SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [thrkhztbpt]>

https://googleads.g.doubleclick.net/pagead/ads?gdpr=0&client=ca-pub-3527572573766151&output=html&h=280&slotname=1284802799&adk=2287826343&adf=3729577814&pi=t.ma~as.1284802799&w=1200&abgtt=6&fwrn=4&fwrnh=100&lmt=1727871535&rafmt=1&format=1200×280&url=https%3A%2F%2Fserverok.in%2Fmysql-cannot-delete-or-update-a-parent-row&fwr=0&fwrattr=true&rpe=1&resp_fmts=3&wgl=1&dt=1727871532783&bpp=7&bdt=228&idt=933&shv=r20240930&mjsv=m202409250101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D7f6b03d523dc17b0%3AT%3D1727700583%3ART%3D1727871419%3AS%3DALNI_MbtvSB1WDRlqL93od_mkPjFy69uGg&gpic=UID%3D00000f155d560fe8%3AT%3D1727700583%3ART%3D1727871419%3AS%3DALNI_MZxj17CTbsHt9d0HEYnptsr8Nyb-A&eo_id_str=ID%3D2784345bb28b9855%3AT%3D1727700583%3ART%3D1727871419%3AS%3DAA-AfjaHwphDwBqcViAWW3OFW6_H&prev_fmts=0x0&nras=1&correlator=1485753264831&frm=20&pv=1&u_tz=300&u_his=1&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&adx=75&ady=938&biw=1349&bih=643&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C31087434%2C42531705%2C95332923%2C95343328%2C95343823%2C31087564&oid=2&pvsid=4314378738186990&tmod=29019772&uas=0&nvt=1&ref=https%3A%2F%2Fserverok.in%2Fcategory%2Fmysql%2Fpage%2F2&fc=1920&brdim=-8%2C-8%2C-8%2C-8%2C1366%2C0%2C1382%2C744%2C1366%2C643&vis=1&rsz=%7C%7CeEbr%7C&abl=CS&pfx=0&fu=128&bc=31&bz=1.01&ifi=2&uci=a!2&btvi=1&fsb=1&dtd=2635

Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement