Objectives. Oracle SQL and SQL*PLus. Database Objects. What is a Sequence?



Similar documents
Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Using TimesTen between your Application and Oracle. between your Application and Oracle. DOAG Conference 2011

Oracle Database 10g: Introduction to SQL

Porting from Oracle to PostgreSQL

Oracle Database 12c: Introduction to SQL Ed 1.1

D B M G Data Base and Data Mining Group of Politecnico di Torino

Programming with SQL

Maximizing Materialized Views

A basic create statement for a simple student table would look like the following.

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Oracle Database: SQL and PL/SQL Fundamentals

Database Extensions Visual Walkthrough. PowerSchool Student Information System

Oracle 10g PL/SQL Training

Oracle Database 10g Express

Oracle Database: Introduction to SQL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database: Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle SQL. Course Summary. Duration. Objectives

How To Create A Table In Sql (Ahem)

Oracle Database 11g SQL

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database 12c Enables Quad Graphics to Quickly Migrate from Sybase to Oracle Exadata

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Experiment 5.1 How to measure performance of database applications?

Optimizing Performance. Training Division New Delhi

Oracle/SQL Tutorial 1

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

Oracle EXAM - 1Z Oracle Database 11g Security Essentials. Buy Full Product.

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Optimizing the Performance of the Oracle BI Applications using Oracle Datawarehousing Features and Oracle DAC

Oracle Database: SQL and PL/SQL Fundamentals

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Database Programming with PL/SQL: Learning Objectives

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Fundamentals of Database Design

Oracle Data Dictionary

SQL. Short introduction

Oracle Database: Introduction to SQL

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

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

David Dye. Extract, Transform, Load

5. CHANGING STRUCTURE AND DATA

Contents at a Glance. Contents... v About the Authors... xiii About the Technical Reviewer... xiv Acknowledgments... xv

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

DATABASE DESIGN & PROGRAMMING WITH SQL COURSE CODE: 5324

Introduction to SQL and database objects

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Administration with MySQL

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Fine Grained Auditing In Oracle 10G

3.GETTING STARTED WITH ORACLE8i


Oracle Database 11 g Performance Tuning. Recipes. Sam R. Alapati Darl Kuhn Bill Padfield. Apress*

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

Cross Platform Transportable Tablespaces Migration in Oracle 11g

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

8- Management of large data volumes

Chapter 2: Security in DB2

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database 11g Security Essentials

Oracle Architecture. Overview

Migrate Topaz databases from One Server to Another

Mini User's Guide for SQL*Plus T. J. Teorey

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

HP Quality Center. Upgrade Preparation Guide

Displaying Data from Multiple Tables. Chapter 4

New SQL Features in Firebird 3

KB_SQL SQL Reference Guide Version 4

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

1 File Processing Systems

Performance Tuning for the Teradata Database

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

Oracle For Beginners Page : 1

Using Temporary Tables to Improve Performance for SQL Data Services

RMAN BACKUP & RECOVERY. Recovery Manager. Veeratteshwaran Sridhar

Oracle to MySQL Migration

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

OLH: Oracle Loader for Hadoop OSCH: Oracle SQL Connector for Hadoop Distributed File System (HDFS)

news from Tom Bacon about Monday's lecture

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Oracle EXAM - 1Z Oracle Database 11g Release 2: SQL Tuning. Buy Full Product.

Oracle 11g PL/SQL training

Oracle Database: Program with PL/SQL

Beginning SQL, Differences Between Oracle and Microsoft

Jason S Wong Sr. DBA IT Applications Manager DBA Developer Programmer M.S. Rice 88, MBA U.H. 94(MIS)

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

MySQL for Beginners Ed 3

Database Extension 1.5 ez Publish Extension Manual

There are five fields or columns, with names and types as shown above.

Transcription:

