POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Data Definition and Integrity Constraints
Chapter 10: Designing Databases
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
1 Constraints, Triggers and Active Databases Chapter 9.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 5/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Fundamentals, Design, and Implementation, 9/e Chapter 5 Database Design.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Programming and SQL Edel Sherratt. Motivation 1: Integrity Checking Sometimes primary keys and foreign keys are not enough For example, they do not enforce.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
Chapter 14 & 15 Conceptual & Logical Database Design Methodology
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
MIS 301 Information Systems in Organizations Dave Salisbury ( )
ABC Insurance Co. Paul Barry Steve Randolph Jing Zhou CSC8490 Database Systems & File Management Dr. Goelman Villanova University August 2, 2004.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
SQL Server 7.0 Maintaining Referential Integrity.
Normalization (Codd, 1972) Practical Information For Real World Database Design.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Consolidation Objectives of the Lecture : To review a given design of a relational database. To amplify the design by adding Integrity Constraints. To.
Constraints Review. What is a constraint? Unique – forbids duplicate values Referencial – Foreign key Check Constraint – sets restrictions on data added.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Databases Introduction - concepts. Concepts of Relational Databases.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
In this session, you will learn to: Manage databases Manage tables Objectives.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Chapter 5 Database Design
Module 5: Implementing Data Integrity by Using Constraints
COS 346 Day 8.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL DATA CONSTRAINTS.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Relational Database Design
SQL – Constraints & Triggers
CPSC-608 Database Systems
Presentation transcript:

POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt

Relations location{ name, description, is_lit } exit{ name, description, is_open, exits_from, leads_to } exit_pair{name_1, exits_from_1, name_2, exits_from_2} item{ name, description, kind, is_lit, is_at, held_by } character{ name, description, kind, location } Nowhere location: (nowhere, ' ', false) Self character: (me, myself, person, my location); Nobody character: (nobody, ' ', nonentity, nowhere)

Location create table location ( name varchar(20) primary key, description text, is_lit boolean ); insert into location (name, description, is_lit) values ('nowhere', ' ', false);

Exit create table exit ( name varchar(20), description text, is_open boolean, exits_from varchar(20) references location(name), leads_to varchar(20) references location(name), primary key(exits_from, name) );

Character with Table Inheritance create table character ( name varchar(20) primary key, description text, location varchar(20) references location(name)); create table monster () inherits (character); create table player () inherits (character);

Problems Primary and foreign key constraints are not inherited (hopefully will be in future PostgreSQL releases) Work round this using functions and triggers

Primary key: Character and descendants /* character.name is a primary key; pkey_character_name checks the inheritance hierarcy from character to ensure that name is unique and not null */ create function pkey_character_name() returns trigger as $pkey_character_name$ BEGIN if (exists (select * from character where character.name = NEW.name)) then raise exception cannot have more than one character named %.', NEW.name; end if; return NEW; END $pkey_character_name$ language plpgsql;

Triggering the not null and unique checks on monster.name create table monster () inherits (character); create trigger pkey_character_name before insert on monster for each row execute procedure pkey_character_name(); The same is needed for other descendants of character (such as player)

Foreign key reference to location: character and descendants create function valid_location() returns trigger as $valid_location$ BEGIN if not exists (select name from location where location.name = NEW.location) then raise exception 'There is no location called %', NEW.location; end if; return NEW; END $valid_location$ language plpgsql;

Triggering the referential integrity constraint on character.location create trigger valid_location before insert on monster for each row execute procedure valid_location(); The same is done for player And the same for item, which also refers to location.name And for the descendants of item

Item with table inheritance create table item ( name varchar (20) not null, description text, location varchar (20) references location(name)); create table portable_item ( held_by varchar (20) ) inherits (item);

More descendants of item create table light_source (is_lit boolean) inherits (item); create table portable_light_source () inherits (portable_item, light_source); And each of these has triggers to enforce entity and referential integrity constraints.

A domain-specific constraint /* The location of a portable item is the same as the location of its holder. When a new portable item is added to the database, its location is set to the location of its holder. */ create function no_bilocation () returns trigger as $no_bilocation$ BEGIN if (NEW.held_by != 'nobody then NEW.location := (select location from character where character.name = NEW.held_by); end if; return NEW; END $no_bilocation$ language plpgsql;

Triggering no_bilocation create trigger no_bilocation before insert on portable_item for each row execute procedure no_bilocation(); create trigger no_bilocation before insert on portable_light_source for each row execute procedure no_bilocation();

Another domain-specific constraint /* when a character changes location, all the portable items held by that character should move as well. */ create function move_portable_items () returns trigger as $move_portable_items$ BEGIN update portable_item set location = NEW.location where portable_item.held_by = NEW.name; return NEW; END $move_portable_items$ language plpgsql;

Triggering move_portable_items create trigger move_portable_items after update on character for each row execute procedure move_portable_items();

Yet another domain-specific constraint /* no_remote_pickup ensures that the held_by attribute of a portable item can only be updated to the name of a holder whose location is the same as that of the item; in other words, a character must move to the place where an item is before picking up the item. */ create function no_remote_pickup() returns trigger as $no_remote_pickup$ BEGIN if NEW.location != (select location from character where character.name = NEW.held_by) then raise exception '% must move to % in order to pick up %', NEW.held_by, NEW.location, NEW.name; end if; return NEW; END $no_remote_pickup$ language plpgsql;

Table Inheritance Convenient, but with some problems Check constraints and not null constraints are inherited, but other kinds of constraints are not Unique, Primary key and foreign key constraints are not inherited Some SQL commands default to accessing descendants; others do not Commands that default to accessing descendants use ONLY to avoid doing so

User defined composite types PostgreSQL also enables user defined composite types Composite types allow table elements to contain structured data Composite types are a kind of user defined type like those discussed in connection with object-relational database management systems.

Functions and Triggers Primary use: to implement domain-specific constraints at the database level Also used to work round lack of constraint inheritance in this example Typically: Define a function that returns a named trigger Then add that trigger to one or more tables

Conclusion Modern relational database management systems provide various extras But it is important to weigh up the benefits of these against their costs