Inserting data into related tables
Posted by:
Alin A
Date: September 06, 2022 11:02PM
Hello,
I am trying to understand how data has to be inserted into related tables.
Lets say I have a "User" table and a "Order" table with the following attributes:
CREATE TABLE User(
userID int PRIMARY KEY AUTO_INCREMENT,
userName varchar(70) UNIQUE NOT NULL,
passwd VARCHAR(70) NOT NULL,
INDEX(userID, userName))
CREATE TABLE Order(
orderID int PRIMARY KEY,
userReference, FOREIGN KEY(userReference) REFERENCES
User(userID),
userName1 VARCHAR(70) UNIQUE NOT NULL, FOREIGN KEY(userName1) REFERENCES
User(userName),
Product VARCHAR(70) NOT NULL,
INDEX(orderID, userName1))
How does a query, that wants to insert data into the "Order" table, has to look like?
To be more specific, how should the query look like to take over the data from the "User" table and relate to it?