我尝试创建3个简单表,但第8行却出现错误,我不知道为什么。
CREATE TABLE Customers(
CustomerID INT AUTO_INCREMENT PRIMARY KEY,
CustomerName VARCHAR(200),
Email VARCHAR(200),
Age INT
)
CREATE TABLE Products(
ProductID INT AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(200),
Price INT
)
CREATE TABLE Orders(
OrderID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID),
ProductID INT,
FOREIGN KEY(ProductID) REFERENCES Products(ProductID),
Quantity INT,
OrderDate DATETIME
)
我得到了错误:
ERROR 1064 (42000): 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 'CREATE TABLE Products(
ProductID INT AUTO_INCREMENT PRIMARY KEY,
Title V' at line 8
有什么想法吗?这是一个简单的表创建。
答案 0 :(得分:0)
您的代码还可以。您必须在每个SQL语句的末尾添加分号(;)。
CREATE TABLE Customers(
CustomerID INT AUTO_INCREMENT PRIMARY KEY,
CustomerName VARCHAR(200),
Email VARCHAR(200),
Age INT
);
CREATE TABLE Products(
ProductID INT AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(200),
Price INT
);
CREATE TABLE Orders(
OrderID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID),
ProductID INT,
FOREIGN KEY(ProductID) REFERENCES Products(ProductID),
Quantity INT,
OrderDate DATETIME
);
在线演示:http://sqlfiddle.com/#!9/cbf746e/2/0
更新
正如Strawberry中提到的comment,请勿将“年龄”用作一列。请改用出生日期。使用出生日期,您可以计算年龄。
DateOfBirth DATE