Professional Documents
Culture Documents
CS342
Creating the Database
Designing the Database
Conceptual Design
Logical Design / Normalization
Physical Design
2
Creating a Table
Column Definitions
Datatypes
Nullability
Default
Primary key
Unique
Check Constraints
Foreign Key
INDEX
3
Creating Tables - Example
Creating a Table and Constraints
4
SQL Statement
DDL
CREATE
ALTER
DROP
DML
INSERT
SELECT
UPDATE
DELETE
5
The INSERT
Statement
Examples
specifying both the column names and the values to be inserted
7
The SELECT
Statement
SELECT Statement
Basic Syntax
9
Get some useful info
10
JOINs
A JOIN clause is used to combine rows from one or
more tables, based on a related column
The different types of the JOINs in SQL are:
(INNER) JOIN: Returns records that have matching values in
both tables
LEFT (OUTER) JOIN: Return all records from the left table, and
the matched records from the right table
RIGHT (OUTER) JOIN: Return all records from the right table,
and the matched records from the left table
FULL (OUTER) JOIN: Return all records when there is a match
in either left or right table
CROSS JOIN: Matches each record from the right table with
each record from the left table
This is the Cartesian Product
11
INNER JOIN - Syntax
Option 1
SELECT column-name-list
FROM table_1 , table_2
WHERE table_1.column_A = table_2.column_B
Option 2
SELECT column-name-list
FROM table_1 INNER JOIN table_2
ON table_1.column_A = table_2.column_B
12
SELF JOIN - Example
Employee
EmpID Fname Lname DepID SupID
1 Taye Mekonnen 1 NULL
2 Sofia Teferi 1 1
3 Genet Abebe 2 2 Employee Name Supervisor
4 Daniel Abera 2 3 Sofia Teferi Taye Mekonnen
5 Hanna Samuel 3 1 Genet Abebe Sofia Teferi
6 Kaleb Kassa 3 5 Daniel Abera Genet Abebe
7 Solomon Michael 3 5 Hanna Samuel Taye Mekonnen
8 Beza Seifu 2 3 Kaleb Kassa Hanna Samuel
9 Michael Yossef 3 5 Solomon Michael Hanna Samuel
10 Hanna Natnael 3 5 Beza Seifu Genet Abebe
Michael Yossef Hanna Samuel
Hanna Natnael Hanna Samuel
13
Understanding GROUP BY
EmpID FName LName DepName Salary
1 Taye Mekonnen Administration 25,000.00
2 Sofia Teferi Research 18,000.00
3 Genet Abebe Administration 20,000.00
4 Daniel Abera Sales 12,000.00
5 Hanna Samuel Sales 15,000.00
6 Kaleb Kassa Sales 5,400.00
7 Solomon Michael Sales 5,000.00
8 Beza Seifu Administration 10,000.00
9 Michael Yossef Sales 4,500.00
10 Hanna Natnael Research 12,000.00
GROUP BY DepName
SELECT DepName , SUM(Salary) AS Total
FROM Employee
DepName Total Salary
GROUP BY DepName Administration 55,000.00
Research 30,000.00
Sales 41,900.00
14