5.1 Database Schema. 5.1.1 Schema Generation in SQL



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

Oracle 10g PL/SQL Training

How To Create A Table In Sql (Ahem)

Instant SQL Programming

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

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

Programming Database lectures for mathema

Oracle Database: SQL and PL/SQL Fundamentals

SQL NULL s, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals NEW

Advance DBMS. Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database 12c: Introduction to SQL Ed 1.1

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

Programming with SQL

14 Triggers / Embedded SQL

Information Systems SQL. Nikolaj Popov

Database Programming with PL/SQL: Learning Objectives

Maintaining Stored Procedures in Database Application

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Database Programming in SQL/ORACLE. Wolfgang May

CSC 443 Data Base Management Systems. Basic SQL

ICAB4136B Use structured query language to create database structures and manipulate data

Oracle Database 10g: Introduction to SQL

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

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

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

Lecture 6. SQL, Logical DB Design

DBMS / Business Intelligence, SQL Server

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

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

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

CS346: Database Programming.

5. CHANGING STRUCTURE AND DATA

The Relational Model. Why Study the Relational Model?

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

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

PL / SQL Basics. Chapter 3

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

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

1 SQL Data Types and Schemas

Databases 2011 The Relational Model and SQL

Oracle Database 11g SQL

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Oracle Database: Introduction to SQL

Data Modeling. Database Systems: The Complete Book Ch ,

The Relational Data Model and Relational Database Constraints

SQL Server. 1. What is RDBMS?

Oracle Database: Introduction to SQL

MySQL for Beginners Ed 3

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

IT2305 Database Systems I (Compulsory)

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

Introduction to Database. Systems HANS- PETTER HALVORSEN,

Once the schema has been designed, it can be implemented in the RDBMS.

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Chapter 5. SQL: Queries, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

Generating XML from Relational Tables using ORACLE. by Selim Mimaroglu Supervisor: Betty O NeilO

A Brief Introduction to MySQL

Database Design and Programming

3.GETTING STARTED WITH ORACLE8i

Best Practices in SQL Programming. Madhivanan

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Introduction to Microsoft Jet SQL

Oracle Database: Introduction to SQL

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

IT2304: Database Systems 1 (DBS 1)

Introduction to Triggers using SQL

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

Chapter 6: Integrity Constraints

Databases. DSIC. Academic Year

Database 2 Lecture I. Alessandro Artale

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Database Query 1: SQL Basics

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

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

DB2 for z/os: Utilities and Application Development

Introduction to PL/SQL Programming

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Transcription:

5.1 Database Schema The database schema is the complete model of the structure of the application domain (here: relational schema): relations names of attributes domains of attributes keys additional constraints value constraints referential integrity constraints storage issues of the physical schema: indexes, clustering etc. also belong to the schema 195 5.1.1 Schema Generation in SQL Definition of Tables Basic form: attribute names and domains CREATE TABLE <table> (<col> <datatype>,. <col> <datatype>) domains: NUMBER, CHAR(n), VARCHAR2(n), DATE... CREATE TABLE City ( Name VARCHAR2(35), Country VARCHAR2(4), Province VARCHAR2(32), Population NUMBER, Longitude NUMBER, Latitude NUMBER ); 196

Integrity constraints Simple constraints on individual attributes are given with the attribute definitions as column constraints : domain definitions are already integrity constraints further constraints on individual attribute values more detailed range restrictions: City: CHECK (population > 0) or CHECK (longitude BETWEEN -180 AND 180) NULL values allowed? : Country: name NOT NULL Definition of key/uniqueness constraints: Country: code PRIMARY KEY or name UNIQUE 197 Integrity constraints (Cont d) Constraints on several attributes are given separately as table constraints : CREATE TABLE <table> (<column definitions>, <table-constraint>,...,<table-constraint>) table-constraints have a name must state which columns are concerned CREATE TABLE City ( Name VARCHAR2(35), Country VARCHAR2(4), Province VARCHAR2(32), Population NUMBER CONSTRAINT CityPop CHECK (Population >= 0), Longitude NUMBER CONSTRAINT CityLong CHECK (Longitude BETWEEN -180 AND 180), Latitude NUMBER CONSTRAINT CityLat CHECK (Latitude BETWEEN -90 AND 90), CONSTRAINT CityKey PRIMARY KEY (Name, Country, Province));... for details see Practical Training SQL. 198

Integrity constraints (Cont d) up to now: only intra-table constraints General Assertions inter-table constraints e.g., sum of inhabitants of provinces equals the population of the country, sum of inhabitants of all cities of a country must be smaller the than population of the country SQL standard: CREATE ASSERTION not supported by most systems other solution: later 199 5.1.2 Referential Integrity Constraints important part of the schema relate foreign keys with their corresponding primary keys: Europe R encompasses name code Country Continent Percent VARCHAR(4) VARCHAR(20) NUMBER Continent encompasses Country R Europe 20 percent R Asia 80 D Europe 100 20......... encompasses.country country.code and encompasses.continent continent.name other examples: city.country country.code and country.(capital,province,code) city.(name,province,country) 200

