CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

Slides:



Advertisements
Similar presentations
The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Chapter 3 : Relational Model
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Design -- Basic SQL
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Minggu 2, Pertemuan 3 The Relational Model Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
1 Pertemuan 04 MODEL RELASIONAL Matakuliah: >/ > Tahun: > Versi: >
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
Chapter 3 The Relational Model Transparencies Last Updated: Pebruari 2011 By M. Arief
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CS 338SQL Queries5-1 SQL Queries Lecture Topics The SQL query language Table aliases Joins and unions Select statement syntax SQL and Views Nulls Textbook.
1 The Relational Model Instructor: Mohamed Eltabakh
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Relational Database. Database Management System (DBMS)
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Lu Chaojun, SJTU Relational Data Model 1. Lu Chaojun, SJTU What’s a Data Model? A notation (collection of conceptual tools) for describing data as seen.
CS 338Relational Algebra4-1 Relational Algebra Lecture Topics Basic concepts and operations of relational algebra Reference, selection, projection Cross-product.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Chapter 4 The Relational Model Pearson Education © 2009.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Lu Chaojun, SJTU.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
SQL OVERVIEW DEFINING A SCHEMA
Chapter 4 The Relational Model Pearson Education © 2009.
The Relational Model Transparencies
Chapter 4 The Relational Model Pearson Education © 2009.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and DML Views Textbook Chapter 3

CS 338The Relational Model2-2 Overview of SQL Structured Query Language (SQL, sometimes pronounced “sequel”) ISO/IEC 9075, international standard for relational database systems the standard is evolving: –1986: SQL1; initial version, 75 pages –1989: SQL89; most commercial products conform to this version, with extensions –1992: SQL2; three levels of conformity, ~600 pages –1999: SQL99 (sometimes called SQL3), ~1200 pages –2007: under revision again

CS 338The Relational Model2-3 powerful view definition language integrity constraints in conceptual schema DML can be embedded in various programming languages (called “embedded SQL”), or used via programming libraries –object/class libraries in OO environments transaction control authorization sublanguage/model...continued Main features:

CS 338The Relational Model2-4 DBMS Application 1Application 2 Application 3 View AView B Conceptual Level Physical Schema Database SQLSQL...continued

CS 338The Relational Model2-5 Underlying relational model Example relational database for a credit card company Vendor VnoVnameCityVbal Sears Kmart Esso Toronto Ottawa Montreal Waterloo Customer AccNumCnameProvCbalClimit Smith Jones Martin Ont BC Que Transaction TnoVnoAccNumTdateAmount

CS 338The Relational Model2-6 Structure of a relational database Database: collection of uniquely named tables (relations) Relation: set of rows (tuples) Attribute: column Domain: set of allowed values for an attribute Attribute values must be atomic (single values): no tuples or sets or repetition Row: distinguishable thing Table: set of related things

CS 338The Relational Model2-7 Diagrammatic conventions Vendor VnoVnameCityVbal or Vendor Vno Vname City Vbal

CS 338The Relational Model2-8 Pictorial schema Vname City VBal Vno Vendor AccNum Cname Prov Cbal Climit Customer Tno Vno AccNum Tdate Amount Transaction

CS 338The Relational Model2-9 The SQL DDL used for defining –tables –views example of table definition (conceptual schema): create table Vendor (Vno INTEGER not null, Vname VARCHAR(20), City VARCHAR(10), Vbal DECIMAL(10,2), primary key (Vno) );

CS 338The Relational Model2-10 create table Customer (AccNum INTEGER not null, Cname VARCHAR(20) not null, Prov VARCHAR(20), Cbal DECIMAL(6,2) not null, Climit DECIMAL(4,0) not null, primary key (AccNum) ); create table Transaction (Tno INTEGER not null, Vno INTEGER not null, AccNum INTEGER not null, Tdate DATE, Amount DECIMAL(6,2) not null, primary key (Tno), foreign key (Vno) references vendor(Vno), foreign key (AccNum) references Customer(AccNum) );...continued

CS 338The Relational Model2-11 Attribute domains in SQL INTEGER: integers representable with 32 bits SMALLINT: integers representable with 16 bits DECIMAL(m,n): fixed point numbers FLOAT: 32 bit floating point numbers CHAR(n): fixed length strings VARCHAR(n): variable length strings BIT(n): n bits BIT VARYING(n): variable number of bits

