gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:16:41 26 4
gpt4 key购买 nike

我正在使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com