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

Similar documents
COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

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

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

Data Definition Language (DDL)

SQL STRUCTURED QUERY LANGUAGE

COSC Assignment 2

Database design process

ECE 650 Systems Programming & Engineering. Spring 2018

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

Slides by: Ms. Shree Jaswal

ECE 650 Systems Programming & Engineering. Spring 2018

SQL Introduction. CS 377: Database Systems

Overview Relational data model

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

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

CSIE30600 Database Systems Basic SQL 2. Outline

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Lab # 4. Data Definition Language (DDL)

Database Technology. Topic 3: SQL. Olaf Hartig.

CS 377 Database Systems

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

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

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

Chapter 8: Relational Algebra

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

CS2300: File Structures and Introduction to Database Systems

Chapter 8: The Relational Algebra and The Relational Calculus

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

Relational Algebra 1

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

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

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

Chapter 6 The Relational Algebra and Calculus

CSC 742 Database Management Systems

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

3ISY402 DATABASE SYSTEMS

SQL DATA DEFINITION LANGUAGE

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Basic SQL. Basic SQL. Basic SQL

Relational Database Systems Part 01. Karine Reis Ferreira

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

SQL DATA DEFINITION LANGUAGE

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

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

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

Full file at

CS 348 Introduction to Database Management Assignment 2

CSEN 501 CSEN501 - Databases I

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

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

CSC 453 Database Technologies. Tanu Malik DePaul University

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQL DATA DEFINITION LANGUAGE

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

SQL: Data Definition Language

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

Relational Model. CS 377: Database Systems

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

RELATIONAL DATA MODEL

COSC344 Database Theory and Applications. Lecture 11 Triggers

CIS611 Lab Assignment 1 SS Chung

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences

RELATIONAL DATA MODEL: Relational Algebra

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Chapter 3. Algorithms for Query Processing and Optimization

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

SQL- Updates, Asser0ons and Views

A taxonomy of SQL queries Learning Plan

Class Information. Textbooks. Grading. Final Project. Grading. CS 440: Database Management Systems. Instructor:

SQL: DDL. John Ortiz Cs.utsa.edu

Simple SQL Queries (contd.)

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 6 Part I The Relational Algebra and Calculus

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

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

Part 1 on Table Function

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

UNIVERSITY OF TORONTO MIDTERM TEST SUMMER 2017 CSC343H Introduction to Databases Instructor Tamanna Chhabra Duration 120 min No aids allowed

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

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

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

From theory to practice. Designing Tables for an Oracle Database System. Sqlplus. Sqlplus. Technicalities. Add the following to your.

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Guides for Installing MS SQL Server and Creating Your First Database. Please see more guidelines on installing procedure on the class webpage

Database Systems External Sorting and Query Optimization. A.R. Hurson 323 CS Building

Principles of Data Management

Chapter-14 SQL COMMANDS

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

Transcription:

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

Overview Last Lecture Relational algebra This Lecture Relational algebra (continued) SQL - DDL CREATE TABLE Data types Domains Date and time Constraints DROP TABLE ALTER TABLE Source: Section 8.3, 6.1-6.2 and Oracle documentation Next Lecture SQL DML Source: Sections 6.3-6.4 and Oracle documentation COSC344 Lecture 5 2

CARTESIAN PRODUCT Q = R S Produce a new relation Q by combining every tuple from relation R with every tuple from relation S. R S Q A a1 a2 a3 B b1 b2 b3 X C c1 c2 D d1 d2 A B C D a1 b1 c1 d1 a1 b1 c2 d2 a2 b2 c1 d1 a2 b2 c2 d2 a3 b3 c1 d1 Also called CROSS PRODUCT R and S do not have to be union compatible # attributes in Q = # attributes in R + # attributes in S # tuples in Q = # tuples in R # tuples in S Meaningless unless used with other operations a3 b3 c2 d2 COSC344 Lecture 5 3

CARTESIAN PRODUCT Example Retrieve the names for each female employee and her dependents. FEMALE_EMPS < σ SEX= F (EMPLOYEE) EMPNAMES < Π FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENS < EMPNAMES DEPENDENT ACTUAL_DEPENDENS < σ SSN=ESSN (EMP_DEPENDENS) RESULT< Π FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPENDENS) COSC344 Lecture 5 4

CARTESIAN PRODUCT Example (cont d) COSC344 Lecture 5 5

