Question

I cannot set the primary key for the invoice_2 table because it gives an error here is my code. invoice_1 is the other table that contains the foreign key of the invoice_2 table

CREATE TABLE invoice_2 ( itemID VARCHAR(20) PRIMARY KEY , invoiceNumber INT,FOREIGN KEY REFERENCES invoice_1.invoiceNumber, quantity INT, sellingPrice REAL, lineTotal REAL )

Was it helpful?

Solution

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`)
 );

OTHER TIPS

Have a look at the mysql syntax for creating tables and setting PK

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) );

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top