EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

The Relational Model of Data (ii)

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

Slides by: Ms. Shree Jaswal

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

Basic SQL. Basic SQL. Basic SQL

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

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

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

The Relational Model 2. Week 3

The Relational Model. Chapter 3

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

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

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

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

CSIE30600 Database Systems Basic SQL 2. Outline

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

Database Applications (15-415)

The Relational Model

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

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

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Week 2

Database Management Systems. Chapter 3 Part 1

Relational data model

Database Systems ( 資料庫系統 )

The Relational Model

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

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

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

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

SQL DATA DEFINITION LANGUAGE

Keys, SQL, and Views CMPSCI 645

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

SQL: Data Definition Language

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

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

CS275 Intro to Databases

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

The Relational Data Model. Data Model

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

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

SQL Introduction. CS 377: Database Systems

CSEN 501 CSEN501 - Databases I

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

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

The Relational Model

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

Conceptual Design. The Entity-Relationship (ER) Model

Introduction to Databases

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

CIS 330: Applied Database Systems

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

CSC 453 Database Technologies. Tanu Malik DePaul University

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

CS W Introduction to Databases Spring Computer Science Department Columbia University

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

CS2300: File Structures and Introduction to Database Systems

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

ECE 650 Systems Programming & Engineering. Spring 2018

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

Full file at

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

SQL STRUCTURED QUERY LANGUAGE

SQL: DDL. John Ortiz Cs.utsa.edu

Database Systems. Course Administration

CMPT 354: Database System I. Lecture 2. Relational Model

From ER to Relational Model. Book Chapter 3 (part 2 )

The Relational Model (ii)

CSCC43H: Introduction to Databases. Lecture 4

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Database Applications (15-415)

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

Database Management Systems,

High-Level Database Models (ii)

Structured Query Language (SQL)

Lab # 4. Data Definition Language (DDL)

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

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

user specifies what is wanted, not how to find it

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

SQL: The Query Language Part 1. Relational Query Languages

The Relational Model Constraints and SQL DDL

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

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

Transcription:

1 EGCI 321: Database Systems Dr. Tanasanee Phienthrakul

2 Chapter 10 Data Definition Language (DDL)

3 Basic SQL SQL language Considered one of the major reasons for the commercial success of relational databases SQL Structured Query Language Statements for data definitions, queries, and updates (both DDL and DML) Core specification Plus specialized extensions

4 SQL Data Definition and Data Types Terminology: Table, row, and column used for relational model terms relation, tuple, and attribute CREATE statement Main SQL command for data definition

5 Schema and Catalog Concepts in SQL SQL schema Identified by a schema name Includes an authorization identifier and descriptors for each element Schema elements include Tables, constraints, views, domains, and other constructs Each statement in SQL ends with a semicolon

6 The CREATE TABLE Command in SQL Specify a new relation Provide name Specify attributes and initial constraints Can optionally specify schema: CREATE TABLE COMPANY.EMPLOYEE... or CREATE TABLE EMPLOYEE...

7 The CREATE TABLE Command in SQL (cont.) Base tables (base relations) Relation and its tuples are actually created and stored as a file by the DBMS Virtual relations Created through the CREATE VIEW statement

8 Rational Model in SQL Creates the Students relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. CREATE TABLE Students ( sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ) Another example: the Enrolled table holds information about courses that students take. CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2) )

9 Destroying Relations Destroys the relation Students. The schema information and the tuples are deleted. DROP TABLE Students

10

11

12 Attribute Data Types and Domains in SQL Basic data types Numeric data types Integer numbers: INTEGER, INT, and SMALLINT Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION Character-string data types Fixed length: CHAR(n), CHARACTER(n) Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n)

13 Attribute Data Types and Domains in SQL (cont.) Bit-string data types Fixed length: BIT(n) Varying length: BIT VARYING(n) Boolean data type Values of TRUE or FALSE or NULL DATE data type Ten positions Components are YEAR, MONTH, and DAY in the form YYYY- MM-DD

14 Attribute Data Types and Domains in SQL (cont.) Additional data types Timestamp data type (TIMESTAMP) Includes the DATE and TIME fields Plus a minimum of six positions for decimal fractions of seconds Optional WITH TIME ZONE qualifier INTERVAL data type Specifies a relative value that can be used to increment or decrement an absolute value of a date, time, or timestamp

15 Domain Attribute Data Types and Domains in SQL (cont.) Name used with the attribute specification Makes it easier to change the data type for a domain that is used by numerous attributes Improves schema readability Example: CREATE DOMAIN SSN_TYPE AS CHAR(9);

16 Specifying Constraints in SQL Basic constraints: Restrictions on attribute domains and NULLs Key and referential integrity constraints Constraints on individual tuples within a relation

17 NOT NULL Specifying Attribute Constraints and Attribute Defaults NULL is not permitted for a particular attribute Default value DEFAULT <value> CHECK clause Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);

18

19 Specifying Key and Referential Integrity Constraints PRIMARY KEY clause Specifies one or more attributes that make up the primary key of a relation Dnumber INT PRIMARY KEY; UNIQUE clause Specifies alternate (secondary) keys Dname VARCHAR(15) UNIQUE;

20 Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key. For a given student and course, there is a single grade. CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade. CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Used carelessly, an IC can prevent the storage of database instances that arise in practice!

21 Specifying Key and Referential Integrity Constraints (cont.) FOREIGN KEY clause Default operation: reject update on violation Attach referential triggered action clause Options include SET NULL, CASCADE, and SET DEFAULT Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE CASCADE option suitable for relationship relations

22 Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students )

23 Referential Integrity in SQL CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT )

24 Giving Names to Constraints Keyword CONSTRAINT Name a constraint Useful for later altering

25 Specifying Constraints on Tuples Using CHECK CHECK clauses at the end of a CREATE TABLE statement Apply to each tuple individually CHECK (Dept_create_date <= Mgr_start_date); CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int CHECK (Age>=18) );

26 The CREATE TABLE Command in SQL (cont.) Some foreign keys may cause errors Specified either via: Circular references Or because they refer to a table that has not yet been created

27 Altering Relations The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. ALTER TABLE Students ADD firstyear integer ALTER TABLE Students DROP COLUMN firstyear ALTER TABLE Students MODIFY COLUMN firstyear char(20)

28 Entity sets to tables: CREATE TABLE Employees ( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn) ) ER to Relational

29 Relationship Sets to Tables In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes. CREATE TABLE Works_In ( ssn CHAR(1), ) did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments

30 Translating ER Diagrams with Key Constraints Map relationship to a table: Note that did is the key now! Separate tables for Employees and Departments. Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments ) CREATE TABLE Dept_Mgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees )