SQL - Unique Key, Primary Key & Foreign Key - sql

What are the differences between Unique Key, Primary Key and Foreign Key with respect to concept of SQL?
How they are different from each other?

A PRIMARY Key and UNIQUE Key constraints both are similar and it provide unique enforce uniqueness of the column on which they are defined.
Primary Key
Primary key cannot have a NULL value.
Each table can have only one primary key.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Primary key can be related with another table's as a Foreign Key.
We can generated ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.
Unique Key
Unique Constraint may have a NULL value.
Each table can have more than one Unique Constraint.
By default, Unique key is a unique non-clustered index.
Unique Constraint can not be related with another table's as a Foreign Key.
Unique Constraint doesn't supports Auto Increment value.
Foreign Key
Foreign key is a field in the table that is primary key in another table.
Foreign key can accept multiple null value.
Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
We can have more than one foreign key in a table.
There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table. What's the advantage? If you are selecting the parent plus all child records, you want the child records next to each other. This is easy to accomplish using a clustered index.
Having a null foreign key is usually a bad idea. In the example below, the record in [dbo].[child] is what would be referred to as an "orphan record". Think long and hard before doing this.

Note: we use constraint for enforce data integrity
Primary Key
1)can't insert null value
2) one table have one primary key
Unique key
1) insert null value one at time
2)one table have multiple unique key
3) you can refereed as candidate key also
foreign key
1) maintain the relationship between two table and also multiple
Note: without any constraint you get data in multiple table but you can not get data peoperly

A note about Unique key
The parent table in a Primary Key-Foreign Key relation is normally called as Primary Key table but PK is not mandatory in a parent table. A unique key/constraint in parent table is sufficient. As PK is always unique, it is often used as foreign key in another table. see this SO post

Related

Why we need a primary key?

I am reading about primary keys and at a lot of tutorials, technical blogs etc., and I found that a primary key cannot be null. I think it's totally wrong because I was able to insert null value in the column.
I suppose a primary key can have a not null value only if the column is declared as not null. But again this is not a feature of primary keys.
My question is why do we have a concept of primary key because I find only one difference between primary key and unique key is that "Primary key can be declared only on one column whereas unique key can be declared on multiple columns". So my understanding is that why can't we also declare the primary key as a unique key if we don't have any other difference.
I suppose a primary key can have a not null value only if the column
is declared as not null.But again this is not a feature of primary
key.
Primary key can't have a null values. By definition of primary key, it is UNIQUE and NOT NULL.
My another question is that why do we have a concept of primary key
because I find only one difference between primary key and unique key
is that "Primary key can be declared only on one column whereas unique
key can be declared on multiple columns"
This is completely wrong. You can create primary key on multiple columns also, the difference between Primary Key and Unique Key is Primary Key is not null and Unique key can have null values.
The main purpose of primary key is to identify the uniqueness of a row, where as unique key is to prevent the duplicates, following are the main difference between primary key and unique key.
Primary Key :
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 Key :
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.
I suggest you read this primary key and unique key
You forgot Indexing. When it comes to large data to find particular data raw it need to travel through memory record by record. To overcome that the concept of indexing is there. Primary key helps in this. So it will help to get your data access faster. After that there is concept of binary search which will helps further in that task.
A primary key is a special relational database table column (or combination of columns) designated to uniquely identify all table records.
A primary key’s main features are:
It must contain a unique value for each row of data.
It cannot contain null values.
A primary key is either an existing table column or a column that is specifically generated by the database according to a defined sequence.
The primary key concept is critical to an efficient relational database. Without the primary key and closely related foreign key concepts, relational databases would not work.
A primary key, also called a primary keyword, is a key in a relational database that is unique for each record.
One Table Have Only One Primary Key.

Can we set foreign key on a unique column that contains null?

What I learned was we can set a foreign key on a unique constrained column, and unique constraint will allow one null (correct me if I am wrong).
My question is that in oracle can we create a foreign key on a column referred on unique constraint column which is in another table and has null?
If yes, how is that possible?
can we create a foreign key based on unique constraint which has null ?
The syntax allows us to create a foreign key which references a unique key. However, I think it would be if not exactly bad practice then at least peculiar practice to do so. Primary keys are the norm.
If yes, how is that possible?
It's possible because a foreign key column can itself be optional. A foreign key constrains us to entering a value in the child column which is present in the referenced primary key column. However, if we put a null in the child column then the foreign key is not enforced. This is true whether the foreign key references a primary key or a unique key.
Obviously, if the child column is mandatory (defined as NOT NULL) then we cannot put a null in it, and it really doesn't matter whether the referenced column is a unique key or a primary key.

