Académique Documents
Professionnel Documents
Culture Documents
EXPERIMENT - 4
Constraints :
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is aborted by the
constraint.
Constraints can be specified when the table is created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE statement).
Constrains:
UNIQUE - Ensures that each row for a column must have a unique value
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column
(or combination of two or more columns) have a unique identity which helps to find a
particular record in a table more easily and quickly
FOREIGN KEY - Ensure the referential integrity of the data in one table to match
values in another table
PRIMARY KEY
UNIQUE
FOREIGN KEY
1
15BCS1071
CHECK
PRIMARY Constraints : A primary key defines the set of columns that uniquely identifies rows
in a table.When you create a primary key constraint, none of the columns included in the primary
key can have NULL constraints i.e they must not permit NULL values.ALTER TABLE ADD
PRIMARY KEY allows you to include existing columns in a primary key if they were first
defined as NOT NULL.
Dropping Constraints:
Any constraint that you have defined can be dropped using the ALTER TABLE
command with the DROP CONSTRAINT option.
For example, to drop the primary key constraint in the EMPLOYEES table, you can use
the following command:
2
15BCS1071
UNIQUE Constraint : UNIQUE constraint is used to ensure that the each row for a column
have a different value. In this page we are going to discuss, how the SQL UNIQUE
CONSTRAINT works if it is used at the end of the CREATE TABLE statement instead of using
the UNIQUE CONSTRAINT in the specific columns.
3
15BCS1071
DEFAULT Constraint:
DEFAULT CONSTRAINT provides a default value when specified none for a column.
4
15BCS1071
FOREIGN KEY:This constraint identifies any column referencing the PRIMARY KEY in
another table. It establishes a relationship between two columns in the same table or between
different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary
Key in the table which it is referring. One or more columns can be defined as Foreign key.
5
15BCS1071
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain
conditions.
6
15BCS1071
NOT NULL Constraint : A Not null constraint restrict the insertion of null values into a column. If we are
using a Not Null Constraint for a column then we cannot ignore the value of this column during an insert of
data into the table.
7
15BCS1071
8
15BCS1071
EXPERIMENT 5
JOINTS :
The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN
is a means for combining fields from two tables by using values common to each.The easiest and
most intuitive way to explain the difference between these four types is by using a Venn diagram,
which shows all possible logical relations between data sets.
Inner
Outer
1. LEFT JOIN
2. RIGHT JOIN
3. FULL JOIN
INNER JOINT :
The INNER JOIN keyword selects all rows from both tables as long as there is a match between
the columns in both tables.
9
15BCS1071
SYNTAX :
SELECT column_name(s)
FROM table1
ON table1.column_name=table2.column_name;
OUTPUT :
10
15BCS1071
LEFT JOINT :
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in
the right table (table2). The result is NULL in the right side when there is no match.
SYNTAX :
SELECT column_name(s)
FROM table1
ON table1.column_name=table2.column_name;
OUTPUT :
11
15BCS1071
RIGHT JOIN :
Returns all rows from the right table, even if there are no matches in the left table.
SYNTAX :
SELECT column_name(s)
FROM table1
ON table1.column_name=table2.column_name;
OUTPUT :
12
15BCS1071
FULL JOINT :
The full join returns a result table with the matched data of two table then remaining rows of
both left table and then the right table.
SYNTAX :
SELECT column_name(s)
FROM table1
ON table1.column_name=table2.column_name;
OUTPUT :
13
15BCS1071
UNION :
The UNION operator is used to combine the result-set of two or more SELECT statements. The
UNION operator selects only distinct values by default. To allow duplicate values, use the ALL
keyword with UNION.
SYNTAX :
UNION
OUTPUT :
14