RDBMS Using Oracle BIT-4 Lecture Week 3 Lecture Overview 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 1
Creating Tables CREATE Table command To create a table, CREATE TABLE command is used to specify 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 2
Create table command Suppose we want to store the simple information of students in a table. For that we will identify columns Name Rollno Address class Then their data types Name varchar2 (15) Rollno number (3) Address varchar2 (20) Class varchar2 (5) Zipcode number (6) 3
CHAR <size> (Oracle9i) 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) 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. 4
Example comparison difference CHAR datatype Yo = Yo VARCHAR2 datatype Yo < Yo Create table command CREATE Table STUDENT ( Name varchar2 (15), Rollno number (3), Address varchar2 (20), Class varchar2 (5), Zipcode number (6) ); 5
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; 6
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 ; 7
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 A table name can contain numbers and special character (underscore _ ). Must not contain SQL reserve words. It is same whether upper or lower case letters are used. For example EMP & emp and Emp all are same. 8
Rules for naming an oracle database table. 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?????????? EMP85 85EMP Stu_Table Stu Table YES NO (Must begin with letter) YES NO (Blank space is not allowed) 9
Valid and Invalid table names This is a valid table name?????????? Stu Table UPDATE Table1 YES (Blank space allowed within quotes) NO (Reserve word) YES Define Variables in SQL 10
Define Variables in SQL 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 DEPT SQL> undefine dept SQL> define dept symbol dept is UNDEFINED Define Variables in SQL SQL> select empno, job, deptno from emp where deptno = &dept; old 1: select empno, job, deptno from emp where deptno = &dept new 1: select empno, job, deptno from emp where deptno = 10 EMPNO JOB DEPTNO --------- --------- --------- 7782 MANAGER 10 7839 PRESIDENT 10 7934 CLERK 10 contd = "10" (CHAR) 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 JOB DEPTNO --------- --------- --------- 7566 MANAGER 20 Saving Variable for a session 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 COMM SAL+COMM ---------- ---------- ---------- 800 1600 300 1900 1250 500 1750 446.25 1250 1400 2650 427.5 367.5 NVL SQL> select sal, comm, NVL(comm,0) from EMP; SAL COMM NVL(COMM,0) ---------- --------- --------------- 800 0 1600 300 300 1250 500 500 446.25 0 1250 1400 1400 427.5 0 367.5 0 14
NVL SQL> select sal, comm, sal + NVL(comm,0) from EMP; SAL COMM SAL+NVL(COMM,0) ---------- --------- --------------- 800 800 1600 300 1900 1250 500 1750 446.25 446.25 1250 1400 2650 427.5 427.5 367.5 367.5 NULL value Function: NVL2 15
NVL2 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 COMM NVL2(COMM,SAL+COMM,SAL) --------- --------- ----------------------- 800 800 1600 300 1900 1250 500 1750 2975 2975 1250 1400 2650 2850 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 ASCII CHR INSTR INSTRB LENGTH LENGTHB LOWER UPPER LPAD RPAD LTRIM RTRIM TRIM REPLACE TRANSLATE SUBSTR SUBSTRB CONCAT INITCAP SOUNDEX Character Function ASCII Returns the ASCII decimal equal to character SQL> select ASCII('A'), ASCII('B'), ASCII('z') from dual; ASCII('A') ASCII('B') ASCII('Z') ---------- ---------- ---------- 65 66 122 17
Character Function CHR 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 C C - - - A Y ß Character Function CONCAT 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 PIPE --------- -------- HELLO MAN 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 INITCAP('HO ---------- ----------- Smith How Are You Allen How Are You Ward How Are You Jones How Are You Martin 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 3 rd character are returns the position of its third occurrence. 19
SQL> select ename, INSTR(ename, 'S') from emp; ENAME INSTR(ENAME,'S') ---------- ---------------- ALLEN 0 WARD 0 JONES 5 MARTIN 0 BLAKE 0 CLARK 0 SCOTT 1 KING 0 Note:- If I is negative then search performs backwards SQL> Select INSTR(ename, 'N'), INSTR ('Mississippi', 'i', 3, 3), ENAME ---------- SMITH ALLEN WARD JONES MARTIN 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. 20
SQL> select job, instr(job, 'ER', 1, 1), instr(job, 'ER', 1, 3) from emp JOB INSTR(JOB,'ER',1,1) INSTR(JOB,'ER',1,3) --------- ------------------- ------------------- CLERK 3 0 SALESMAN 0 0 SALESMAN 0 0 MANAGER 6 0 SALESMAN 0 0 MANAGER 6 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 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) LOWER(ENAME) ---------- ---------- ALLEN allen WARD ward JONES jones MARTIN martin.. Thanks kamran@niit.edu.pk 22