Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+10 −2
Q&A What are the pros and cons of a composite primary key versus a unique constraint?

Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique. One could either do a unique constraint or a comp...

2 answers  ·  posted 3y ago by Charlie Brumbaugh‭  ·  edited 3y ago by Alexei‭

#19: Post edited by user avatar Alexei‭ · 2020-12-20T19:45:48Z (over 3 years ago)
added relevant tags
#18: Post edited by user avatar pnuts‭ · 2020-12-20T19:44:42Z (over 3 years ago)
[software-practices] as a tag here is just useless noise. [Ref](https://software.codidact.com/questions/277891#comment-3960))
What are the pros and cons of a composite primary key versus a unique constraint?
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#17: Post edited by user avatar ArtOfCode‭ · 2020-09-02T21:16:32Z (over 3 years ago)
#16: Post edited by user avatar Moshi‭ · 2020-09-02T21:15:47Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#15: Post edited by user avatar Moshi‭ · 2020-09-02T21:15:12Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#14: Post edited by user avatar Moshi‭ · 2020-09-02T21:15:05Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#13: Post edited by user avatar Moshi‭ · 2020-09-02T21:13:02Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#12: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:57Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#11: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:57Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#10: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:56Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#9: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:56Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#8: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:56Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#7: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:56Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#6: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:56Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#5: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:55Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#4: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:55Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#3: Post edited by user avatar Moshi‭ · 2020-09-02T21:07:53Z (over 3 years ago)
Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.

One could either do a unique constraint or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
#2: Post edited by user avatar Charlie Brumbaugh‭ · 2020-08-24T02:33:31Z (over 3 years ago)
  • Lets say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys are unique.
  • One could either do a unique constraint on those foreign keys or a composite primary key on those columns.
  • What would the pros and cons of either solution to this be?
  • Let's say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys is unique.
  • One could either do a unique constraint or a composite primary key on those columns.
  • What would the pros and cons of either solution to this be?
#1: Initial revision by user avatar Charlie Brumbaugh‭ · 2020-08-24T00:18:39Z (over 3 years ago)
What are the pros and cons of a composite primary key versus a unique constraint?
Lets say we have two tables A and B and a join table C that has foreign keys to both A and B and the combination of those foreign keys are unique.

One could either do a unique constraint on those foreign keys or a composite primary key on those columns.

What would the pros and cons of either solution to this be?
sql