简体   繁体   English

用外键删除表

[英]deleting table with a foreign key

I am trying to delete a column with a foreign key in it. 我正在尝试删除带有外键的列。 I am sitting on this for a while know and it feels like I am overseeing something and the awnser must be obvious, but I just dont get it. 我坐在这上面已经知道了一段时间了,感觉就像我正在监督某些事情,并且遮篷必须很明显,但我只是不明白。 The error I get from the loggs is: 我从loggs中得到的错误是:

PHP Fatal error: Cannot declare class User, because the name is already in use user_id is a foreign key of the Identifier from the Class user. PHP致命错误:无法声明类User,因为名称已在使用中user_id是来自类用户的标识符的外键。

The database I use is InnoDB. 我使用的数据库是InnoDB。 I have tried this: 我已经试过了:

  $drop = 'ALTER TABLE `Group` DROP FOREIGN KEY `user_id`';
    $stmt = $conn->prepare($drop);
    $stmt->execute();

However this had no effect, no error in the loggs or anything. 但是,这没有效果,loggs中没有错误或其他任何错误。

I apologize if this is obvious, Im a student and this is my 3rd week. 我很抱歉,这很明显,我是学生,这是我第三周。 ¯_(ツ)_/¯ _(ツ)_ /¯

<?php
      require 'connect.php';
      require 'model.php';
      $identifier = null;

      if(!empty($_GET['identifier'])) {
        $identifier = $_REQUEST['identifier'];
      }


      if(!empty($_POST)) {
        require 'connect.php';
        require 'model.php';


        $identifier = $_REQUEST['identifier'];
        settype($identifier, 'integer');
        $group = Group::retrieve($conn, $_GET['identifier']);


        Group::delete($conn, $group);
        header('Location: view.php');
      } else {
        echo 'Its not working';
      }

    ?>

<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <link href='bootstrap/bootstrap/css/bootstrap.min.css' rel='stylesheet'>
  <script src='bootstrap/bootstrap/js/bootstrap.min.js'></script>
</head>

<body>
  <div class='container'>

    <div class='span10 offset1'>
      <div class='row'>
        <h3>Delete</h3>
      </div>

      <form class='form-horizontal' action='delete.php?identifier=<?php echo $identifier?>' method='post'>
        <input type='hidden' name='identifier' value='<?php echo $identifier;?>'/>
        <p class='alert alert-error'>DO YOU REALLY WANT TO Delete?</p>
        <div class='form-actions'>
          <button type='submit' class='btn btn-danger'>EXTERMINATE</button>
          <a class='btn' href='view.php'>No</a>
        </div>
      </form>
    </div>

  </div>
</body>
</html>

The relevant parts of the Class Group in my model looks like this: 我的模型中班级组的相关部分如下所示:

 public static function retrieve($conn, $identifier) {
    $query = 'SELECT * FROM `Group` WHERE `identifier` = :identifier';
    $stmt = $conn->prepare($query);
    $stmt->bindParam(':identifier', $identifier, PDO::PARAM_INT);
    $stmt->execute();

    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    return new Group((int)$result['identifier'], $result['admin'], 
   $result['superuser'], $result['testaccount'], (int)$result['user_id']);
   }

  public static function delete($conn, Group $group) {
    $query = 'DELETE FROM `Group` WHERE identifier = :identifier, admin = :admin,
    superuser = :superuser, testaccount = :testaccount, user_id = :user_id';

    $stmt = $conn->prepare($query);

    $stmt->bindValue(':identifier', $group->getIdentifier(), PDO::PARAM_INT);
    $stmt->bindValue(':admin', $group->getAdmin(), PDO::PARAM_STR);
    $stmt->bindValue(':superuser', $group->getSuperuser(), PDO::PARAM_STR);
    $stmt->bindValue(':testaccount', $group->getTestaccount(), PDO::PARAM_STR);
    $stmt->bindValue(':user_id', $group->getUser_id(), PDO::PARAM_INT);

    $stmt->execute();

    return TRUE;

  }

Check this if your query failed in the MySql console. 如果您的查询在MySql控制台中失败,请检查此选项。 if it failed then it means constraint name can be incorrect. 如果失败,则表示约束名称可能不正确。 If that is the case try the below code. 如果是这种情况,请尝试以下代码。 Foreign key has to be removed with it's constraint name not by column name. 必须使用约束名称而不是列名称来删除外键。

To get the constraint name you can use 要获取约束名称,您可以使用

SHOW CREATE TABLE Group

ALTER TABLE `Group` DROP FOREIGN KEY `your_constraint_name_here`

More info can be found at 可以在以下位置找到更多信息

MySQL Removing Some Foreign keys MySQL删除一些外键

Thank you for your help. 谢谢您的帮助。 I had an error in my code to drop the foreign key, it worked with this: 我在删除外键的代码中出现错误,它可以这样工作:

 $drop = 'ALTER TABLE `Group` DROP FOREIGN KEY `Group_ibfk_1`';
 $stmt = $conn->prepare($drop);
 $stmt->execute();

Also in the delete function it needed to be AND instead of a commata, like this: 同样在delete函数中,它必须是AND而不是逗号,如下所示:

$query = 'DELETE FROM `Group` WHERE identifier = :identifier AND admin = :admin AND
    superuser = :superuser AND testaccount = :testaccount AND user_id = :user_id';

Anyway is this considered good practice? 无论如何,这被认为是好的做法? Is there an other way to deal with deleting columns with foreign keys, other then dropping the key for awhile? 还有其他方法可以删除带有外键的列,然后暂时删除键吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM