I’ve been racking my brain trying to figure out how to accomplish a link through foreign keys to help make life easier. I have two tables set up and want to make a 3rd linking to them both adding a couple columns to the 3rd one. here’s the syntax I came up with…
SQL Fiddle (with sample data)
Shirts
CREATE TABLE shirts(
shirt_id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
shirt_name VARCHAR(100) NOT NULL,
shirt_type VARCHAR(10) NOT NULL,
shirt_size VARCHAR(20) NOT NULL,
qp_price_id VARCHAR(20) NOT NULL,
o_price_id VARCHAR(20) NOT NULL
)ENGINE=INNODB;
Tthere will be more with the V-Necks, Raglan Sleeve, Cap Sleeve etc. shirts….
Price List
CREATE TABLE price_list(
price_id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
price_cat VARCHAR(20) NOT NULL,
price NUMERIC(6,2) NOT NULL
)ENGINE=INNODB
what I would like to do is create a 3rd table that selects the shirt_name, shirt_type, and shirt_size columns from from the shirts table then create two columns, one being qp_price and the other o_price and make those foreign keys to the price list table to grab the price for that shirt… and I figured maybe using a WHERE clause somehow where the price that shows is controlled by
WHERE shirts.qp_price_id=price_list.price_cat AND shirts.o_price_id=price_list.price_cat
I want to do it this way so I can make price changes as easily as possible to multiple items. This chunk is just for each variety of crewneck shirts I need to represent… there’s more… all the crewneck shirts cost the same price from New Born- 1X adult, so I have those attributed to “crn-qp-a” and “crn-o-a” so if I can just change the price value of “crn-qp-a” ONE TIME and have it automatically update to 20 instances so I don’t have to do it 20 times…. does this make sense?…. please help… lol
You want to, first, remove price information from
shirtstable and keep itprice_list, and then create the third table, and in it you can can link shirts to prices byshirt_idandprice_idSo for example, if your
shirtstable has the following shirts:and your
price_listtable has the following prices:Then each shirt will link to 2 prices, one for the
qpprice, and the other for theoprice.To query the price of all men shirts, you do the following:
Useful Reading: