RDBMS Using Oracle. BIT-4 Lecture Week 3. Lecture Overview

Similar documents
SQL. - single row functions - Database Design ( 데이터베이스설계 ) JUNG, Ki-Hyun ( 정기현 )

GIFT Department of Computing Science. [Spring 2016] CS-217: Database Systems. Lab-3 Manual. Single Row Functions in SQL

Programming Languages

CS2 Current Technologies Lecture 2: SQL Programming Basics

CS2 Current Technologies Note 1 CS2Bh

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

King Fahd University of Petroleum and Minerals

Objectives. After completing this lesson, you should be able to do the following:

Pivot Tables Motivation (1)

Informatics Practices (065) Sample Question Paper 1 Section A

Database Management System. * First install Mysql Database or Wamp Server which contains Mysql Databse.

Real-World Performance Training SQL Introduction

SQL. Char (30) can store ram, ramji007 or 80- b

Creating and Managing Tables Schedule: Timing Topic

Database implementation Further SQL

SQL Structured Query Language Introduction

Topic 8 Structured Query Language (SQL) : DML Part 2

Department of Computer Science and Information Systems, College of Business and Technology, Morehead State University

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Part III. Data Modelling. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Table : Purchase. Field DataType Size Constraints CustID CHAR 5 Primary key CustName Varchar 30 ItemName Varchar 30 PurchaseDate Date

@vmahawar. Agenda Topics Quiz Useful Links

KENDRIYA VIDYALAYA ALIGANJ SHIFT-II HOLIDAY HOMEWORK CLASS-XII INFORMATICS PRACTICES

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Introduction. Introduction to Oracle: SQL and PL/SQL

THE INDIAN COMMUNITY SCHOOL, KUWAIT

CIS Reading Packet: "Views, and Simple Reports - Part 1"

1 SQL Structured Query Language

CS Reading Packet: "Views, and Simple Reports - Part 1"

Create Rank Transformation in Informatica with example

1 SQL Structured Query Language

SQL Functions (Single-Row, Aggregate)

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

What are temporary tables? When are they useful?


Downloaded from

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Slide 17-1

Practical Workbook Database Management Systems

INDIAN SCHOOL MUSCAT FINAL TERM EXAMINATION INFORMATICS PRACTICES

Common Expression Editor Functions in Informatica Analyst

Oracle Database 18c. Gentle introduction to Polymorphic Tables Functions with Common patterns and sample use cases

Built-in SQL Functions. Chapter 5

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Database Compatibility for Oracle Developers Tools and Utilities Guide

ajpatelit.wordpress.com

SQL FUNCTIONS. Prepared By:Dr. Vipul Vekariya.



NURSING_STAFF NNbr NName Grade

CS Week 10 - Page 1

5 Integrity Constraints and Triggers

Introduc.on to Databases

Database Compatibility for Oracle Developers Tools and Utilities Guide

Chapter. Relational Database Concepts COPYRIGHTED MATERIAL

Objectives. After completing this lesson, you should be able to do the following:

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

Database Modelling. Lecture 4 (b): Database Integrity, Keys & Constraints. Akhtar Ali 10/14/2014 1

ES I INFORMATICS PRACTICES Set I Class XII Sec- A,B,C Date: April 2017 Time: 1 hr 10 min M.M.: 30

RESTRICTING AND SORTING DATA

Active Databases Part 1: Introduction CS561

Oracle Database Lite. SQL Reference Release 10.3 E

CS Reading Packet: "Writing relational operations using SQL"

Power Up Your Apps with Recursive Subquery Factoring. Jared Still 2014

Relational Database Language

Databases - 5. Problems with the relational model Functions and sub-queries

Deepak Bhinde PGT Comp. Sc.

TO_CHAR Function with Dates

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Writing PL/SQL Executable Statements. Copyright 2007, Oracle. All rights reserved.

Chapter 14: MySQL Revision Tour. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh (Raj.)

CIS Week 11 Lab Exercise p. 1 Fall 2009

Topics - System Administration for Glovia

Practical Workbook Database Management Systems

Creating SQL Tables and using Data Types

SYSTEM CODE COURSE NAME DESCRIPTION SEM

Trigger is a stored procedure which is called implicitly by oracle engine whenever a insert, update or delete statement is fired.

Databases IIB: DBMS-Implementation Exercise Sheet 13

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Unit 6. Scalar Functions

Sample Paper for class XII IP with Answers Prepared by Successbook Group Sub: - Informatics Practices Total Marks :70 Time:3hr

Definitions. Database Architecture. References Fundamentals of Database Systems, Elmasri/Navathe, Chapter 2. HNC Computing - Databases

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

D a t a b a s e M a n a g e m e n t S y s t e m L a b Institute of Engineering and Management

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Chapter-14 SQL COMMANDS

Oracle Database 12c SQL Fundamentals

The Encryption Wizard for Oracle. API Library Reference

Visit for more.

a 64-bit Environment Author: Rob procedures. SSIS servers. Attunity.

INDEX. 1 Introduction. 2 Types of Cursors 2.1 Explicit cursor 2.2 Attributes 2.3 Implicit cursor 2.4 Attributes. 3 Parameterized cursor

Programming the Database

Relational Database Management Systems Mar/Apr I. Section-A: 5 X 4 =20 Marks

Course Outline and Objectives: Database Programming with SQL

Chapter 9: Working with MySQL

Database Management. This transition was facilitated by the induction of database technology into the organizations. Approaches to Data Management

Chapter _CH06/CouchmanX 10/2/01 1:32 PM Page 259. Manipulating Oracle Data

P.G.D.C.M. (Semester I) Examination, : ELEMENTS OF INFORMATION TECHNOLOGY AND OFFICE AUTOMATION (2008 Pattern)

UNIT III-DATA MANAGEMENT-1 COMPLETE NOTES

Transcription:

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