Data Manipulation (DML) and Data Definition (DDL)

Similar documents
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Basant Group of Institution

Mahathma Gandhi University

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Chapter 1 SQL and Data

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

SQL DATA DEFINITION LANGUAGE

Part VIII Transactions, Integrity and Triggers

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

SQL DATA DEFINITION LANGUAGE

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

SQL DATA DEFINITION LANGUAGE

CSC 261/461 Database Systems Lecture 6. Fall 2017

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

8) A top-to-bottom relationship among the items in a database is established by a

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Relational Data Model. Christopher Simpkins

The Relational Model

CS275 Intro to Databases

Oracle Database 11g: SQL and PL/SQL Fundamentals

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Introduction to Computer Science and Business

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Database Systems Relational Model. A.R. Hurson 323 CS Building

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Lab # 4. Data Definition Language (DDL)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

An Introduction to Structured Query Language

Integrity Constraints (Reminder)

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

An Introduction to Structured Query Language

Slides by: Ms. Shree Jaswal

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL STRUCTURED QUERY LANGUAGE

Database Management System 9

Index *EXTIND option, ADDPFTRG command. See CL command Alias CREATE ALIAS for, 62, 64 for a column, 22, for a table, 15-17, 62, 66-67,

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

An Introduction to Structured Query Language

Integrity and Security

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

An Introduction to Structured Query Language

Lab IV. Transaction Management. Database Laboratory

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Enhanced Data Models for Advanced Applications. Active Database Concepts

An Introduction to Structured Query Language

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

SQL: Data Definition Language

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

The Relational Model Constraints and SQL DDL

Oracle Database: SQL and PL/SQL Fundamentals NEW

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

Midterm Review. Winter Lecture 13

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Chapter 1: Introduction

Chapter 4: Intermediate SQL

CSEN 501 CSEN501 - Databases I

Relational Database Systems Part 01. Karine Reis Ferreira

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

DATABASE DESIGN - 1DL400

The Relational Model of Data (ii)

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Basic SQL. Basic SQL. Basic SQL

Solved MCQ on fundamental of DBMS. Set-1

Database Applications (15-415)

Data about data is database Select correct option: True False Partially True None of the Above

5. Single-row function

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Chapter 2. DB2 concepts

1) Introduction to SQL

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

DATABASES SQL INFOTEK SOLUTIONS TEAM

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

SQL. SQL DDL Statements

Slides by: Ms. Shree Jaswal

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Lecture 2: Introduction to SQL

A database consists of several tables (relations) AccountNum

Oracle Create Table Foreign Key On Delete No

SQL Interview Questions

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

DATABASE DESIGN - 1DL400

Transcription:

Data Manipulation (DML) and Data Definition (DDL) 114

SQL-DML Inserting Tuples INSERT INTO REGION VALUES (6,'Antarctica','') INSERT INTO NATION (N_NATIONKEY, N_NAME, N_REGIONKEY) SELECT NATIONKEY, NAME, 6 FROM ANTARCTIC_NATIONS Updating Tuples UPDATE LINEITEM SET L_DISCOUNT = L_DISCOUNT + 0.01 WHERE L_SUPPKEY = 12 Deleting Tuples DELETE FROM REGION WHERE R_REGIONKEY > 5 115

SQL-DML (2) Merging Data (MERGE) Combination of INSERT, DELETE and UPDATE (SQL:2003) Merges two relations in a predetermined manner MERGE INTO <table1> USING <table2> <fullselect> ON <condition> WHEN [NOT] MATCHED THEN <operation> Parameters <table1>: table which should be updated <table2>: new data <condition>: tuple comparison (when are two tuples equal?) MATCHED: Tuples exist in both relations NOT MATCHED: Tuple appear only in the new relation <operation>: Action (UPDATE, INSERT, DELETE) (Not fully supported in H2) 116

