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...
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 shirts
table and keep it price_list
, and then create the third table, and in it you can can link shirts to prices by shirt_id
and price_id
create table shirt_price (
shirt_id int UNSIGNED not null,
qp_price_id int UNSIGNED not null,
o_price_id int UNSIGNED not null,
primary key (shirt_id, price_id),
CONSTRAINT shirt_price_ibfk_1 FOREIGN KEY (shirt_id) REFERENCES shirts (shirt_id),
CONSTRAINT shirt_price_ibfk_2 FOREIGN KEY (qp_price_id) REFERENCES price_list (price_id),
CONSTRAINT shirt_price_ibfk_3 FOREIGN KEY (o_price_id) REFERENCES price_list (price_id)
)ENGINE=INNODB;
So for example, if your shirts
table has the following shirts:
SHIRT_ID SHIRT_NAME SHIRT_TYPE SHIRT_SIZE
1 Crewneck Tee Men S
2 Crewneck Tee Men M
3 Crewneck Tee Men L
4 Crewneck Tee Men 1X
5 Crewneck Tee Men 2X
and your price_list
table has the following prices:
PRICE_ID PRICE_CAT PRICE
1 crn_qp_a 27.2
2 crn_o_a 25.31
3 crn_qp_b 28.1
Then each shirt will link to 2 prices, one for the qp
price, and the other for the o
price.
SHIRT_ID QP_PRICE_ID O_PRICE_ID
1 1 2
2 1 2
3 1 2
4 3 2
5 1 2
To query the price of all men shirts, you do the following:
select s.shirt_name, s.shirt_type, s.shirt_size, p_qp.price, p_o.price
from shirts s,
price_list p_qp,
price_list p_o,
shirt_price rel
where s.shirt_type = 'Men'
and rel.shirt_id = s.shirt_id
and rel.qp_price_id = p_qp.price_id
and rel.o_price_id = p_o.price_id;
Useful Reading: