MySQL DROP all tables, ignoring foreign keys

前端 未结 24 2573
醉梦人生
醉梦人生 2020-12-02 03:44

Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?

相关标签:
24条回答
  • 2020-12-02 03:54

    Here is SurlyDre's stored procedure modified so that foreign keys are ignored:

    DROP PROCEDURE IF EXISTS `drop_all_tables`;
    
    DELIMITER $$
    CREATE PROCEDURE `drop_all_tables`()
    BEGIN
        DECLARE _done INT DEFAULT FALSE;
        DECLARE _tableName VARCHAR(255);
        DECLARE _cursor CURSOR FOR
            SELECT table_name 
            FROM information_schema.TABLES
            WHERE table_schema = SCHEMA();
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = TRUE;
    
        SET FOREIGN_KEY_CHECKS = 0;
    
        OPEN _cursor;
    
        REPEAT FETCH _cursor INTO _tableName;
    
        IF NOT _done THEN
            SET @stmt_sql = CONCAT('DROP TABLE ', _tableName);
            PREPARE stmt1 FROM @stmt_sql;
            EXECUTE stmt1;
            DEALLOCATE PREPARE stmt1;
        END IF;
    
        UNTIL _done END REPEAT;
    
        CLOSE _cursor;
        SET FOREIGN_KEY_CHECKS = 1;
    END$$
    
    DELIMITER ;
    
    call drop_all_tables(); 
    
    DROP PROCEDURE IF EXISTS `drop_all_tables`;
    
    0 讨论(0)
  • 2020-12-02 03:55

    Building on the answer by @Dion Truter and @Wade Williams, the following shell script will drop all tables, after first showing what it is about to run, and giving you a chance to abort using Ctrl-C.

    #!/bin/bash
    
    DB_HOST=xxx
    DB_USERNAME=xxx
    DB_PASSWORD=xxx
    DB_NAME=xxx
    
    CMD="mysql -sN -h ${DB_HOST} -u ${DB_USERNAME} -p${DB_PASSWORD} ${DB_NAME}"
    
    # Generate the drop statements
    TMPFILE=/tmp/drop-${RANDOM}.sql
    echo 'SET FOREIGN_KEY_CHECKS = 0;' > ${TMPFILE}
    ${CMD} $@ >> ${TMPFILE} << ENDD
        SELECT concat('DROP TABLE IF EXISTS \`', table_name, '\`;')
        FROM information_schema.tables
        WHERE table_schema = '${DB_NAME}';
    ENDD
    echo 'SET FOREIGN_KEY_CHECKS = 1;' >> ${TMPFILE}
    
    # Warn what we are about to do
    echo
    cat ${TMPFILE}
    echo
    echo "Press ENTER to proceed (or Ctrl-C to abort)."
    read
    
    # Run the SQL
    echo "Dropping tables..."
    ${CMD} $@ < ${TMPFILE}
    echo "Exit status is ${?}."
    rm ${TMPFILE}
    
    0 讨论(0)
  • 2020-12-02 03:57

    I use the following with a MSSQL server:

    if (DB_NAME() = 'YOUR_DATABASE') 
    begin
        while(exists(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='FOREIGN KEY'))
        begin
             declare @sql nvarchar(2000)
             SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']')
             FROM information_schema.table_constraints
             WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
             exec (@sql)
             PRINT @sql
        end
    
        while(exists(select 1 from INFORMATION_SCHEMA.TABLES))
        begin
             declare @sql2 nvarchar(2000)
             SELECT TOP 1 @sql2=('DROP TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']')
             FROM INFORMATION_SCHEMA.TABLES
            exec (@sql2)
            PRINT @sql2
        end
    end
    else
        print('Only run this script on the development server!!!!')
    

    Replace YOUR_DATABASE with the name of your database or remove the entire IF statement (I like the added safety).

    0 讨论(0)
  • 2020-12-02 04:02

    I came up with this modification on Dion Truter's answer to make it easier with many tables:

    SET GROUP_CONCAT_MAX_LEN = 10000000;
    SELECT CONCAT('SET FOREIGN_KEY_CHECKS=0;\n', 
                  GROUP_CONCAT(CONCAT('DROP TABLE IF EXISTS `', table_name, '`')
                               SEPARATOR ';\n'),
                  ';\nSET FOREIGN_KEY_CHECKS=1;')
    FROM information_schema.tables
    WHERE table_schema = 'SchemaName';
    

    This returns the entire thing in one field, so you can copy once and delete all the tables (use Copy Field Content (unquoted) in Workbench). If you have a LOT of tables, you may hit some limits on GROUP_CONCAT(). If so, increase the max len variable (and max_allowed_packet, if necessary).

    0 讨论(0)
  • 2020-12-02 04:02

    Just put here some useful comment made by Jonathan Watt to drop all tables

    MYSQL="mysql -h HOST -u USERNAME -pPASSWORD DB_NAME"
    $MYSQL -BNe "show tables" | awk '{print "set foreign_key_checks=0; drop table `" $1 "`;"}' | $MYSQL
    unset MYSQL
    

    It helps me and I hope it could be useful

    0 讨论(0)
  • 2020-12-02 04:03

    From http://www.devdaily.com/blog/post/mysql/drop-mysql-tables-in-any-order-foreign-keys:

    SET FOREIGN_KEY_CHECKS = 0;
    drop table if exists customers;
    drop table if exists orders;
    drop table if exists order_details;
    SET FOREIGN_KEY_CHECKS = 1;
    

    (Note that this answers how to disable foreign key checks in order to be able to drop the tables in arbitrary order. It does not answer how to automatically generate drop-table statements for all existing tables and execute them in a single script. Jean's answer does.)

    0 讨论(0)
提交回复
热议问题