SQL DATA DEFINITION LANGUAGE

Similar documents
SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

Databases 1. Defining Tables, Constraints

Constraints. Local and Global Constraints Triggers

Chapter 2 The relational Model of data. Relational model introduction

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

CSCI3030U Database Models

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

Constraints and Triggers

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

CS54100: Database Systems

Databases-1 Lecture-01. Introduction, Relational Algebra

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

SQL: Data Manipulation Language

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

A tuple is dangling if it doesn't join with any

CS2300: File Structures and Introduction to Database Systems

user specifies what is wanted, not how to find it

From E/R Diagrams to Relations

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

The Relational Model of Data (ii)

Chapter 7: Constraints and Triggers. Foreign Keys Local and Global Constraints Triggers

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

CS W Introduction to Databases Spring Computer Science Department Columbia University

Grouping Operator. Applying γ L (R) Recall: Outerjoin. Example: Grouping/Aggregation. γ A,B,AVG(C)->X (R) =??

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

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

Basic SQL. Basic SQL. Basic SQL

Chapter 6 The database Language SQL as a tutorial

Lab # 4. Data Definition Language (DDL)

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Slides by: Ms. Shree Jaswal

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

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

Design Techniques. 1. Avoid redundancy 2. Limit the use of weak entity sets 3. Don t use an entity set when an attribute will do

Chapter 6 The database Language SQL as a tutorial

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

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

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

Views, Indexes, Authorization. Views. Views 8/6/18. Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges

Constraints, Views & Indexes. Running Example. Kinds of Constraints INTEGRITY CONSTRAINTS. Keys Foreign key or referential integrity constraints

INDEXES MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

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

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

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

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

MTAT Introduction to Databases

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

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

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

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

CSEN 501 CSEN501 - Databases I

Full file at

Introduction to Data Management CSE 344

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

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

CSIE30600 Database Systems Basic SQL 2. Outline

SQL Introduction. CS 377: Database Systems

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Chapter 10 Advanced topics in relational databases

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

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

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

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

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

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

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

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

SQL: DDL. John Ortiz Cs.utsa.edu

Introduction to Database Systems CSE 414

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

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

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

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

CSCC43H: Introduction to Databases. Lecture 4

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

Introduction to Data Management CSE 344

Informatics 1: Data & Analysis

Lecture 2: Introduction to SQL

Data Definition Language (DDL)

After completing this unit, you should be able to: Define the terms

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

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Conceptual Design. The Entity-Relationship (ER) Model

COMP 430 Intro. to Database Systems

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Transcription:

9/27/16 DATABASE SCHEMAS IN SQL SQL DATA DEFINITION LANGUAGE SQL is primarily a query language, for getting information from a database. SFWR ENG 3DB3 FALL 2016 But SQL also includes a data-definition component for describing database DML: Data Manipulation Language MICHAEL LIUT (LIUTM@MCMASTER.CA) schemas. DDL: Data Definition Language DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY 2 CREATING (DECLARING) A RELATION ELEMENTS OF TABLE DECLARATIONS Simply: Most basic element: an attribute and its type. Common Types: CREATE TABLE <table_name> ( <list of elements> Deleting a relation: DROP TABLE <table_name>; 3 INT or INTEGER (synonyms). REAL or FLOAT (synonyms). CHAR(n) = fixed-length string of n characters. VARCHAR(n ) = variable-length string of up to n characters. 4 1

EXAMPLE: CREATE TABLE SQL VALUES beer VARCHAR(20), price REAL Integers and reals are represented as you would expect. Strings are too, except they require single quotes. Two single quotes = real quote e.g. Joe s Bar. Any value can be NULL Unless attribute has NOT NULL constraint e.g. price REAL not null, 5 6 DATES AND TIMES TIMES AND VALUES DATE and TIME are types in SQL The form of a date value is: DATE yyyy-mm-dd The form of a time value is: TIME hh:mm:ss à with an optional decimal point for a fraction of a second. e.g. TIME 15:30:02.5 = two and a half seconds after 3:30pm. e.g. DATE 2007-09-30 is September 30 th, 2007. 7 8 2

DECLARING KEYS EXAMPLE An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Either says that no two tuples of the relation may agree in all the attribute(s) on the list. Beers (name, manf) Bars (name, addr, license) Drinkers (name, addr, phone) Likes (drinker, beer) Sells (bar, beer, price) Frequents (drinker, bar) Underline = key (tuples cannot have the same value in all key attributes) 9 10 DECLARING SINGLE-ATTRIBUTE KEYS DECLARING MULTI-ATTRIBUTE KEYS Place PRIMARY KEY or UNIQUE KEY after the type in the declaration of the attribute. Example: CREATE TABLE Beers ( name CHAR(20) UNIQUE, manf CHAR(20) 11 A key declaration can also be another element in the list of elements of a CREATE TABLE statement. This form is essential if the key consists of more than one attribute. Could be used for one-attribute keys. 12 3

EXAMPLE PRIMARY KEY VS. UNIQUE The bar and beer together are the key for Sells: beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) 13 1. There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes. 2. No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL s, and there may be several tuples with NULL. 14 TYPES OF CONSTRAINTS FOREIGN KEYS Keys Foreign-key, or referential-integrity. Domain constraints Constrain values of a particular attribute. Tuple-based constraints Relationship among components. Assertions: any SQL boolean expressions Values appearing in attributes of one relation must appear together in certain attributes of another relation. Example: in Sells(bar, beer, price), we might expect that a beer value also appears in Beers.name 15 16 4

EXPRESSING FOREIGN KEYS Use keyword REFERENCES, either: 1. After an attribute (for one-attribute keys). 2. As an element of the schema: FOREIGN KEY (<list of attributes>) REFERENCES <relation> (<attributes>) Referenced attributes must be declared PRIMARY KEY or UNIQUE. EXAMPLE: WITH ATTRIBUTE CREATE TABLE Beers ( name CHAR(20) manf CHAR(20) PRIMARY KEY, beer CHAR(20) REFERENCES Beers(name), price REAL 17 18 EXAMPLE: AS SCHEMA ELEMENT CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) beer CHAR(20), price REAL FOREIGN KEY (beer) REFERENCES Beers(name) 19 ENFORCING FOREIGN-KEY CONSTRAINTS If there is a foreign-key constraint from relation R to relation S, two violations are possible: 1. An insert or update to R introduces values not found in S. 2. A deletion or update to S causes some tuples of R to dangle. 20 5

ACTIONS TAKEN ACTIONS TAKEN Example: suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways. Default: Reject the modification. Cascade: Make the same changes in Sells. Deleted beer: delete Sells tuple. Updated beer: change value in Sells. Set Null: Change the beer to NULL. 21 22 CITATIONS, IMAGES AND RESOURCES Based on the slides of Dr. Fei Chiang - http://www.cas.mcmaster.ca/~fchiang/ Database Management Systems (3 rd Ed.), Ramakrishnan & Gehrke http://csharpcorner.mindcrackerinc.netdna-cdn.com/uploadfile/blogimages/06112016031910am/sql.png 26 6