我正在为大学的项目建立一个数据库。我收到错误:
CREATE DATABASE P4_Projekt
USE P4_Projekt
CREATE TABLE shop_table
(
Shop_ID INT (12) NOT NULL AUTO_INCREMENT,
Shop_name VARCHAR (30) NOT NULL,
City VARCHAR (30) NOT NULL,
Address VARCHAR (50) NOT NULL,
Zip_code INT (4) NOT NULL,
Phone_number CHAR (8) NOT NULL,
PRIMARY KEY (Shop_id)
)
CREATE TABLE Orders
(
Order_id INT (12) NOT NULL AUTO_INCREMENT,
Customer_id INT (12) NOT NULL,
Pickup_time DATETIME NOT NULL,
Products_id INT (12) NOT NULL,
CONSTRAINT Orders_Product_id_fk
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
Notes VARCHAR (250),
Price FLOAT (8) NOT NULL,
Pre_paid BOOLEAN NOT NULL,
Status_ TINYINT (2) NOT NULL,
PRIMARY KEY (Order_id)
)
CREATE TABLE User
(
User_id INT (12) NOT NULL AUTO_INCREMENT,
First_name VARCHAR (20) NOT NULL,
Last_name VARCHAR (30) NOT NULL,
Address VARCHAR (50) NOT NULL,
ZIP_code INT (4) NOT NULL,
Phone_number CHAR (8) NOT NULL,
Email VARCHAR (50),
PRIMARY KEY (User_id)
)
CREATE TABLE Products
(
Product_id INT (12) NOT NULL AUTO_INCREMENT,
Name_ VARCHAR (40) NOT NULL,
Price FLOAT (8) NOT NULL,
Description VARCHAR (250) NOT NULL,
Production_time TIME (6) NOT NULL,
IMG BLOB,
PRIMARY KEY (Product_id)
)
当我尝试创建订单表时,我得到错误“键列'product_id'在表中不存在。它是关于外键的吗?
答案 0 :(得分:1)
你在主键行之前错过了一个逗号,这样的改变可以很好地运作
CREATE TABLE Orders
(
Order_id INT (12) NOT NULL AUTO_INCREMENT,
Customer_id INT (12) NOT NULL,
Pickup_time DATETIME NOT NULL,
Products_id INT (12) NOT NULL,
CONSTRAINT Orders_Product_id_fk
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
Notes VARCHAR (250),
Price FLOAT (8) NOT NULL,
Pre_paid BOOLEAN NOT NULL,
Status_ TINYINT (2) NOT NULL,
PRIMARY KEY (Order_id)
)
答案 1 :(得分:1)
逗号错误,请使用此
CREATE TABLE Orders
(
Order_id INT (12) NOT NULL AUTO_INCREMENT,
Customer_id INT (12) NOT NULL,
Pickup_time DATETIME NOT NULL,
Products_id INT (12) NOT NULL,
CONSTRAINT Orders_Product_id_fk
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
Notes VARCHAR (250),
Price FLOAT (8) NOT NULL,
Pre_paid BOOLEAN NOT NULL,
Status_ TINYINT (2) NOT NULL,
PRIMARY KEY (Order_id)
)
答案 2 :(得分:0)
在每个结账表后使用分号。
像
CREATE TABLE User(
User_id INT (12) NOT NULL AUTO_INCREMENT,
First_name VARCHAR (20) NOT NULL,
Last_name VARCHAR (30) NOT NULL,
Address VARCHAR (50) NOT NULL,
ZIP_code INT (4) NOT NULL,
Phone_number CHAR (8) NOT NULL,
Email VARCHAR (50),
PRIMARY KEY (User_id)
);