Suppose I want to create a purchase form for a hospital database. How do I enter many product names under one bill number, which will be unique?
The underlying problem is that each bill can have zero or more detail lines. Each detail line has one product, so the decomposition into tables looks something like:
CREATE TABLE tBills ( billId INT NOT NULL CONSTRAINT XPKtBills PRIMARY KEY (billId) -- other bill attributes ) CREATE TABLE tProducts ( productId INT NOT NULL CONSTRAINT XPKtProducts PRIMARY KEY (productId) -- other product attributes ) CREATE TABLE tBillDetails ( billDetailId INT NOT NULL CONSTRAINT XPKtBillDetails PRIMARY KEY (billDetailsId) , billId INT NOT NULL CONSTRAINT XFK01tBillDetails FOREIGN KEY (billId) REFERENCES tBills (billId) , productId INT NOT NULL CONSTRAINT XFK02tBillDetails FOREIGN KEY (productId) REFERENCES tProduct (productId) -- other detail attributes )Although many people get confused on this issue, a bill can exist without detail lines. In fact when a bill is first created, it implicitly must exist without detail lines at least until the first detail line can be added. Think about the process when you write a bill manually if you doubt this!
For More Information
- Dozens more answers to tough database design questions from Pat Phelan
- The Best Database Design Web Links: tips, tutorials, scripts, and more
- Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your database design questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.