Identity Column, Primary Key and Foreign Key
Hello, and welcome to the blog. In this blog we are going to talk about "Identity columns, Primary Key and Foreign Key".
In the previous blog we have seen some constraints and I told you there are more coming
and here they are now. These constraints hold way too much powers as compare
to what we have learnt in previous blog. Believe me if you these have arrows
in your quiver, it is very easy for you to conquer the practices of
designing a fully functional database. So let's not create more hype about
this and start exploring theses things.
In this blog we are going to talk about
- Identity Column
- Primary Key
- Foreign Key
Let's start the discussion with
1) Identity Column:
- When you want to create a column whose value should get incremented every time you insert a new record then IDENTITY COLUMN is the thing you should be looking for.
- IDENTITY column is very helpful when we want to create a unique column. Because of identity column you don't have to worry about having unique values for a column.
- Now a days almost every time a person wants to create a PRIMARY KEY which is our next topic he uses Identity column as base for it and then apply Primary Key to that column.
- The keyword "Identity" that we are using can be different for different DBMS software. For example: For this feature AUTO-INCREMENT keyword is used in MySQL and IDENTITY keyword is used in Microsoft SQL Server.
- One more thing I would like to add is that you don't need to specify value for identity column while inserting a record. We will see an example to understand this better.
-
Now let's know the syntax for creating a a identity column:Syntax :IDENTITY(seed , increment_by)"seed" is the starting value that is the very first row which is inserted will hold that value and "increment_by" is the value by which the value of column is incremented every-time a new record is inserted
-
Example:CREATE TABLE [dbo].[Employee2]([Id] [int] IDENTITY(1,1),[FirstName] [varchar](50) NOT NULL,[LastName] [varchar](50) NULL,[Department] [varchar](50) NULL,[Salary] [decimal](18, 2) ,[Designation] [varchar](50)
-
Now "Id" column of Employee table is IDENTITY column and its value will be auto incremented
-
The last thing you must know is that you can't alter a existing column to identity column you must specify Identity column at the time of creating table.
- Now let's see how IDENTITY column affects insert statement.
-
insert into Employee2 values (1,'John','Doe','Sales',12000,'Trainee')
-
We got error saying "An explicit value for the identity column in table 'Employee2' can only be specified when a column list is used and IDENTITY_INSERT is ON." As I said you can't specify value for Identity column and we have specified value for Identity column so we got this error.
-
Let' see same query without specifying value for identity column
-
Let's add one more record to check if the value in Identity column gets incremented or not.
2) Primary Key
- It is very important to have a individuality as a person and it is same for database records as well.
- A database table must have a column or a combination of column that contains a value by which you can identify each and every record in the table.
- When a column value is used to uniquely identify each and every record then that column is a "PRIMARY KEY" Column.
- Data in Primary Key column is always unique and not null. Null values are not accepted in a primary key column.
- As data in primary key must be unique "IDENTITY" column is used as a base for "Primary Key" column. And this happens almost every time.
- When you define a column as a "Primary key" for that table, then database engine enforces data uniqueness by automatically creating unique index for Primary key column because of which access of data becomes faster when you use primary key column in your query.
- Note: Primary key cannot be applied to nullable column
-
Syntax for creating Primary keyCREATE TABLE [dbo].[Employee2]([Id] [int] IDENTITY(1,1) PRIMARY KEY,[FirstName] [varchar](50) NOT NULL,[LastName] [varchar](50) NULL,[Department] [varchar](50) NULL,[Salary] [decimal](18, 2) ,[Designation] [varchar](50))ORCREATE TABLE [dbo].[Employee2]([Id] [int] IDENTITY(1,1),[FirstName] [varchar](50) NOT NULL,[LastName] [varchar](50) NULL,[Department] [varchar](50) NULL,[Salary] [decimal](18, 2) ,[Designation] [varchar](50)[constraint PK_EmployeePrimary Key (Id)
-
You can add Primary Key constraint to an existing column. For that we have syntax like:ALTER TABLE EmployeeADD PRIMARY KEY(Id)orALTER TABLE EmployeeADD CONSTRAINT PK_Employee
-
Now let's finally see syntax for deleting primary key:ALTER TABLE Employee
here comes the final thing
3) Foreign Key
- FOREIGN KEY is the most important concept when we will talk about joins. normalization in future, foreign key plays a very important role.
- "Foreign key" is a column in your table which represents information or record of other table.
- "Foreign key" mainly refers to Primary Key in another table. It's basically a link between two or more tables.
- When you add foreign key constraint to a column, it also make sure that you are not inserting inappropriate data, which is not present in the referring table.
-
I have created a "Employee" table in which
"DepartmentId" is a foreign key then I can't add
"DepartmentId" which is not present in "Department" table.
For example I can't add DepartmentID=11 in "Employee"
table as I don't have any record in my "Department" table
with Id as 11.
-
Now let's see syntax for adding foreign key while creating table:CREATE TABLE [dbo].[Employee2]([Id] [int] IDENTITY(1,1) PRIMARY KEY,[FirstName] [varchar](50) NOT NULL,[LastName] [varchar](50) NULL,[DepartmentId] [int] FOREIGN KEY references DEPARTMENT(Id),[Salary] [decimal](18, 2) ,[Designation] [varchar](50))ORCREATE TABLE [dbo].[Employee2]([Id] [int] IDENTITY(1,1),[FirstName] [varchar](50) NOT NULL,[LastName] [varchar](50) NULL,[DepartmentId] [int],[Salary] [decimal](18, 2) ,[Designation] [varchar](50),constraint FK_Department FOREIGN KEY(DepartmentId)REFERENCES DEPARTMENT(Id)
-
Syntax for adding foreign key after creating tableALTER TABLE EmployeeADD constraint FK_DepartmentFOREIGN KEY(DepartmentId)
-
now, finally let's learn to delete a primary keyALTER TABLE Employee
Congratulation you have understood some of the most important topic
of SQL. See you in some another blog till then "Keep coding, keep learning because trust me you can make it
big".
"Have a great day"

Very helpful 🤘🏻
ReplyDeleteExcellent explaination sir 👍
ReplyDeleteExcellent explaination sir 👍
ReplyDeleteVery nicely explained. It was very much helpful to me. Keep making more.
ReplyDelete