WHAT SEQUENCE OBJECTS ARE (AND ARE NOT) Louis Davidson Data Architect.

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 13 Managing Databases with SQL Server 2000.
Practical uses of new T-SQL functionality in SQL Server 2012 & Windows Azure SQL Database.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Chapter 6 Additional Database Objects
Database Technical Session By: Prof. Adarsh Patel.
By Lecturer / Aisha Dawood 1.  You can control the number of dispatcher processes in the instance. Unlike the number of shared servers, the number of.
Stored Procedures, Transactions, and Error-Handling
Oracle’s take on joins Where it differs from ANSI standard.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 5 Sequences.
November 6-9, Seattle, WA Triggers: Born Evil or Misunderstood? Louis Davidson.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
WHAT SEQUENCE OBJECTS ARE (and are not) Presentation by Louis Davidson
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Objectives Database triggers and syntax
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Visual Programing SQL Overview Section 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
Louis Davidson drsql.org.  Introduction  Trigger Coding Basics  Designing a Trigger Solution  Advanced Trigger Concepts  Summary.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
Louis Davidson drsql.org.  Introduction  Designing a Trigger Solution  Trigger Coding Basics  Advanced Trigger Concepts  Summary SQL Saturday East.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL Creating and Managing Tables
SQL Saturday Birmingham #112
ITEC 313 Database Programming
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 5 Sequences.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Transactions, Locking and Query Optimisation
Database systems Lecture 3 – SQL + CRUD
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Improving the Performance of Functions
Presentation transcript:

WHAT SEQUENCE OBJECTS ARE (AND ARE NOT) Louis Davidson Data Architect

A CANDIDATE FOR THE PRIZE FOR THE WORLD’S LONGEST PRESENTATION (ON THE WORLD’S SMALLEST TOPIC) Louis Davidson Data Architect

Who is this guy? Been in IT for over 17 years Microsoft MVP For 7 Years Written five books on database design Ok, so they were all versions of the same book. They at least had slightly different titles each time Worked for Christian Broadcasting Network for nearly 15 years.

Sequence Objects are: Whole Number Generators Additive progression next value = current value + factor Objects They are schema owned objects. They have standard permissions (UPDATE only) Guaranteed to generate a value in order (with cycles if desired) Allows you to use them to order data

Sequence Objects are: Far less limited in their use as opposed to identities In a table, identity property limited to 1 column, you can have > 1 column with a default using a sequence Identity property applicable only to table column A reasonable replacement for identity generated values Far more flexible Can be added to column after creation, and removed from column anytime Column values will are updatable Tunable – knobs available to tune how values are cached

Sequence Objects are not: Usable exactly like identity property No way to get the last one used in scope Doesn’t automatically skip defaulted column in INSERT “Naked” insert - INSERT TABLE VALUES (1,2) The table metadata will not know about the sequence No special syntax for working with identity column in a table A direct replacement for: A numbers table ROW_NUMBER() windowing function

Sequence Objects are not: Available as a temporary object Subject to Transactions Allows for highly concurrent usage Every value generated is lost even if ROLLBACK Uses locks for concurrency, but not held in any isolation level beyond getting next value Exception: ALTER SEQUENCE in transaction will block Users of that sequence Other SEQUENCE creates/alters too Slower than identity generated values Performance characteristics directly tied to caching and need to persist metadata

v v Identity syntax and management overview

