我看到了很多相同的问题,但无法解决我的问题。

如果我运行此代码:

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/config.php');

$servername = HOST;
$username = USERNAME;
$password = PASSWORD;
$dbname = DB;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS Articls (
            id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(254) COLLATE utf8_persian_ci NOT NULL
) DEFAULT COLLATE utf8_persian_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table Articls created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

/////////////////////////////////////////////////////////////////////////

$sql = "CREATE TABLE IF NOT EXISTS Tags (
            id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            id_articls INT(10) UNSIGNED NOT NULL,
            name VARCHAR(256) COLLATE utf8_persian_ci NOT NULL,
            FOREIGN KEY(Tags.id_articls) REFERENCES Articls(Articls.id)
) DEFAULT COLLATE utf8_persian_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table Tags created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>


我收到此错误消息:(如果删除FOREIGN KEY,则可以正常工作)


  成功创建了表Articls创建表时出错:无法创建
  表'admin_wepar.Tags'(错误号:150)


编辑

如果将into Articls.idTags.id_articls更改,则会出现此错误:


  成功创建了表关节创建表时出错:您有一个
  您的SQL语法错误;检查与您的手册相对应的手册
  MySQL服务器版本,可在'FOREIGN KEY附近使用正确的语法
  (Tags.id_articls)参考Articls(Articls.id))DEFAULT COLLA'at
  5号线

最佳答案

您需要声明Articls.idTags.id_articls签名或未签名

关于php - 无法在FOREIGN KEY上创建表(errno:150),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27679917/

10-12 18:15