Professional Documents
Culture Documents
Lecture 1
Lecture 1
Overview of Lecture
Overview of Databases Introduction to SQL Oracle SQL Tools
Overview of Databases
What's a Database
Persistent Structured Information Repository Provides
API (Application Programming Interface) Protocol Language (SQL for Relational DB's)
Client/Server Architecture
API includes: executeQuery( sqlstr )
SQL Statements are passed through API & protocol
User
DB Application
DB Application Client DB Client
A P I
Database Server
Implements DB Operations
Client-Side
Ellis Cohen 2001-2008
Server-Side
5
ename
ALLEN MARTIN BLAKE KING TURNER STERN
sal
1600 1250 2850 5000 1500 1500
comm
300 1400
Assignments
etc.
Ellis Cohen 2001-2008 7
SELECT empno, ename FROM Employees WHERE sal < 2000 ORDER BY empno
Other SQL commands are used to create, modify & manage the data in the database
Ellis Cohen 2001-2008 8
Database Features
Security & Access Control
Allows control over which users can access which information Allows database to automatically take actions based on changes it monitors Makes it possible to ensure related changes are all made together, or not at all (atomicity) When operation is completed, changes are actually stored persistently (durability) Ensures concurrent users cannot "step on each other's toes" (isolation) Allows control over aspects of how information is stored, and how storage and retrieval operations are executed
Ellis Cohen 2001-2008 9
Performance Tuning
Introduction to SQL
10
Primary Key
Emps Table
ename -----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER job --------CLERK SALESMAN SALESMAN DEPTMGR SALESMAN DEPTMGR DEPTMGR ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK hiredate --------17-DEC-80 20-FEB-81 22-FEB-81 02-APR-81 28-SEP-81 01-MAY-81 09-JUN-81 19-APR-87 17-NOV-81 08-SEP-81 23-MAY-87 03-DEC-81 03-DEC-81 23-JAN-82 sal comm ---- ---800 1600 300 1250 500 2975 1250 1400 2850 2450 3000 5000 1500 0 1100 950 3000 1300
11
empno ----7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934
SQL Queries
SELECT ename FROM Emps WHERE empno = 7499
Note symmetry of lookups
ENAME -----ALLEN
SELECT empno FROM Emps WHERE ename = 'ALLEN' SELECT empno, ename FROM Emps WHERE sal > 2975 ORDER BY ename
Query
Ellis Cohen 2001-2008
EMPNO ----7499
Query Result
12
Ordering
Relational Database tables are not intrinsically ordered in any way. SELECT empno, ename FROM Emps
(which generates the employee number and name of every employee in the table)
may come out in one order today and a different one tomorrow (if the DBA reorganizes the database) You MUST use ORDER BY if you want your results to come out in a specific order: SELECT empno, ename FROM Emps ORDER BY ename
Ellis Cohen 2001-2008 13
Boolean Expressions
< less than > greater than = equal <= <> less than or equal != not equal >= greater than or equal
SELECT empno, ename FROM Emps WHERE (sal > 1200) AND (sal <= 1500) AND (comm > 200)
What will this generate?
Ellis Cohen 2001-2008 14
15
ORDER BY ename
It is possible to order query results by attributes which are not projected. However, it is also possible to define and name computed attributes, and then order the results based on them
Ellis Cohen 2001-2008 16
2. Ordering
SQL Exercise
Note: this represents a date; not just a year!
List the employee #, employee name and job of all non-clerks hired after 1991. Sort the output by job; within employees with the same job, sort by employee name
Ellis Cohen 2001-2008 17
SQL
A language for dealing with tables
DML: Data Manipulation Language
Querying: Extracting data from tables Modification: Inserting, updating, deleting rows in a tables
20
Emps
empno, ename
RichEmps
21
SQL Insert/Update/Delete Actions SQL DDL (e.g. create table) SQL DCL (e.g. grant access)
22
SQL-99 (SQL3)
Adds Programmability and OO extensions Not generally implemented Oracle: PL/SQL, Oracle OO extensions SQL Server: Transact-SQL
23
24
Connect to SQL*Plus
1) type scott
2) type tiger
3) click OK
25
SQL*Plus Example
Start with these SQL*Plus set commands
set linesize 125 set pagesize 1000 select empno, ename, sal, comm from emp where deptno <> 20 order by sal; End SQL command with ENAME -----JAMES WARD MARTIN MILLER TURNER ALLEN CLARK BLAKE KING SAL COMM ---- -----950 1250 500 1250 1400 1300 1500 0 1600 300 2450 2850 5000
EMPNO ------7900 7521 7654 7934 7844 7499 7782 7698 7839
Query Result
also called the Result Set
The SID is the name of the database you used when you installed Oracle
2.
28
Open SCOTT
29
Connection Elements
30
SCOTT's Tables
31
32
33
SQL*Plus
DB Application
API Library
Client-side
Server-side
Ellis Cohen 2001-2008 34