By: Lloyd Albin 11/6/2012. Serials are really integers that have a sequence attached to provide the capability to have a auto incrementing integer. There.

Slides:



Advertisements
Similar presentations
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
CPIT 102 CPIT 102 CHAPTER 1 COLLABORATING on DOCUMENTS.
How to corrupt your data by accident BY: LLOYD ALBIN 9/3/2013.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Database Design.  Define a table for each entity  Give the table the same name as the entity  Make the primary key the same as the identifier of the.
Excel 2007 Part (2) Dr. Susan Al Naqshbandi
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Security, Transactions, and Views. Security Achieved through GRANT & REVOKE Assumes the database can recognize its users and verify their identity can.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Lesson 17 Getting Started with Access Essentials
© Logicalis Group Using DB2/400 effectively. Data integrity facilities Traditional iSeries database usage Applications are responsible for data integrity.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Maintaining a Database Access Project 3. 2 What is Database Maintenance ?  Maintaining a database means modifying the data to keep it up-to-date. This.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
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.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
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.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Lesson 4.  After a table has been created, you may need to modify it. You can make many changes to a table—or other database object—using its property.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Work with Tables and Database Records Lesson 3. NAVIGATING AMONG RECORDS Access users who prefer using the keyboard to navigate records can press keys.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
1 CS 430 Database Theory Winter 2005 Lecture 7: Designing a Database Logical Level.
Access Module Implementing a Database with Microsoft Access A Great Module on Your CD.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Copyright © Curt Hill SQL The Data Manipulation Language.
Copyright © 2009 Pearson Education, Inc. Publishing as Prentice Hall. 1 Skills for Success with Microsoft ® Office 2007 PowerPoint Lecture to Accompany.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Tables and Triggers.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Creating and Managing Tables
and Defining Table Relationships
Designing Tables for a Database System
Module 5: Implementing Data Integrity by Using Constraints
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL DATA CONSTRAINTS.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Presentation transcript:

By: Lloyd Albin 11/6/2012

Serials are really integers that have a sequence attached to provide the capability to have a auto incrementing integer. There are three sizes of serials: NameStorage SizeRange smallserial2 bytes1 to 32,767 serial4 bytes1 to 2,147,483,647 bigserial8 bytes1 to 9,223,372,036,854,775,807

The simplest way to create a serial is to use the serial type. CREATE TABLE tablename ( colname SERIAL ); Using the serial type will cause this notice to show up. NOTICE: CREATE TABLE will create implicit sequence "tablename_colname_seq" for serial column "tablename.colname"

The notice can be avoided by creating a serial the long method. CREATE SEQUENCE tablename_colname_seq; CREATE TABLE tablename ( colname integer NOT NULL DEFAULT nextval('tablename_colname_seq') ); ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

The dump method does the same thing but in a different order and does two extra command. CREATE TABLE tablename ( colname integer NOT NULL ); CREATE SEQUENCE tablename_colname_seq; ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname; SELECT pg_catalog.setval('tablename_colname_seq', 1, true); ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename_colname_seq'::regclass);

Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A NOT NULL constraint is applied to ensure that a null value cannot be inserted. (In most cases you would also want to attach a UNIQUE or PRIMARY KEY constraint to prevent duplicate values from being inserted by accident, but this is not automatic.) Lastly, the sequence is marked as "owned by" the column, so that it will be dropped if the column or table is dropped. The sequence created for a serial column is automatically dropped when the owning column is dropped. You can drop the sequence without dropping the column, but this will force removal of the column default expression.

When you need to move a table with a serial, you may just simply move the table and the sequence will automatically move with the table. ALTER TABLE old_schemaname.tablename SET SCHEMA new_schemaname;

When you need to delete a table with a serial, you may just simply delete the table and the sequence will automatically be deleted with the table. DROP TABLE tablename; Even if you use restrict, the sequence will be deleted. DROP TABLE tablename RESTRICT;

According to the docs, 9.2 and older, you may delete the sequence and this will remove the default expression. DROP SEQUENCE tablename_colname_seq; ERROR: cannot drop sequence tablename_colname_seq because other objects depend on it DETAIL: default for table tablename column colname depends on sequence tablename_colname_seq HINT: Use DROP... CASCADE to drop the dependent objects too.