SQL-DML (3) Example (MERGE) Two relations: PERSONS (left) and NEW_PERSONS (right) NAME SALARY NAME SALARY ------- --------- ------- --------- Paul 2000 Fritz 1000 Fritz 1500 Dirk 3000 Merging both relations MERGE INTO PERSONS P USING NEW_PERSONS N ON P.NAME = N.NAME WHEN MATCHED THEN UPDATE SET P.SALARY = N.SALARY WHEN NOT MATCHED THEN INSERT (NAME,SALARY) VALUES (N.NAME, N.SALARY) Result (PERSONS) NAME SALARY ------- --------- Paul 2000 Fritz 1500 Dirk 3000 117

DB2 Database Objects Relation (TABLE) Unordered set of tuples Permanent or temporary View (VIEW) Virtual relation e.g. simplification of queries, user-defined view on data Index (INDEX) Primary or secondary Data structure to store / find data Additional: preserving uniqueness and sorting What else DATABASE (not in H2), TABLESPACE (not in H2), TYPE (DOMAIN in H2), SCHEMA, BUFFERPOOL (not in H2), TRIGGER, USER, CONSTANT, ALIAS, 118

SQL-DDL Definition of Database Objects (DDL) e.g. table Creating using CREATE command CREATE TABLE REGION ( R_REGIONKEY INTEGER NOT NULL PRIMARY KEY, R_NAME CHAR(25) NOT NULL, R_COMMENT VARCHAR(152) ) Deleting using DROP DROP TABLE REGION Changing using ALTER ALTER TABLE REGION ADD COLUMN AREA INT 119

SQL-DDL (2) Integrity Constraints (constraints) Primary Key, Foreign Key, Distinct values (UNIQUE), value-based (CHECK) ALTER TABLE <table> ADD CONSTRAINT <constraint-name> PRIMARY KEY (<attribute-list>) FOREIGN KEY (<attr-list>) REFERENCES <table>(<attr-list>) UNIQUE (<attribute-list>) CHECK (<predicate>) Example ALTER TABLE REGION ADD CONSTRAINT MAX5 CHECK (R_REGIONKEY BETWEEN 1 AND 5) 120

SQL-DDL (3) View (VIEW) Virtual relation to simplify queries and to define user-specific representations Also used for security reasons to hide tuples or attributes Specified by SQL queries Example CREATE VIEW NAT_REG AS SELECT N_NAME, R_NAME FROM NATION, REGION WHERE N_REGIONKEY = R_REGIONKEY SELECT * FROM NAT_REG N_NAME R_NAME ------------- -------- ALGERIA AFRICA MOZAMBIQUE AFRICA MOROCCO AFRICA 121

Modifying Views Modifying Views INSERT, DELETE and UPDATE possible, if Alternative 1: INSTEAD OF trigger is defined Alternative 2: specific constraints are met http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0000935.htm WITH CHECK OPTION New or updated tuples must fulfill the view constraints Example CREATE VIEW R AS SELECT R_REGIONKEY, R_NAME FROM REGION WHERE R_REGIONKEY<10 WITH CHECK OPTION INSERT INTO R VALUES (8, 'A') OK INSERT INTO R VALUES (11, 'B') Error 122

CREATE TABLE: Example SQL-statement defining a table FootballMatch containing the results of football matches in the national league: table elements table name CREATE TABLE FootballMatch ( Date date, HomeTeam text, GoalsH number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), GuestTeam text, GoalsG number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), Round number(15) NOT NULL CHECK ( > 0 AND < 35), PRIMARY KEY (Date, HomeTeam), FOREIGN KEY ( HomeTeam ) REFERENCES Teams, FOREIGN KEY ( GuestTeam ) REFERENCES Teams ) ; 123

CREATE TABLE: General structure Each table definition consists of two parts: The definitions of the individual columns, and (possibly) constraints valid for the entire table: column definitions table constraints CREATE TABLE FootballMatch ( Date date, HomeTeam text, GoalsH number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), GuestTeam text, GoalsG number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), Round number(15) NOT NULL CHECK ( > 0 AND < 35), PRIMARY KEY (Date, HomeTeam), FOREIGN KEY ( HomeTeam ) REFERENCES Teams, FOREIGN KEY ( GuestTeam ) REFERENCES Teams ) ; 124

CREATE TABLE: Column definitions Each column definition itself consists of two parts, too: the declaration of a column name and a type of its values (possibly) special constraints for the values in this column CREATE TABLE FootballMatch ( Date date, HomeTeam text, GoalsH number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), GuestTeam text, GoalsG number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), Round number(15) NOT NULL CHECK ( > 0 AND < 35),... ) Syntax of column definitions: unique within the same table <column-name> { <data-type> <domain> } [ <column-constraints> ] alternatives 125

Data types and domains As in each programming language: There are various predefined data types for column entries (i.e., for fields of the table) Attention! Data type names in Access-SQL deviate from those of the SQLstandard (See books or tutorials for more details.) In addition: Application-specific value domains can be introduced via separate domain definitions, e.g. default value domain constraint CREATE DOMAIN big_eu_capitals AS text(15) DEFAULT??? CHECK ( VALUE IN ( Paris, London, Berlin, Rome, Madrid, Brussels, Vienna,??? ) ) Self-defined domains can be used like basic predefined data types in column definitions. In this Introduction: no further discussion of the domain concept 126

CREATE TABLE: Column Constraints Each column definition itself consists of two parts, too: the declaration of a column name and a type of its values (possibly) special constraints for the values in this column CREATE TABLE FootballMatch ( Date date, HomeTeam text, GoalsH number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), GuestTeam text, GoalsG number(15) DEFAULT NULL CHECK ( > = 0 OR IS NULL), Round number(15) NOT NULL CHECK ( > 0 AND < 35),... ) left-hand side remains implicit: current column Syntax of column constraints: [ NOT NULL UNIQUE ] [ PRIMARY KEY ] [ DEFAULT { <literal> NULL } ] [ REFERENCES <table-name> ] [ CHECK <condition> ] 127

CREATE TABLE: Table Constraints The second part of a table definition is optional. It consists of one or more table constraints, normally expressing a restriction on several columns: CREATE TABLE FootballMatch (... PRIMARY KEY (Date, HomeTeam), FOREIGN KEY (HomeTeam) REFERENCES Teams, FOREIGN KEY ( GuestTeam ) REFERENCES Teams ) Syntax of table constraints: [ UNIQUE ( <list-of-column-names> ) ] [ PRIMARY KEY ( <list-of-column-names> ) ] [ FOREIGN KEY ( <list-of-column-names> ) REFERENCES <table-name> ] [ CHECK ( <condition> ) ] 128

Constraints in Table Definitions Table definitions (CREATE TABLE) contain two very similar kinds of constraints: column constraints table constraints (also called: row constraints) Column constraints are abbreviations of certain special forms of table constraints where the name of the resp. column remains implicit, e.g. column constraint: Type number(15) CHECK ( > 0 AND < 35 ), table constraint: CHECK ( Type > 0 AND Type < 35 ) The condition part of such a CHECK constraint has to be satisfied in each admissible (legal, consistent) state of the database. 129

UNIQUE and NOT NULL UNIQUE-option: definition of a key (or: candidate key) single-column key: in a column definition: <column-name>... UNIQUE multi-column key: separate UNIQUE-clause as table constraint: UNIQUE ( <list-of-column-names>) Semantics: No two rows will ever have the same value in columns belonging to a key. Exception: Null values NULL may occur several times in a UNIQUE-column. Per table: Arbitrarily many UNIQUE-declarations are possible. In a table with UNIQUE-declarations no duplicates (identical rows) can exist! Exclusion of null values for individual columns: <column-name>... NOT NULL 130

PRIMARY KEY and DEFAULT Per table: At most one (candidate) key can be declared the primary key. single-column primary key: in column definition : <column name>... PRIMARY KEY multi-column primary key: separate clause: PRIMARY KEY ( <list-of-column-names> ) In addition: No column within a primary key may contain NULL! PRIMARY KEY is not the same as UNIQUE NOT NULL! (in addition: Uniqueness of the p. key within the table) Not a real constraint", but rather similar in syntax: Declaration of a default value for columns of a table: Value which is automatically inserted if no explicit value is given during the insertion of a new row, e.g. Type number(15) DEFAULT 0 131

Foreign key constraints Second special form of constraint within a table declaration: foreign key constraint (aka referential constraint) Situation: Column(s) of the table declared (called A) reference(s) (i.e., contains values of) a candidate key or primary key of a another ( foreign ) table B A B columns forming the foreign key Condition: A-columns contain only values actually occurring in the referenced B-column(s)! 132

Foreign key constraints (2) Syntax of the corresponding constraint (as table constraint): FOREIGN KEY ( <list-of-column-names> ) REFERENCES <table-name> [ ( <list-of-column-names> ) ] e.g.: CREATE TABLE t 1 ( a 1 INT PRIMARY KEY,..... ) b 1 references a 1 if target columns are missing: primary key assumed abbreviated form as column constraint CREATE TABLE t 2 ( b 1 INT REFERENCES t 1,..... ) 133

Foreign key constraints (3) Complete syntax of a referential constraint provides for various optional extensions: FOREIGN KEY ( <list-of-column-names> ) REFERENCES <base-table-name> [ ( <list-of-column-names> ) ] [ MATCH { FULL PARTIAL } ] [ ON DELETE { NO ACTION CASCADE SET DEFAULT SET NULL} ] [ ON UPDATE { NO ACTION CASCADE SET DEFAULT SET NULL} ] referential actions specify what happens in case of integrity violations Detailed discussion of all these extensions is beyond the scope of this short introduction. DB systems usually treat references and referential integrity quite similarly: with change propagation: ON UPDATE CASCADE with delete propagation: ON DELETE CASCADE 134

Global Constraints in SQL: Assertions Assertions are not supported by any commercial DB system till now, but defined in the SQL standard: Assertions serve as a means for expressing global integrity constraints not tied to a particular table, but ranging over several table. Syntax: CREATE ASSERTION <constraint-name> CHECK ( <conditional-expression> ) In principle, assertions are sufficient for expressing all imaginable constraints, i.e. all "local" forms of constraints are redundant. On the other hand, many constraints can only be expressed via assertions, but not by means of table constraints. Example: CREATE ASSERTION lazy_professor CHECK EXISTS ( SELECT * FROM professor WHERE Name NOT IN ( SELECT Teacher FROM courses ) ; 135

Integrity checking in SQL Important topic related to SQL constraints: Modalities of checking for constraint violations Changes in SQL are usually part of greater units of change called transactions: Transaction: Sequence of DML statements viewed as indivisible units" Transactions are either executed completely, or not at all! Transactions always have to lead to consistent DB states satisfying all integrity constraints stated in the resp. DB schema. more detailed discussion of the concept transaction": later! Important motivation for introducing transactions: Some transitions from a consistent state into a consistent followup state are only possible via inconsistent intermediate steps! Consequence for integrity checking during transaction processing: Checking of constraints should (more or less always) take place at the end of a transaction! 136

Integrity checking in SQL (2) Unless defined otherwise, integrity checking always happens immediately (i.e., directly after the execution of each update). Motivation: Many simple table constraints can and ought to be checked immediately as they are independent of any other updates. But in particular for referential cycles : Checking at transaction end is inevitable! e.g.: C 1 : Each course is given by a professor!" C 2 : Each professor has to give at least one course!" course professor When hiring a new professor a consistent state can be reached only via a transaction consisting of two individual insertions: INSERT INTO professor INSERT INTO course Each intermediate state would be inconsistent: No sequence possible! 137

Integrity checking in SQL (3) Two forms of integrity checking in SQL: IMMEDIATE and DEFERRED Meaning: IMMEDIATE-constraints are immediately checked, for DEFERREDconstraints checking is deferred to the end of the current transaction. Unfortunately: Without explicitly stating one of these alternatives, IMMEDIATE is assumed (which somehow contradicts the idea of a transaction). This default assumption can be changed for individual constraints by declaring them as INITIALLY DEFERRED. INITIALLY, because the checking status can be changed dynamically during a running transaction: SET CONSTRAINTS { < list-of-constraints > ALL } { DEFERRED IMMEDIATE } In addition: Some constraints can be declared NOT DEFERRABLE. But the even more important NOT IMMEDIATE does not exists in SQL! 138