Oracle SQL and SQL*PLus Lesson 12: Other Database Objects Objectives After completing this lesson, you should be able to do the following: Describe some database objects and their uses Create, maintain, and use sequences Create and maintain indexes Create private and public synonyms Prep by Dr. Frank Chao SQL & SQL*Plus 2 Database Objects What is a Sequence? Object Table View Sequence Index Synonym Description Basic unit of storage; composed of rows and columns Logically represents subsets of data from one or more tables Generates primary key values Improves the performance of some queries Alternative name for an object Automatically generates unique numbers Is a sharable object Is typically used to create a primary key value Replaces application code Speeds up the efficiency of accessing sequence values when cached in memory Prep by Dr. Frank Chao SQL & SQL*Plus 3 Prep by Dr. Frank Chao SQL & SQL*Plus 4 1

The CREATE SEQUENCE Statement Define a sequence to generate sequential numbers automatically. CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n NOMAXVALUE}] [{MINVALUE n NOMINVALUE}] [{CYCLE NOCYCLE}] [{CACHE n NOCACHE}]; Note: CACHE n NOCACHE - specifies how many values the Oracle Server will preallocate and keep in memory (By default, the Oracle Server will cache 20 values.) Creating a Sequence Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. Do not use the CYCLE option unless you have a reliable mechanism that purges old rows faster than the sequence cycles SQL> CREATE SEQUENCE dept_deptno 2 INCREMENT BY 1 3 START WITH 91 4 MAXVALUE 100 5 NOCACHE 6 NOCYCLE; Sequence created. Prep by Dr. Frank Chao SQL & SQL*Plus 5 Prep by Dr. Frank Chao SQL & SQL*Plus 6 Confirming Sequences Verify your sequence values in the USER_SEQUENCES data dictionary view. SQL> SELECT sequence_name, min_value, max_value, 2 increment_by, last_number 3 FROM user_sequences; The LAST_NUMBER column displays the next available sequence number. SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAST_NUMBER -------------- ---------- ---------- ------------ ----------- CUSTID 1 1.0000E+27 1 109 DEPT_DEPTNO 1 100 1 91 NEXTVAL and CURRVAL Pseudocolumns NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value. Prep by Dr. Frank Chao SQL & SQL*Plus 7 Prep by Dr. Frank Chao SQL & SQL*Plus 8 2

Using a Sequence Insert a new department named MARKETING in San Diego. SQL> INSERT INTO dept(deptno, dname, loc) 2 VALUES (dept_deptno.nextval, 3 'MARKETING', 'SAN DIEGO'); 1 row created. View the current value for the DEPT_DEPTNO sequence. SQL> SELECT dept_deptno.currval 2 FROM dual; Using a Sequence Caching sequence values in memory allows faster access to those values. Gaps in sequence values can occur when: A rollback occurs The system crashes A sequence is used in another table View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table. Prep by Dr. Frank Chao SQL & SQL*Plus 9 Prep by Dr. Frank Chao SQL & SQL*Plus 10 Modifying a Sequence Change the increment value, maximum value, minimum value, cycle option, or cache option. SQL> ALTER SEQUENCE dept_deptno 2 INCREMENT BY 1 3 MAXVALUE 999999 4 NOCACHE 5 NOCYCLE; Sequence altered. Guidelines for Modifying a Sequence You must be the owner or have the ALTER privilege for the sequence. Only future sequence numbers are affected. The sequence must be dropped and re-created to restart the sequence at a different number. The START WITH option cannot be changed using ALTER SEQUENCE. Some validation is performed. a new MAXVALUE cannot be imposed that is less than the current sequence number Prep by Dr. Frank Chao SQL & SQL*Plus 11 Prep by Dr. Frank Chao SQL & SQL*Plus 12 3

