Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 969 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to truncate a foreign key constrained table?

#11
Just use CASCADE

TRUNCATE "products" RESTART IDENTITY CASCADE;

But be ready for cascade deletes )
Reply

#12
While this question was asked I didn't know about it, but now if you use phpMyAdmin you can simply open the database and select the table(s) you want to truncate.

- At the bottom there is a drop down with many options. Open it and select `Empty` option under the heading `Delete data or table`.
- It takes you to the next page automatically where there is an option in checkbox called `Enable foreign key checks`. Just unselect it and press the `Yes` button and the selected table(s) will be truncated.

Maybe it internally runs the query suggested in [user447951's answer](

[To see links please register here]

), but it is very convenient to use from phpMyAdmin interface.
Reply

#13
***Tested on MYSQL Database***

**Solution 1:**

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table1;

**Solution 2:**

DELETE FROM table1;
ALTER TABLE table1 AUTO_INCREMENT = 1;
TRUNCATE table1;

This works for me. I hope, this will help you also. Thanks for asking this question.
Reply

#14
Another workaround is delete all rows in the table then reset auto-increment columns:

delete from table_name where 1

then Run:

ALTER TABLE table_name AUTO_INCREMENT = 1


Reply

#15
How to truncate a foreign key constrained table?
This illustration will demonstrate how to solve mysql error when truncating a table with foreign key constraint.
If you are using PHPMYADMIN, it is very easy to truncate a table with foreign key constraint.

1. Login to PHPMYADMIN and click the table you want to truncate.
2. Then go to SQL tab Place your code to truncate the table in the SQL
Editor example truncate table students; Replace students with the
name of the table.
3. At the bottom of the editor untick the "Enable foreign key checks" checkbox as shown below:

[![enter image description here][1]][1]

It will work like magic.


[1]:
Reply

#16
if you are using laravel migrations, you can do this using facades helpers

prefer to use Eloquent objects, answer the "Eloquent" way
~~~
Schema::disableForeignKeyConstraints();
Teacher::truncate();
Schema::enableForeignKeyConstraints();
~~~

In Laravel 7 and 8, for compatibility across 4 databases (MySql, Postgres, SQLite and SqlServer) and no Eloquent, you can use:

~~~
Schema::disableForeignKeyConstraints();
DB::table('teachers')->truncate();
Schema::enableForeignKeyConstraints();
~~~
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through