Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.

Slides:



Advertisements
Similar presentations
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Advertisements

9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Chapter Thirteen Sequences Dr. Chitsaz Objectives: Sequence objects Create and use sequences Application of sequences.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
11-1 Copyright © Oracle Corporation, All rights reserved. Different type of keys.
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Structured Query Language (SQL)
Sequence Sen Zhang. The AutoNumber data type stores an integer that Access increments (adds to) automatically as you add new records. You can use the.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Chapter 6 Additional Database Objects
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 5 Sequences.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
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.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Controlling User Access. 2 home back first prev next last What Will I Learn? Compare the difference between object privileges and system privileges Construct.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
George Mpopo | Rosebank College ADVANCED DATABASES WITH ORACLE 11g FOR ADDB7311 LEARNING UNIT 2 of 7.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Chapter 3 Table Creation and Management Oracle 10g: SQL.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
Other database objects (Sequence and view). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically.
SQL Creating and Managing Tables
ITEC 313 Database Programming
Oracle Certified 1z0-047 Exam Questions
Creating Other Schema Objects
Other Database Objects
SQL Creating and Managing Tables
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
SQL Creating and Managing Tables
Chapter 5 Sequences.
Database Programming Sections 11-12–Sequences, Indexes & Synonyms, Controlling User Access, Creating & Revoking Object Privileges 11/25/08.
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
IST 318 Database Administration
Other Database Objects
Presentation transcript:

Other database objects (Sequence)

What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to create a primary key value Sequence numbers are stored and generated independently of tables. Therefore, the same sequence can be used for multiple tables

The CREATE SEQUENCE Statement Syntax Define a sequence to generate sequential numbers automatically. Syntax: CREATE SEQUENCE sequence INCREMENT BY n START WITH n MAXVALUE n [CYCLE | NOCYCLE] [CACHE n | NOCACHE];

The CREATE SEQUENCE Statement Syntax INCREMENT BY n specifies the interval between sequence numbers where n is an integer (If this clause is omitted, the sequence increments by 1.) START WITH n specifies the first sequence number to be generated (If this cla use is omitted, the sequence starts with 1.) MAXVALUE n specifies the maximum value the sequence can generate CYCLE | NOCYCLE specifies whether the sequence continues to generate values after reaching its maximum value (NOCYCLE is the default option.) CACHE n | NOCACHE specifies how many values the Oracle Server preallocates and keep in memory (By default, the Oracle Server caches 20 values.)

Creating a Sequence Create a sequence named DEPT_DEPTID_SEQ to be used for the primary key of the DEPARTMENTS table. Do not use the CYCLE option. CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE;

NEXTVAL and CURRVAL 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.

Using a Sequence Insert a new department named “Support” in location ID INSERT INTO departments VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500); View the current value for the DEPT_DEPTID_SEQ sequence. SELECT dept_deptid_seq.CURRVAL FROM dual;

The DUAL Table The DUAL table is owned by the user SYS and can be accessed by all users. It contains one column, DUMMY, and one row with the value X. The DUAL table is useful when you want to return a value once only: for instance, the value of a constant, or expression that is not derived from a table with user data. The DUAL table is generally used for SELECT clause because both SELECT and FROM clauses are mandatory, and several calculations do not need to select from actual tables.

Rules for Using NEXTVAL and CURRVAL You can use NEXTVAL and CURRVAL in the following contexts: The SELECT list of a SELECT statement that is not part of a subquery The SELECT list of a subquery in an INSERT statement The VALUES clause of an INSERT statement The SET clause of an UPDATE statement You cannot use NEXTVAL and CURRVAL in the following contexts: The SELECT list of a view A SELECT statement with the DISTINCT keyword A SELECT statement with GROUP BY, HAVING, or ORDER BY clauses A subquery in a SELECT, DELETE, or UPDATE statement The DEFAULT expression in a CREATE TABLE or ALTER TABLE statement

Modifying a Sequence Change the increment value, maximum value, minimum value, ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE NOCACHE NOCYCLE;

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 START WITH option cannot be changed using ALTER SEQUENCE. The sequence must be dropped and re-created in order to restart the sequence at a different number.

Guidelines for Modifying a Sequence Some validation is performed. For example, a new MAXVALUE that is less than the current sequence number cannot be imposed. ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 90 NOCACHE NOCYCLE; ALTER SEQUENCE dept_deptid_seq * ERROR at line 1: ORA-04009: MAXVALUE cannot be made to be less than the currentvalue

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. DROP SEQUENCE dept_deptid_seq; You must be the owner of the sequence or have the DROP ANY SEQUENCE privilege to remove it.