[英]MySql table with Foreign key
I cannot set the primary key for the invoice_2 table because it gives an error here is my code.我无法为 invoice_2 表设置主键,因为它给出了一个错误,这是我的代码。 invoice_1 is the other table that contains the foreign key of the invoice_2 table
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 )
Try below syntax:尝试以下语法:
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`)
);
Have a look at the mysql syntax for creating tables and setting PK看看用于创建表和设置PK的mysql语法
CREATE TABLE table_name ( column1 column_definition, column2 column_definition, ... CONSTRAINT [constraint_name] PRIMARY KEY [ USING BTREE | HASH ] (column1, column2, ... column_n));
TRY: adding a pk constraint 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) );
尝试:添加 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) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.