Constraints in SQL
Hello, and welcome to the blog. In this blog we are going to talk about "Constraints in SQL".
We have learn CRUD operations so far, now its time to advance our
skills. We will again create a table with some properties and make our
database table more reliable and effective. For making our database table
more reliable and effective we are going to add some "Constraints" in our
table.
So in this blog we are going to talk about
- What is constraints ?
- Types Of Constraint.
- Create a table with constraints.
1) What is Constraint?
- Constraints are nothing but rules which a data row must satisfy to get inserted in database.
- For example: When a person wants to take flight he/she have to go through many checks like security, passport, boarding pass, etc. these checks are there to ensure only valid person can enter flight and flight will be safe from intruders.
- The same way constraints are there to make sure only valid data gets inserted into the table.
- Constraints also ensures reliability and accuracy of data in database table.
- If there is any violation of constraint the whole action of inserting or updating data is aborted.
2) Types Of Constraint.
1) NOT NULL :
- This constraint ensures that value in the column is not null.
- If you want a particular column in the table should always have a value then apply NOT NULL constraint to it.
2) UNIQUE :
- As the name suggests this constraint is use to ensure unique values in a column.
- This constraints make sure that a value is not repeated. To avoid duplicate value if you have to add null value in that column that is okay.
3) CHECK :
- It is used to apply specific condition to a column which data should satisfy in order get inserted in database table. This you will understand better in example.
4) DEFAULT :
- Use to specify default value for a column
- If you don't provide value for a column while inserting or updating data then the value you have specified with default constraint get inserted into table.
5) INDEX :
- Index is used to make data retrieval faster. The way book index help us to go to desired page the same way we have INDEX in SQL.
-
Suppose we have a query:SELECT FirstNameFROM EmployeeWHERE EmployeeId=1
- Then your DBMS will read all rows in the table and extracts the rows that meet the criteria of the query.
- Checking each row of table need a lot of time.
- When we use INDEX, it searches the INDEX key column , finds the storage location of row needed by the query and extract the matching row from location.
3) Create a table with constraints
CREATE TABLE [dbo].[Employee1](
[Id] [int] UNIQUE ,
[FirstName] [varchar](50) NOT
NULL,
[LastName] [varchar](50) NULL,
[Department] [varchar](50) NULL,
[Salary] [decimal](18, 2) check
(salary>10000),
[Designation] [varchar](50)
Default 'Trainee'
)
Now our table is ready, let's try to insert some records
-
insert into Employee1 values (1,null,'Doe','Sales',12000,'Trainee')
-
As we are trying to enter a null value in FirstName column we are getting error "Cannot insert the vaalue NULL into column 'FirstName', table 'EmployeeManagementSystem.dbo.Employee1'; column does not allow nulls. INSERT fails."
-
As we have specify SALARY as 2000 which less than 10000 we are getting error "The INSERT statement conflicted with the CHECK constraint "CK__Employee1__Salar__5CD6CB2B". The conflict occurred in database "EmployeeManagementSystem", table "dbo.Employee1", column 'Salary'."
-
As we have specified DEFAULT VALUE for DESIGNATION the record got inserted without any error and with default value as 'Trainee'.
- As we have already inserted a record with Id as 1 we are getting this error "Violation of UNIQUE KEY constraint 'UQ__Employee__3214EC06A8EC7CF2'. Cannot insert duplicate key in object 'dbo.Employee1'. The duplicate key value is (1)."
- So all the constraint we enforced on our table are working fine.
Congratulation on understanding constraints. See you in some another
blog till then "Keep coding, keep learning because trust me you can make it big".
"Have a great day"

Nice Example to relate ,Thanks
ReplyDelete