Chapter 03 DBMS.

Slides:



Advertisements
Similar presentations
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
SQL: Part 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Advanced SQL (part 2) and SQL in practice CS263 Lecture 8.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Dr. Philip Cannata 1 Programming Languages Prolog Part 3 SQL & Prolog.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
Subqueries.
Subqueries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic SQL These slides are licensed under.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Dr. Philip Cannata 1 Programming Languages Chapter 19 - Continuations.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
Defining a Column Alias
Copyright  Oracle Corporation, All rights reserved. Introduction.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Relational Normalization Theory
Enhanced Guide to Oracle 10g
Difference between Oracle PL/SQL and MySQL
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Writing Control Structures
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Part B Entity Relationship Diagram TITLE #* ID * title * description
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
(SQL) Manipulating Data
Writing Basic SQL Statements
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Restricting and Sorting Data
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Chapter 03 DBMS

TYPES OF OPERATORS ARTHEMATIC OPERATORS LOGICAL OPERATORS COMPARISON OPERATORS

TYPES OF OPERATORS

AND OPERATOR SQL> SELECT * FROM EMP WHERE COMM = 300 AND DEPTNO = 30; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- --------- --------- --------- --------- --------- 7499 ssssd SALESMAN 7698 20-FEB-81 1600 300 30

OR OPERATOR 1* SELECT * FROM EMP WHERE COMM = 300 OR DEPTNO = 30 SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- --------- --------- --------- --------- --------- 7499 ssssd SALESMAN 7698 20-FEB-81 1600 300 30 7521 ssssd SALESMAN 7698 22-FEB-81 1250 500 30 7654 ssssd SALESMAN 7698 28-SEP-81 1250 1400 30 7698 ssssd MANAGER 7839 01-MAY-81 2850 30 7844 ssssd SALESMAN 7698 08-SEP-81 1500 0 30 7900 ssssd CLERK 7698 03-DEC-81 200 30 6 rows selected.

NOT OPERATOR 1* SELECT * FROM EMP WHERE NOT COMM = 300 SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- --------- --------- --------- --------- --------- 7521 ssssd SALESMAN 7698 22-FEB-81 1250 500 30 7654 ssssd SALESMAN 7698 28-SEP-81 1250 1400 30 7844 ssssd SALESMAN 7698 08-SEP-81 1500 0 30

TYPES OF COMPARISON OPERATORS SYMBOLIC OPERATORS <,>,<=,>=,=,!= WORD COMPARISON OPERATORS LIKE NOT LIKE IN NOT IN BETWEEN NOT BETWEEN IS NULL IS NOT NULL

Character functions SQL> select initcap('school') from dual; INITCA ------ School

Character functions SQL> select translate('school','o','m')from dual; TRANSL ------ schmml

