Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL:Alle Fremdschlüssel programmgesteuert entfernen

Hier ist ein PHP-Skript, um die Tabelle information_schema.key_column_usage zu durchlaufen und jeden Fremdschlüssel zu löschen:

<?php
    $DBNAME = 'your db name';
    $USER = 'username';
    $PASSWORD = 'your password';
    $SERVER = 'localhost';  //Or Write your IP Address

    $conexion = new mysqli($SERVER,$USER,$PASSWORD,$DBNAME);
    $SQL = "SELECT DISTINCT table_name, constraint_name"
    ." FROM information_schema.key_column_usage"
    ." WHERE constraint_schema = '$DBNAME'"
    ." AND referenced_table_name IS NOT NULL";

    $result = mysqli_query($conexion, $SQL);

    while($row = mysqli_fetch_assoc($result)) {
        mysqli_query($conexion, "ALTER TABLE `$row[table_name]`"
        ."DROP FOREIGN KEY `$row[constraint_name]`")
        or die(mysqli_error());
    }
    mysqli_close($conexion);
?>