Can a table have two primary keys? – SQLServerCentral Forums (2024)

Post reply

  • seshu67

    Old Hand

    Points: 325

    More actions

    August 7, 2012 at 2:47 am

    #279921

    Can a table have two primary keys

  • Andy Hyslop

    SSCrazy Eights

    Points: 9198

    More actions

    August 7, 2012 at 2:51 am

    #1522444

    No

    You can have a composite PK that consists of two (or more) keys but not two PK's

    ==========================================================================================================================
    A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe

  • Ant-Green

    SSC Guru

    Points: 113485

    More actions

    August 7, 2012 at 2:52 am

    #1522445

    Two individual primary keys - No

    A primary key which spans multiple columns - Yes

    What is it your trying to acheive?

    Could get cleaver and put in a UNIQUE NONCLUSTERED index on the second column, just ensure that the column cannot accept nulls, and you get more or less the same result.

  • August 7, 2012 at 6:41 am

    #1522537

    No.You can't add 2 primary key in a table.Instead u can add Unique key which accept null value in it.

  • Ant-Green

    SSC Guru

    Points: 113485

    More actions

    August 7, 2012 at 7:28 am

    #1522579

    rajprabuit (8/7/2012)

    No.You can't add 2 primary key in a table.Instead u can add Unique key which accept null value in it.

    But if the OP wants it to act like a primary key, you would want to ensure that the column is not nullable as a PK won't accept a NULL value.

  • Sean Lange

    SSC Guru

    Points: 286590

    More actions

    August 7, 2012 at 9:15 am

    #1522668

    Sounds like an interview or a homework question to me. 😀

    _______________________________________________________________

    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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • mtassin

    SSC-Insane

    Points: 23099

    More actions

    August 7, 2012 at 9:44 am

    #1522685

    Could be a test question.

    A decade ago when I taught Database Theory, my tests were take home week long projects.

    25 T/F

    25 Multiple Choice

    25 Fill in the blank

    25 point Essay question.

    During each week long test, it was open book, open notes, open internet. If they had come here for help, I was fine with it. Real world, we get to use google. I didn't see the harm in it. Still don't. I think the most annoying thing about MCITP exams is the need to memorize a bunch of stuff I could look up in little to no time at all.

    I seriously think the number of questions should be increased dramatically, but the internet should be availalbe. If you can get enough correct in 2 hours or whatever, then you get the cert. 🙂

    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Jeff Moden

    SSC Guru

    Points: 1004175

    More actions

    August 7, 2012 at 7:25 pm

    #1522929

    seshu67 (8/7/2012)

    Can a table have two primary keys

    By absolute definition, no. That's why it's called a "Primary" key and there can only be one primary.

    In all practicality, yes. You can create a UNIQUE constraint on a NOT NULL column and for all practical intent and purposes it works the same way as a Primary Key. It's what some folks call an "AK" or "Alternate Key".

    If this is for an interview, be sure to mention that the PK does NOT have to be the clustered index. You can only have one clustered index.

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

    Change is inevitable... Change for the better is not.

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • sqlvogel

    SSCrazy Eights

    Points: 9526

    More actions

    August 12, 2013 at 6:39 am

    #1640546

    seshu67 (8/7/2012)

    Can a table have two primary keys

    In principle, yes. In practice it depends on what special functions or properties you want to assign to a primary key.

    In the relational model a key is a minimal superkey - irreducibly unique and not permitting nulls. You can have more than one of those per relation. By convention when there is more than one such key then one of the keys is designated a "primary" one. But that primary designation doesn't make it "special" in any prescribed or fundamental respect. The choice and the difference (if any) between a primary key and a non-primary key is up to you.

    Perhaps what you meant to ask is whether SQL's PRIMARY KEY constraint can be used more than once per table. The answer is no. It is an in-built limitation of SQL that PRIMARY KEY can only be used once per table. As Jeff says, you can use a UNIQUE constraint instead and that usually achieves the same things so the restriction on only having one PRIMARY KEY constraint per table isn't necessarily a serious limitation.

  • ScottPletcher

    SSC Guru

    Points: 101042

    More actions

    August 12, 2013 at 12:31 pm

    #1640669

    seshu67 (8/7/2012)

    Can a table have two primary keys

    Absolutely no, period. The fact that separate unique constraints can be defined does not make them primary keys as well.

    Similarly, a table could have 999 unique constraints that didn't allow nulls but not have a primary key.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Jeff Moden

    SSC Guru

    Points: 1004175

    More actions

    August 12, 2013 at 11:08 pm

    #1640785

    ScottPletcher (8/12/2013)

    seshu67 (8/7/2012)

    Can a table have two primary keys

    Absolutely no, period. The fact that separate unique constraints can be defined does not make them primary keys as well.

    Similarly, a table could have 999 unique constraints that didn't allow nulls but not have a primary key.

    Technically speaking and by definition, I absolutely agree. I think the OP was really asking if it were possible to have more than one non-null unique index on a table. Of course, the answer there is "Yes" and it's a fairly common practice when it comes to things like ISO standard lookup tables.

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

    Change is inevitable... Change for the better is not.

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply

Can a table have two primary keys? – SQLServerCentral Forums (2024)
Top Articles
Google Workspace vs Microsoft 365: Why not both?
Avoid Paying 1099 Taxes As An Independent Contractor
Foxy Roxxie Coomer
Duralast Gold Cv Axle
Truist Bank Near Here
Is pickleball Betts' next conquest? 'That's my jam'
Chase Bank Operating Hours
Bucks County Job Requisitions
Los Angeles Craigs List
Gwdonate Org
Tracking Your Shipments with Maher Terminal
Shreveport Active 911
Kris Carolla Obituary
2016 Ford Fusion Belt Diagram
Gon Deer Forum
Bitlife Tyrone's
Overton Funeral Home Waterloo Iowa
Driving Directions To Bed Bath & Beyond
Clear Fork Progress Book
라이키 유출
Tygodnik Polityka - Polityka.pl
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Georgia Cash 3 Midday-Lottery Results & Winning Numbers
Cpt 90677 Reimbursem*nt 2023
Craigslist Ludington Michigan
Pixel Combat Unblocked
Pfcu Chestnut Street
Metro By T Mobile Sign In
Graphic Look Inside Jeffrey Dresser
Litter-Robot 3 Pinch Contact & DFI Kit
2016 Honda Accord Belt Diagram
Does Iherb Accept Ebt
Synchrony Manage Account
Myql Loan Login
Mcgiftcardmall.con
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
Paperless Employee/Kiewit Pay Statements
Anhedönia Last Name Origin
Amc.santa Anita
Strange World Showtimes Near Century Stadium 25 And Xd
Port Huron Newspaper
Tacos Diego Hugoton Ks
Phmc.myloancare.com
Dying Light Mother's Day Roof
Das schönste Comeback des Jahres: Warum die Vengaboys nie wieder gehen dürfen
Mlb Hitting Streak Record Holder Crossword Clue
Random Warzone 2 Loadout Generator
Quest Diagnostics Mt Morris Appointment
Julies Freebies Instant Win
Fallout 76 Fox Locations
Goosetown Communications Guilford Ct
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 6363

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.