SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create a table with two primary keys.


create a table with two primary keys.

Author
Message
Ryan George
Ryan George
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 59
As per the requirement, it is asked to create a table with two primary keys. How can we implement the same?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88975 Visits: 45284
A table cannot have two primary keys. Sure the requirement isn't for a single primary key consisting of two columns?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ryan George
Ryan George
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 59
it is really interesting to hear. In a table, two prmary keys.
but this is a question which i got for an interview

i had options too. Doze

as per the requirement, it is asked to create a table with two primary keys. how can we implement the same
(composite Primary key /
One primary key and one Unique key /
one primary key and unique key with NOT NULL constraint /
None of the above)

Thanks for the immediate response.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26444 Visits: 17557
Ryan George (1/30/2014)
it is really interesting to hear. In a table, two prmary keys.
but this is a question which i got for an interview

i had options too. Doze

as per the requirement, it is asked to create a table with two primary keys. how can we implement the same
(composite Primary key /
One primary key and one Unique key /
one primary key and unique key with NOT NULL constraint /
None of the above)

Thanks for the immediate response.


So what do you think? We are not here to provide answers for interview questions so you can get a job that you are not qualified for.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16842 Visits: 19122
I'd say it depends on what you need because a table can't have 2 primary keys by definition. It's not about SQL Server, it's about relational theory.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88975 Visits: 45284
Ryan George (1/30/2014)
as per the requirement, it is asked to create a table with two primary keys. how can we implement the same


At the risk of sounding like an echo...
A table cannot have two primary keys.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88094 Visits: 41128
Ryan George (1/30/2014)
it is really interesting to hear. In a table, two prmary keys.
but this is a question which i got for an interview

i had options too. Doze

as per the requirement, it is asked to create a table with two primary keys. how can we implement the same
(composite Primary key /
One primary key and one Unique key /
one primary key and unique key with NOT NULL constraint /
None of the above)

Thanks for the immediate response.


I feel your pain and I hate written questions like that and that's why I'd never be able to pass a test. I do, however, love such questions in an interview because they give me the chance to shine.

By definition and by design and as the others have stated, you cannot have two Primary Keys on a table. If you try to create a second real Primary Key on a table, it will give you an error (as the others have already stated).

That, not withstanding, I frequently do have the need to have a Primary Key and another key that could be used as a primary key on many of my tables and that answer is one of the 3 other answers on the list of possibilities you provided. So, now we have to guess... did the person that wrote the question understand that there truly can be one and only one primary key and he just wrote a question trying to trick you or is he someone that thinks that other kind of key is considered to be a Primary Key? Or is it a test of your interpretive capabilites to see if you understand that a lot of people don't know you can only have one real Primary Key on a table and what you would do about it if asked such a thing in real life?

If it's a written test that a human would review, I'd give the obvious answer that you can only have one and only one Primary Key for a table but I'd also write a small paragraph that you can also have a ALTERNATE KEY that could be used as a Primary Key if there wasn't one already and then I would identify which answer that one was.

If it's a test to be graded by machine, I'd go with the technically correct answer and argue it to the max if I got it wrong.

If it's a question during an interview, then it's "discussion time" where you'd explain all about PKs, AKs, and their relationship with FKs. That's what interviewers look for when they ask questions. Do you know the subject well enough to spontaneously add facts, uses, and methods or are you just "book learned".

Remember, requirements in real life are rarely technically correct and THAT might be what they were actually looking for. A lot of people don't understand that "Before you can think outside the box, you must first realize... YOU'RE IN A BOX!" :-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ryan George
Ryan George
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 59

So what do you think? We are not here to provide answers for interview questions so you can get a job that you are not qualified for.



Dear Mr. Lange,

if you dont know the answer, just shut up and search some other threads. dont poke your nose into this. I will ask questions which I ve doubt. Please dont reply/make any quotes if you have no clarity in the answer. And this is not the right way to handle this.

Be a Professional, Man..!!

Cheers.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88975 Visits: 45284
Ryan George (1/31/2014)

So what do you think? We are not here to provide answers for interview questions so you can get a job that you are not qualified for.



Dear Mr. Lange,

if you dont know the answer, just shut up and search some other threads. dont poke your nose into this. I will ask questions which I ve doubt. Please dont reply/make any quotes if you have no clarity in the answer. And this is not the right way to handle this.


Wow, just...

I can tell you Sean knows perfectly well the answer to the question. We however don't tend to like interview questions here, especially when they don't appear to be accompanied by any research (google, books online, etc). I've interviewed enough people who didn't have a clue and appeared to have just memorised answers.

Be a Professional, Man..!!


Good idea. Maybe take your own advice? Would you tell someone, someone that that you've never met and that you asked for free advice from, to their face to shut up because you didn't like their advice?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lempster
Lempster
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3235 Visits: 1657
GilaMonster (1/31/2014)
Ryan George (1/31/2014)

[quote]Be a Professional, Man..!!


Good idea. Maybe take your own advice? Would you tell someone that you've never met and that you asked for free advice from to their face to shut up because you didn't like their advice?

+1

@Ryan George: you are fairly new to the Forums and perhaps Sean could have been a little kinder, but there's no excuse for your response.

Some of the most knowledgeable and respected people in the SQL Server community (I'm not including myself in that category!) post on these forums and if you want their help in future, I suggest adjusting the tone of your posts.

Hopefully just a one-off eh?

Lempster
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search