?
Solved

Tables Relations, best approach

Posted on 2013-01-11
7
Medium Priority
?
425 Views
Last Modified: 2013-01-11
Hi, I have several doubts about this.

What I see before in relations is this:

Master -> student table
SchoolID
ClassID
StudentID
Name

Details -> movements
MovID
SchoolID
ClassID
StudentID
etc....

the other scenario is:


Master -> student table
ID Incremental
SchoolID
ClassID
StudentID
Name

Details -> movements
IDMaster ---the Master ID
etc. etc. ..

What is the best
0
Comment
Question by:Crashman
7 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38767936
It's not clear to me what you're trying to design, but neither of those is properly designed.

For example, Student Name and ClassID should never be in the same table.
0
 
LVL 8

Author Comment

by:Crashman
ID: 38767956
ok,

Lets say,

-- Table Bill
Company
Date
DocNo
Client


--Table Bill details
Company -- foreign
DocNo -- foreign
ProductID
Quantity
Amount

in this, you have Two columns from Master.

-- Table Bill
ID --Incremental
Company
Date
DocNo
Client

--Table Bill details
IDRefParent --ID incremental from Parent
ProductID
Quantity
Amount

In this other I have only one column for reference...

is more clearly now?
0
 
LVL 8

Author Comment

by:Crashman
ID: 38768050
the scenario is more complicated, lets say we have a multiple foreign key in one table, and this table can not contains duplicates in this combinations of foreign keys and this table has a child table with only one field in common, how could you create relations between this two tablesExample
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 800 total points
ID: 38768075
For all many-to-many relationships, you have to create an "intersection" table.

You can use the two natural keys (better) or a single "dummy" generated id key (often poor for performance).

You would not use the full company name, of course, but instead a company code: that can be a code you make up or a legitimate standard id, such as a DUNS number.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 38768109
Taking your last example, you would want something more like this:

tblCompanies
-----------------------------------------
CompanyID (PK)
CompanyName

tblBills
-----------------------------------------
BillID (PK)
CompanyID (FK)
BillDate

tblBill Details
-----------------------------------------
BillDetailID (PK)
BillID (FK)
Product
Quantity
Amount
0
 
LVL 13

Accepted Solution

by:
LIONKING earned 1200 total points
ID: 38768170
In order for you to create a one to many relation, your "one" table must be "uniquely identified" by your primary key.
The problem with the ID key is that it doesn't guarantee uniqueness of the other three fields, to do this, you would have to create a UNIQUE INDEX using these three columns.

If this uniqueness exists, you can perfectly create the one to many relation with those two tables. Because I assume your concern is that you will have the relation, but that doesn't mean that only unique values exist in table PARTIDAENC for a given key.
0
 
LVL 8

Author Comment

by:Crashman
ID: 38768421

tblCompanies
-----------------------------------------
CompanyID (PK)
CompanyName

tblBills
-----------------------------------------
BillID (PK) --this must be incremental, unless, tblBillDetails must have CompanyID ref.
                  --Multi companies
CompanyID (FK)
BillDate

tblBill Details
-----------------------------------------
BillDetailID (PK)
BillID (FK)
Product
Quantity
Amount
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question