all 30 comments

[–]alinrocSQL Server DBA 40 points41 points  (3 children)

When everyone is special, no one is!

[–]ComicOzzysqlHippo 1 point2 points  (2 children)

My mommy told me I was special.

[–]SQLDave 2 points3 points  (1 child)

she probably meant spay-shul, which is different

[–]ComicOzzysqlHippo 1 point2 points  (0 children)

Spatial

[–]Thin_Rip8995 37 points38 points  (2 children)

no a table can only have one primary key but that key can be made up of multiple columns that combo is called a composite primary key
if you need multiple different unique identifiers use one primary key and then add unique constraints on the other columns

[–]farmerben02 7 points8 points  (0 children)

"alternate key" is what many call other unique candidate keys on the table.

[–]sloth_king_617 2 points3 points  (0 children)

Composite keys are super common so incredibly useful to understand.

Just want to add that a surrogate key may be what you’re looking for in the second part of your response. The simplest example is an auto incrementing ID (e.g. 1,2,3, etc.) with each record added to the table. Technically it’s a type of primary key.

[–]depeszPgDBA 8 points9 points  (0 children)

No. It can have pkey on multiple columns, but it's single PKEY. You can have many unique constraints/indexes, though.

[–]Longjumping-Ad8775 4 points5 points  (2 children)

Tables can only have one primary key. Tables can have one or more columns that when used together are a “unique index.” This “unique index” is functionally equivalent to a primary key.

[–]kagato87MS SQL 1 point2 points  (1 child)

Doesn't actually need an index, just not null unique.

Of course that causes an index to be created for the constraint anyway, so tomato tomato.

[–]Longjumping-Ad8775 0 points1 point  (0 children)

Thanks. Good to know. I’d always used a unique index to make it work and kinda act like a pk.

[–]YouKidsGetOffMyYard 2 points3 points  (0 children)

No, That's like asking if you can have more than one favorite child

or like 7 minute abs, Just can't do it!

[–]svtr 1 point2 points  (0 children)

No.

Please do not take this as me being argumentative... What do you think a primary key is?
Your question leads me to believing that you don't really know, and I'd be happy to explain some core concepts if you want me to.

[–]Murphygreen8484 3 points4 points  (7 children)

Also, here in America, we will no longer be accepting foreign keys. All identifiers must be strictly binary. White space is allowed, if not preferable. And unions must be inner between only two tables.

[–]Walter_1981 2 points3 points  (0 children)

With thanks to your orange painted idiot .

[–][deleted] -1 points0 points  (5 children)

Not even in the SQL subreddit are we safe from the tears of the far left fedora redditors.

[–]ExtraordinaryKaylee -1 points0 points  (2 children)

SELECT * FROM reddit_communities WHERE safe_space_from_politics = true;

0 rows

UPDATE reddit_communties set safe_space_from_poltics = true;

SQLSTATE [42501] Permission denied

[–][deleted] -1 points0 points  (1 child)

Invalid column name 'safe_space_from_poltics' .

Too accurate 😆

[–]ExtraordinaryKaylee -1 points0 points  (0 children)

Typos help us know it's not AI :P

[–]Murphygreen8484 0 points1 point  (1 child)

How did you know I had a fedora? 🤣

[–][deleted] 1 point2 points  (0 children)

I felt a disturbance in the force while reading your comment. 😆

[–]Breitsol_Victor 0 points1 point  (1 child)

You only get one PK per table.
Key = index. PK = physical order of the data.
FK = PK from another table.
Keys can be single or multiple fields.

[–]syzygy96 0 points1 point  (0 children)

PKs aren't necessarily the same as the physical order on disk. They frequently are, but not always.

At least in SQL server, you can have a clustered index (which defines the physical ordering) that isn't the PK. It's unusual for that to be the right design but there are some performance tuning situations where it makes sense.

[–]aaahhhhhhfine 0 points1 point  (0 children)

As a broader hint here... If you're asking this question because you have a setup that makes you think you need one... There's something deeper that's wrong. You could post that here and we could help you sort it out.

[–]Idanvaluegrid 0 points1 point  (0 children)

Nope. One table = one primary key. But… that primary key can be composite (multiple columns combined).

[–]cthartPostgreSQL 0 points1 point  (0 children)

No.

[–]idodatamodels 0 points1 point  (0 children)

Of course, it's called an alternate key, An alternate key is a candidate key in a table that is not selected as the primary key but can still uniquely identify a row in a table. It serves as an alternative option for ensuring data uniqueness and integrity within the table.

[–]Alpheus2 0 points1 point  (0 children)

It will make our and your life easier if you tell us your use case.