This document discusses SQLite constraints and joins. It provides an overview of different types of constraints like NOT NULL, PRIMARY KEY, UNIQUE, CHECK, and FOREIGN KEY that can be applied at the column or table level. It also explains different types of joins in SQLite like INNER JOIN, LEFT OUTER JOIN, CROSS JOIN, and NATURAL JOIN with examples and uses the concept of sets to describe joins. The document is intended to teach about constraints and joins in SQLite through examples and explanations.
4. www.inforepos.com
SQLite Constraints
• CONSTRAINTS are an integrity which defines some conditions that restrict the column to
contain the true data while inserting or updating or deleting. We can use two types of
constraints, that is column level or table level constraint. The column level constraints can
be applied only on a specific column where as table level constraints can be applied to the
whole table.
Column constraints:
• NOT NULL
• PRIMARY KEY (COMPOSITE PRIMARY KEY)
• UNIQUE
• CHECK
• FOREIGN KEY (COMPOSITE FOREIGN KEY)
Constraint Action Clauses
• ON DELETE
• NO ACTION
• CASCADE
• RESTRICT
6. www.inforepos.com
SQLite JOINS
• INNER JOIN
• INNER JOIN selects all rows from both participating tables to appear in the result if and only if both tables meet the
conditions specified in the ON clause.
• LEFT OUTER JOIN
• Left Join or Left Outer Join operation takes two relations, A and B, and returns the inner join of A and B along with the
unmatched rows of A. A is the first relation defined in the FROM clause and is hence the left relation. The left join
includes the unmatched rows of the left relation along with the matched columns in the result.
• CROSS JOIN
• When the two tables have no relation in any way, SELECT would produce a more fundamental kind of join , which is
called a cross join or Cartesian join. CROSS JOIN produces a result set which is the product of rows of two associated
tables when no WHERE clause is used with CROSS JOIN.
• NATURAL JOIN
• NATURAL JOIN is such a join that performs the same task as an INNER or LEFT JOIN, in which the ON or USING clause
refers to all columns that the tables to be joined have in common. A natural join joins two tables by their common
column names. Thus, using the natural join you can get the inner join without having to add the join condition.
8. www.inforepos.com
JOINS
SQLite INNER JOIN
• INNER JOIN
• There are three types of INNER JOINS:
• INNER JOIN, NATURAL INNER JOIN, and CROSS INNER JOIN. The INNER keyword can be omitted.
SELECT result
FROM table1
INNER JOIN table2
ON table1.keyfield1 = table2.keyfield2
[WHERE expr]
9. www.inforepos.com
JOINS
SQLite INNER JOIN
INNER JOIN
Doctors Table
doctor_id doctor_name degree
---------- -------------- ----------
210 Dr. John Linga MD
211 Dr. Peter Hall MBBS
212 Dr. Ke Gee MD
213 Dr. Pat Fay MD
Doctor Visits Table
doctor_id patient_name v_date
---------- ------------ ----------
210 Julia Nayer 2013-10-15
214 TJ Olson 2013-10-14
215 John Seo 2013-10-15
212 James Marlow 2013-10-16
212 Jason Mallin 2013-10-12
SELECT
doctors.doctor_id, doctors.doctor_name, visits.patient_name
FROM doctors
INNER JOIN visits
ON doctors.doctor_id = visits.doctor_id
WHERE doctors.degree = 'MD';
Output:
doctor_id doctor_name patient_name
---------- -------------- ------------
210 Dr. John Linga Julia Nayer
212 Dr. Ke Gee James Marlow
212 Dr. Ke Gee Jason Mallin
10. www.inforepos.com
JOINS
SQLite INNER JOIN
• INNER JOIN Explanation
SELECT a.doctor_id,a.doctor_name,b.spl_descrip
FROM doctors a
INNER JOIN speciality b
ON a.doctor_id=b.doctor_id;
11. www.inforepos.com
JOINS
SQLite INNER JOIN
• INNER JOIN Explanation
SELECT a.doctor_id,a.doctor_name,
b.spl_descrip,c.patient_name,c.vdate
FROM doctors a
INNER JOIN speciality b
ON a.doctor_id=b.doctor_id
INNER JOIN visits c
ON a.doctor_id=c.doctor_id;
12. www.inforepos.com
JOINS
SQLite INNER JOIN
• INNER JOIN Explanation
SELECT a.doctor_id,a.doctor_name,
b.spl_descrip,c.patient_name,c.vdate
FROM doctors a
INNER JOIN speciality b
ON a.doctor_id=b.doctor_id
INNER JOIN visits c
ON a.doctor_id=c.doctor_id
WHERE a.doctor_id=212 AND c.vdate='2013-10-16';
13. www.inforepos.com
JOINS
SQLite LEFT JOIN or LEFT OUTER JOIN
• LEFT OUTER JOIN
SELECT result
FROM table1
LEFT [OUTER] JOIN table2
ON table1.keyfield1 = table2.keyfield2
[WHERE expr]
14. www.inforepos.com
JOINS
SQLite LEFT JOIN or LEFT OUTER JOIN
Output:
doctor_id doctor_name patient_name v_date
---------- -------------- ------------ ----------
210 Dr. John Linga Julia Nayer 2013-10-15
211 Dr. Peter Hall
212 Dr. Ke Gee James Marlow 2013-10-16
212 Dr. Ke Gee Jason Mallin 2013-10-12
213 Dr. Pat Fay
To find which doctor attended which patient and the date for attend
SELECT a.doctor_id,a.doctor_name,
c.patient_name,c.vdate
FROM doctors a
LEFT JOIN visits c
ON a.doctor_id=c.doctor_id;
LEFT OUTER JOIN
Doctors Table
doctor_id doctor_name degree
---------- -------------- ----------
210 Dr. John Linga MD
211 Dr. Peter Hall MBBS
212 Dr. Ke Gee MD
213 Dr. Pat Fay MD
Doctor Visits Table
doctor_id patient_name v_date
---------- ------------ ----------
210 Julia Nayer 2013-10-15
214 TJ Olson 2013-10-14
215 John Seo 2013-10-15
212 James Marlow 2013-10-16
212 Jason Mallin 2013-10-12
16. www.inforepos.com
JOINS
SQLite LEFT JOIN or LEFT OUTER JOIN
• LEFT JOIN with USING
SELECT doctor_id,doctor_name,
patient_name,vdate
FROM doctors
LEFT JOIN visits
USING(doctor_id);
doctor_id doctor_name patient_name vdate
---------- -------------- ------------ ----------
210 Dr. John Linga Julia Nayer 2013-10-15
211 Dr. Peter Hall
212 Dr. Ke Gee James Marlow 2013-10-16
212 Dr. Ke Gee Jason Mallin 2013-10-12
213 Dr. Pat Fay
• NATURAL LEFT OUTER JOIN
SELECT doctor_id,doctor_name,
patient_name,vdate
FROM doctors
NATURAL LEFT OUTER JOIN visits;
doctor_id doctor_name patient_name vdate
---------- -------------- ------------ ----------
210 Dr. John Linga Julia Nayer 2013-10-15
211 Dr. Peter Hall
212 Dr. Ke Gee James Marlow 2013-10-16
212 Dr. Ke Gee Jason Mallin 2013-10-12
213 Dr. Pat Fay