MySQL的Create tabel语法错误

mrphzbgm  于 2023-04-04  发布在  Mysql
关注(0)|答案(1)|浏览(184)

我试着在我的SQL数据库中创建这样的:

CREATE TABLE OrderID (
  order_id INT AUTO_INCREMENT PRIMARY KEY,
  customer_name VARCHAR(255) NOT NULL,
  book_title VARCHAR(255) NOT NULL,
  delivery_method VARCHAR(255) NOT NULL,
  password_confirmation VARCHAR(255) NOT NULL,
  FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
  FOREIGN KEY (book_title) REFERENCES book(book_title),
  FOREIGN KEY (password_confirmation) REFERENCES customer(customer_password)
);

但它总是显示错误:

CREATE TABLE OrderID (
  order_id INT AUTO_INCREMENT PRIMARY KEY,
  customer_name VARCHAR(255) NOT NULL,
  book_title VARCHAR(255) NOT NULL,
  delivery_method VARCHAR(255) NOT NULL,
  password_confirmation VARCHAR(255) NOT NULL,
  FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
  FOREIGN KEY (book_title) REFERENCES book(book_title),
  FOREIGN KEY (password_confirmation) REFERENCES customer(customer_password)
) ENGINE=InnoDB;

MySQL说:文档

1005 -无法创建表book_store. orderid(错误号:150“外键约束格式不正确”)(详细信息...)`

请告诉我正确的语法!

nr7wwzry

nr7wwzry1#

确保在customerbook上创建了indexes/keys,以便它们(列)可以作为新表的外键
对于您的情况,请执行以下操作(如果尚未执行):

CREATE INDEX customer_password ON customer (customer_password); 
CREATE INDEX customer_name ON customer (customer_name);

CREATE INDEX book_title ON book (book_title)

完成上述操作后,再次尝试创建OrderID表。

相关问题