CSCC43H: Introduction to Databases. Lecture 4

Similar documents
CSCC43H: Introduction to Databases. Lecture 3

Database Management Systems,

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Chapter 3: SQL. Chapter 3: SQL

QQ Group

DATABASE DESIGN I - 1DL300

Silberschatz, Korth and Sudarshan See for conditions on re-use

DATABASE TECHNOLOGY. Spring An introduction to database systems

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

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Chapter 2: Relational Model

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

The SQL database language Parts of the SQL language

CS352 - DATABASE SYSTEMS. To give you experience with developing a database to model a real domain

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CS352 - DATABASE SYSTEMS

Database Systems SQL SL03

Database Systems SQL SL03

Chapter 4: SQL. Basic Structure

Simple SQL Queries (2)

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

CSC309: Introduction to Web Programming. Lecture 6

DATABASE TECHNOLOGY - 1MB025

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

DATABASE DESIGN - 1DL400

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

DATABASTEKNIK - 1DL116

DATABASE DESIGN - 1DL400

CSIE30600 Database Systems Basic SQL 2. Outline

1. (a) Explain the Transaction management in a database. (b) Discuss the Query Processor of Database system structure. [8+8]

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

Basic SQL. Basic SQL. Basic SQL

CSCC43H: Introduction to Databases

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

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

DATABASE TECHNOLOGY - 1MB025

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

SQL (Structured Query Language)

Views, assertions, and triggers

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

Information Systems and Software Systems Engineering (12CFU)

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Database Systems. Answers

Database System Concepts

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

Chapter 5: Other Relational Languages

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

Chapter 8: Relational Algebra

SQL DATA DEFINITION LANGUAGE

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 3: Introduction to SQL

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

The SQL data-definition language (DDL) allows defining :

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

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

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

Chapter 1: Introduction

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Database Management System. Fundamental Database Concepts

DATABASDESIGN FÖR INGENJÖRER - 1DL124

SQL DATA DEFINITION LANGUAGE

Chapter 3: Introduction to SQL

Other Relational Query Languages

Chapter 6: Formal Relational Query Languages

Other Query Languages II. Winter Lecture 12

SQL: Data Definition Language

BRANCH(branch_name: string, branch_city:string, assets:real) ACCOUNT(accno: int, branch_name:string, balance: real)

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Slides by: Ms. Shree Jaswal

Database Systems CSE Comprehensive Exam Spring 2005

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

CS W Introduction to Databases Spring Computer Science Department Columbia University

The Relational Model of Data (ii)

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

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

Chapter 6: Relational Database Design

Chapter 1: Introduction

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

Chapter 4: Advanced SQL

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

SQL DATA DEFINITION LANGUAGE

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Chapter 1: Introduction

Lecture 14 of 42. E-R Diagrams, UML Notes: PS3 Notes, E-R Design. Thursday, 15 Feb 2007

Relational Model. Prepared by: Ms. Pankti Dharwa ( Asst. Prof, SVBIT)

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

DB Creation with SQL DDL

Example: specific person, company, event, plant

Chapter 7: Relational Database Design

Design Process Modeling Constraints E-R Diagram Design Issues Weak Entity Sets Extended E-R Features Design of the Bank Database Reduction to

Distributed Query Processing. Banking Example

Transcription:

CSCC43H: Introduction to Databases Lecture 4 Wael Aboulsaadat Acknowledgment: these slides are partially based on Prof. Garcia-Molina & Prof. Ullman slides accompanying the course s textbook.

CSCC43: Introduction to Databases Database Management System (DBMS) A collection of programs that enable: Defining (describing the structure), Populating by data (Constructing), Manipulating (querying, updating), Preserving consistency, Protecting from misuse, Recovering from failure, and Concurrent using of a database.

Banking Example

E-R Diagram for the Banking Enterprise

Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number)

