Solved

Composite primary key

Posted on 2014-09-10
6
154 Views
Last Modified: 2014-09-10
I have the following table
EmployeeTable
EmpId int
CompanyId int
EmpType char(20)
Name varchar(50)
Address varchar(50)
Dob Date
JoinDate Date
.....,,,,
EmpId, companyid, EmpType are the composite primary keys of the table. Should composite primary keys always be at top of the column list and one next to one OR can it be in the any position ? What are the advantages of making composite primary key in top of the column list one next one?
0
Comment
Question by:Varshini S
6 Comments
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40314147
As much as I know the order is not important at all so I can't say there is any advantage or disadvantage in having columns ordered but for view purpose only.
0
 
LVL 33

Accepted Solution

by:
ste5an earned 300 total points
ID: 40314164
I'd prefer my key columns to be in top of the column layout. But this is not necessary at all. I makes reading easier, as you can see the relevant columns at a glance.

btw, I don't think that your composite key of EmpId, companyid, EmpType is "correct".
0
 

Author Comment

by:Varshini S
ID: 40314264
ste5an,
Why the composite  key is not correct ?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 46

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 100 total points
ID: 40314280
"correct" and not correct :)

Means that you aren't following the good practices for PK definition. You should avoid description fields. In this case EmpType.
Why you have a CompanyID and don't have a EmpTypeID?
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 40314290
the shorter your primary key is, the best it is. You normally select just what it takes to make it unique
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40314346
The table is named Employee. Thus I would expect that each row descibes exactly one employee. But your composite key allows this:

CREATE TABLE #Employees
    (
      EmpId INT ,
      CompanyId INT ,
      EmpType CHAR(20) ,
      Name VARCHAR(50) ,
      Dob DATE ,
      JoinDate DATE ,
      CONSTRAINT PK_Employees PRIMARY KEY ( EmpId, CompanyId, EmpType )
    );

INSERT  INTO #Employees
VALUES  ( 1, 1, 'Manager', 'I am Weasle', GETDATE(), GETDATE() ),
        ( 1, 2, 'CEO', 'I am Weasle', GETDATE(), GETDATE() ),
        ( 1, 3, 'Trainee', 'I are Baboon', GETDATE(), GETDATE() );

DROP TABLE #Employees;

Open in new window


So derived from the naming, I think it's a violation of at least 2NF.

As you're using SQL Server, you should take a look at the HumanResources schema of AdventureWorks.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now