The columns in table do not match an existing primary key or UNIQUE constraint

How does one work around the issue of not matching an existig primary key or unique constraint when creating a FK in SSMS?
I have two tables namely : user and firstdb where both fields have the same datatype. In user table I have two primary keys, and in the foreign key table I have one field which I want to make it the target of a foreign key.
I tried to have one field as a primary key in the user table, the relationship worked fine at that time but once I set the second field to primary it forces me to delete the existing relationship for some reason.
I tried to create a relationship like this in MySQL, it worked fine with me, but it doesn't work.
You seem to have a profound misunderstand of what a primary key is. A primary key (singular) is one or more columns that uniquely identify each row (and none of the columns are NULL). When you click "primary key" by two columns, you are generating a composite primary key.
Foreign key references need to be to the complete primary key. If you have a composite primary key, you need a composite foreign key.
Having single column foreign key references is one reason why the best type of primary key is an identity column. I am guessing that userid is such a column.
If you want uEmailId to be unique and non-NULL, then define it as unique and non-NULL. However, don't add it to the primary key!

Unique key related problem in SQL Server

I am very confused in difference between unique key and primary key, and unique key constraint and primary key constraint in SQL Server.
Can I define more than 1 primary key in my table?
Again I am not getting how to set unique key in my table and what is the practical use of it. Second thing, I found statement like this on internet
A PRIMARY KEY constraint automatically has a UNIQUE constraint
defined on it.
Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Does it mean that when I create primary key, it automatically define the unique key on that, that is I can have only 1 null value inserted in it?
your primary key is always unique. you can add additional keys and the may or may not be unique. If i am not wrong, the primary key decides how the records are actually stored on the disk.
You cannot have more than one primary key on a table, but you can have as many (within reasonable technical limits) unique constraints on a table.
The primary key cannot contain nulls, a unique constraint does allow nulls to occur, but treats NULLs as values in this case (so for a single column constraint, only one null value can occur).
Both Primary Keys and Unique constraints may be the referenced key for a foreign key constraint.
Many relational purists believe that the introduction of "Primary Key" in SQL was a mistake - there's no real reason to select one key as "better" than any other key - if there are genuinely multiple keys for the table. You could abandon primary keys entirely and only use UNIQUE constraints, and treat all keys equally.
Ok:
you can have only one primary key for each table
the primary key is by definition unique and does not allow NULL values
On the other hand:
you can define as many unique indices as you like
The primary key is a very special thing - it defines the way to clearly and uniquely identify each row. Therefore it must be unique, and it cannot be NULL.
The unique index is just that - a mechanism to ensure something (a column, several columns toegther) are unique. You can have multiple of those constraints on a table.
Each unique index could be your primary key - it's a so-called candidate key. But only one of those can be picked and chosen to actually be your primary key.

can we have a foreign key which is not a primary key in any other table?

it is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table
Yes - you can have a foreign key that references a unique index in another table.
CREATE UNIQUE INDEX UX01_YourTable ON dbo.YourTable(SomeUniqueColumn)
ALTER TABLE dbo.YourChildTable
ADD CONSTRAINT FK_ChildTable_Table
FOREIGN KEY(YourFKColumn) REFERENCES dbo.YourTable(SomeUniqueColumn)
By definition a foreign key must reference a candidate key of some table. It doesn't necessarily have to be the primary key.
As a matter of detail the constraint called a FOREIGN KEY in SQL isn't exactly equivalent to the textbook definition of a foreign key in the relational model. SQL's FOREIGN KEY constraint differs because:
it can reference any set of columns subject to a uniqueness constraint even if they are not candidate keys (superkeys or nullable columns for example).
it may include nulls, in which case the constraint is not enforced
its syntax depends on column order, so a fk constraint on (A,B) referencing (A,B) is different to a constraint on (B,A) referencing (A,B).
Yes , There can be a foreign key which is unique key in other table as Unique key is subset of primary key but not the exact primary key.
So that's possible that foreign key is unique key in aother table.
General standard answer is no. It is only possible if foreign key refers any column uniquely in other table. That means foreign key must be the candidate key in other table and primary key is also a candidate key the table.