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

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

doc. RNDr. Tomáš Skopal, Ph.D. Mgr. Martin Nečaský, Ph.D. RNDr. Michal Kopecký, Ph.D.

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

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

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Introduction to Data Management CSE 344

Introduction to Database Systems CSE 414

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Introduction to Data Management CSE 344

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

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

SQL DATA DEFINITION LANGUAGE

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

Lab # 4. Data Definition Language (DDL)

SQL DATA DEFINITION LANGUAGE

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

Oracle Create Table Foreign Key On Delete No

Chapter 1 SQL and Data

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

Introduction to SQL Server. nikos bikakis

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

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

Full file at

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

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

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

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

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

Query formalisms for relational model relational calculus

CS275 Intro to Databases

course: Database Systems (NDBI025) SS2017/18

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

The Relational Model of Data (ii)

Chapter 17: Table & Integrity Contraints. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

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

Integrity Constraints (Reminder)

SQL: DDL. John Ortiz Cs.utsa.edu

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

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

SQL Constraints and Triggers

MTAT Introduction to Databases

3ISY402 DATABASE SYSTEMS

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

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Data Manipulation (DML) and Data Definition (DDL)

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

CSC 261/461 Database Systems Lecture 6. Fall 2017

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Relational Database Systems Part 01. Karine Reis Ferreira

Introduction to Data Management CSE 344

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Standard Query Language. SQL: Data Definition Transparencies

SQL Data Definition and Data Manipulation Languages (DDL and DML)

CSC 453 Database Technologies. Tanu Malik DePaul University


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

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

Database design process

CSE 344 MAY 14 TH ENTITIES

Chapter 4. The Relational Model

Relational data model

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

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

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

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

1- a> [5pts] Create the new table by writing and executing a full SQL DDL statement based on the following schema:

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

Constraints. Local and Global Constraints Triggers

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

Basic SQL. Basic SQL. Basic SQL

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

G64DBS Database Systems. Lecture 6 More SQL Data. Creating Relations. Column Constraints. Last Lecture s Question

CS2300: File Structures and Introduction to Database Systems

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

user specifies what is wanted, not how to find it

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 8 INTEGRITY 1

Introduction to Database Systems

CSE 344 AUGUST 1 ST ENTITIES

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

A database consists of several tables (relations) AccountNum

The Relational Model

Data Definition Language (DDL)

Downloaded from

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

DATABASE DESIGN - 1DL400

Transcription:

course: Database Systems (NDBI025) SS2017/18 doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

data definition definition of tables (their schemes) and integrity constraints CREATE TABLE altering the definitions ALTER TABLE data manupulation INSERT, UPDATE, DELETE database views T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 2

construction of table schema and an empty table table name, columns defined, column-scope integrity constraints (IC), table-scope integrity constraints T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 3

each column has a data_type optionally default value within a new record (DEFAULT NULL value) column-scope integrity constraints T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 4