Example Queries Find all loans of over $1200 σ amount > 1200 (loan) Find the loan number for each loan of an amount greater than $1200 loan_number (σ amount > 1200 (loan)) Find the names of all customers who have a loan, an account, or both, from the bank customer_name (borrower) customer_name (depositor)

Example Queries Find the names of all customers who have a loan at the Perryridge branch. customer_name (σ branch_name= Perryridge (σ borrower.loan_number = loan.loan_number (borrower x loan))) Find the names of all customers who have a loan at the Perryridge branch but did not deposit at any branch of the bank. customer_name (σ branch_name = Perryridge (σ borrower.loan_number = loan.loan_number (borrower x loan))) customer_name (depositor)

Example Queries Find the names of all customers who have a loan at the Perryridge branch. Query 1 customer_name (σ branch_name = Perryridge ( σ borrower.loan_number = loan.loan_number (borrower x loan))) Query 2 customer_name (σ loan.loan_number = borrower.loan_number ( (σ branch_name = Perryridge (loan)) x borrower))

CSCC43: Introduction to Databases Example Queries Find the largest account balance Strategy: Find those balances that are not the largest Rename account relation as d so that we can compare each account balance with all others Use set difference to find those account balances that were not found in the earlier step. The query is: balance (account) - account.balance (σ account.balance < d.balance (account x ρ d (account)))

Bank Example Queries Find the names of all customers who have a loan and an account at bank. customer_name (borrower) customer_name (depositor) Find the name of all customers who have a loan at the bank and the loan amount customer_name, loan_number, amount (borrower loan)

Bank Example Queries Find all customers who have an account at all branches located in Brooklyn city.

Bank Example Queries Find all customers who have an account at all branches located in Brooklyn city. customer_name, branch_name (depositor account) branch_name (σ branch_city = Brooklyn (branch))

Example 2 Given relational schema: Sailors (sid, sname, rating, age) Reservation (sid, bid, date) Boats (bid, bname, color) 1) Find names of sailors who ve reserved boat #103 2) Find names of sailors who ve reserved a red boat 3) Find sailors who ve reserved a red or a green boat 4) Find sailors who ve reserved a red and a green boat 5) Find the names of sailors who ve reserved all boats

Structured Query Language (SQL)

Structure Query Language Data Definition Language (DDL) Data Manipulation Language (DML) CMSC424, Spring 2005

Data Definition Language (DDL) Allows the specification of not only a set of relations but also information about each relation, including: The schema for each relation. The domain of values associated with each attribute. Integrity constraints (what s valid.) The set of indices (keys..) to be maintained for each relations. Security and authorization information for each relation. The physical storage structure of each relation on disk.

CSCC43: Introduction to Databases Domains Domains specify allowable values for attributes. Two categories: Elementary (predefined by the standard); User-defined.

CSCC43: Introduction to Databases Elementary Domains Character Character Single characters or strings; Strings may be of variable length; A Character set different from the default one can be used (e.g., Latin, Greek, Cyrillic, etc.) Syntax: character [ varying ] [ (Length) ] [ character set CharSetName ] It is possible to use char and varchar, for character and character varying respectively

CSCC43: Introduction to Databases More Elementary Domains Bit Single Boolean values or strings of Boolean values (may be variable in length); Syntax: bit [ varying ] [ (Length) ] Exact numeric domains Exact values, integer or with a fractional part Four alternatives: numeric(6,3) numeric [ ( Precision [, Scale ] ) ] decimal [ ( Precision [, Scale ] ) ] integer smallint # of significant digits decimal digits

CSCC43: Introduction to Databases Approximate Numeric Domains Approximate numeric domains Approximate real values Based on a floating point representation float [ ( Precision ) ] double precision

CSCC43: Introduction to Databases Temporal Instant Domains Temporal instants date has fields year,month,day time [ ( Precision) ] [ with time zone ] has fields hour,minute,second timestamp [ ( Precision) ] [ with time zone ] Temporal intervals interval FirstUnitOfTime [ to LastUnitOfTime ] Units of time are divided into two groups: (i) year, month, (ii) day, hour, minute, second For example, year(5) to month allows intervals up to 99999yrs + 11mo

