Hi guys, Im using MySQL 5.0.
Here is my two tables.
The purpose of this is to record 1 InvoiceID to refer to multipul transaction items. So heres the problem. What if 2 transactions occured, then the below code will not work.Code:CREATE TABLE IF NOT EXISTS Invoice ( InvoiceID MEDIUMINT NOT NULL AUTO_INCREMENT, InvoiceDate DATE NOT NULL, ClientID VARCHAR(7) NOT NULL, Total INTEGER(10,2) NOT NULL, AmountPaid INTEGER(10,2) NOT NULL, PRIMARY KEY (InvoiceID), FOREIGN KEY (ClientID) REFERENCES Clients(ClientID) ); CREATE TABLE IF NOT EXISTS Transaction ( InvoiceID MEDIUMINT NOT NULL, ProductID VARCHAR(7) NOT NULL, QTY INTEGER(2) NOT NULL, FOREIGN KEY (InvoiceID) REFERENCES Invoice(InvoiceID), FOREIGN KEY (ProductID) REFERENCES Products(ProductID) );
Is there a better way to construct this or any advise on how to go about this?Code:INSERT INTO Invoice (InvoiceDate, ClientID, Total, AmountPaid) VALUES (NOW(), 'OZT0001', 130.00, 50.00); INSERT INTO Transaction (InvoiceID, ClientID, ProductID, QTY, Total) VALUES (LAST_INSERT_ID(), 'RAM0001', 2);




Reply With Quote