Ads Here

Wednesday 17 January 2018

Oracle SQL - Constraint

We have already learned about creating a table and we have also learned different types of operation we can perform with a table. Now, after creating a table we can insert data into the table. But, before entering data into a table we have to ensure that the data user entering into a specific column of a table is a valid data. Now, in oracle database setting up this validation is known as Constraints.
Now, we have to remember some basic about constraints -
1) It is a rule to which data must conform.
2) We can also set name for a constraint if we want. Now, column level constraint is only work with a specific column whereas a table constraint can be applicable to multiple columns.
3) We can add constraints at table level as well as column level also.
4) We can enable/disable/drop constraints as it required.
Now, let's see what are available constraints we can use -
1. NOT NULL Constraint:
NOT NULL constraint is used for ensure that a column does not accept a null value. That means the user have to enter a value to that specific column where we set a NOT NULL constraint. For example, we have a employee detail table with First_Name column and we want that the first name column will not be null for any employee. So, in this case we can set a NOT NULL constraint.
2. UNIQUE Constraint:
Now, UNIQUE constraint used for ensure uniqueness of each record for a specific column. That means when we set unique constraint for a column, it will except a unique value for each row of that column. For example, we have employee detail table with a column Mobile No. & it must be unique for each member. So, in this case we can set a unique constraint for Mobile no.
3. PRIMARY KEY Constraint:
PRIMARY KEY is like combination of NOT NULL & UNIQUE constraint. That means if you set a column as PRIMARY KEY, then the column will not accept null value & each value have to be unique. And remember that we can use only one primary key in one table. Now, suppose we have a table with employee ID column. Now, ID for each employee must be unique and cannot be null. So, for that we can set a primary key on the ID column.
4. CHECK Constraint:
Now, CHECK constraint is used for ensure that the data user entering into a specific column is satisfied the specific conditions you can set on a column. For example, we have a salary column and it except numbers only. But, someone can enter negative numbers also which is also accepted but it is not a actual value of Salary. So, using check we can set some specific conditions for entering data.
5. FOREIGN KEY Constraint:
Now, FOREIGN KEY is used for reference integration between two or more tables. For example we have a column  in one table & we also have the same column in another table. So, for connect them we can use the Foreign Key constraint.
So, this are the common constraints we can use. Now,  this is for now and in upcoming posts we will discuss this all constraints in more detail. If anyone have any question & face any problem then please comment in this post, thank you.


No comments:

Post a Comment