我无法为invoice_2表设置主键,因为它会出错 这是我的代码。 invoice_1是包含invoice_2表的外键的另一个表
CREATE TABLE invoice_2
(
itemID VARCHAR(20) PRIMARY KEY ,
invoiceNumber INT,FOREIGN KEY REFERENCES invoice_1.invoiceNumber,
quantity INT,
sellingPrice REAL,
lineTotal REAL
)
答案 0 :(得分:1)
请尝试以下语法:
CREATE TABLE invoice_2
(
itemID VARCHAR(20),
invoiceNumber INT,
quantity INT,
sellingPrice REAL,
lineTotal REAL,
PRIMARY KEY (itemID),
CONSTRAINT `FK_invoiceNumber` FOREIGN KEY (`invoiceNumber`) REFERENCES `invoice_1` (`invoiceNumber`)
);
答案 1 :(得分:0)
查看用于创建表和设置PK的mysql语法
CREATE TABLE table_name
(
column1 column_definition,
column2 column_definition,
...
CONSTRAINT [constraint_name]
PRIMARY KEY [ USING BTREE | HASH ] (column1, column2, ... column_n));
尝试:添加pk约束
CREATE TABLE invoice_2(
itemID VARCHAR(20),
invoiceNumber INT,FOREIGN KEY REFERENCES invoice_1.invoiceNumber,
quantity INT,
sellingPrice REAL,
lineTotal REAL
CONSTRAINT itemID PRIMARY KEY (itemID)
);