Solved

Tables Relations, best approach

Posted on 2013-01-11
7
418 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 69

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 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 92

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 300 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check ALL SP in database make sure there are no errors 17 43
SQL Query 2 31
AD and SQL Server 2016 2 28
SQL - Simple Pivot query 8 13
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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