0

After googling a lot, I found 2 statements for Primary Key.

Some says, Primary key can be only one in a table.

Some says, Primary key can consists of more than one column.

And also says, it is the difference between Primary key and Unique key.

I read this post: difference between primary key and unique key

But, this post also confuses this difference.

My Question is:

When Primary key can consists of multiple columns, then how can it be only 
1 for a table and then  how this can differ from Unique Key except from   
null value difference?
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 3
    Unique and primary keys can both consist of multiple columns (but only from the same table). A table can have many unique keys, but only one of them can be considered primary. There is debate about whether ANY of them should really be considered primary. – Strawberry Nov 03 '18 at 09:12
  • Think of a key as a set of columns instead of an individual column. What "Primary key can be only one in a table" means is that there is only one such set of columns that is considered to be primary – Sahil Dhoked Nov 03 '18 at 10:09
  • @Strawberry one of the unique keys should be considered primary because it makes implementation for the database system developer easier. – Sahil Dhoked Nov 03 '18 at 10:10
  • There is no reason that "a PK can consist of multiple columns" interferes with "only 1 for a table". So why do you ask about it? – philipxy Nov 03 '18 at 10:46
  • Also: A `PRIMARY KEY` is a unique and an index. A `UNIQUE` key is an index. – Rick James Nov 03 '18 at 17:07
  • @philipxy - **In MySQL**, the following apply: A `PRIMARY KEY` includes a uniqueness constraint and an index. A `UNIQUE KEY` is both a uniqueness constraint and an index. Any index is a structure designed to speed up access to tables. Defining a `FOREIGN KEY` gives you an index unless a suitable one already exists. (Since it was tagged "mysql", I am assuming that the user is interested in MySQL, not standard SQL.) – Rick James Nov 03 '18 at 22:30
  • 1
    @Android -- If your question is about MySQL specifically, remove the "sql" tag. If your question is about standards, remove "mysql" tag. – Rick James Nov 03 '18 at 22:32
  • @RickJames You are talking about default indexes for certain constraints. One can drop & declare indexes independently of constraints & vice versa. The notions are distinct & should be presented separately. – philipxy Nov 04 '18 at 01:42

7 Answers7

2

A primary key is used by the optimiser to create a clustered index around that key. If that key happens to be a combination of columns (for instance in a table that handles a many to many relationship) then that is fine. A unique key is an attribute of something that is unique to that thing but not an attribute that you want to use as the primary key.

One example that i work with is cars. The vin is unique to the car. However because the vin and body number are used at different stages in the vehicles life. Whilst it is unique to the car, it isn't a good primary key candidate. So we have a unique identifier per car as a primary key.

Similarly we handle user group tables with primary keys over multiple columns. So the combination of the userid and the usergroupid is a primary key preventing the entry of people in to the same group more than once. The use of a primary key here rather than a unique key is to conform to the 3rd normal form mostly.

I hope that helps but if you would like further clarification please let me know.

1

You can have only one primary key in a table, but it can consist of multiple columns. This doesn't mean that each column is a primary key, but that the combination of all these columns' values are unique.

A unique constraint is similar to a primary in preventing duplicate values, but a unique constraint allows nulls (since they aren't values). A primary key does not. You could think of a primary key as a unique not null constraint.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • In this case [Primary key can consists of multiple columns], we call it a Composite key. Right? –  Nov 03 '18 at 09:22
  • @Android yup, that's correct - although a composite key just means it's made up of multiple columns. You could also have composite unique keys or composite foreign keys – Mureinik Nov 03 '18 at 09:31
1

In SQL: Any number of sets of one or more columns can be declared UNIQUE. One set of columns can be declared PK (PRIMARY KEY). PK means UNIQUE NOT NULL. A set of one column is "simple" & a set of more than one is "composite".

In standard SQL a UNIQUE column set has a distinct subrow value in every row--where NULL<>NULL. MySQL does that--but some DBMSs evaluate that "distinct" treating NULL equal to NULL. Read the manuals.

SQL DBMSs use UNIQUE & PK declarations for other things--typically for default INDEXes. INDEX is not standard SQL & is a different notion. Read the manuals.

PK & other terms have different definitions in the RM (relational model)--even for tables that have an obvious interpretation as relations because they have no duplicate rows & no NULLs. Read some published academic textbook(s).

There's no need for PK in SQL or the RM--it's just a tradition.

Re related terms in SQL & the RM.

philipxy
  • 14,867
  • 6
  • 39
  • 83
1

Primary keys can consist of any number of columns. They then have three properties:

  • The combination of values in the columns is unique.
  • The values in each column are never NULL.
  • There is only one primary key per table.

The first two conditions can be true on any number of columns or combinations of columns in a table. These are called candidate primary keys. You would implement them with unique and not null constraints.

The third condition is simply states that at most one of these candidates can be chosen as the primary key.

One way to think about this is that the primary key is special. For example, in MySQL, the data is actually ordered on the data pages by primary key (this is called a clustered index). However, nothing in SQL in general requires that the primary key also be a clustered index, and not all databases implement this.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

There is no inconsistency in what you read

A primary key is "one or more columns, whose combination of values must appear on only one row"

A primary key can be single column, and then the value must be unique. It can be multiple columns, and all values considers together must be unique:

Key1, Key2
1   , A
1   , B
2   , A
2   , B

Here it's true that both key1 column and key2 column have repeated values but when we consider the combinations of key 1 and key 2, they are unique: 1A, 1B, 2A, 2B

Regarding the PK vs Unique Key query, see this answer. It goes into more detail about primary keys, the basic principle of which is that none of the columns making up the primary key allows a null value: What's wrong with nullable columns in composite primary keys?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I didn't understand. Can you make it clear by some good example? –  Nov 03 '18 at 09:16
  • So, you mean, in the example above , PK will be 1A, 1B , 2A and 2B. Right? –  Nov 03 '18 at 09:18
  • And, in this case, will we call it a Primary Key or Composite Key? –  Nov 03 '18 at 09:20
  • It's a composite, and in this case it's also the primary. Call it whichever makes sense for the context you're using – Caius Jard Nov 03 '18 at 22:36
0

the main differences between a primary key and a unique key is the primary key main job is to uniquely identify a row in a table and the main job for a unique key is to allow you to place additional unique conditions on your columns

For example say you have a employees table with employee ID as the primary key and a accountNumber.. the accountNumber would be set as the unique ID... so you wouldn’t have the accountNumber set as the primary key incase accountNumber is generated by another organisation outside of your database..

stu
  • 122
  • 5
0

When Primary key can consists of multiple columns, then how can it be only 1 for a table and then how this can differ from Unique Key except from null value difference?

Its true that a table can only have one primary key. This key can contains multiple columns(combination of columns). That combination of columns is collective is a Primary Key.

Lets take an example

Table A

Id, Clientid, Branchid, Name, Address, .......

Here I created a primary key with combination of "Id, Clientid, Branchid"

so its is possible to have duplicate Id or duplicate CLientid or duplicate Branchid but its not possible to have the whole combination asa duplicate

for example

Id    Clientid    Branchid  
1        1           1  
1        2           1  
1        1           2  
2        1           1

the above combination is true but below combination is not allowed as its a primary key and cann't have duplicate values

Id    Clientid    Branchid  
 1        1           1  
 1        1           1  

and all columns as null is also not possible as primary key doesn't allow null

Ravi
  • 1,157
  • 1
  • 9
  • 19