CREATE TABLE Product (Id INTEGER, Name VARCHAR(128), Price DECIMAL(6,2), ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 5

column-based IC allows to limit the domain of values in column in record (new or modified) named CREATE TABLE... (..., CONSTRAINT constraint...) unnamed 5 types limiting a valid value NOT NULL value cannot be null UNIQUE value must be unique (w.r.t all records in the table) PRIMARY KEY primary key definition (key = NOT NULL + UNIQUE, primary = primary index) REFERENCES one-column foreign key (both columns must share definition) CHECK generic condition, similarly as in SELECT... WHERE applied only on the inserted/updated row(s) data inserted/update only if TRUE if an IC is not validated, the record is not updated T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 6

CREATE TABLE Product (Id INTEGER CONSTRAINT pk PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, Producer INTEGER REFERENCES Producer (Id)) CREATE TABLE Producer (Id INTEGER PRIMARY KEY, ProducerName VARCHAR(128), HQ VARCHAR(256)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 7

CREATE TABLE Employee (IdEmp INTEGER PRIMARY KEY, Name VARCHAR(128), Boss INTEGER REFERENCES Employee (IdEmp)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 8

generalization of column-scope IC to use for multiple columns apart NOT NULL, this is only meaningful for single column UNIQUE n-tuple of values is unique FOREIGN KEY the same as REFERENCES in column-scope IC CHECK T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 9

when updating referencing or referenced table, violation of foreign keys may occur record update with foreign key value that is not valid (not present in column of referenced table) delete/update of a record in the referenced table (so that value in referencing table becomes not covered) when a violation of foreign keys occurs, there are two options if there is no referential action defined, an error message appears, aborting the action (SQL 89) referential action is triggered, referential_action (SQL 92) ON UPDATE, ON DELETE definition of when to trigger the action, either by update or delete of row in the referenced table CASCADE the row with the referencing value is treated the same (i.e., updated or deleted) SET NULL referencing value in the row is set to NULL SET DEFAULT referencing value in the row is set to a default value defined in CREATE TABLE NO ACTION default, no action takes place, resp., DBMS displays a message (SQL 89) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 10

CREATE TABLE Product (Id INTEGER PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, Producer VARCHAR(128), ProducerHQ VARCHAR(256), CONSTRAINT fk FOREIGN KEY (Producer, ProducerHQ) REFERENCES Producer (ProducerName,HQ)) CREATE TABLE Producer (ProducerName VARCHAR(128), HQ VARCHAR(256), Subject VARCHAR(64), CONSTRAINT pk PRIMARY KEY(ProducerName, HQ)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 11

CREATE TABLE Product (Id INTEGER PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, CONSTRAINT chk CHECK (Weight > 0)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 12

CREATE TABLE Product (Id INTEGER CONSTRAINT pk PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, Producer INTEGER REFERENCES Producer (Id) ON DELETE CASCADE) CREATE TABLE Producer (Id INTEGER PRIMARY KEY, ProducerName VARCHAR(128), HQ VARCHAR(256)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 13

table scheme definition altering column add/remove column, change of DEFAULT value IC add/remove IC however, there may be data that won t allow the IC change (e.g., primary key definition) ALTER TABLE table-name... ADD [COLUMN] column-name column-definition... ADD constraint-definition... ALTER [COLUMN] column-name SET... ALTER [COLUMN] column-name DROP... DROP COLUMN column-name... DROP CONSTRAINT constraint-name T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 14

CREATE TABLE Product (Id INTEGER PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, CONSTRAINT chk CHECK (AND Weight > 0)) ALTER TABLE Product DROP CONSTRAINT chk ALTER TABLE Product ADD CONSTRAINT chk CHECK (Weight > 10)) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 15

DROP TABLE table complementary to CREATE TABLE table the table content is deleted and also the definition (i.e., scheme) if we need to delete only the content we use DELETE FROM table (see next) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 16

apart SELECT, the SQL DML offers three commands for data manipulation INSERT INTO insertion of rows DELETE FROM deletion of rows UPDATE update of rows T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 17

insertion of row by enumeration, two options INSERT INTO table (col1, col3, col5) VALUES (val1, val3, val5) INSERT INTO table VALUES (val1, val2, val3, val4, val5) insertion of multiple rows that result from a SELECT INSERT INTO table1 (column list) (SELECT... FROM...) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 18

CREATE TABLE Product (Id INTEGER CONSTRAINT pk PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2) NOT NULL, ProductionDate DATE, Available BIT DEFAULT TRUE, Weight FLOAT, Producer INTEGER REFERENCES Producer (Id)) INSERT INTO Product VALUES (0, Chair, 86, 2005-5-6, TRUE, 3, 123456) INSERT INTO Product (Id, Name, Price, ProductionDate, Weight, Producer) VALUES (0, Chair, 86, 2005-5-6, 3, 123456) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 19

CREATE TABLE ProductInStore (Id INTEGER PRIMARY KEY, Name VARCHAR(128) UNIQUE, Price DECIMAL(6,2)) INSERT INTO ProductInStore (SELECT Id, Name, Price FROM Product WHERE Available = TRUE) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 20

update of rows matching a condition the values of chosen columns are set to NULL value given by expression (also constant) query result T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 21

UPDATE Product SET Name = Notebook WHERE Name = Laptop UPDATE Product SET Price = Price * 0.9 WHERE ProductionDate < CAST( 2017-01-01 AS DATE) UPDATE Product SET Price = Price * 0.9 WHERE ProductionDate < 2017-01-01 /* automatic conversion */ UPDATE Product AS V1 SET Weight = (SELECT AVG(Weight) FROM Product AS V2 WHERE V1.Name = V2.Name) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 22

deletes rows that match condition DELETE FROM table deletes all rows Example: DELETE FROM Product WHERE Price > 100 T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 23

named query that could be used as a table (its result, resp.) evaluated dynamically could be used also for data inserts/updates/deletes CHECK OPTION ensures that after row insert/update into the view the change will be visible at least in this view in all dependent views T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 24

CREATE VIEW NewProductInStore AS SELECT * FROM Product WHERE Available = TRUE AND ProductionDate > CAST( 2017-01-01 AS DATE) WITH LOCAL CHECK OPTION INSERT INTO NewProductInStore VALUES (0, SwingChair, 135, 2017-05-06, TRUE, 4, 3215) - inserted (transitivelly into table Product) INSERT INTO NewProductInStore VALUES (0, Box, 135, 1999-11-07, TRUE, 4, 3215)!! Error this record cannot be inserted, as it wouldn t be visible (too old box) T. Skopal SQL data definition & modification, views (NDBI025, Lect. 4) 25