Referential Integrity Constraints: SQL Syntax as column constraints (only single-column keys): <column-name> <datatype> REFERENCES <table>(<column>) as table constraints (also compound keys): CONSTRAINT <name> FOREIGN KEY (<column-list>) REFERENCES <table>(<column-list>) CREATE TABLE ismember (Country VARCHAR2(4) REFERENCES Country(Code), Organization VARCHAR2(12) REFERENCES Organization(Abbreviation), Type VARCHAR2(30)); CREATE TABLE City ( Name VARCHAR2(35), Country VARCHAR2(4) REFERENCES Country(Code), Province VARCHAR2(32), Population NUMBER..., Longitude NUMBER..., Latitude NUMBER..., CONSTRAINT CityKey PRIMARY KEY (Name, Country, Province), FOREIGN KEY (Country,Province) REFERENCES Province (Country,Name) ); 201 5.1.3 Virtual Tables: Views Views are tables that are not materialized, but defined by a query against the database: CREATE VIEW <name> AS <query> CREATE OR REPLACE VIEW symm borders AS SELECT * FROM borders UNION SELECT Country2, Country1, Length FROM borders; SELECT country2 FROM symm borders WHERE country1= D ; classical views: the content of a view is always computed when it is queried. Materialized Views: view is materialized and automatically maintained view maintenance problem: when a base table changes, what modifications have to be applied to which views? 202

5.2 SQL: Data Manipulation Language... everything is based on the structure of the SELECT-FROM-WHERE clause: Deletions: DELETE FROM... WHERE... Updates: UPDATE <table> SET <attribute> = <value>,..., <attribute> = <value> WHERE... value can be a subquery (also a correlated one) Insertions: INSERT INTO <table> VALUES (...) INSERT INTO <table> (SELECT... FROM... WHERE...) 203 5.3 SQL: The DATE Datatype... and Customization many applications in business and administration use dates computations on dates (e.g., last of the third month after..., number of days between ) SQL provides comprehensive datatypes DATE, TIME, TIMESTAMP A More General View I: Datatypes DATE etc. are just some (important and typical) examples of built-in datatypes specific operators (and behavior, cf. the XMLTYPE datatype in the SQLX standard) handled via one or more lexical representations as strings 204

A MORE GENERAL VIEW II: INTERNATIONALIZATION AND CUSTOMIZATION Database systems are used anywhere in the world (like most software), and their contents is exchanged all over the world people use different languages (e.g. for error messages!) people use different representations even for numbers: 3,1415 vs. 1.000.000 (german), 3.14 vs. 1,000,000 (anywhere else) for dates: 31.12.2007, 12/31/2007 or 12-31-2007 (USA), 01-JAN-2003 etc., 01 Janeiro 2003 even language dependent. 205 SQL: INTERNATIONALIZATION AND CUSTOMIZATION This issue is handled syntactically differently (but using the same idea) between different products. Oracle: Natural Language Support NLS LANG (language and localization issues in general), NLS NUMERIC CHARACTERS (decimal point/dezimalkomma) and NLS DATE FORMAT (date format), NLS SORT (sorting order) ALTER SESSION SET NLS LANG = Language Territory.CharacterSet ; Language: error messages, etc, Territory: more detailed formats (America/Canada/UK) including default for decimal point and date format. ALTER SESSION SET NLS NUMERIC CHARACTERS =,. ; german style, ALTER SESSION SET NLS NUMERIC CHARACTERS =., ; ALTER SESSION SET NLS DATE FORMAT = string-pattern, e.g. DD.MM.YYYY, DD-MON-YY, DD hh:mm:ss 206

SQL: Internationalization and Customization Then, e.g., INSERT INTO Politics VALUES( D, 18.01.1871, federal republic ) is correctly interpreted. In the output, DATE values are also represented in the currently specified format. SQL provides comprehensive datatypes DATE, TIME, TIMESTAMP semantics: year/month/date/hour/minute/second timestamp: additionally fractions of seconds as decimal (Oracle: only DATE and TIMESTAMP) built-in calendar knows about length of months, leap years etc. operators on date and time: date + days MONTHS BETWEEN(date 1, date 2 ), ADD MONTHS(date, n), LAST DAY(date) SYSDATE syntax: different syntactical representations/formats can be chosen for input/output: 207 The DATE Datatype: Example CREATE TABLE Politics ( Country VARCHAR2(4), Independence DATE, Government VARCHAR2(120)); ALTER SESSION SET NLS DATE FORMAT = DD MM YYYY ; INSERT INTO politics VALUES ( B, 04 10 1830, constitutional monarchy ); All countries that have been founded between 1200 und 1600: SELECT Country, Independence FROM Politics WHERE Independence BETWEEN 01 01 1200 AND 31 12 1599 ; Land Datum MC 01 01 1419 NL 01 01 1579 E 01 01 1492 THA 01 01 1238 208

