SQL Basics
SQL Basics
Language (SQL)
Presented to NAM
18-Feb-2016
By Kuldeep Singh
SQL*PLUS
DDL
Data Definition
DML
Data Manipulation
DCL
Data Control
SQL
Introduction to SQL
First task:
Authentication
Schema
Data Types
Primary key
Foreign key
CREATE TABLE order_items (
order_number NUMBER(10,0) NOT NULL,
line_item NUMBER(4,0) NOT NULL,
part_number VARCHAR(12) NOT NULL,
quantity NUMBER(4,0),
PRIMARY KEY (order_number, line_item),
FORIEGN KEY (order_number)
REFERENCES order_header (order_number);
Specifying Constraints on
Columns and Tables
Constraints on attributes:
UNIQUE specification
SQL Indexes
INSERT
Syntax:
Syntax:
COMMIT [WORK];
SELECT
Syntax:
SELECT columnlist
FROM tablename;
UPDATE
Syntax:
UPDATE tablename
SET columnname = expression [, columname =
expression]
[WHERE conditionlist];
ROLLBACK
Syntax:
ROLLBACK;
DELETE
Syntax:
INSERT
Executed first
Syntax:
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Arithmetic Operators:
The Rule of Precedence
Arithmetic Operators:
The Rule of Precedence (continued)
Special Operators
BETWEEN
IS NULL
LIKE
IN
EXISTS
ADD
MODIFY
DROP
Adding a Column
Syntax:
Count
Calculate averages
Aggregate Functions
DEPARTMENTS
table1.column, table2.column
table1, table2
table1.column1=table2.column2
Types of Joins
Joins that are compliant with the SQL include the
following:
Cross joins
Equijoin
Natural joins
USING clause
Self join
Non-equijoin
Outer join
Outer join
SELECT
table1.column, table2.column
FROM
table1
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]
Generating a Cartesian
Product
EMPLOYEES (20 rows)
Cartesian product:
20 x 8 = 160 rows
DEPARTMENTS (8 rows)
Foreign key
DEPARTMENTS
Primary key
Using Equijoin
Write SQL statement to do this: Employees Department
Select *
From employees ,departments
Where employees.department_id=departments.department_id
EMPLOYEES (MANAGER)
Departments
Locations
OR:
select first_name,department_name,city
from employees
JOIN departments ON(employees.department_id=departments.department_id)
JOIN locations
ON(departments.location_id=locations.location_id)
OR:
select first_name,department_name,city
from employees E,departments D,locations L
where E.department_id=D.department_id
and D.location_id=L.location_id
NON-EQUIJOINS
EMPLOYEES
JOB_GRADES
Summary
71
Summary (continued)
72
Summary (continued)
73
Thank You