So now we will try dropping the sequence with a cascade command. DROP SEQUENCE tablename_colname_seq CASCADE; This works perfectly leaving the table and having removed the default and switching the serial to a integer.

Everything seems to be great. You go along moving tables/views/etc to other schemas, so anything left over must me ok to drop before dropping the schema. There might even be some sequences that you can drop without any error messages. But the problem appears later when you try entering data into a table and receive this message. INSERT INTO tablename VALUES (DEFAULT); ERROR: relation "tablename_colname_seq" does not exist

So while these two serials may look the same on the outside, they don’t act the same and so must be different on the inside.

When looking closely at two tables, one that the sequence moved and one that the sequence did not move, I noticed that they were slightly different. Working Style – Sequence moves: CREATE TABLE tablename ( colname integer DEFAULT nextval('tablename_colname_seq'::regclass) NOT NULL ); Problem Style – Sequence does not move: CREATE TABLE tablename ( colname integer DEFAULT nextval(('tablename_colname_seq'::text)::regclass) NOT NULL );

To fix the default value, you need to remove the text by issuing an alter table command. ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename_colname_seq'::regclass);

If you now move the tables to a different schema and then try to delete the sequence in the old schema you will get the error message about “cannot drop sequence”. This is good because we are now seeing some referential integrity. But they sequence did not move with the table. If you delete the table the sequence will not be deleted. So there is currently only one way referential integrity.

With my GUI tools the tables/sequence now look the same, so I dumped both types of tables and found one more difference. ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

Now the table acts the way we expect it should. When we move the table, the sequence moves also. We are not allowed to delete the sequence without deleting the table. If we delete the table, the sequence is automatically deleted. We now have bi-directional referential integrity.

When looking at the Postgres documentation about Sequence Functions, I found when everything changed. It happened between versions 8.0 and 8.1. If you have any serials created in 8.0 or before and are now using 8.1 or newer, you do not have any referential integrity on those early serials but will have either one- way or bi-directional referential integrity on the newer serials.

If you have a 1-1 relationship, sequence to table, then you will want bi-directional referential integrity. If you have a 1 to many relationship, one sequence to many tables, then you will want one way referential integrity. This one way relationship keeps you from dropping the sequence when other tables still rely on it. But you must move the sequence separately when moving the tables.

Note: Before PostgreSQL 8.1, the arguments of the sequence functions were of type text, not regclass, and the above-described conversion from a text string to an OID value would happen at run time during each call. For backwards compatibility, this facility still exists, but internally it is now handled as an implicit coercion from text to regclass before the function is invoked. When you write the argument of a sequence function as an unadorned literal string, it becomes a constant of type regclass. Since this is really just an OID, it will track the originally identified sequence despite later renaming, schema reassignment, etc. This "early binding" behavior is usually desirable for sequence references in column defaults and views. But sometimes you will want "late binding" where the sequence reference is resolved at run time. To get late-binding behavior, force the constant to be stored as a text constant instead of regclass: nextval('foo'::text) foo is looked up at runtime Note that late binding was the only behavior supported in PostgreSQL releases before 8.1, so you may need to do this to preserve the semantics of old applications. Of course, the argument of a sequence function can be an expression as well as a constant. If it is a text expression then the implicit coercion will result in a run-time lookup.

What we need to do is look at all the default values and see if there are any ‘ ::text)::regclass) ’. SELECT adsrc FROM pg_catalog.pg_attrdef WHERE adnum = 1 AND adsrc LIKE '%::text)::regclass)'; adsrc nextval(('schemaname.tablename_colname_seq'::text)::regclass)

The answer is yes, with conditions. 1. If you don’t use the same sequence on more than one table some of the time. It needs to be one way or the other. This is so that you can either do the OWNED BY or not. 2. If you use the default name formatting for the sequence name, this will allow you to easily figure out the table and field name, otherwise it is more complex but not impossible.

ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname; This provides referential integrity from the table to the sequence. If you move or drop the table, the sequence will either move or be dropped at the same time. ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename_colname_seq'::regclass); This provides referential integrity from the sequence to the table. You may not delete the sequence if any table relies on it. But must be of type regclass and not a re-cast to type regclass.