User-Defined Domains Comparable to definitions of variable types in programming languages. A domain is characterized by name, elementary domain, default value, set of constraints Syntax: create domain DomainName as ElementaryDomain [ DefaultValue ] [ Constraints ] Example: create domain Mark as smallint default null

Default Domain Values Define the value that the attribute must assume when a value is not specified during row insertion. Syntax: default < GenericValue user null > GenericValue represents a value compatible with the domain, in the form of a constant or an expression. user is the login name of the user who assigns a value to this attribute.

Summary: domain types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machinedependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. Null values are allowed in all the domain types. Declaring an attribute to be not null prohibits null values for that attribute. create domain construct in SQL-92 creates user-defined domain types create domain person-name char(20) not null

CSCC43: Introduction to Databases Summary: domain types in SQL (cont.) date. Dates, containing a (4 digit) year, month and date E.g. date 2001-7-27 time. Time of day, in hours, minutes and seconds. E.g. time 09:00:30 time 09:00:30.75 timestamp: date plus time of day E.g. timestamp 2001-7-27 09:00:30.75 Interval: period of time E.g. Interval 1 day Subtracting a date/time/timestamp value from another gives an interval value Interval values can be added to date/time/timestamp values Can extract values of individual fields from date/time/timestamp E.g. extract (year from r.starttime) Can cast string types to date/time/timestamp E.g. cast <string-valued-expression> as date

Schema Definition A schema is a collection of objects: domains, tables, indexes, assertions, views, privileges A schema has a name and an owner (who determines authorization privileges) Syntax: create schema [ SchemaName ] [ [ authorization ] Authorization ] { SchemaElementDefinition }

Table Definition An SQL table consists of an ordered set of attributes, and a (possibly empty) set of constraints Statement create table defines a relation schema, creating an empty instance. Syntax: create table TableName ( AttributeName Domain [ DefaultValue ] [ Constraints ] {, AttributeName Domain [ DefaultValue ] [ Constraints ] } [ OtherConstraints ] )

CSCC43: Introduction to Databases Create Table Construct An SQL relation is defined using the create table command: create table r (A 1 D 1, A 2 D 2,..., A n D n, (integrity-constraint 1 ),..., (integrity-constraint k )) r is the name of the relation each A i is an attribute name in the schema of relation r D i is the data type of values in the domain of attribute A i

Example of create table Employee: RegNo is 6 characters FirstName is 20 characters Surname is 20 characters Salary is 9 numeric City is 15 characters

Example of create table create table Employee ( RegNo character(6), FirstName character(20), Surname character(20), Salary numeric(9), City character(15) )

CSCC43: Introduction to Databases Intra-Relational Constraints Constraints are conditions that must be verified by every database instance Intra-relational constraints involve a single relation not null (on single attributes) unique: permits the definition of keys; syntax: for single attributes: unique, after the domain for multiple: unique ( Attribute {, Attribute } ) primary key: defines the primary key (once for each table; implies not null); syntax like unique check: described later

Example of Intra-Relational Constraints Each pair of FirstName and Surname uniquely identifies each element FirstName char(20) not null, Surname char(20) not null, unique(firstname,surname)

CSCC43: Introduction to Databases Inter-Relational Constraints Constraints may involve several relations: check: checks whether an assertion is true; references and foreign key permit the definition of referential integrity constraints; Syntax for single attributes references after the domain Syntax for multiple attributes foreign key ( Attribute {, Attribute } ) references... It is possible to associate reaction policies to violations of referential integrity constraints.

Example create table Employee ( RegNo char(6), FirstName char(20) not null, Surname char(20) not null, Dept char(15), Salary numeric(9) default 0, City char(15), primary key(regno), foreign key(dept) references Department(DeptName), unique(firstname,surname) )