JOIN Q = R <join condition> S Produce a new relation Q that contains the combinations of tuples in R and S satisfying the join condition. R S A a1 a2 a3 a4 B b1 b1 b2 b3 B = BB BB b1 b2 C c1 c2 A B BB C a1 b1 b1 c1 a2 b1 b1 c1 a3 b2 b2 c2 The main difference from CARTESIAN PRODUCT is that only the combinations of tuples satisfying the join condition appear. The join condition can be a compound set of conditions using AND and OR. The usual comparison operators are used. When the comparison operator is =, it is called an EQUIJOIN. COSC344 Lecture 5 6 Q

JOIN (cont d) In the earlier example illustrating CARTESIAN PRODUCT, the following operations EMP_DEPENDENS < EMPNAMES DEPENDENT ACTUAL_DEPENDENS < σ SSN=ESSN (EMP_DEPENDENS) can be replaced with a single JOIN operation ACTUAL_DEPENDENS < EMPNAMES SSN=ESSN DEPENDENT COSC344 Lecture 5 7

NATURAL JOIN Q = R * S Performs an Equijoin of the two relations R and S over all common attributes. One occurrence of each common attribute is eliminated from Q A a1 a2 a3 a4 R B b1 b1 b2 b3 * B b1 b2 S C c1 c2 Q A B C a1 b1 c1 a2 b1 c1 a3 b2 c2 The standard definition requires that the two join attributes (or each pair of join attributes) have the same name in both relations. If not, a renaming operation is applied first. COSC344 Lecture 5 8

Q = R S DIVISION Produce a relation Q in which each tuple must appear in R in combination with every tuple in S. A a1 a2 a1 a4 R B b1 b1 b2 b2 B b1 b2 S Can be expressed as a sequence of PROJECT, CARTESIAN PRODUCT and DIFFERENCE Q 1 < Π A (R) Q 2 < Π A ((S Q 1 ) - R) Q < Q 1 - Q 2 COSC344 Lecture 5 9 Q A a1

A Complete Set of Relational Operators The six primitive relational operators are: SELECT PROJECT UNION DIFFERENCE CARTESIAN PRODUCT RENAME Any other original relational operators can be expressed as a sequence of operations using the above six operators. The relational algebra includes operators like JOIN and DIVISION as they are convenient to use in database applications. COSC344 Lecture 5 10

miniworld ER Diagram Relational Schema Define the database in a DBMS

SQL - History & Introduction Structured Query Language (SQL) Standard ANSI & ISO SQL (1986) SQL2 (1992) SQL3 (1999) SQL - 2003 SQL - 2006 Declarative Includes DDL and DML Other facilities Standard Well almost COSC344 Lecture 5 12

The CREATE Statement The main SQL command for data definition, which can create Schema, table, view, assertions, triggers, A simple CREATE table example CREATE TABLE test (i INT, r NUMBER(6,2), s VARCHAR2(20) ); Convention on SQL-scripts Case is unimportant except for text strings. Using upper case letters for SQL keywords and lower case for other things. COSC344 Lecture 5 13

Data Types Oracle Built-in data types NUMBER(p, s): precision p [1,38], scale s [-84, 127] CHAR(size): fixed-length character data VARCHAR2(size): variable-length character string DATE CLOB: a large object containing characters BLOB: a binary large object BFILE: contains a locator to a large binary file ANSI Data types INTEGER/INT: equivalent to NUMBER(38) CHARACTER(n): equivalent to CHAR(n) Refer to Oracle Documentations for more detailed data types COSC344 Lecture 5 14

