php - PHP脚本中FOREIGN KEY SQL语法错误

标签 php mysql foreign-keys syntax-error

我正在使用php脚本在数据库中创建表。表1到表3完美地工作,但其余部分我得到此错误:

Error adding table 4: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'position(region), FOREIGN KEY (illness) REFERENCES illness(name_ill) )' at line 9 Error adding table 5: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'position(region), FOREIGN KEY (illness) REFERENCES illness(name_ill) )' at line 9 Error adding table 6: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'position(region), FOREIGN KEY (illness) REFERENCES illness(name_ill) )' at line 10 Error adding table 7: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'position(region), FOREIGN KEY (illness) REFERENCES illness(name_ill) )' at line 12



我附上了表的代码,因为找不到SQL错误。外键语法似乎是一个问题,但看起来很好。我尝试添加和删除括号,更改外键的顺序,并在语句后添加分号,但没有任何效果。
$table1 = "CREATE TABLE user (
    id INT NOT NULL AUTO_INCREMENT,
    email VARCHAR(255) NOT NULL UNIQUE,
    username VARCHAR(255) NOT NULL UNIQUE,
    passwd VARCHAR(255) NOT NULL,
    age DATE DEFAULT NULL,
    moderator BOOLEAN DEFAULT 0,
    PRIMARY KEY (id)
)";

$table2 = "CREATE TABLE position (
    region VARCHAR(255) NOT NULL UNIQUE,
    x_coordinate FLOAT(24) NOT NULL,
    y_coordinate FLOAT(24) NOT NULL,
    PRIMARY KEY (region)
)";

$table3 = "CREATE TABLE illness (
    name_ill VARCHAR(255) NOT NULL,
    PRIMARY KEY (name_ill)
)";

$table4 = "CREATE TABLE user_vaccine (
    id INT NOT NULL AUTO_INCREMENT,
    time_vacc DATE NOT NULL,
    illness VARCHAR(255) NOT NULL,
    region VARCHAR(255) NOT NULL,
    usr_id INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (usr_id) REFERENCES user(id),
    FOREIGN KEY (region) REFERENCES position(region),
    FOREIGN KEY (illness) REFERENCES illness(name_ill)
)";

$table5 = "CREATE TABLE user_illness (
    id INT NOT NULL AUTO_INCREMENT,
    time_ill DATE NOT NULL, 
    illness VARCHAR(255) NOT NULL, 
    region VARCHAR(255) NOT NULL, 
    usr_id INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (usr_id) REFERENCES user(id), 
    FOREIGN KEY (region) REFERENCES position(region),
    FOREIGN KEY (illness) REFERENCES illness(name_ill)
)";

$table6 = "CREATE TABLE official_illness (
    id INT NOT NULL AUTO_INCREMENT, 
    time_ill DATE NOT NULL, 
    illness VARCHAR(255) NOT NULL, 
    web_url VARCHAR(255) NOT NULL, 
    region VARCHAR(255) NOT NULL, 
    usr_id INT NOT NULL, 
    PRIMARY KEY (id),
    FOREIGN KEY (usr_id) REFERENCES user(id),
    FOREIGN KEY (region) REFERENCES position(region),
    FOREIGN KEY (illness) REFERENCES illness(name_ill)
)";

$table7 = "CREATE TABLE official_vaccine (
    id INT NOT NULL AUTO_INCREMENT, 
    year_vacc SMALLINT NOT NULL, 
    perc_vacc FLOAT(4,2) NOT NULL, 
    region VARCHAR(255) NOT NULL, 
    age_group INT, 
    report_organisation VARCHAR(255), 
    usr_id INT NOT NULL, 
    illness VARCHAR(255) NOT NULL, 
    PRIMARY KEY (id),
    FOREIGN KEY (usr_id) REFERENCES user(id),
    FOREIGN KEY (region) REFERENCES position(region),
    FOREIGN KEY (illness) REFERENCES illness(name_ill)
)";

最佳答案

在mysql中,有一个名为position()的字符串函数,因此由于position(region),该代码的(region)部分被解释为函数调用。这会导致语法错误。

解决方案是使用反引号指示位置和区域为表和字段名称:

`position`(`region`)

您必须在每个表中更改代码的这一部分。

关于php - PHP脚本中FOREIGN KEY SQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60097515/

相关文章:

python - SQLAlchemy:从表名获取模型。据我所知,这可能意味着将一些函数附加到元类构造函数中

postgresql - jsonb 和主/外键 : which performs better in PostgreSQL?

mysql - 如何将一条记录插入到以用户 ID 作为外键的表中

php - php长轮询中的页面重定向

javascript - 使用 jQuery 和 Ajax 发送输入数组

php - 查询树的项目数

mysql - SQL性能: Using OR is slower than IN when using order by

php - Laravel 5.2 中无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]

php - 制作需要数据库的常见 View 的最佳方式

MySQL:将多个结果显示为新行?