Character functions 1* select replace('school','o','mtf')from dual SQL> / REPLACE('S ---------- schmtfmtfl

Character functions SQL> select substr('school',2,3) from dual; SUB --- cho

Character functions 1* select empno || ' is from ' || deptno from emp SQL> / EMPNO||'ISFROM'||DEPTNO ---------------------------------------------------------------------------- 7369 is from 20 7499 is from 30 7521 is from 30 7566 is from 20 7654 is from 30 7698 is from 30 7782 is from 10 14 rows selected.

Numeric functions SQL> select ceil(89.77),floor(89.77) from dual; ----------- ------------ 90 89

Numeric functions SQL> select round(89.66,1),trunc(89.66,1) from dual; ROUND(89.66,1) TRUNC(89.66,1) -------------- -------------- 89.7 89.6

Numeric functions SQL> select power(2,3) from dual; POWER(2,3) ---------- 8

If then else SQL> declare 2 a number := 10; 3 b number := 20; 4 c number := 30; 5 begin 6 if a < b then 7 dbms_output.put_line('a is lesser'); 8 else if a < c then 9 dbms_output.put_line('a is less'); 10 else if b<c then 11 dbms_output.put_line('b is lesser than c'); 12 end if; 13 end if; 14 end if; 15 end; 16 / a is lesser PL/SQL procedure successfully completed.

Syntax Simple Loop Loop <Sequence of Statements > End Loop;

Simple Loop Dec lare v_a number := 0; begin Loop v_a := v_a+2; dbms_output.put_line(' The value for v_a is'|| v_a); exit when v_a >= 10; End Loop; dbms_output.put_line('End of Loop'); End; /

Simple Loop Demo SQL> @loopdemo Input truncated to 1 characters PL/SQL procedure successfully completed. SQL> set serveroutput on; SQL> @loopdemo The value for v_a is 2 The value for v_a is 4 The value for v_a is 6 The value for v_a is 8 The value for v_a is 10 End of Loop

Syntax While loop While<condition> Loop <Sequence of Statements > End Loop;

While loop demo declare v_a number := 0; begin while(v_a <= 10) dbms_output.put_line(' The value for v_a is'|| v_a); v_a := v_a+2; End Loop; dbms_output.put_line('End of Loop'); End; /

While loop SQL> @loopdemo Input truncated to 1 characters The value for v_a is 0 The value for v_a is 2 The value for v_a is 4 The value for v_a is 6 The value for v_a is 8 The value for v_a is 10 End of Loop PL/SQL procedure successfully completed.

For Loop declare v_a number ; begin for v_a in 1..10 Loop dbms_output.put_line(' The value for v_a is'|| v_a); End Loop; dbms_output.put_line('End of Loop'); End; /

For Loop SQL> @loopdemo; Input truncated to 1 characters The value for v_a is1 The value for v_a is2 The value for v_a is3 The value for v_a is4 The value for v_a is5 The value for v_a is6 The value for v_a is7 The value for v_a is8 The value for v_a is9 The value for v_a is10 End of Loop PL/SQL procedure successfully completed.

Explicit Cursor

Cursor DECLARE emp_rec emp%rowtype; CURSOR emp_cur IS SELECT * FROM emp WHERE sal > 1000; BEGIN IF NOT emp_cur%ISOPEN THEN OPEN emp_cur; End if; FETCH emp_cur INTO emp_rec; dbms_output.put_line (emp_rec.ename || ' ' || emp_rec.sal); CLOSE emp_cur; END; /

Output SQL> @curdemo ALLEN 1600

DECLARE emp_rec emp%rowtype; CURSOR emp_cur IS SELECT * FROM emp WHERE sal > 1000; BEGIN IF NOT emp_cur%ISOPEN THEN OPEN emp_cur; End if; Loop FETCH emp_cur INTO emp_rec; EXIT WHEN emp_cur%NOTFOUND; dbms_output.put_line (emp_rec.ename || ' ' || emp_rec.sal); End Loop; CLOSE emp_cur; END;

Output SQL> @curdemo; ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 FORD 3000 MILLER 1300 PL/SQL procedure successfully completed.

EXCEPTIONS SYNTAX (GENERAL FORM) Declare =declarations Begin =executable statements Exception = Exception handlers.

Exception Exception when <Exception name> then <user defined action to be carried on> End;

Example SQL> select * from account; ACC_NO CUST_NAME BALANCE --------- ---------- --------- 1 misha 1000 2 minu 8000 3 sheeba 4000

Exception declare current_balance account.balance%type; Account_no account.acc_no%type; less_bal exception; v_acc number := &v_acc; v_withdrawal number := & v_withdrawal; begin Select balance,Acc_no into current_balance,Account_no from account where acc_no = v_acc; if (current_balance - v_withdrawal <= 500) then raise less_bal; else update account set balance = balance-v_withdrawal dbms_output.put_line('withdraw success'); end if; Exception when less_bal then dbms_output.put_line('sorry'); End; /

Exception SQL> @errdemo Enter value for v_acc: 1 old 5: v_acc number := &v_acc; new 5: v_acc number := 1; Enter value for v_withdrawal: 500 old 6: v_withdrawal number := & v_withdrawal; new 6: v_withdrawal number := 500; sorry PL/SQL procedure successfully completed.

Exception SQL> / Enter value for v_acc: 2 old 5: v_acc number := &v_acc; new 5: v_acc number := 2; Enter value for v_withdrawal: 500 old 6: v_withdrawal number := & v_withdrawal; new 6: v_withdrawal number := 500; withdraw success PL/SQL procedure successfully completed. SQL> select * from account 2 ; ACC_NO CUST_NAME BALANCE --------- ---------- --------- 1 misha 1000 2 minu 7500 3 sheeba 4000

System defined Exception SQL> set serveroutput on SQL> SQL> DECLARE   2          Num_a NUMBER := 6;   3          Num_b NUMBER;   4  BEGIN   5          Num_b := 0;   6          Num_a := Num_a / Num_b;   7          Num_b := 7;   8          dbms_output.put_line(' Value of Num_b ' || Num_b);   9  EXCEPTION  10          WHEN ZERO_DIVIDE  11  THEN  12                dbms_output.put_line('Trying to divide by zero');  13                dbms_output.put_line(' Value of Num_a ' || Num_a);  14                dbms_output.put_line(' Value of Num_b ' || Num_b);  15  END;  16  / Trying to divide by zero Value of Num_a 6 Value of Num_b 0 PL/SQL procedure successfully completed.

Views 2 as 3* select empno,sal from emp SQL> / View created. 1 create view view_v1 2 as 3* select empno,sal from emp SQL> / View created. SQL> select * from view_v1; EMPNO SAL --------- --------- 7369 200 7499 1600 7521 1250 7566 2975 7654 1250

sequence 1 create sequence s1 2 increment by 1 3 start with 1 4 maxvalue 10 5 minvalue 1 6 cycle 7* cache 9 SQL> / Sequence created. SQL> insert into stud values(s1.nextval,'meena',90); 1 row created. SQL> select * from stud; ROLLNO NAME MARKS --------- ---------- --------- 1 meena 90

indexes SQL> select rowid from emp; ROWID ------------------ AAAAeFAACAAAAEbAAA AAAAeFAACAAAAEbAAB AAAAeFAACAAAAEbAAC AAAAeFAACAAAAEbAAD AAAAeFAACAAAAEbAAE AAAAeFAACAAAAEbAAF AAAAeFAACAAAAEbAAG AAAAeFAACAAAAEbAAH AAAAeFAACAAAAEbAAI

synonym SQL> create synonym emp1 for emp; Synonym created. SQL> select * from emp1; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- --------- --------- --------- --------- --------- 7369 ssssd CLERK 7902 17-DEC-80 200 20 7499 ssssd SALESMAN 7698 20-FEB-81 1600 300 30 7521 ssssd SALESMAN 7698 22-FEB-81 1250 500 30 7566 ssssd MANAGER 7839 02-APR-81 2975 20 7654 ssssd SALESMAN 7698 28-SEP-81 1250 1400 30 7698 ssssd MANAGER 7839 01-MAY-81 2850 30 7782 ssssd MANAGER 7839 09-JUN-81 2450 10 7788 ssssd ANALYST 7566 19-APR-87 3000 20