Removing a Sequence Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. Once removed, the sequence can no longer be referenced. SQL> DROP SEQUENCE dept_deptno; Sequence dropped. Sequence Information in Data Dictionary You can get the sequence information from the data dictionary view USER_SEQUENCES SQL> DESC SYS.user_sequences; Name Null? Type ------------------------------- -------- ---- SEQUENCE_NAME NOT NULL VARCHAR2(30) MIN_VALUE NUMBER MAX_VALUE NUMBER INCREMENT_BY NOT NULL NUMBER CYCLE_FLAG VARCHAR2(1) ORDER_FLAG VARCHAR2(1) CACHE_SIZE NOT NULL NUMBER LAST_NUMBER NOT NULL NUMBER Prep by Dr. Frank Chao SQL & SQL*Plus 13 Prep by Dr. Frank Chao SQL & SQL*Plus 14 What Is an Index? Is a schema object used by the Oracle Server to speed up the retrieval of rows by using a pointer Can reduce disk I/O by using rapid path access method to locate the data quickly Is independent of the table it indexes This means that they can be created or dropped at any time and have no effect on the base tables or other indexes. Is used and maintained automatically by the Oracle Server Note: When you drop a table, corresponding indexes are also dropped. How Are Indexes Created? Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition. Manually: Users can create non-unique indexes on columns to speed up access time to the rows. Prep by Dr. Frank Chao SQL & SQL*Plus 15 Prep by Dr. Frank Chao SQL & SQL*Plus 16 4

Creating an Index Create an index on one or more columns. CREATE INDEX index ON table (column[, column]...); Improve the speed of query access on the ENAME column in the EMP table. SQL> CREATE INDEX emp_ename_idx 2 ON emp(ename); Index created. When to Create an Index? The column is used frequently in the WHERE clause or in a join condition. The column contains a wide range of values. The column contains a large number of null values. Two or more columns are frequently used together in a WHERE clause or a join condition. The table is large and most queries are expected to retrieve less than 2 4% of the rows. Note: The above is extremely important if you take Oracle Certification exam. Prep by Dr. Frank Chao SQL & SQL*Plus 17 Prep by Dr. Frank Chao SQL & SQL*Plus 18 When Not to Create an Index? The table is small. The columns are not often used as a condition in the query. Most queries are expected to retrieve more than 2 4% of the rows. The table is updated frequently. Note: Again, the above is also extremely important if you will take Oracle Certification exam. Confirming Indexes The USER_INDEXES data dictionary view contains the name of the index and its uniqueness. The USER_IND_COLUMNS view contains the index name, the table name, and the column name. SQL> SELECT ic.index_name, ic.column_name, 2 ix.uniqueness 3 FROM user_indexes ix, user_ind_columns ic 4 WHERE ic.index_name = ix.index_name 5 AND ic.table_name = 'EMP'; Prep by Dr. Frank Chao SQL & SQL*Plus 19 Prep by Dr. Frank Chao SQL & SQL*Plus 20 5

Function-Based Indexes A function-based index is an index based on expressions. The index expression is built from table columns, constants, SQL functions, and user-defined functions. SQL> CREATE TABLE test (col1 NUMBER); SQL> CREATE INDEX test_index on test(col1,col1+10); SQL> SELECT col1+10 FROM test; Removing an Index Syntax: SQL> DROP INDEX index; For example, To remove the EMP_ENAME_IDX index from the data dictionary. SQL> DROP INDEX emp_ename_idx; Index dropped. To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege. Prep by Dr. Frank Chao SQL & SQL*Plus 21 Prep by Dr. Frank Chao SQL & SQL*Plus 22 Synonyms Simplify access to objects by creating a synonym (another name for an object). Refer to a table owned by another user. Shorten lengthy object names. CREATE [PUBLIC] SYNONYM synonym FOR object; Creating and Removing Synonyms Create a shortened name for the table or a view. SQL> CREATE SYNONYM dp 2 FOR dept; Synonym Created. Drop a synonym. SQL> DROP SYNONYM dp; Synonym dropped. Note: PUBLIC- a synonym accessible to all users Prep by Dr. Frank Chao SQL & SQL*Plus 23 Prep by Dr. Frank Chao SQL & SQL*Plus 24 6

Summary Automatically generate sequence numbers by using a sequence generator. View sequence information in the USER_SEQUENCES data dictionary table. Create indexes to improve query retrieval speed. View index information in the USER_INDEXES dictionary table. Use synonyms to provide alternative names for objects. Prep by Dr. Frank Chao SQL & SQL*Plus 25 7