SQL - Part-1
SQL - Part-1
SQL - Part-1
1
Database
Every Organization had some information needs.
Organizations can store ‘pieces of information’ called DATA.
Data stored in different formats, hard copy document, electronic
spread
sheet or in database.
A database is an organized collection of information.
DBMS a program that stores retrieves and modifies data in the
database
on request.
Types of Database – Hierarchical, Network, Relational, Object
Relational.
2
2
Agenda
SQL
Database Basics
Different types of commands(DDL,DML,TCL and DCL)
Functions and Aggregations
Joins and Set Operators
Object types – Constraints, Index, Views, M Views ,Synonyms, DB Links and Sequences
Oracle Architecture
PL SQL
Basics of PLSQL
Anonymous Blocks
Cursors
Procedures, Function, Package and Trigger
Ref Cursor, Nested Cursor, Parametric cursor, IOT, PLSQL Tables
TOAD Overview
3
Entity Relationship Model
emp dept
number number
name name
job title location
Scenario:
- “ … assign one or more emp to a department…”
- “ … some dept do not yet have assigned emp…”
4
4
Architecture Oracle
Client Client
Application Server
Server
Database Server
TNSNAMES.ORA
C:\Oracle\ORA10_2\NETWORK\ADMIN
5
Communicating with a RDBMS using SQL
Statement is sent
LAST_NAME
to Oracle Server
King
Abel
Grant
6
SQL Statements
Data Retrieval
SELECT
7
Creating and Managing Tables
8
Table
9
Creating Table
CREATE TABLE student
(sno number(2),
sname varchar2(15),
dob date,
age number(2),
tno number(2));
CREATE TABLE teacher
(tno number(2),
tname varchar2(15),
dept varchar2(15) DEFAULT ‘no dept’);
Creating a table by using a Subquery
CREATE TABLE dept
AS SELECT employee_id, last_name, hire_date,
deptno
FROM emp
WHERE deptno = 80;
10
The ALTER TABLE statement
Use ALTER TABLE statement to add, modify or drop columns
11
Dropping a Table
DROP TABLE dept;
Truncating a table
TRUNCATE TABLE detail_dept;
ALL_COL_COMMENTS USER_COL_COMMENTS
ALL_TAB_COMMENTS USER_TAB_COMMENTS
13
Summary
CREATE TABLE
ALTER TABLE
DROP TABLE
RENAME
TRUNCATE
COMMENT
14
Oracle SQL Data Type
Datatype Description Max/Min Size:
VARCHAR2(size) Variable length character string having 4000 bytes. Minimum is 1.
max length size bytes. You must
specify size
CHAR(size) Fixed length character data of length 2000 bytes.Default and minimum
size bytes. This should be used for size is 1 byte.
fixed length data. Such as codes A100,
B102…
NUMBER(p,s) Number having precision p and scale s. The precision p can range from 1 to
38. The scale s can range from -84 to
127.
LONG Character data of variable length (A 2 Gigabytes
bigger version the VARCHAR2
datatype)
DATE Valid date range from January 1, 4712 BC to
December 31, 9999 AD.
TIMESTAMP the number of digits in the fractional Accepted values of
(fractional_seconds_precision) part of the SECOND datetime field. fractional_seconds_precision are 0
to 9. (default = 6)
CLOB Character Large object 4 Gigabytes
15
Constraints
16
Constraints
17
Defining Constraint
CREATE TABLE student1
(sno number(2) CONSTRAINT s_sno_pk PRIMARY KEY,
sname varchar2(15) NOT NULL,
dob date UNIQUE, SYS_Cnnnnnn
age number(2) CHECK (age < 25),
tno number(2),
CONSTRAINT st_te_fk FOREIGN KEY (tno)
REFERENCES teach(tno) ON DELETE CASCADE
);
18
Constraints
19
Manipulating Data
20
SQL Statements
21
Data Manipulation Language
22
Inserting New Rows
23
Inserting New Rows
24
The UPDATE statement
Modify existing rows with the UPDATE statement
UPDATE table
SET column = value [, column = value,…]
[WHERE condition];
UPDATE emp
SET deptno = 70
WHERE employee_id = 113;
UPDATE empp
SET deptno = 70;
25
The UPDATE statement
UPDATE emp
SET job_id = (SELECT job_id FROM emp
WHERE employee_id = 205),
26
The DELETE statement
Remove existing rows with the DELETE statement
DELETE [FROM] table
[WHERE condition];
DELETE emp
WHERE deptno = (SELECT deptno
FROM dept
WHERE department_name
LIKE ‘%Public%’);
27
The MERGE statement
Insert or Update rows in the COPY_EMP table to match the emp
table.
28
Transaction
29
Controlling Transactions
COMMIT
Transaction
DELETE
SAVEPOINT A
INSERT
UPDATE
SAVEPOINT B
INSERT
30
Summary
31
Writing Basic SQL
SELECT Statements
32
Basic SELECT Statement
SELECT *
FROM dept;
33
Selecting Specific Columns
34
Arithmetic Expressions
Operator Precedence
SELECT last_name,salary,12*salary+300,12+salary*300
FROM emp;
Override the rule by using
Parentheses 12*(salary+300)
35
Defining a Null Value
A NULL is a value that is unavailable, unassigned, unknown,
or inapplicable.
A null is not the same as zero or a blank space.
Arithmetic expressions containing a null value evaluate to null.
36
Defining a Column Alias
37
Concatenation Operator
38
Duplicate Rows
The default display of queries is all rows, including duplicate rows
SELECT deptno
FROM emp;
39
Summary
SELECT statement
- Returns all rows and columns from a table
- Returns specified columns from a table
- Uses column aliases to give descriptive column headings
40
Restricting and Sorting Data
41
Limiting Rows using a Selection
42
Character Strings and Dates
43
Comparison Conditions
44
BETWEEN Condition
Use the BETWEEN condition to display rows based on a
range of values.
SELECT employee_id, last_name, salary
FROM emp
WHERE salary BETWEEN 2500 AND 3500 ;
IN Condition
Use the IN membership condition to test for values in a list.
45
LIKE Condition
LIKE condition to perform wildcard searches of valid search
string values.
Can contain either literal character or numbers
% denotes zero or many characters.
_ denotes any single character.
46
Using the NULL Conditions
FROM emp
WHERE manager_id IS NULL;
Logical Conditions
AND Returns TRUE if both component conditions are true
47
Using the AND Operator
48
Using the NOT Operator
SELECT employee_id, last_name, job_id
FROM emp
WHERE job_id NOT IN (‘IT_PROG’, ‘ST_CLERK’);
Rules of Precedence
1 Arithmetic operators
2 Concatenation operators
3 Comparison Conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 NOT logical condition
7 AND logical condition
8 OR logical condition
Override rules of precedence by using parentheses
49
Rule of Precedence
Select the row if an employee is AD_PRES and earns more than 15000,
or if the employee is a SA_REP
50
ORDER BY Clause
51
Sorting by Column Alias
SELECT employee_id, last_name, salary*12 annsal
FROM emp
ORDER BY annsal ;
52
Summary
53
Single-Row Functions
54
Single-Row Functions
55
Character Functions
Case-manipulation functions
LOWER(‘Hello Bangalore’) hello bangalore
UPPER(‘Hello Bangalore’) HELLO BANGALORE
INITCAP(‘HeLlo BangAlore’) Hello Bangalore
56
Character Functions
Character - manipulation functions
CONCAT(‘Hello’, ’World’) HelloWorld
SUBSTR(‘HelloWorld,1,5) Hello
LENGTH(‘HelloWorld’) 10
INSTR(‘HelloWorld’,’o’) 5
LPAD(salary,10,’*’) *****24000
RPAD(salary,8,’#’) 9900####
WHERE SOUNDEX(city) = SOUNDEX(‘Sidny’)
RTRIM(’AMERICA, ‘A’) AMERIC
LTRIM(’AMERICA, ‘A’) MERICA
TRIM(‘H’ FROM ’HelloWorld’) elloWorld
TRIM(‘A’ FROM ’AMERICA’) MERIC
TRANSLATE
REPLACE
57
Number Functions
ABS(-6) 6
ABS(6) 6
CEIL(1.4) 2
FLOOR(1.6) 1
SIGN(-25) -1
SIGN(25) 1
SQRT(36) 6
VSIZE(city) – The storage size in bytes
58
Number Functions
DUAL is a dummy table you can use to view results from functions and calculations
59