difference between primary key and unique key
I'm using mysql database. I have a confusion between primary key and unique key.
Please help me where should I create primary and unique key. I mean in which situation we create unique key or primary key .
database primary-key unique-key
add a comment |
I'm using mysql database. I have a confusion between primary key and unique key.
Please help me where should I create primary and unique key. I mean in which situation we create unique key or primary key .
database primary-key unique-key
6
wrt null-ability a good way to distinguish b/w them isPRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28
add a comment |
I'm using mysql database. I have a confusion between primary key and unique key.
Please help me where should I create primary and unique key. I mean in which situation we create unique key or primary key .
database primary-key unique-key
I'm using mysql database. I have a confusion between primary key and unique key.
Please help me where should I create primary and unique key. I mean in which situation we create unique key or primary key .
database primary-key unique-key
database primary-key unique-key
edited Dec 4 '13 at 13:39
user2277147
4317
4317
asked Mar 5 '12 at 11:39
AnujAnuj
1,1062810
1,1062810
6
wrt null-ability a good way to distinguish b/w them isPRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28
add a comment |
6
wrt null-ability a good way to distinguish b/w them isPRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28
6
6
wrt null-ability a good way to distinguish b/w them is
PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
wrt null-ability a good way to distinguish b/w them is
PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28
add a comment |
14 Answers
14
active
oldest
votes
Primary Key:
- There can only be one primary key in a table
- In some DBMS it cannot be
NULL
- e.g. MySQL addsNOT NULL
- Primary Key is a unique key identifier of the record
Unique Key:
- Can be more than one unique key in one table
- Unique key can have
NULL
values - It can be a candidate key
- Unique key can be
NULL
and may not be unique
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
|
show 11 more comments
Unique Key (UK): It's a column or a group of columns that can identify a uniqueness in a row.
Primary Key (PK): It's also a column or group of columns that can identify a uniqueness in a row.
So the Primary key is just another name for unique key, but the default implementation in SQL Server is different for Primary and Unique Key.
By Default:
- PK creates a Clustered index and UK creates a Non Clustered Index.
- PK is not null, but UK allows nulls (Note: By Default)
- There can only be one and only one PK on a table, but there can be multiple UK's
- You can override the default implementation depending upon your need.
It really depends what is your aim when deciding whether to create a UK or PK. It follows an analogy like
"If there is a team of three people, so all of them are peers, but there will be one of them who will be a pair of peers: PK and UK has similar relation.". I would suggest reading this article: The example given by the author may not seem suitable, but try to get an overall idea.
http://tsqltips.blogspot.com/2012/06/difference-between-unique-key-and.html
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.
– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
add a comment |
For an organization or a business, there are so many physical entities (such as people, resources, machines, etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business needs to record and process information of those business entities.
These business entities are identified within a whole business domain by a Key.
As per RDBMS prospective, Key (a.k.a Candidate Key) is a value or set of values that uniquely identifies an entity.
For a DB-Table, there are so many keys are exist and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
However, DBA selected a key from candidate key for searching records is called Primary key.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table, whereas Unique-key is to prevent duplicate values in a column (with the exception of a null entry).
2. Indexing: By default SQL-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values, whereas Unique-key can.
4. Existence: A table can have at most one primary key, but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values, but Unique-key values can.
For more information and Examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
add a comment |
A primary key must be unique.
A unique key does not have to be the primary key - see candidate key.
That is, there may be more than one combination of columns on a table that can uniquely identify a row - only one of these can be selected as the primary key. The others, though unique are candidate keys.
add a comment |
A primary key has the semantic of identifying the row of a database. Therefore there can be only one primary key for a given table, while there can be many unique keys.
Also for the same reason a primary key cannot be NULL (at least in Oracle, not sure about other databases)
Since it identifies the row it should never ever change. Changing primary keys are bound to cause serious pain and probably eternal damnation.
Therefor in most cases you want some artificial id for primary key which isn't used for anything but identifying single rows in the table.
Unique keys on the other hand may change as much as you want.
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
add a comment |
Difference between Primary Key and Unique Key
+-----------------------------------------+-----------------------------------------------+
| Primary Key | Unique Key |
+-----------------------------------------+-----------------------------------------------+
| Primary Key can't accept null values. | Unique key can accept only one null value. |
+-----------------------------------------+-----------------------------------------------+
| By default, Primary key is clustered | By default, Unique key is a unique |
| index and data in the database table is | non-clustered index. |
| physically organized in the sequence of | |
| clustered index. | |
+-----------------------------------------+-----------------------------------------------+
| We can have only one Primary key in a | We can have more than one unique key in a |
| table. | table. |
+-----------------------------------------+-----------------------------------------------+
| Primary key can be made foreign key | In SQL Server, Unique key can be made foreign |
| into another table. | key into another table. |
+-----------------------------------------+-----------------------------------------------+
You can find detailed information from:
http://www.dotnet-tricks.com/Tutorial/sqlserver/V2bS260912-Difference-between-Primary-Key-and-Unique-Key.html
add a comment |
A Primary key is a unique key.
Each table must have at most ONE primary key but it can have multiple unique key. A primary key is used to uniquely identify a table row. A primary key cannot be NULL
since NULL
is not a value.
add a comment |
- Think the table name is employe.
- Primary key
- Primary key can not accept null values. primary key enforces uniqueness of a
column. We can have only one Primary key in a table. - Unique key
- Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
add a comment |
I know this question is several years old but I'd like to provide an answer to this explaining why rather than how
Purpose of Primary Key: To identify a row in a database uniquely => A row represents a single instance of the entity type modeled by the tableA primary key enforces integrity of an entity, AKA Entity Integrity. Primary Key would be a clustered index i.e. it defines the order in which data is physically stored in a table.
Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.
There you go!
add a comment |
If your Database design is such that their is no need of foreign key, then you can go with Unique key( but remember unique key allow single null value ).
If you database demand foreign key then you leave with no choice you have to go with primary key.
To see the difference between unique and primary key visit here
add a comment |
Simply Primary Key is a unique and can't be null, unique can be null and may not be unique.
"unique can be null and may not be unique". What doesmay not be unique
means here?
– Yusuf Hassan
Apr 17 '18 at 10:31
add a comment |
Primary Keys
The main purpose of the primary key is to provide a means to identify each record in the table.
The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.
A primary key has the following characteristics:
- There can only be one primary key for a table.
- The primary key consists of one or more columns.
- The primary key enforces the entity integrity of the table.
- All columns defined must be defined as NOT NULL.
- The primary key uniquely identifies a row.
- Primary keys result in CLUSTERED unique indexes by default.
Unique Keys
A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database.
Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.
In SQL Server the unique key has the following characteristics:
- There can be multiple unique keys defined on a table.
- Unique Keys result in NONCLUSTERED Unique Indexes by default.
- One or more columns make up a unique key.
- Column may be NULL, but on one NULL per column is allowed.
- A unique constraint can be referenced by a Foreign Key Constraint.
source : here
add a comment |
Unique key :-
It should be used when you have to give unique value.In the case of
unique key it means null values are also allowed.Unique keys are those
keys which are unique and non similar in that column like for example
your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true
primary key :-
It should be used when you have to give uniquely identify a row.primary is key which unique for every row in a database constraint is that it doesn't allow null in it.so, you might have seen that the database have a column which is auto increment and it is the primary key of the table. plus it can be used as a foreign key in another table.example can be orderId on a order Table,billId in a bill Table.
now coming back to situation when to use it:-
1) primary key in the column which can
not be null in the table and you are using as foreign key in another
table for creating relationship
2) unique key in table where it
doesn't affect in table or in the whole database whether you take the
null for the particular column like snacks in the restaurant it is
possible you don't take snacks in a restaurant
add a comment |
difference between Primary Key and Unique Key
Both Primary key
and Unique Key
is used to uniquely define of a row in a table.
Primary Key
creates a clustered index
of the column whereas a Unique creates an unclustered index of the column
.
A Primary Key
doesn’t allow NULL value
, however a Unique Key
does allow one NULL value
.
add a comment |
protected by Community♦ Oct 27 '13 at 12:38
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
Primary Key:
- There can only be one primary key in a table
- In some DBMS it cannot be
NULL
- e.g. MySQL addsNOT NULL
- Primary Key is a unique key identifier of the record
Unique Key:
- Can be more than one unique key in one table
- Unique key can have
NULL
values - It can be a candidate key
- Unique key can be
NULL
and may not be unique
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
|
show 11 more comments
Primary Key:
- There can only be one primary key in a table
- In some DBMS it cannot be
NULL
- e.g. MySQL addsNOT NULL
- Primary Key is a unique key identifier of the record
Unique Key:
- Can be more than one unique key in one table
- Unique key can have
NULL
values - It can be a candidate key
- Unique key can be
NULL
and may not be unique
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
|
show 11 more comments
Primary Key:
- There can only be one primary key in a table
- In some DBMS it cannot be
NULL
- e.g. MySQL addsNOT NULL
- Primary Key is a unique key identifier of the record
Unique Key:
- Can be more than one unique key in one table
- Unique key can have
NULL
values - It can be a candidate key
- Unique key can be
NULL
and may not be unique
Primary Key:
- There can only be one primary key in a table
- In some DBMS it cannot be
NULL
- e.g. MySQL addsNOT NULL
- Primary Key is a unique key identifier of the record
Unique Key:
- Can be more than one unique key in one table
- Unique key can have
NULL
values - It can be a candidate key
- Unique key can be
NULL
and may not be unique
edited Aug 3 '17 at 13:28
Abdullah Khan
5,72922944
5,72922944
answered Nov 12 '12 at 18:15
Mr. KBMr. KB
1,946192
1,946192
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
|
show 11 more comments
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
10
10
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
a unique key can be candidate key.
– Falaque
Apr 25 '13 at 14:08
1
1
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
yes @Falaque both PF and FK can be candidate
– shashwat
Aug 9 '13 at 8:26
9
9
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
Also want to add on that primary key can be created on multiple columns, e.g. Primary key (CustomerID, ProductID). This is called composite primary key. This is to clarify the first point, as it might be take as it is (read one key => one column ) by new comer to sql : )
– ken
Apr 10 '14 at 13:53
63
63
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
Unique key can be null and may not be unique Means ??
– Pratik C Joshi
Sep 30 '15 at 7:56
19
19
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
@PratikCJoshi He probably means that the can be multiple rows with null on the otherwise unique key.
– John
Jun 10 '16 at 15:01
|
show 11 more comments
Unique Key (UK): It's a column or a group of columns that can identify a uniqueness in a row.
Primary Key (PK): It's also a column or group of columns that can identify a uniqueness in a row.
So the Primary key is just another name for unique key, but the default implementation in SQL Server is different for Primary and Unique Key.
By Default:
- PK creates a Clustered index and UK creates a Non Clustered Index.
- PK is not null, but UK allows nulls (Note: By Default)
- There can only be one and only one PK on a table, but there can be multiple UK's
- You can override the default implementation depending upon your need.
It really depends what is your aim when deciding whether to create a UK or PK. It follows an analogy like
"If there is a team of three people, so all of them are peers, but there will be one of them who will be a pair of peers: PK and UK has similar relation.". I would suggest reading this article: The example given by the author may not seem suitable, but try to get an overall idea.
http://tsqltips.blogspot.com/2012/06/difference-between-unique-key-and.html
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.
– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
add a comment |
Unique Key (UK): It's a column or a group of columns that can identify a uniqueness in a row.
Primary Key (PK): It's also a column or group of columns that can identify a uniqueness in a row.
So the Primary key is just another name for unique key, but the default implementation in SQL Server is different for Primary and Unique Key.
By Default:
- PK creates a Clustered index and UK creates a Non Clustered Index.
- PK is not null, but UK allows nulls (Note: By Default)
- There can only be one and only one PK on a table, but there can be multiple UK's
- You can override the default implementation depending upon your need.
It really depends what is your aim when deciding whether to create a UK or PK. It follows an analogy like
"If there is a team of three people, so all of them are peers, but there will be one of them who will be a pair of peers: PK and UK has similar relation.". I would suggest reading this article: The example given by the author may not seem suitable, but try to get an overall idea.
http://tsqltips.blogspot.com/2012/06/difference-between-unique-key-and.html
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.
– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
add a comment |
Unique Key (UK): It's a column or a group of columns that can identify a uniqueness in a row.
Primary Key (PK): It's also a column or group of columns that can identify a uniqueness in a row.
So the Primary key is just another name for unique key, but the default implementation in SQL Server is different for Primary and Unique Key.
By Default:
- PK creates a Clustered index and UK creates a Non Clustered Index.
- PK is not null, but UK allows nulls (Note: By Default)
- There can only be one and only one PK on a table, but there can be multiple UK's
- You can override the default implementation depending upon your need.
It really depends what is your aim when deciding whether to create a UK or PK. It follows an analogy like
"If there is a team of three people, so all of them are peers, but there will be one of them who will be a pair of peers: PK and UK has similar relation.". I would suggest reading this article: The example given by the author may not seem suitable, but try to get an overall idea.
http://tsqltips.blogspot.com/2012/06/difference-between-unique-key-and.html
Unique Key (UK): It's a column or a group of columns that can identify a uniqueness in a row.
Primary Key (PK): It's also a column or group of columns that can identify a uniqueness in a row.
So the Primary key is just another name for unique key, but the default implementation in SQL Server is different for Primary and Unique Key.
By Default:
- PK creates a Clustered index and UK creates a Non Clustered Index.
- PK is not null, but UK allows nulls (Note: By Default)
- There can only be one and only one PK on a table, but there can be multiple UK's
- You can override the default implementation depending upon your need.
It really depends what is your aim when deciding whether to create a UK or PK. It follows an analogy like
"If there is a team of three people, so all of them are peers, but there will be one of them who will be a pair of peers: PK and UK has similar relation.". I would suggest reading this article: The example given by the author may not seem suitable, but try to get an overall idea.
http://tsqltips.blogspot.com/2012/06/difference-between-unique-key-and.html
edited Mar 17 '17 at 9:27
stuart_mad
97114
97114
answered Jun 26 '12 at 0:14
dhidhi
86773
86773
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.
– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
add a comment |
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.
– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
read around 10 webpages, which say, PK can contain more than one column. Then how can there be one and only one PK on a table?
– user6192706
Nov 3 '18 at 8:17
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name
[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.– Poik
Nov 15 '18 at 16:51
@android A PK with more than one column acts as a single column with respect to the uniqueness. In PostgreSQL at least, this means that a new column (with default name
[table_name]_pkey
) is added to the table (I've heard this referred to as a surrogate key). Source: postgresqltutorial.com/postgresql-primary-key I'm new to all this so I'd appreciate a more knowledgeable poster to point out the nuances I missed.– Poik
Nov 15 '18 at 16:51
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
Okay, it's not a column. I misread. It's a contraint, not a column. There's still the clustered index, but it's over two columns instead of one. And each column in it is not a primary key on its own, instead the whole set is a primary key. So there are not more than one PK in these instances.
– Poik
Nov 15 '18 at 18:16
add a comment |
For an organization or a business, there are so many physical entities (such as people, resources, machines, etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business needs to record and process information of those business entities.
These business entities are identified within a whole business domain by a Key.
As per RDBMS prospective, Key (a.k.a Candidate Key) is a value or set of values that uniquely identifies an entity.
For a DB-Table, there are so many keys are exist and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
However, DBA selected a key from candidate key for searching records is called Primary key.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table, whereas Unique-key is to prevent duplicate values in a column (with the exception of a null entry).
2. Indexing: By default SQL-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values, whereas Unique-key can.
4. Existence: A table can have at most one primary key, but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values, but Unique-key values can.
For more information and Examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
add a comment |
For an organization or a business, there are so many physical entities (such as people, resources, machines, etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business needs to record and process information of those business entities.
These business entities are identified within a whole business domain by a Key.
As per RDBMS prospective, Key (a.k.a Candidate Key) is a value or set of values that uniquely identifies an entity.
For a DB-Table, there are so many keys are exist and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
However, DBA selected a key from candidate key for searching records is called Primary key.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table, whereas Unique-key is to prevent duplicate values in a column (with the exception of a null entry).
2. Indexing: By default SQL-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values, whereas Unique-key can.
4. Existence: A table can have at most one primary key, but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values, but Unique-key values can.
For more information and Examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
add a comment |
For an organization or a business, there are so many physical entities (such as people, resources, machines, etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business needs to record and process information of those business entities.
These business entities are identified within a whole business domain by a Key.
As per RDBMS prospective, Key (a.k.a Candidate Key) is a value or set of values that uniquely identifies an entity.
For a DB-Table, there are so many keys are exist and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
However, DBA selected a key from candidate key for searching records is called Primary key.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table, whereas Unique-key is to prevent duplicate values in a column (with the exception of a null entry).
2. Indexing: By default SQL-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values, whereas Unique-key can.
4. Existence: A table can have at most one primary key, but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values, but Unique-key values can.
For more information and Examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
For an organization or a business, there are so many physical entities (such as people, resources, machines, etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business needs to record and process information of those business entities.
These business entities are identified within a whole business domain by a Key.
As per RDBMS prospective, Key (a.k.a Candidate Key) is a value or set of values that uniquely identifies an entity.
For a DB-Table, there are so many keys are exist and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
However, DBA selected a key from candidate key for searching records is called Primary key.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table, whereas Unique-key is to prevent duplicate values in a column (with the exception of a null entry).
2. Indexing: By default SQL-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values, whereas Unique-key can.
4. Existence: A table can have at most one primary key, but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values, but Unique-key values can.
For more information and Examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
edited Mar 17 '17 at 12:07
stuart_mad
97114
97114
answered Jan 10 '14 at 16:40
nayeemDotNetAuthoritiesnayeemDotNetAuthorities
1,9791137
1,9791137
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
add a comment |
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
7
7
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
In 5th point you say we can't change or delete primary values. we for sure can change the primary values in the table by using an update statement.
– Kapil
Oct 28 '15 at 13:04
3
3
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
@Kapil doing so beats the whole purpose of using a primary key.
– Gokigooooks
Jan 23 '16 at 18:48
2
2
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
clustered index: the rows are stored physically on the disk in the same order as the index
– Duy Đặng
Jan 15 '18 at 4:08
add a comment |
A primary key must be unique.
A unique key does not have to be the primary key - see candidate key.
That is, there may be more than one combination of columns on a table that can uniquely identify a row - only one of these can be selected as the primary key. The others, though unique are candidate keys.
add a comment |
A primary key must be unique.
A unique key does not have to be the primary key - see candidate key.
That is, there may be more than one combination of columns on a table that can uniquely identify a row - only one of these can be selected as the primary key. The others, though unique are candidate keys.
add a comment |
A primary key must be unique.
A unique key does not have to be the primary key - see candidate key.
That is, there may be more than one combination of columns on a table that can uniquely identify a row - only one of these can be selected as the primary key. The others, though unique are candidate keys.
A primary key must be unique.
A unique key does not have to be the primary key - see candidate key.
That is, there may be more than one combination of columns on a table that can uniquely identify a row - only one of these can be selected as the primary key. The others, though unique are candidate keys.
edited Mar 5 '12 at 11:48
answered Mar 5 '12 at 11:42
OdedOded
414k74749923
414k74749923
add a comment |
add a comment |
A primary key has the semantic of identifying the row of a database. Therefore there can be only one primary key for a given table, while there can be many unique keys.
Also for the same reason a primary key cannot be NULL (at least in Oracle, not sure about other databases)
Since it identifies the row it should never ever change. Changing primary keys are bound to cause serious pain and probably eternal damnation.
Therefor in most cases you want some artificial id for primary key which isn't used for anything but identifying single rows in the table.
Unique keys on the other hand may change as much as you want.
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
add a comment |
A primary key has the semantic of identifying the row of a database. Therefore there can be only one primary key for a given table, while there can be many unique keys.
Also for the same reason a primary key cannot be NULL (at least in Oracle, not sure about other databases)
Since it identifies the row it should never ever change. Changing primary keys are bound to cause serious pain and probably eternal damnation.
Therefor in most cases you want some artificial id for primary key which isn't used for anything but identifying single rows in the table.
Unique keys on the other hand may change as much as you want.
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
add a comment |
A primary key has the semantic of identifying the row of a database. Therefore there can be only one primary key for a given table, while there can be many unique keys.
Also for the same reason a primary key cannot be NULL (at least in Oracle, not sure about other databases)
Since it identifies the row it should never ever change. Changing primary keys are bound to cause serious pain and probably eternal damnation.
Therefor in most cases you want some artificial id for primary key which isn't used for anything but identifying single rows in the table.
Unique keys on the other hand may change as much as you want.
A primary key has the semantic of identifying the row of a database. Therefore there can be only one primary key for a given table, while there can be many unique keys.
Also for the same reason a primary key cannot be NULL (at least in Oracle, not sure about other databases)
Since it identifies the row it should never ever change. Changing primary keys are bound to cause serious pain and probably eternal damnation.
Therefor in most cases you want some artificial id for primary key which isn't used for anything but identifying single rows in the table.
Unique keys on the other hand may change as much as you want.
answered Mar 5 '12 at 11:44
Jens SchauderJens Schauder
47.4k18115243
47.4k18115243
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
add a comment |
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
22
22
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
+1 for mentioning risk of eternal damnation. It's time to introduce theology into relational database theory.
– Neville Kuyt
Mar 5 '12 at 11:48
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
PK cannot be NULL in SQL Server as well
– mrd3650
Oct 7 '15 at 14:51
add a comment |
Difference between Primary Key and Unique Key
+-----------------------------------------+-----------------------------------------------+
| Primary Key | Unique Key |
+-----------------------------------------+-----------------------------------------------+
| Primary Key can't accept null values. | Unique key can accept only one null value. |
+-----------------------------------------+-----------------------------------------------+
| By default, Primary key is clustered | By default, Unique key is a unique |
| index and data in the database table is | non-clustered index. |
| physically organized in the sequence of | |
| clustered index. | |
+-----------------------------------------+-----------------------------------------------+
| We can have only one Primary key in a | We can have more than one unique key in a |
| table. | table. |
+-----------------------------------------+-----------------------------------------------+
| Primary key can be made foreign key | In SQL Server, Unique key can be made foreign |
| into another table. | key into another table. |
+-----------------------------------------+-----------------------------------------------+
You can find detailed information from:
http://www.dotnet-tricks.com/Tutorial/sqlserver/V2bS260912-Difference-between-Primary-Key-and-Unique-Key.html
add a comment |
Difference between Primary Key and Unique Key
+-----------------------------------------+-----------------------------------------------+
| Primary Key | Unique Key |
+-----------------------------------------+-----------------------------------------------+
| Primary Key can't accept null values. | Unique key can accept only one null value. |
+-----------------------------------------+-----------------------------------------------+
| By default, Primary key is clustered | By default, Unique key is a unique |
| index and data in the database table is | non-clustered index. |
| physically organized in the sequence of | |
| clustered index. | |
+-----------------------------------------+-----------------------------------------------+
| We can have only one Primary key in a | We can have more than one unique key in a |
| table. | table. |
+-----------------------------------------+-----------------------------------------------+
| Primary key can be made foreign key | In SQL Server, Unique key can be made foreign |
| into another table. | key into another table. |
+-----------------------------------------+-----------------------------------------------+
You can find detailed information from:
http://www.dotnet-tricks.com/Tutorial/sqlserver/V2bS260912-Difference-between-Primary-Key-and-Unique-Key.html
add a comment |
Difference between Primary Key and Unique Key
+-----------------------------------------+-----------------------------------------------+
| Primary Key | Unique Key |
+-----------------------------------------+-----------------------------------------------+
| Primary Key can't accept null values. | Unique key can accept only one null value. |
+-----------------------------------------+-----------------------------------------------+
| By default, Primary key is clustered | By default, Unique key is a unique |
| index and data in the database table is | non-clustered index. |
| physically organized in the sequence of | |
| clustered index. | |
+-----------------------------------------+-----------------------------------------------+
| We can have only one Primary key in a | We can have more than one unique key in a |
| table. | table. |
+-----------------------------------------+-----------------------------------------------+
| Primary key can be made foreign key | In SQL Server, Unique key can be made foreign |
| into another table. | key into another table. |
+-----------------------------------------+-----------------------------------------------+
You can find detailed information from:
http://www.dotnet-tricks.com/Tutorial/sqlserver/V2bS260912-Difference-between-Primary-Key-and-Unique-Key.html
Difference between Primary Key and Unique Key
+-----------------------------------------+-----------------------------------------------+
| Primary Key | Unique Key |
+-----------------------------------------+-----------------------------------------------+
| Primary Key can't accept null values. | Unique key can accept only one null value. |
+-----------------------------------------+-----------------------------------------------+
| By default, Primary key is clustered | By default, Unique key is a unique |
| index and data in the database table is | non-clustered index. |
| physically organized in the sequence of | |
| clustered index. | |
+-----------------------------------------+-----------------------------------------------+
| We can have only one Primary key in a | We can have more than one unique key in a |
| table. | table. |
+-----------------------------------------+-----------------------------------------------+
| Primary key can be made foreign key | In SQL Server, Unique key can be made foreign |
| into another table. | key into another table. |
+-----------------------------------------+-----------------------------------------------+
You can find detailed information from:
http://www.dotnet-tricks.com/Tutorial/sqlserver/V2bS260912-Difference-between-Primary-Key-and-Unique-Key.html
edited Nov 14 '18 at 2:15
Pang
6,9991666105
6,9991666105
answered Nov 16 '15 at 20:31
Omer KOmer K
3,02474567
3,02474567
add a comment |
add a comment |
A Primary key is a unique key.
Each table must have at most ONE primary key but it can have multiple unique key. A primary key is used to uniquely identify a table row. A primary key cannot be NULL
since NULL
is not a value.
add a comment |
A Primary key is a unique key.
Each table must have at most ONE primary key but it can have multiple unique key. A primary key is used to uniquely identify a table row. A primary key cannot be NULL
since NULL
is not a value.
add a comment |
A Primary key is a unique key.
Each table must have at most ONE primary key but it can have multiple unique key. A primary key is used to uniquely identify a table row. A primary key cannot be NULL
since NULL
is not a value.
A Primary key is a unique key.
Each table must have at most ONE primary key but it can have multiple unique key. A primary key is used to uniquely identify a table row. A primary key cannot be NULL
since NULL
is not a value.
answered Mar 5 '12 at 11:47
Buhake SindiBuhake Sindi
71.6k24146205
71.6k24146205
add a comment |
add a comment |
- Think the table name is employe.
- Primary key
- Primary key can not accept null values. primary key enforces uniqueness of a
column. We can have only one Primary key in a table. - Unique key
- Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
add a comment |
- Think the table name is employe.
- Primary key
- Primary key can not accept null values. primary key enforces uniqueness of a
column. We can have only one Primary key in a table. - Unique key
- Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
add a comment |
- Think the table name is employe.
- Primary key
- Primary key can not accept null values. primary key enforces uniqueness of a
column. We can have only one Primary key in a table. - Unique key
- Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.
- Think the table name is employe.
- Primary key
- Primary key can not accept null values. primary key enforces uniqueness of a
column. We can have only one Primary key in a table. - Unique key
- Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.
answered Nov 19 '17 at 13:15
Mahedi Hasan DurjoyMahedi Hasan Durjoy
14219
14219
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
add a comment |
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
1
1
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
we can't insert more than one null values in Unique key and it will not allow duplicates also.
– Masum
Apr 9 '18 at 12:17
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
@mahedi-hasan Isn't Unique key column should have only one NULL value? How come last two rows in Citizen ID NULL? Am I missing something here?
– supernova
Sep 10 '18 at 4:32
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
Just got answer to my own comment above. Looks like MySQL allows multiple NULL in unique also so looks like @Mahedi_Hasan used MySQL. stackoverflow.com/questions/3712222/…
– supernova
Sep 10 '18 at 4:38
add a comment |
I know this question is several years old but I'd like to provide an answer to this explaining why rather than how
Purpose of Primary Key: To identify a row in a database uniquely => A row represents a single instance of the entity type modeled by the tableA primary key enforces integrity of an entity, AKA Entity Integrity. Primary Key would be a clustered index i.e. it defines the order in which data is physically stored in a table.
Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.
There you go!
add a comment |
I know this question is several years old but I'd like to provide an answer to this explaining why rather than how
Purpose of Primary Key: To identify a row in a database uniquely => A row represents a single instance of the entity type modeled by the tableA primary key enforces integrity of an entity, AKA Entity Integrity. Primary Key would be a clustered index i.e. it defines the order in which data is physically stored in a table.
Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.
There you go!
add a comment |
I know this question is several years old but I'd like to provide an answer to this explaining why rather than how
Purpose of Primary Key: To identify a row in a database uniquely => A row represents a single instance of the entity type modeled by the tableA primary key enforces integrity of an entity, AKA Entity Integrity. Primary Key would be a clustered index i.e. it defines the order in which data is physically stored in a table.
Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.
There you go!
I know this question is several years old but I'd like to provide an answer to this explaining why rather than how
Purpose of Primary Key: To identify a row in a database uniquely => A row represents a single instance of the entity type modeled by the tableA primary key enforces integrity of an entity, AKA Entity Integrity. Primary Key would be a clustered index i.e. it defines the order in which data is physically stored in a table.
Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.
There you go!
answered Dec 27 '18 at 5:30
ManuriManuri
368213
368213
add a comment |
add a comment |
If your Database design is such that their is no need of foreign key, then you can go with Unique key( but remember unique key allow single null value ).
If you database demand foreign key then you leave with no choice you have to go with primary key.
To see the difference between unique and primary key visit here
add a comment |
If your Database design is such that their is no need of foreign key, then you can go with Unique key( but remember unique key allow single null value ).
If you database demand foreign key then you leave with no choice you have to go with primary key.
To see the difference between unique and primary key visit here
add a comment |
If your Database design is such that their is no need of foreign key, then you can go with Unique key( but remember unique key allow single null value ).
If you database demand foreign key then you leave with no choice you have to go with primary key.
To see the difference between unique and primary key visit here
If your Database design is such that their is no need of foreign key, then you can go with Unique key( but remember unique key allow single null value ).
If you database demand foreign key then you leave with no choice you have to go with primary key.
To see the difference between unique and primary key visit here
answered Apr 17 '16 at 7:49
user2903536user2903536
8891117
8891117
add a comment |
add a comment |
Simply Primary Key is a unique and can't be null, unique can be null and may not be unique.
"unique can be null and may not be unique". What doesmay not be unique
means here?
– Yusuf Hassan
Apr 17 '18 at 10:31
add a comment |
Simply Primary Key is a unique and can't be null, unique can be null and may not be unique.
"unique can be null and may not be unique". What doesmay not be unique
means here?
– Yusuf Hassan
Apr 17 '18 at 10:31
add a comment |
Simply Primary Key is a unique and can't be null, unique can be null and may not be unique.
Simply Primary Key is a unique and can't be null, unique can be null and may not be unique.
answered Nov 11 '14 at 9:50
Mohammed F. GhazoMohammed F. Ghazo
58110
58110
"unique can be null and may not be unique". What doesmay not be unique
means here?
– Yusuf Hassan
Apr 17 '18 at 10:31
add a comment |
"unique can be null and may not be unique". What doesmay not be unique
means here?
– Yusuf Hassan
Apr 17 '18 at 10:31
"unique can be null and may not be unique". What does
may not be unique
means here?– Yusuf Hassan
Apr 17 '18 at 10:31
"unique can be null and may not be unique". What does
may not be unique
means here?– Yusuf Hassan
Apr 17 '18 at 10:31
add a comment |
Primary Keys
The main purpose of the primary key is to provide a means to identify each record in the table.
The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.
A primary key has the following characteristics:
- There can only be one primary key for a table.
- The primary key consists of one or more columns.
- The primary key enforces the entity integrity of the table.
- All columns defined must be defined as NOT NULL.
- The primary key uniquely identifies a row.
- Primary keys result in CLUSTERED unique indexes by default.
Unique Keys
A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database.
Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.
In SQL Server the unique key has the following characteristics:
- There can be multiple unique keys defined on a table.
- Unique Keys result in NONCLUSTERED Unique Indexes by default.
- One or more columns make up a unique key.
- Column may be NULL, but on one NULL per column is allowed.
- A unique constraint can be referenced by a Foreign Key Constraint.
source : here
add a comment |
Primary Keys
The main purpose of the primary key is to provide a means to identify each record in the table.
The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.
A primary key has the following characteristics:
- There can only be one primary key for a table.
- The primary key consists of one or more columns.
- The primary key enforces the entity integrity of the table.
- All columns defined must be defined as NOT NULL.
- The primary key uniquely identifies a row.
- Primary keys result in CLUSTERED unique indexes by default.
Unique Keys
A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database.
Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.
In SQL Server the unique key has the following characteristics:
- There can be multiple unique keys defined on a table.
- Unique Keys result in NONCLUSTERED Unique Indexes by default.
- One or more columns make up a unique key.
- Column may be NULL, but on one NULL per column is allowed.
- A unique constraint can be referenced by a Foreign Key Constraint.
source : here
add a comment |
Primary Keys
The main purpose of the primary key is to provide a means to identify each record in the table.
The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.
A primary key has the following characteristics:
- There can only be one primary key for a table.
- The primary key consists of one or more columns.
- The primary key enforces the entity integrity of the table.
- All columns defined must be defined as NOT NULL.
- The primary key uniquely identifies a row.
- Primary keys result in CLUSTERED unique indexes by default.
Unique Keys
A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database.
Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.
In SQL Server the unique key has the following characteristics:
- There can be multiple unique keys defined on a table.
- Unique Keys result in NONCLUSTERED Unique Indexes by default.
- One or more columns make up a unique key.
- Column may be NULL, but on one NULL per column is allowed.
- A unique constraint can be referenced by a Foreign Key Constraint.
source : here
Primary Keys
The main purpose of the primary key is to provide a means to identify each record in the table.
The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.
A primary key has the following characteristics:
- There can only be one primary key for a table.
- The primary key consists of one or more columns.
- The primary key enforces the entity integrity of the table.
- All columns defined must be defined as NOT NULL.
- The primary key uniquely identifies a row.
- Primary keys result in CLUSTERED unique indexes by default.
Unique Keys
A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database.
Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.
In SQL Server the unique key has the following characteristics:
- There can be multiple unique keys defined on a table.
- Unique Keys result in NONCLUSTERED Unique Indexes by default.
- One or more columns make up a unique key.
- Column may be NULL, but on one NULL per column is allowed.
- A unique constraint can be referenced by a Foreign Key Constraint.
source : here
answered Jul 24 '18 at 9:58
Manish VadherManish Vadher
519412
519412
add a comment |
add a comment |
Unique key :-
It should be used when you have to give unique value.In the case of
unique key it means null values are also allowed.Unique keys are those
keys which are unique and non similar in that column like for example
your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true
primary key :-
It should be used when you have to give uniquely identify a row.primary is key which unique for every row in a database constraint is that it doesn't allow null in it.so, you might have seen that the database have a column which is auto increment and it is the primary key of the table. plus it can be used as a foreign key in another table.example can be orderId on a order Table,billId in a bill Table.
now coming back to situation when to use it:-
1) primary key in the column which can
not be null in the table and you are using as foreign key in another
table for creating relationship
2) unique key in table where it
doesn't affect in table or in the whole database whether you take the
null for the particular column like snacks in the restaurant it is
possible you don't take snacks in a restaurant
add a comment |
Unique key :-
It should be used when you have to give unique value.In the case of
unique key it means null values are also allowed.Unique keys are those
keys which are unique and non similar in that column like for example
your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true
primary key :-
It should be used when you have to give uniquely identify a row.primary is key which unique for every row in a database constraint is that it doesn't allow null in it.so, you might have seen that the database have a column which is auto increment and it is the primary key of the table. plus it can be used as a foreign key in another table.example can be orderId on a order Table,billId in a bill Table.
now coming back to situation when to use it:-
1) primary key in the column which can
not be null in the table and you are using as foreign key in another
table for creating relationship
2) unique key in table where it
doesn't affect in table or in the whole database whether you take the
null for the particular column like snacks in the restaurant it is
possible you don't take snacks in a restaurant
add a comment |
Unique key :-
It should be used when you have to give unique value.In the case of
unique key it means null values are also allowed.Unique keys are those
keys which are unique and non similar in that column like for example
your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true
primary key :-
It should be used when you have to give uniquely identify a row.primary is key which unique for every row in a database constraint is that it doesn't allow null in it.so, you might have seen that the database have a column which is auto increment and it is the primary key of the table. plus it can be used as a foreign key in another table.example can be orderId on a order Table,billId in a bill Table.
now coming back to situation when to use it:-
1) primary key in the column which can
not be null in the table and you are using as foreign key in another
table for creating relationship
2) unique key in table where it
doesn't affect in table or in the whole database whether you take the
null for the particular column like snacks in the restaurant it is
possible you don't take snacks in a restaurant
Unique key :-
It should be used when you have to give unique value.In the case of
unique key it means null values are also allowed.Unique keys are those
keys which are unique and non similar in that column like for example
your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true
primary key :-
It should be used when you have to give uniquely identify a row.primary is key which unique for every row in a database constraint is that it doesn't allow null in it.so, you might have seen that the database have a column which is auto increment and it is the primary key of the table. plus it can be used as a foreign key in another table.example can be orderId on a order Table,billId in a bill Table.
now coming back to situation when to use it:-
1) primary key in the column which can
not be null in the table and you are using as foreign key in another
table for creating relationship
2) unique key in table where it
doesn't affect in table or in the whole database whether you take the
null for the particular column like snacks in the restaurant it is
possible you don't take snacks in a restaurant
edited Jul 24 '18 at 14:23
answered Jul 24 '18 at 9:52
ayushs27ayushs27
5718
5718
add a comment |
add a comment |
difference between Primary Key and Unique Key
Both Primary key
and Unique Key
is used to uniquely define of a row in a table.
Primary Key
creates a clustered index
of the column whereas a Unique creates an unclustered index of the column
.
A Primary Key
doesn’t allow NULL value
, however a Unique Key
does allow one NULL value
.
add a comment |
difference between Primary Key and Unique Key
Both Primary key
and Unique Key
is used to uniquely define of a row in a table.
Primary Key
creates a clustered index
of the column whereas a Unique creates an unclustered index of the column
.
A Primary Key
doesn’t allow NULL value
, however a Unique Key
does allow one NULL value
.
add a comment |
difference between Primary Key and Unique Key
Both Primary key
and Unique Key
is used to uniquely define of a row in a table.
Primary Key
creates a clustered index
of the column whereas a Unique creates an unclustered index of the column
.
A Primary Key
doesn’t allow NULL value
, however a Unique Key
does allow one NULL value
.
difference between Primary Key and Unique Key
Both Primary key
and Unique Key
is used to uniquely define of a row in a table.
Primary Key
creates a clustered index
of the column whereas a Unique creates an unclustered index of the column
.
A Primary Key
doesn’t allow NULL value
, however a Unique Key
does allow one NULL value
.
edited Nov 26 '18 at 13:09
answered Nov 26 '18 at 13:03
Gufran HasanGufran Hasan
3,67841628
3,67841628
add a comment |
add a comment |
protected by Community♦ Oct 27 '13 at 12:38
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
6
wrt null-ability a good way to distinguish b/w them is
PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
– KNU
Nov 24 '14 at 12:33
Take a look at dba.stackexchange.com/questions/15572/…. And stackoverflow.com/questions/1401572/…
– Lijo
Sep 14 '16 at 18:28