3. www.SunilOS.com 3
Sales System Tables
Order
id date part_id qty
1 2/12/2006 1 100
2 3/15/2006 2 200
3 3/15/2006 3 100
4 4/5/2006 2 300
5 4/15/2006 3 200
6 6/15/2006 1 400
7 8/1/2006 1 100
Part
id name color unit_id
1 Nut Grey 2
2 Bolt Grey 3
3 Screw Silver 2
Unit
id city Capacity
1 New York 1000
2 London 2000
3 Paris 3000
Primary Key
Foreign Key
Foreign Key
Primary
Key
4. www.SunilOS.com 4
SQL Statements
DDL data definition language
o Statement for defining tables
Create & Alter Tables
o Statement for deleting tables
Drop table
DML data manipulation language
o Statement for Queries
SELECT * FROM part;
o Statement for Inserting and Updating data
INSERT into part VALUES(4,'plat','Green',1);
UPDATE part SET color = 'Green', unit_id = 1 where id=4;
o Statement for deleting rows
DELETE FROM part WHERE id=4;
DCL – Data Control Language
o Commit : Saves data changes
o Rollback : Reverts data changes
o Savepoint : transaction demarcation.
6. www.SunilOS.com 6
DML Statements
Statement to insert all columns into part table.
INSERT INTO part VALUES (4,'plat','Green',1);
Statement to insert id and name columns into part table.
INSERT INTO part (id,name) VALUES (4,'plat');
Statement to update color and unit_id into part table.
UPDATE part SET color = 'Green', unit_id = 1 WHERE id=4;
Statement to delete record from part table.
DELETE FROM part WHERE id=4;
7. www.SunilOS.com 7
DML - Select
Get all parts
o SELECT * FROM part;
Get all parts’ ids, names and colors
o SELECT id, name, color FROM part;
Get all grey color parts
o SELECT * FROM part WHERE color = ‘Grey’
Get all parts sorted by name
o SELECT * FROM part ORDER BY name
Get all parts those name starts with “N”
o SELECT * FROM part WHERE name LIKE ‘N%’
8. Pagination
Get parts record number 11 to 20 sorted by name
o SELECT * FROM part LIMIT 10,10 ORDER BY name
www.SunilOS.com 8
9. www.SunilOS.com 9
DML – Aggregate Functions
How many parts are there?
o SELECT count(*) from part;
o SELECT count(id) from part;
How many parts have been sold?
o SELECT sum(qty) from order
Which is the biggest deal so far?
o SELECT max(qty) from order;
Which is the minimum deal so far?
o SELECT min(qty) from order;
10. www.SunilOS.com 10
Joins
Get parts with their cities of units.
o Columns :part.id, name, color, unit.city
o Tables :part & unit
o Condition :part.unit_id = unit.id;
SELECT part.id, name, color, unit.city FROM part, unit
WHERE part.unit_id = unit.id;
11. www.SunilOS.com 11
Aliases
SELECT p.id PartID, name, color, u.city FROM part p,
unit u WHERE p.unit_id = u.id
Or
SELECT p.id as PartID, name, color, u.city FROM part as
p, unit as u WHERE p.unit_id = u.id
12. SQL IN/BETWEEN
Get the list of all Silver and Grey parts.
o SELECT * FROM part WHERE color IN ('Grey','Silver')
Get orders those ordered quantities are between 100 to 200.
o SELECT * from orders WHERE qty BETWEEN 100 AND 200
Get part counts for each color.
o SELECT color, count (*) part FROM part GROUP BY color
www.SunilOS.com 12
13. www.SunilOS.com 13
Nested Query
A Query can be nested in another query.
Get part ids those are ordered more than 100.
o SELECT part_id FROM orders WHERE qty > 100
Get part names those are ordered more than 100.
o SELECT name FROM part
o WHERE id IN
o ( SELECT part_id FROM orders WHERE qty > 100)
15. Joins
www.SunilOS.com 15
SELECT * from TableA A
LEFT JOIN TableB B
ON A.Key = B.Key
SELECT * from TableA A
RIGHT JOIN TableB B
ON A.Key = B.Key
SELECT * from TableA A
OUTER JOIN TableB B
ON A.Key = B.Key
SELECT * from TableA A
INNER JOIN TableB B
ON A.Key = B.Key
16. Database elements
Index
o Pointer to records. Makes searching faster.
o One table may have multiple indexes.
o Index can be created on single or multiple columns.
Views
o Logical Tables.
Triggers
o Before or After of INSERT/UPDATE/DELETE.
Stored Procedure
o Do not return a value. Like a void method.
Stored Function
o Returns a value.
www.SunilOS.com 16
17. Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 17