2 A
2 A
Constraints in SQL means we are applying certain conditions or restrictions on the database.
This further means that before inserting data into the database, we are checking for some
conditions. If the condition we have applied to the database holds true for the data which is to be
inserted, then only the data will be inserted into the database tables.
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
7. CREATE INDEX
1. NOT NULL
NOTE: NULL does not mean zero. NULL means empty column, not even zero.
1.
Example:
Create a student table and apply a NOT NULL constraint on one of the table's column while
creating a table.
Ans:CREATE TABLE student(StudentID INT NOT NULL, Student_FirstName VARCHAR(20
), Student_LastName VARCHAR(20), Student_PhoneNumber VARCHAR(20), Student_
Email_ID VARCHAR(40));
To verify that the not null constraint is applied to the table's column and the student table is
created successfully, we will execute the following query:
2. UNIQUE
Duplicate values are not allowed in the columns to which the UNIQUE constraint is
applied.
The column with the unique constraint will always contain a unique value.
This constraint can be applied to one or more than one column of a table, which means
more than one unique constraint can exist on a single table.
Using the UNIQUE constraint, you can also modify the already created tables.
1.
Example:
Create a student table and apply a UNIQUE constraint on one of the table's column while
creating a table.
1.
To verify that the unique constraint is applied to the table's column and the student table is
created successfully, we will execute the following query:
1.
Example:
Create a student table and apply a UNIQUE constraint on more than one table's column while
creating a table.
1.
To verify that the unique constraint is applied to more than one table's column and the student
table is created successfully, we will execute the following query:
Example:
Consider we have an existing table student, without any constraints applied to it. Later, we
decided to apply a UNIQUE constraint to one of the table's column. Then we will execute the
following query:
3. PRIMARY KEY
1.
Example:
Create a student table and apply the PRIMARY KEY constraint while creating a table.
1.
To verify that the primary key constraint is applied to the table's column and the student table is
created successfully, we will execute the following query:
Example:
Consider we have an existing table student, without any constraints applied to it. Later, we
decided to apply the PRIMARY KEY constraint to the table's column. Then we will execute the
following query:
4. FOREIGN KEY
1.
Example:
Create an employee table and apply the FOREIGN KEY constraint while creating a table.
To create a foreign key on any table, first, we need to create a primary key on a table.
1.
mysql> CREATE TABLE employee (Emp_ID INT NOT NULL PRIMARY KEY, Emp_
Name VARCHAR (40), Emp_Salary VARCHAR (40));
To verify that the primary key constraint is applied to the employee table's column, we will
execute the following query:
Now, we will write a query to apply a foreign key on the department table referring to the
primary key of the employee table, i.e., Emp_ID.
1.
mysql> CREATE TABLE department(Dept_ID INT NOT NULL PRIMARY KEY, Dept
_Name VARCHAR(40), Emp_ID INT NOT NULL, FOREIGN KEY(Emp_ID) REFERE
NCES employee(Emp_ID));
To verify that the foreign key constraint is applied to the department table's column, we will
execute the following query:
1.
Example:
Create an employee table and apply the FOREIGN KEY constraint with a constraint name while
creating a table.
To create a foreign key on any table, first, we need to create a primary key on a table.
1.
mysql> CREATE TABLE employee (Emp_ID INT NOT NULL PRIMARY KEY, Emp_
Name VARCHAR (40), Emp_Salary VARCHAR (40));
To verify that the primary key constraint is applied to the student table's column, we will execute
the following query:
1.
mysql> CREATE TABLE department(Dept_ID INT NOT NULL PRIMARY KEY, Dept
_Name VARCHAR(40), Emp_ID INT NOT NULL, CONSTRAINT emp_id_fk FOREI
GN KEY(Emp_ID) REFERENCES employee(Emp_ID));
To verify that the foreign key constraint is applied to the department table's column, we will
execute the following query:
Example:
Consider we have an existing table employee and department. Later, we decided to apply a
FOREIGN KEY constraint to the department table's column. Then we will execute the following
query:
1.
To verify that the foreign key constraint is applied to the department table's column, we will
execute the following query:
Whenever a check constraint is applied to the table's column, and the user wants to insert
the value in it, then the value will first be checked for certain conditions before inserting
the value into that column.
For example: if we have an age column in a table, then the user will insert any value of
his choice. The user will also enter even a negative value or any other invalid value. But,
if the user has applied check constraint on the age column with the condition age greater
than 18. Then in such cases, even if a user tries to insert an invalid value such as zero or
any other value less than 18, then the age column will not accept that value and will not
allow the user to insert it due to the application of check constraint on the age column.
1.
Example:
Create a student table and apply CHECK constraint to check for the age less than or equal to 15
while creating a table.
1.
1.
Example:
Create a student table and apply CHECK constraint to check for the age less than or equal to 15
and a percentage greater than 85 while creating a table.
1.
Example:
Consider we have an existing table student. Later, we decided to apply the CHECK constraint on
the student table's column. Then we will execute the following query:
6. DEFAULT
Whenever a default constraint is applied to the table's column, and the user has not specified the
value to be inserted in it, then the default value which was specified while applying the default
constraint will be inserted into that particular column.
1.
Example:
Create a student table and apply the default constraint while creating a table.
1.
Example:
Consider we have an existing table student. Later, we decided to apply the DEFAULT constraint
on the student table's column. Then we will execute the following query:
1.
To verify that the default constraint is applied to the student table's column, we will execute the
following query:
1. mysql> DESC student;
7. CREATE INDEX
CREATE INDEX constraint is used to create an index on the table. Indexes are not visible to the
user, but they help the user to speed up the searching speed or retrieval of data from the
database.
Example:
Create an index on the student table and apply the default constraint while creating a table.
To verify that the create index constraint is applied to the student table's column, we will execute
the following query:
1.
Example:
1.
To verify that the create index constraint is applied to the student table's column, we will execute
the following query:
Consider we have an existing table student. Later, we decided to apply the DEFAULT constraint
on the student table's column. Then we will execute the following query:
To verify that the create index constraint is applied to the student table's column, we will execute
the following query: