Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
BIT-4
Lecture Week 3
Lecture Overview
n
n
n
n
n
n
Creating Tables
CREATE Table
command
To create a table, CREATE TABLE
command is used to specify
n
n
n
n
varchar2 (15)
number (3)
varchar2 (20)
varchar2 (5)
number (6)
CHAR datatype
VARCHAR2 datatype
Yo = Yo
Yo < Yo
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;
Copying Table
Create table abc (name , job , salary) as
select ename, job, sal from emp
where job = CLERK;
Naming a Table
EMP85
YES
85EMP
NO (Must begin with letter)
Stu_Table
YES
Stu Table
NO (Blank space is not allowed)
Stu Table
YES (Blank space allowed
within quotes)
UPDATE
NO (Reserve word)
Table1
YES
Define Variables in
SQL
10
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
contd
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
12
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
13
NVL
COMM
----------
SAL+COMM
----------
300
500
1900
1750
1400
2650
NVL
COMM
--------300
500
1400
NVL(COMM,0)
--------------0
300
500
0
1400
0
0
14
NVL
COMM
--------300
500
1400
SAL+NVL(COMM,0)
--------------800
1900
1750
446.25
2650
427.5
367.5
15
NVL2
n
Example
NVL2
COMM
--------300
500
1400
NVL2(COMM,SAL+COMM,SAL)
----------------------800
1900
1750
2975
2650
2850
16
q LTRIM
q
q
q
q
q
q
q
q
q
RTRIM
TRIM
REPLACE
TRANSLATE
SUBSTR
SUBSTRB
CONCAT
INITCAP
SOUNDEX
ASCII('Z')
---------122
17
C
Y
C
-
n
n
SQL> Select
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
INSTR(ENAME,'S')
---------------0
0
5
0
0
0
1
0
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
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
21
LOWER(ENAME)
---------allen
ward
jones
martin
Thanks
kamran@niit.edu.pk
22