MySQL 错误 1064 创建 table

MySQL error 1064 creating table

尝试使用两个外键创建 table 并不断收到此错误:

#1064 - 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 'idbed MEDIUMINT UNSIGNED NOT NULL,
idnumber MEDIUMINT UNSIGNED NOT NULL,' at line 2

table是:

CREATE TABLE care(
idbed MEDIUMINT UNSIGNED NOT NULL,
idnumber MEDIUMINT UNSIGNED NOT NULL,
PRIMARY KEY(idbed, idnumber),
FOREIGN KEY(idbed) REFERENCES intensivecarebed(idbed)
FOREIGN KEY(idnumber) REFERENCES employee(idnumber));

另外两个 table 是:

CREATE TABLE Employee(
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
address VARCHAR(60) NOT NULL,
salary FLOAT UNSIGNED NOT NULL,
specialization VARCHAR(50) NOT NULL<
clinic_name VARCHAR(50) NOT NULL,
PRIMARY KEY(idnumber)
);

CREATE TABLE intensivecarebed(
idbed MEDIUMINT UNSIGNED NOT NULL,
clinic_name VARCHAR(50) NOT NULL,
tax_reg_number TINYINT(10) UNSIGNED NOT NULL,
PRIMARY KEY(idbed)
);

任何人的见解?

您的代码中有几个错误:

  • 您在 Employee table 中错过了 idnumber
  • 你错过了外键之间的昏迷。
  • 你有 < 而不是 ,

您还必须在创建其他两个 table 之后创建 care table,因为您指的是这些 table。

试试这个:

CREATE TABLE Employee(
    idnumber MEDIUMINT UNSIGNED NOT NULL,
    first_name VARCHAR(30) NOT NULL,
    last_name VARCHAR(30) NOT NULL,
    address VARCHAR(60) NOT NULL,
    salary FLOAT UNSIGNED NOT NULL,
    specialization VARCHAR(50) NOT NULL,
    clinic_name VARCHAR(50) NOT NULL,
    PRIMARY KEY(idnumber)
);

CREATE TABLE intensivecarebed(
    idbed MEDIUMINT UNSIGNED NOT NULL,
    clinic_name VARCHAR(50) NOT NULL,
    tax_reg_number TINYINT(10) UNSIGNED NOT NULL,
    PRIMARY KEY(idbed)
);

CREATE TABLE care(
    idbed MEDIUMINT UNSIGNED NOT NULL,
    idnumber MEDIUMINT UNSIGNED NOT NULL,
    PRIMARY KEY(idbed, idnumber),
    FOREIGN KEY(idbed) REFERENCES intensivecarebed(idbed),
    FOREIGN KEY(idnumber) REFERENCES employee(idnumber)
);