Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Approach for Storing One-Many Relationship - Practical Example/Dilemma

Tags:

sql

I need to model an idea which can be broken down and thought of as follows:

  1. BookDetails
  2. BookPrices

The problem here is that you can have many prices for books and these prices are likely to change. Here's an example

BookDetails:
-----------------
ID  Name
1   Harry Potter…

This is easy enough.

Where it is more interesting is that for this one book I might have ten different prices on that day, e.g.:

BookPrices:
------------------------------------
Book_Details_Id  Kind          Price
1                SpecialOffer     10
1                BulkPurchase     20
1                Normal           30

I need to provide a list of books and all their prices in columns - something like:

BookName         SpecialOffer   BulkPurchase      Normal    
Harry Potter…              10             20          30

My question is: Should the book price table actually have all the different price types as columns? To me this is ugly and a better idea is to have each price as a row

If I use that approach I cannot think of a SQL query to generate me the result set. I have been thinking about this all morning.

EDIT: I have no leeway on calculating prices - they have to be stored down.

EDIT: This is basically the 1-n appraoch I can think of (Thanks to comment below) - Its what I actually had in mind

SELECT book.bookid, bp1.price, bp2.price FROM book JOIN bookprice bp1 JOIN bookprice bp2 ON bp1.bookid = book.bookid AND bp1.pricetype=1 AND bp2.bookid = book.bookid AND bp2.pricetype=2...

The problem is for ten prices you will be joining ten times which stinks!

like image 752
Jack Kada Avatar asked Feb 08 '10 14:02

Jack Kada


People also ask

What are some examples of a many-to-many relationship?

A many-to-many relationship exists when one or more items in one table can have a relationship to one or more items in another table. For example: Your Order table contains orders placed by multiple customers (who are listed in the Customers table), and a customer may place more than one order.

How do you store a one-to-many relationship in a database?

To implement a one-to-many relationship in the Teachers and Courses table, break the tables into two and link them using a foreign key. We have developed a relationship between the Teachers and the Courses table using a foreign key.

How will you express many-to-many relationship in database give any practical use case for Manytomany relationship?

A many-to-many relationship between tables is accommodated in databases by means of junction tables. A junction table contains the primary key columns of the two tables you want to relate.

What is a one-to-many relationship explain using an appropriate example?

In a one-to-many relationship, one record in a table can be associated with one or more records in another table. For example, each customer can have many sales orders.


2 Answers

how about this

BookDetails
BookID BookName

BookPrice
BookID PriceID PriceTypeID

BookPriceType
PriceTypeID DEscription
like image 82
IordanTanev Avatar answered Oct 05 '22 22:10

IordanTanev


This answer is t-SQL specific, and could use a little refinement, but it works on SQL 2005.

Uncomment the commented lines and it'll update like MAGIC! (okay, not magic, but niftily hacky)

DROP TABLE Books
DROP TABLE Prices
DROP TABLE bookpricing

CREATE TABLE Books ( id INT, title VARCHAR(20) )
CREATE TABLE Prices ( id INT, [desc] VARCHAR(20), pricingchange VARCHAR(20))
CREATE TABLE bookpricing ( id INT, bookid INT, priceid INT, bookprice MONEY )

INSERT INTO Books VALUES (1, 'Hi Mom')
--INSERT INTO Books Values (2, 'This is another book') 
INSERT INTO Prices VALUES (1, 'Standard', '1')
INSERT INTO Prices  VALUES (2, 'Discount', '.5')
INSERT INTO Prices VALUES(3, 'HyperMarkup', '1.5')
INSERT INTO prices VALUES(4, 'Remaindered', '.1')

INSERT INTO BookPricing VALUES (1,1,1,20.00)
INSERT INTO BookPricing VALUES (2,1,2,10.00)
INSERT INTO BookPricing VALUES (3,1,3,30.00)
--INSERT INTO BookPricing VALUES (4,2,1,30.00)
--INSERT INTO BookPricing VALUES (5,2,2,15.00)
--INSERT INTO BookPricing VALUES (6,2,4,3.00)

SELECT * FROM bookpricing 

/** this bit stolen from http://www.tsqltutorials.com/pivot.php **/

DECLARE @columns VARCHAR(max)

SELECT @columns = COALESCE(@columns + ',[' + cast(id as varchar) + ']',
'[' + cast(id as varchar)+ ']')
FROM prices


DECLARE @query VARCHAR(max)

SET @query = '
SELECT * FROM (SELECT BookID, PriceID, BookPrice FROM BookPricing) AS BookTable PIVOT (SUM(bookprice) FOR priceid 
IN (' + @columns + ')
)
AS p'

EXECUTE(@query)
like image 29
DigDoug Avatar answered Oct 06 '22 00:10

DigDoug