1. DATABASE MANAGEMENT SYSTEM -
JOINS & CONSTRAINTS
By
Ms.V.VENNILA,MCA.,M.Phil.,
Assistant Professor,
Bon Secours College for Women,Thanjavur.
2. SQL JOIN
A SQL join is a Structured Query Language (SQL) instruction to combine data from two sets of data (i.e.
two tables). Before we dive into the details of a SQL join, let’s briefly discuss what SQL is, and why
someone would want to perform a SQL join.
SQL is a special-purpose programming language designed for managing information in a relational
database management system (RDBMS).
Different Types of Joins
There are mainly four types of joins that you need to understand.
They are:
• INNER JOIN
• FULL JOIN
• LEFT JOIN
• RIGHT JOIN
3.
4. INNER JOIN
This type of join returns those records which have matching values in both
tables. So, if you perform an INNER join operation between the Employee
table and the Projects table, all the tuples which have matching values in
both the tables will be given as output.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
INNER JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
FULL JOIN
Full Join or the Full Outer Join returns all those records which either have
a match in the left(Table1) or the right(Table2) table.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
FULL JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
5. LEFT JOIN
The LEFT JOIN or the LEFT OUTER JOIN returns all the records
from the left table and also those records which satisfy a condition
from the right table. Also, for the records having no matching values in
the right table, the output or the result-set will contain the NULL
values.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
LEFT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
RIGHT JOIN
The RIGHT JOIN or the RIGHT OUTER JOIN returns all the
records from the right table and also those records which satisfy a
condition from the left table. Also, for the records having no
matching values in the left table, the output or the result-set will
contain the NULL values.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
RIGHT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
6. Cross Join
The CROSS JOIN is a type of join in which a join clause is applied to
each row of a table to every row of the other table. Also, when the
WHERE condition is used, this type of JOIN behaves as an INNER
JOIN, and when the WHERE condition is not present, it behaves like a
CARTESIAN product.
SYNTAX
SELECT *
FROM employee CROSS JOIN department;
Self Join
SELF JOIN in other words is a join of a table to itself. This implies that
each row in a table is joined with itself.
SYNTAX
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
7. Integrity Constraints
Integrity Constraints
•Integrity constraints are a set of rules. It is used to maintain the quality of
information.
•Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected.
•Thus, integrity constraint is used to guard against accidental damage to the
database.
9. some of the most commonly used constraints available in SQL
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
• FOREIGN KEY - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column when no value is specified
• INDEX - Used to create and retrieve data from the database very quickly
10. 1. Domain constraints
• Domain constraints can be defined as the definition of a valid set of values for an attribute.
• The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain.
Syntax
CHECK (search condition)
2. Entity integrity constraints
• The entity integrity constraint states that primary key value can't be null.
• This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
• A table can contain a null value other than the primary key field.
Syntax
PRIMRYKEY(attribute (,…))
11. 3. Referential Integrity Constraints
• A referential integrity constraint is specified between two tables.
• In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2,
then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
4. Key constraints
• Keys are the entity set that is used to identify an entity within its entity set uniquely.
• An entity set can have multiple keys, but out of which one key will be the primary key. A primary key can
contain a unique and null value in the relational table.
12. NOT NULL:
This constraint tells that we cannot store a null value in a column. That is, if a column is specified
as NOT NULL then we will not be able to store null in this particular column any more.
Example using NOT NULL constraint
CREATE TABLE Student(s_id int NOT NULL, Name varchar(60), Age int);
UNIQUE:
This constraint when specified with a column, tells that all the values in the column must be unique. That
is, the values in any row of a column must not be repeated.
Using UNIQUE constraint when creating a Table (Table Level)
CREATE TABLE Student(s_id int NOT NULL UNIQUE, Name varchar(60), Age int);
Using UNIQUE constraint after Table is created (Column Level)
ALTER TABLE Student ADD UNIQUE(s_id);
13. PRIMARY KEY:
A primary key is a field which can uniquely identify each row in a table. And this constraint is used to
specify a field in a table as primary key.
Using PRIMARY KEY constraint at Table Level
CREATE table Student (s_id int PRIMARY KEY, Name varchar(60) NOT NULL, Age int);
Using PRIMARY KEY constraint at Column Level
ALTER table Student ADD PRIMARY KEY (s_id);
CHECK:
This constraint helps to validate the values of a column to meet a particular condition. That is, it helps to
ensure that the value stored in a column meets a specific condition.
Using CHECK constraint at Table Level
CREATE table Student(
s_id int NOT NULL CHECK(s_id > 0),
Name varchar(60) NOT NULL,
Age int
);
Using CHECK constraint at Column Level
ALTER table Student ADD CHECK(s_id > 0);
14. FOREIGN KEY:
A Foreign key is a field which can uniquely identify each row in a another table. And this
constraint is used to specify a field as Foreign key.
Using FOREIGN KEY constraint at Table Level
CREATE table Order_Detail(
order_id int PRIMARY KEY,
order_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id)
);
Using FOREIGN KEY constraint at Column Level
ALTER table Order_Detail ADD FOREIGN KEY (c_id) REFERENCES Customer_Detail(c_id)