CS 338The Relational Model2-12 DATE (year, month, day) TIME (hour, minute, second) TIME(i) (hour, minute, second, second fraction) TIMESTAMP (date, time, second fraction) INTERVAL YEAR/MONTH (year month interval) INTERVAL DAY/TIME (day time interval) plus many, many product-specific (non- standard) extensions...continued

CS 338The Relational Model2-13 Modifying table definitions Table schemas can be changed after the table has been created: –adding columns –removing columns –removing constraints (e.g. p-key) –some SQL implementations allow renaming a column modifying a column Example: ALTER TABLE Vendor ADD Street VARCHAR(15)

CS 338The Relational Model2-14 Removing tables SQL operation is “drop” Tables can be dropped at any time Dropping a table deletes the schema and the instance All views, foreign-key definitions are also removed Example: DROP TABLE Transaction

CS 338The Relational Model2-15 SQL has a non-navigational DML: E.g. “Find names and provinces of customers who owe more than $1000 to the company.” select Cname, Prov from Customer where Cbal > 1000; The SQL DML

CS 338The Relational Model2-16 basic querying: select columns from R 1,..., R k [ where filter ] ; result is a relation over columns (columns = “*” means all attributes in R 1,..., R k ) R 1,..., R k : tables from which the data is retrieved filter : conditions on tuples used to form the result; optional...continued

CS 338The Relational Model2-17 conditions may include: –arithmetic operators +, -, *, / –comparisons =, <>,, >= –logical connectives and, or and not...continued E.g. “List the names of the customers who live in Ontario and whose balance is over 80% of their balance limit.” select Cname from Customer where Prov = 'Ont' and Cbal > 0.8 * Climit;

CS 338The Relational Model2-18 basic insertion: insert into Customer values (104, ‘Anne', 'ON', 0, 4000); deletion: delete Customer rows for customers named Smith: delete from Customer where Cname = 'Smith'; delete all transactions: delete from Transaction;...continued

CS 338The Relational Model2-19 modification, changing existing rows set balance of account 102 to zero: update Customer set Cbal = 0 where AccNum = 102; add $100 to each customer’s monthly limit: update Customer set Climit = Climit + 100;...continued

CS 338The Relational Model2-20 SQL external schema called views a view is a named query (result is usually computed when the view is used) create view WatVendors as select VNo, VName, VBal from Vendor where City = 'Waterloo'; views can be used in retrieval exactly like tables (but updates of views are restricted)

CS 338The Relational Model2-21 Advantages of views logical data independence simplified perception of the database different views for different users restricting data access

CS 338The Relational Model2-22 Basic relational concepts Relating to descriptions of data: Attribute (column): a name denoting a property or characteristic Relation schema (table header): a finite set of attributes and a mapping of each attribute to a domain (defined below)

CS 338The Relational Model2-23 Relating to data: Domain: an “abstract data type” (i.e. a name, a set of values and a number of functions defined over the values) Null value: a special exception value (meaning “not known”, “not applicable”) Tuple: a set of attribute/value pairs, with each attribute occurring at most once Relation (table): a relation schema, and a finite set of tuples Relational database: a finite set of relation names and a mapping of each relation name to a relation...continued

CS 338The Relational Model2-24 Other: Intention of a relation: the associated relation schema Extension of a relation: the associated set of tuples...continued The relational model assumes no ordering of either rows or columns for any table.

CS 338The Relational Model2-25 Basic rules Domain constraints: the value associated with each attribute in a tuple must occur in the set of values associated with the domain of the attribute; or the value is Null First normal form: domain values cannot be tuples or relations Completeness: each tuple in a relation has an attribute/value pair for precisely the set of attributes in the associated relation schema Closed world: the database “knows of” all tuples in all relations Unique rows: no two distinct tuples in any given relation consist of the same set of attribute/value pairs

CS 338The Relational Model2-26 Keys Relation superkey: a subset of the associated relation schema for which no pair of distinct tuples in the relation will ever agree on the corresponding values. Relation candidate key: a minimal superkey Relation primary key: a distinguished candidate key of the relation Foreign key: primary key of one relation appearing as attributes of another relation Foreign keys enable capturing more complex entity structure

CS 338The Relational Model2-27 Integrity of primary and foreign keys Entity integrity: No component of a primary key value may be the null value, nor may be updated. Referential integrity: A tuple with a non-null value for a foreign key that does not match the primary key value of a tuple in the referenced relation is not allowed.