All parameters set at table create (or when adding an identity column using ALTER TABLE) Management tied directly to the table Get recently entered value using one of the following SCOPE_IDENTITY() – Within current execution scope – In current session IDENT_CURRENT('tablename’) – On any session (even someone else’s) To insert values manually, need: SET IDENTITY_INSERT ON …. SET IDENTITY_INSERT OFF CREATE TABLE ( int NOT NULL IDENTITY(, )

Identity management IDENT_INCR(' ‘) gets increment IDENT_SEED(' ') gets the original seed IDENT_CURRENT(' ') gets the current identity value across connections $IDENTITY can be used in query instead of identity column name Removing data from table DELETE leaves identity value alone TRUNCATE TABLE resets the value to the original seed DBCC CHECKIDENT to reset/fix values Cannot change the increment without dropping and recreating column

v v Sequence syntax and management overview

All parameters must be constants No variables Frustrating, but can use dynamic SQL CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

Datatype Bigint, Int, SmallInt, TinyInt, Numeric(N,0) Can also be an alias type that is based on one of these types Datatype by default is bigint CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

START WITH Tells the sequence where to start Default is to start with the minimum value for datatype (includes negative values) Note for column compression, min/max values compress less than small numbers CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

INCREMENT BY Tells the sequence how much to add to value to get next value Positive or negative values allowed CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

MINVALUE and MAXVALUE Defines lowest and highest values allowed Default values of NO MINVALUE or NO MAXVALUE is the minimum and maximum values for the datatype This is independent of the STARTWITH value, if specified. When MINVALUE or MAXVALUE is reached, how this is handled is dependent on CYCLE setting CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

CYCLE CYCLE indicates that when MAXVALUE (or MINVALUE for negative INCREMENT BY value) is reached, it will loop back to the min or max value, depending on the INCREMENT BY VALUE CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

CACHE Allows you to performance tune the number of pre- calculated values to make available Can make a large difference Default does caching, amount controlled by SQL Server One value (the next uncached value) is stored to disk, all other values persisted in ram. Value will be restored during a restore of a backup CREATE SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

Naming Sequences are schema bound objects Eg. Mustn't be named the same as any table, view, procedure, function, etc. in the same schema My naming standard is to name them: [ ]_[purpose]_SEQUENCE Example: For surrogate key of table "Fred“: Fred_SEQUENCE, Fred_NotKey_SEQUENCE, WholeSystemOrderingValue_SEQUENCE SELECT * FROM sys.objects WHERE type_desc = 'SEQUENCE_OBJECT' SELECT * --objects columns plus FROM sys.sequences

Fetching value from Sequence OBJECT NEXT VALUE FOR function Example: SELECT NEXT VALUE FOR SchemaS.Sequence FROM SchemaT.Table Evaluated once per row Limited usage … No usage in: Queries with SET operators: UNION, UNION ALL CASE Expressions Queries with ORDER BY without OVER clause on NEXT VALUE FOR call Etc. Can be used in a function call: SchemaF.FunctionName(NEXT VALUE FOR SchemaS.Sequence)

Pretty much the exact same syntax as the CREATE SEQUENCE statement Tons of control over settings even after object is creating ALTER SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ RESTART [ WITH ] ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

RESTART lets you reset the sequence, starting at a given point. If WITH is left off, goes back to value it was started with (or last restart value) ALTER SEQUENCE [schema_name. ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ RESTART [ WITH ] ] [ INCREMENT BY ] [ { MINVALUE [ ] } | { NO MINVALUE } ] [ { MAXVALUE [ ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ ] } | { NO CACHE } ]

Inserting large numbers of rows You can allocate multiple rows at a time Use sp_sequence_get_range EXEC = = OUTPUT; Be careful with the math.. Remember increment if trying to match singleton usage 23Track # – Session #

v v Typical Usage Patterns

Typical Usage patterns Surrogate key generation (#1 by far!) Simply instead of identity Allow client to gen their own numbers Great for data warehouse loads Hash bucket (using multiple sequences in same statement/table) Basis for generating complex identifiers Multiple tables with the same value Database wide version number Numbering “actions” (not as surrogate) Can fill gaps later before processing

Artificial/Surrogate Key Generation Most (not all!) usage will be to create an artificial surrogate key for a table, for performance purposes When used this way the goal should be to hide the value of the key from the user. Gaps should be ignored It is a very widely used pattern that almost every table gets a one column surrogate key. I feel it necessary to caution you that the meaning of “surrogate” is a stand in, not a replacement All tables should have some form of natural key (more or less a value that has meaning to the user)

Artificial Key Generation Requirement: Table of Lego Sets Always find some other key to protect against duplicate data so you don’t end up with LegoSetId Name =========== M Falcon 4567 M Falcon M Falcon Scale Minifig Micro Minifig SetNumber ~~~~~~~~~~~ LegoSetId Name Scale SetNumber =========== M Falcon Minifig M Falcon Minifig M Falcon Minifig 4488

Typical Coding Patterns Identity and sequences have slightly different usage patterns making usage slightly different With sequences if you want to know the value inserted, you generate the value manually and use it in the INSERT clause For identities, we ask afterwards

Identity Usage Patterns INSERT TableName (NonIdentityColumns) VALUES ('Some Value') int = scope_identity() INSERT RelatedTableName (TableNameId) VALUES

Sequence Usage Pattern int = (NEXT VALUE FOR Seq) INSERT TableName (IdentityColumn, NonIdentityColumns) VALUES 'Some Value') INSERT RelatedTableName (TableNameId) VALUES 30Track # – Session #

Output Clause Works for both Assuming your table qualifies, can get the new values from INSERT statement directly However, there are massive limitations ( The output table cannot: Have enabled triggers defined on it. Have CHECK constraints or enabled rules. Participate on either side of a FOREIGN KEY constraint. 31Track # – Session # In other words, in “real” database, it is useless for normal tables.

v v Demos Some I will do, and more for you to play with later! What you probably have been waiting for…