5.4 Beyond Relational Completeness The Relational Algebra and SQL are only relationally complete. can e.g. not compute the transitive closure of a relation applications require a more complex behavior: SQL als the core query language with something around it... 209 MAKING SQL TURING-COMPLETE embedded SQL in C/Pascal: EXEC SQL SELECT... FROM... WHERE... embedded into Java: JDBC (Java Database Connectivity) SQL-92: Procedural Extensions to SQL: CREATE procedures and functions as compiled things inside the database standardized concepts, but product-specific syntax basic programming constructs of a typical Turing-complete language: Variables, BEGIN... END, IF... THEN... ELSIF..., WHILE... LOOP..., FOR... LOOP SQL can be used inside PL/SQL statements 210

IMPEDANCE MISMATCH BETWEEN DB AND PROGRAMMING LANGUAGES (cf. Slide 3) Set-oriented (relations) vs. value-oriented (variables) how to handle the result of a query in C/Pascal/Java? Iterators (common programming pattern for all kinds of collections) explicit: new/init( < query > )/open() first(), next(), isempty() fetch() (into a record/tuple variable) implicit (PL/SQL s Cursor FOR LOOP ): FOR <record-variable> IN <query> LOOP do something with <record-variable> END LOOP;... for details see Practical Training SQL. 211 5.5 Integrity Maintenance if a tuple is changed/inserted/deleted it is immediately checked whether all constraints in the current database state are satisfied afterwards. Otherwise the operation is rejected. if a constraint is defined/enabled, it is immediately checked whether it is satisfied by the current database state. Otherwise the operation is rejected. Any further possibilities? 212

Integrity Maintenance (Cont d): referential integrity Consider again country - organization - is member: ismember.organization organization.abbrev ismember.country country.code deletion of a membership entry: no problem deletion of a country: any membership entries for it are now dangling remove them! Referential Actions FOREIGN KEY ismember(country) REFERENCES country(code) ON DELETE CASCADE ON DELETE CASCADE: delete referencing tuple ON DELETE RESTRICT: referenced tuple cannot be deleted ON DELETE NO ACTION: referenced tuple can be deleted if the same transaction also deletes the referencing tuple ON DELETE SET NULL: foreign key of referencing tuple is set to NULL ON DELETE SET DEFAULT: foreign key of referencing tuple is set to a default value same for ON UPDATE 213 Referential Actions Country Name Code Capital Province Germany D Berlin Berlin United States USA Washington Distr. Columbia............ CASCADE NO ACTION City Name Country Province Berlin D Berlin Washington USA Distr. Columbia......... 1. DELETE FROM City WHERE Name= Berlin ; 2. DELETE FROM Country WHERE Name= Germany ; 3. UPDATE Country SET code= DE WHERE code= D ; 214

Referential Actions: Problems Country Name Code Capital Province Germany D Berlin Berlin United States US Washington Distr.Col............. SET NULL CASCADE City Name Country Province Berlin D B Washington USA Distr.Col.......... CASCADE Province Name Country Capital Berlin D Berlin Distr.Col. US Washington......... DELETE FROM Country WHERE Code= D... ambiguous semantics! see http://dbis.informatik.uni-goettingen.de/refint. 215... active behavior/reaction on events! 5.6 Active Databases/Triggers reacting on an event external event/signal internal event: modification/insertion/deletion internal event: time if a condition is satisfied then do something/execute an action ECA: Event-Condition-Action rules 216

ECA-Rules Consider database updates only: one or more tuples of a table are changed. Granularity: execute action once for all updates together (e.g., afterwards, update a sum) execute action for each changed tuple (e.g. cascading update) Timepoint: after execution of original update before execution of original update instead of original update Actions: can read the before- and after value of the updated tuple read and write other tables 217 Triggers The SQL standard provides Triggers for implementation of ECA rules: CREATE TRIGGER specify event: ON {DELETE UPDATE INSERT} ON <table> specify condition WHEN <condition> specify granularityfor EACH STATEMENT ROW specify action Actions are programmed using the above-mentioned procedural extensions to SQL. Applications implement application-specific business rules integrity maintenance monitoring of assertions... for details see Practical Training SQL. 218

Chapter 6 Running a Database: Safety and Correctness Issues Transactions Safety against failure Not discussed here: Access control, Authentication 219