Sei sulla pagina 1di 14

15BCS1071

EXPERIMENT - 4

AIM: IMPLEMENTATION OF CONSTRAINTS

OBJECTIVE : To study all SQL Constraints.

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:

NOT NULL - Indicates that a column cannot store NULL value

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

CHECK - Ensures that the value in a column meets a specific condition

DEFAULT - Specifies a default value for a column

Table constraints include:

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.

SYNTAX FOR COLUMN LEVEL :

column name datatype [CONSTRAINT constraint_name] PRIMARY KEY

SYNTAX FOR TABLE LEVEL :

[CONSTRAINT constraint_name] PRIMARY KEY (column_name1,column_name2,..)

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.

SYNTAX FOR COLUMN LEVEL :

column name datatype [CONSTRAINT constraint_name] UNIQUE KEY

SYNTAX FOR TABLE LEVEL :

[CONSTRAINT constraint_name] UNIQUE KEY(column_name)

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.

SYNTAX FOR COLUMN LEVEL :

[CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name)

SYNTAX FOR TABLE LEVEL :

5
15BCS1071

[CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES


referenced_table_name(column_name);

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

AIM : Implementation of Different Joints and Union.

OBJECTIVE : To Study and implement different joints and Union.

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.

There are four basic types of SQL joins:

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

INNER JOIN table2

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

LEFT JOIN table2

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

RIGHT JOIN table2

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

FULL JOIN table2

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 :

SELECT column_name(s) FROM table1

UNION

SELECT column_name(s) FROM table2;

OUTPUT :

14

Potrebbero piacerti anche