Sei sulla pagina 1di 22

RDBMS Using Oracle

BIT-4
Lecture Week 3

Lecture Overview
n
n
n
n
n
n

Creating Tables, Valid and Invalid table names


Copying data between tables
Character and Varchar2 DataType Size
Define Variables in SQL
NVL and NVL2 Function
Using Single ROW Character Functions

Creating Tables

CREATE Table
command
To create a table, CREATE TABLE
command is used to specify
n
n
n
n

The name of the table


The name of each column
The type of data to be stored in each column
The width of each column

Create table command


n

Suppose we want to store the simple


information of students in a table.

For that we will identify columns


Name
n Rollno
n Address
n class
n

Then their data types


Name
n Rollno
n Address
n Class
n Zipcode
n

varchar2 (15)
number (3)
varchar2 (20)
varchar2 (5)
number (6)

CHAR <size> (Oracle9i)


n
n
n
n
n

Fixed length alphanumeric string


Has maxim length in bytes
Size range from min of 1 byte to Max 2000 byte
Default Size is 1
If data is shorter then the specified length then
space is padded to the right of specified length.

VARCHAR2 <size> (Oracle9i)


n
n
n
n
n

Variable length alphanumeric string


Has maxim length in bytes
Size range from min of 1 byte to Max 4000 byte
No Default Size
An empty VARCHAR2(2000) column takes up
as much room in the database as an empty
VARCHAR2(2) column.

Example comparison difference

CHAR datatype

VARCHAR2 datatype

Yo = Yo
Yo < Yo

Create table command


CREATE Table STUDENT
( Name
varchar2 (15),
Rollno
number (3),
Address
varchar2 (20),
Class
varchar2 (5),
Zipcode
number (6) );

Copying ROWS
between tables

Copying Rows
Create table employee
(empno number,
name varchar(25));
SQL> INSERT into employee (empno, name)
select empno, ename from emp;

SQL> INSERT into employee (empno, name) select empno,


ename from emp;
8 rows created.
SQL> select * from employee;
EMPNO NAME
---------- ------------------------7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT

INSERT into employee (empno, name)


select empno, ename from emp
where job = CLERK;

Copying Table
Create table abc (name , job , salary) as
select ename, job, sal from emp
where job = CLERK;

Naming a Table

The first step in creating a table is to name it.

The name you choose for a table must follow


some Standard Rules for naming an oracle
database table.

Rules for naming an oracle database table.


Table name must begin with a letter, A Z
or a z
n A table name can contain numbers and
special character (underscore _ ).
n Must not contain SQL reserve words.
n It is same whether upper or lower case letters
are used. For example EMP & emp and Emp
all are same.
n

Rules for naming an oracle database table.


n Name

can be up to 30 characters in length.

If we specify table name in CREATE


TABLE statement in double quotes then
EMP & Emp and emp are three
different names.

Valid and Invalid table names


This is a valid table name??????????
n

EMP85
YES

85EMP
NO (Must begin with letter)

Stu_Table
YES

Stu Table
NO (Blank space is not allowed)

Valid and Invalid table names


This is a valid table name??????????
n

Stu Table
YES (Blank space allowed
within quotes)

UPDATE
NO (Reserve word)

Table1
YES

Define Variables in
SQL

10

Define Variables in SQL


n

To create interactive SQL statements, variables


can be defined in SQL command.

SQL> define dept = 10;


SQL> define dept
DEFINE DEPT
= "10" (CHAR)

SQL> define dept

Define Variables
SQL
DEFINEin
DEPT
SQL> select empno, job,

contd
= "10" (CHAR)
SQL> undefine dept
deptno
fromdept
emp where deptno =
SQL> define
symbol dept is UNDEFINED

&dept;

old 1: select empno, job, deptno from emp where deptno = &dept
new 1: select empno, job, deptno from emp where deptno = 10
EMPNO
--------7782
7839
7934

JOB
--------MANAGER
PRESIDENT
CLERK

DEPTNO
--------10
10
10

Unedifying Variable

11

Define Variables in SQL

contd

USING Substitution Variable


SQL> select

empno, job, deptno from emp where deptno = &deptno and

job=&job;
Enter value for deptno: 20
Enter value for job: 'MANAGER'
old 1: select empno, job, deptno from emp where deptno = &deptno and job=&job
new 1: select empno, job, deptno from emp where deptno = 20 and job='MANAGER'
EMPNO
--------7566

JOB
--------MANAGER

DEPTNO
--------20

Saving Variable for a session


n

Consider the following SQL that is saved as


Script and used with Substitution Variables

SQL> select &col1, &col2 from &Table Order by &Order_by_col


2
SQL> save ex01
Created file ex01
Once file is saved, then we can run it using @<file-name> from SQL Plus

12

Saving Variable for a session

contd..

SQL> @ex01
Enter value for col1: empno
Enter value for col2: ename
Enter value for table: emp
Enter value for order_by_col: ename
old 1: select &col1, &col2 from &Table Order by &Order_by_col
new 1: select empno, ename from emp Order by ename
EMPNO ENAME
--------- ---------7876 ADAMS
7499 ALLEN
7698 BLAKE
7782 CLARK
7902 FORD
7900 JAMES
7566 JONES

NOTE:For Setting Verification Text Off


SQL> Set verify off
For Setting Verification Text Off
SQL> SET VERIFY ON

The NULL value


Function: NVL

