You are on page 1of 13

Introduction to SQL

Database Systems
(IT-310)
Relational Database Terminology
 A relational db contains one or more tables.
 Table/Relation: Basic storage structure of RDBMS
 A table contains:
1. Row / Tuple
2. Column / Attribute
a. Primary Key
b. Non-Key Column
c. Foreign Key
3. Field
a. Field with a Null value
Oracle
A (RDBMS) produced and marketed by Oracle
Corporation.
Different versions:
1992: Oracle version 7 appeared with support for referential
integrity, stored procedures and triggers.
2001: Oracle9i with 400 new features, including the ability to read
and write XML documents.
2003: Oracle Database 10g.
2005: Oracle Database 10.2.0.1 — also known as Oracle
Database 10g Release 2 (10gR2) — appeared.
2007: Oracle Corporation released Oracle Database 11g for
Linux and for Microsoft Windows.
SQL, PL/SQL, SQL *Plus

SQL:Standard Language for creating & querying relational DBs.


 Accepted by ANSI and ISO
 Used to create tables, translate user requests, maintain data
dictionary & system catalog
 Update & maintain tables, establish security
 Carry out backup & recovery procedures
PL/SQL: is Oracle Corporation's proprietary procedural
extension to the SQL database language, used in the Oracle
database.
 SQL *Plus: Oracle tool that recognizes & submits SQL & PL/SQL
statements to server for execution.
SQL Statements
Select Data retrieval

Insert Data Manipulation Language


Update (DML)
Delete
Create, Alter, Drop Data Definition Language
Rename, Truncate (DML)

Commit Transaction Control


Rollback
Savepoint
Grant Data Control Language
Revoke (DCL)
Tables

EMP: Stores employee’s data


EMPNO,ENAME, JOB, MGR, HIREDATE, SAL,
COMM, DEPTNO
DEPT: Details of all departments
DEPTNO, DNAME, LOC
SALGRADE: Details of salaries of various
grades
GRADE, LOSAL, HISAL
Select Statement
 Select allows:
 Selection: Choosing rows
 Projection: Selecting columns
 Join: Fetch data from multiple tables
 Syntax:
 Select [Distinct] {*, column [alias], …} from table;
 Select: what columns
 From: which table
 Distinct: suppress duplicates
 * : select all columns
 Alias: gives selected columns different headings
Writing SQL statements

SQL statements are not case sensitive


Can be placed on one or more lines
Keywords cannot be split across lines
Place clauses on different lines for
readability
Place a semicolon or slash at the end of
the last clause/line
Selecting all columns:
Select * from dept;
Selecting specific columns:
Select deptno, lo from dept;
Column Headings:
Default justification:
Left for Date & Character
Right for Numeric data
Default display: Uppercase
Using Arithmetic operators
Select ename, sal, sal+300 from emp;
Select ename, sal,12*sal+100 from emp;
Select ename, sal,12*(sal+100) from emp;
Null values with arithmetic expressions:
Select ename, sal,12*sal+comm from emp where
ename= ‘KING’;
Column Alias

Select ename as Name, sal Salary from


emp;
Select ename as “Name” from emp;
Select ename “Employee Name” from
emp;
Select ename, sal,12*sal “Annual
Salary” from emp;
Concatenation Operator

Select ename || job as “Employee”


from emp;
Select ename || ‘ ’ || ‘is a’ || ‘ ’ || job as
“Employee Details” from emp;
Handling Duplicate Rows

Duplicate rows:
Select deptno from emp;
Eliminating duplicate rows:
Select Distinct deptno from emp;
Displaying table structure:
Desc[ribe] tablename;
Describe dept;