The CREATE TABLE Command Used to create a new relation by Giving it a name, specifying its attributes and data types Specifying constraints: attributes, tuples, key and referential integrity CREATE TABLE syntax CREATE TABLE tablename ( column1 datatype [constraint][, [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)[index_option]... [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition CHECK (expr) ); COSC344 Lecture 5 15

Attributes Constraints NOT NULL Specify that attribute values are not allowed to be set to NULL Always implicitly specified for attributes that are part of the primary key Attribute Defaults: DEFAULT <value> The default value is included in any new tuple if an explicit value is not provide for that attribute Example: Dno INT NOT NULL DEFAULT 1, CECHK clause Restrict attribute or domain values following an attribute or domain definition Oracle does not have a CREATE DOMAIN command Example Suppose Dnumber is restricted to integer between 1 and 20 Dnumber INT NOT NULL CHECK (Dnumber >0 and Dnumber <21), COSC344 Lecture 5 16

Key and Entity Integrity Constraints PRIMARY KEY clause The primary key is a single attribute Dunmber INT PRIMARY KEY, The primary key is a composite of multiple attributes PRIMARY KEY (Essn, Pno), UNIQUE clause: specifies candidate keys Pname VARCHAR2(15) NOT NULL UNIQUE, COSC344 Lecture 5 17

Referential Integrity Constraints FOREIGN KEY clause: specifies referencing integrity Can be defined by following the attribute directly dnum INT NOT NULL REFERENCES department(dnumber), Can be defined using the FOREIGN KEY clause FOREIGN KEY (dnum) REFERENCES department(dnumber), Specify actions to deal with integrity violations SET NULL, CASCADE, SET DEFAULT FOREIGN KEY (Super_ssn) REFERENCES employee(ssn) ON DELETE SET DEFAULT ON UPDATE CASCADE, COSC344 Lecture 5 18

Giving Names to Constraints The names of all constraints within a particular schema must be unique. A constraint name is used to identify a particular constraint in case the constraint must be dropped later or replaced with another constraint. CONSTRAINT superssn_cnst REFERENCES employee(ssn), COSC344 Lecture 5 19

A Complex CREATE TABLE Example CREATE TABLE EMPLOYEE (Fname VARCHAR2(10) NOT NULL, Minit CHAR, Lname VARCHAR2(50) NOT NULL, Ssn CHAR(9) PRIMARY KEY, Bdate DATE, Address VARCHAR2(30), Sex CHAR CONSTRAINT sck CHECK (sex IN ( M, F )), Salary DECIMAL(10,2), Super_ssn CHAR(9) CONSTRAINT superssn_cnst REFERENCES EMPLOYGEE(ssn) DISABLE, Dno INT NOT NULL DEFAULT 1 REFERENCES DEPARTMENT(Dnumber) ); COSC344 Lecture 5 20

SQL specifies Date and Time DATE: Year, Month and Day in the form YYYY-MM-DD TIME: Hour, Minute and Second in the form HH:MM:SS TIMESTAMP: include both DATE and TIME plus a minimum of six positions for decimal fractions of seconds in the form YYYY- MM-DD HH:MI:SS[.ssssss] Oracle uses DATE for both date and time DD - number of days in month (e.g., 24) MM numeric month (e.g., 07) MON abbreviated month name (e.g., JUL) MONTH full month name (e.g., JULY) YY - last two digits of year (e.g., 12) YYYY - all digits of year (e.g., 2012) HH (or HH12) - hours of the day in 12 hour format HH24 - hours of the day in 24 hour format MI - minutes of the hour SS - seconds of the minute COSC344 Lecture 5 21

Date and Time in Oracle Use TO_DATE to specify the format TO_DATE('string', 'format') Must be used when entering dates e.g, TO_DATE('20-03-2000','dd-mm-yyyy') INSERT INTO x VALUES (...,..., TO_DATE( 27-Jul-2010, dd-mon-yyyy )); Use TO_CHAR to format the output of a date TO_CHAR(attr, 'format') TO_CHAR(bdate, 'dd/mon/yy') SELECT TO_CHAR (bdate, 'dd/mon/yy ) FROM EMPLOYEE; COSC344 Lecture 5 22

DROP TABLE: DROP/ALTER TABLE in Oracle DROP TABLE table [CASCADE CONSTRAINTS]; CASCADE CONSTRAINTS: drop all referential integrity constraints that refer to primary and unique keys in the dropped table. DROP TABLE project; DROP TABLE employee cascade constraints; ALTER TABLE ALTER TABLE table [ADD/DROP (column_element constraint)] [MODIFY (column_element)] Add or drop a column Change a column definition Add or drop a column constraint [ENABLE DISABLE constraint] COSC344 Lecture 5 23

Add a column ALTER TABLE in Oracle ALTER TABLE employee ADD Job VARCHAR(12); Change a column definition ALTER TABLE employee MODIFY Mgr_ssn SET DEFAULT 333445555 ; Add a column constraint ALTER TABLE employee ADD CONSTRAINT superssn_cnst FOREIGN KEY (Super_ssn) REFERENCES employee(ssn); ALTER TABLE employee ENABLE CONSTRAINT superssn_cnst; COSC344 Lecture 5 24

Summary Relational algebra Cartesian product Join Natural join Division SQL - DDL CREATE TABLE Data types Constraints DROP TABLE ALTER TABLE COSC344 Lecture 5 25