13

NVL

SQL> select SAL , COMM, sal + comm from emp;


SAL
---------800
1600
1250
446.25
1250
427.5
367.5

COMM
----------

SAL+COMM
----------

300
500

1900
1750

1400

2650

NVL

SQL> select sal, comm , NVL(comm,0) from EMP;


SAL
---------800
1600
1250
446.25
1250
427.5
367.5

COMM
--------300
500
1400

NVL(COMM,0)
--------------0
300
500
0
1400
0
0

14

NVL

SQL> select sal, comm, sal + NVL(comm,0) from EMP;


SAL
---------800
1600
1250
446.25
1250
427.5
367.5

COMM
--------300
500
1400

SAL+NVL(COMM,0)
--------------800
1900
1750
446.25
2650
427.5
367.5

NULL value Function:


NVL2

15

NVL2
n

The function NVL2 is a variation of NVL


function takes three arguments
NVL2(x1, x2, x3)
NVL2 returns X3 if X1 is null and returnes X2 if
X1 is Not Null.

Example

NVL2

SQL>select Sal, Comm, NVL2(comm, sal + comm, sal) from emp;


SAL
--------800
1600
1250
2975
1250
2850

COMM
--------300
500
1400

NVL2(COMM,SAL+COMM,SAL)
----------------------800
1900
1750
2975
2650
2850

// If COMM is NULL then SALARY is returned, If COMM


is not null then SAL + COMM is returned.

16

Using Single ROW


Character Functions
OPERATE ON CHARACTER DATA
q ASCII
q CHR
q INSTR
q INSTRB
q LENGTH
q LENGTHB
q LOWER
q UPPER
q LPAD
q RPAD

q LTRIM
q
q
q
q
q
q
q
q
q

RTRIM
TRIM
REPLACE
TRANSLATE
SUBSTR
SUBSTRB
CONCAT
INITCAP
SOUNDEX

Character Function ASCII


n

Returns the ASCII decimal equal to character

SQL> select ASCII('A'), ASCII('B'), ASCII('z') from dual;


ASCII('A') ASCII('B')
------------------65
66

ASCII('Z')
---------122

17

Character Function CHR


n
n

CHR takes a single integer argument


This function returns the Character equal to the
decimal (binary) representation of that character.

SQL> select CHR(65), CHR(89), CHR(223) from dual


C
A

C
Y

C
-

Character Function CONCAT


n

n
n

CONCAT(<c1>, <c2>) takes two arguments,


where c1 and c2 are both character strings.
This function returns C2 appended to C1.
It returns same result as C1 || C2

SQL> Select

CONCAT('HELLO', ' MAN') CONCAT,


'HELLO' || ' Mr.' as "PIPE" from dual;

CONCAT
--------HELLO MAN

PIPE
-------HELLO Mr

18

INITCAP
INITCAP is used to convert first letter of a
string in capital
SQL> select initcap(ename), initcap('how are you') from emp;
INITCAP(EN
---------Smith
Allen
Ward
Jones
Martin

INITCAP('HO
----------How Are You
How Are You
How Are You
How Are You
How Are You

INSTR
INSTR Function is used to find the position of any
character in a string.
INSTR (<c1>, <c2> [, <i>[ , <j> ]]) takes four
arguments , where last 2 arguments are optional.
For Example , INSTR (ename , S) Returns the position
of first S in ENAME.
Select INSTR (Mississippi, i , 3, 3) from dual;
// Returns 11
Search begin from 3rd character are returns
the position of its third occurrence.

19

SQL> select ename , INSTR(ename , 'S') from emp;


ENAME
---------ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING

INSTR(ENAME,'S')
---------------0
0
5
0
0
0
1
0

Note:If I is negative then search performs backwards

ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN

SQL> Select INSTR(ename, 'N'),


INSTR ('Mississippi', 'i' , 3, 3),
INSTR('Mississippi', 'i' , -3, 3) from emp
INSTR(ENAME,'N') INSTR('MISSISSIPPI','I',3,3) INSTR('MISSISSIPPI','I',-3,3)
----------------------------------------------------------------------0
11
2
5
11
2
0
11
2
3
11
2
6
11
2

INSTR Function is used to find the position of any character in a


string. Where as INSTRB returns bytes instead of characters.
n

20

SQL> select job, instr(job, 'ER', 1, 1), instr(job, 'ER', 1, 3) from emp
JOB
--------CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

INSTR(JOB,'ER',1,1)
------------------3
0
0
6
0
6

INSTR(JOB,'ER',1,3)
------------------0
0
0
0
0
0

LENGTH
LENGTH takes single character argument
LENGTH is used to get the length of a string in characters.
SQL> select ename, length(ename) from emp;
ENAME
---------SMITH
WARD
JONES
MARTIN

LENGTH(ENAME)
------------5
4
5
6

LENGTHB() is same as LENGTH(), except it returns Bytes instead Of


characters, for single byte character LENGTHB() is same as LENGTH().

21

LOWER & UPPER


n

UPPER is used to convert a string in UPPER case,


and LOWER is used to convert a string in LOWER
case
SQL> select UPPER(ename) , LOWER(ename) from emp;
UPPER(ENAME)
---------ALLEN
WARD
JONES
MARTIN
..

LOWER(ENAME)
---------allen
ward
jones
martin

Thanks
kamran@niit.edu.pk

22

Potrebbero piacerti anche