CMPT 354: Database System I

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
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.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Information storage: Introduction of database 10/7/2004 Xiangming Mu.
The Relational Model These slides are based on the slides of your text book.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Instructor: Mohamed Eltabakh
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
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.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Fundamentals of DBMS Notes-1.
Structured Query Language (SQL) DDL
Logical DB Design: ER to Relational
Relational Data Model Lu Chaojun, SJTU.
COP Introduction to Database Structures
CS 480: Database Systems Lecture 13 February 13,2013.
Understand Data Manipulation Language (DML)
Database Management System
Introduction To Database Systems
Chapter 4 Relational Databases
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Database Management Systems (CS 564)
Understand Data Manipulation Language (DML)
Introduction to Database Management System
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Relational Data Model
CS 405G: Introduction to Database Systems
SQL OVERVIEW DEFINING A SCHEMA
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
CMPT 354: Database System I
CMPT 354: Database System I
SQL-1 Week 8-9.
CMPT 354: Database System I
Flat Files & Relational Databases
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Design: Relational Model
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Instructor: Mohamed Eltabakh
Lectures 2: Introduction to SQL 1
CS3220 Web and Internet Programming SQL and MySQL
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language Path from Unorganized to Organized….
DATABASE Purpose of database
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
SQL (Structured Query Language)
Presentation transcript:

CMPT 354: Database System I Lecture 2. Relational Model

Outline An overview of data models Basics of the Relational Model Define a relational schema in SQL

Outline An overview of data models Basics of the Relational Model Define a relational schema in SQL

Review What is a database? What is a DBMS? A collection of files that store related data What is a DBMS? A piece of software designed to store and manage databases

Data Storage with DBMS File 1 Application Program 1 File 2 . File n Application Program m

Data Model Data Model mathematical formalism (or conceptual way) for describing the data The description generally consists of three parts: Structure of the data Operations on the data Constraints on the data

Structure of the data Schema (e.g., table names, attribute names) Describe the conceptual structure of the data Different from data structure (e.g., list, array) Data structure can be seen as a physical data model

Operations on the data Query language (e.g., SQL) Describe what operations that can be performed on data Two kinds of operations operations that retrieve information operations that change the database Different from programming languages (e.g., C, Java) Support a set of limited operations Allow for query optimizations

Constraints on the data. Constraints (e.g., age > 0, student# is unique) describe limitations on what the data can be. Different kinds of constraints Domain constraints Integrity constraints Why does it matter? Ensure the correctness of data

Commonly Used Data Models Relational Data Model Key-Value Data Model Semi-structured Data Model (e.g., Json, XML)

The Relational Model in Brief Id Name Age GPA 1000 Mike 21 3.8 1001 Bill 19 3.4 1002 Alice 20 3.6 Structure of the Data Table structure Query language SQL Constraints on the data E.g., id is unique, age > 10, name is not NULL

The Key-Value Model in Brief 1000  (Mike, 21, 3.8) 1001  (Bill, 19, 3.4) 1002  (Alice, 20, 3.6) Structure of the Data (Key, Value) pairs Key is an integer/string, value can be any object Query language get(key), put(key, value) Constraints on the data E.g., key is unique, value is not NULL

The Semistructured Model in Brief Structure of the Data Tree structure Query language XPath Constraints E.g., <Age> has to be integer, each <Student> has a <Name> element nested within in

Outline An overview of data models Basics of the Relational Model Define a relational schema in SQL

Terminology Relations/Tables Columns/Attributes/Fields Rows/Tuples/Records Degree (arity) of a relation = #attributes Cardinality of a relation = #tuples Columns/ Attributes/ Fields Id Name Age GPA 1000 Mike 21 3.8 1001 Bill 19 3.4 1002 Alice 20 3.6 Rows/ Tuples/ Records

Schema Relation schema Database schema The name of a relation + The set of attributes for a relation Database schema The set of schemas for the relations of a database Student(id, sname, age, gpa) Student (sid, sname, age, gpa) Take (sid, cid) Course (cid, cname, credit)

Domains Each attribute has a domain (date type) Examples Text: CHAR(20), VARCHAR(50), TEXT Integer: INT, SMALLINT Real: DOUBLE, FLOAT Few more that are vendor specific Student(id:INT, sname:VARCHAR(50), age:INT, gpa:FLOAT)

Equivalent Representations of a Relation Order does not matter! Id Name GPA Age 1000 Mike 3.8 21 1001 Bill 3.4 19 1002 Alice 3.6 20 Id Name Age GPA 1000 Mike 21 3.8 1001 Bill 19 3.4 1002 Alice 20 3.6 Id Name Age GPA 1000 Mike 21 3.8 1002 Alice 20 3.6 1001 Bill 19 3.4

Exercise-1: Terminology Accounts AcctNo Type Balance 12345 savings 12000 23456 checking 1000 34567 25 List two other sayings of “rows” List two other sayings of “columns” List another saying of “table” Customers tuples, records fields, attributes relation fname lname idNo account Robbie Banks 901-222 12345 Lena Hand 805-333 23456

Exercise-2: Terminology Accounts Indicate the attributes of each relation Indicate the tuples of each relation Indicate the degree of each relation Indicate the cardinality of each relation AcctNo Type Balance 12345 savings 12000 23456 checking 1000 34567 25 Customers 4. Accounts: AcctNo, Type, Balance Customers: fname, lname, idNo, account 5. Accounts: (12345, “savings”, 12000), (23456, “checking”, 1000), (34567, ”savings”, 25) Customers: (“Robbie”, “Banks”, 901-222, 12345), (“Lena”, “Hand”, 805-333, 12345), (“Lena”, “Hand”, 805-333, 23456) 6. Accounts: 3 Customers: 4 7. Accounts 3 Customers: 3 fname lname idNo account Robbie Banks 901-222 12345 Lena Hand 805-333 23456

Exercise-3: Terminology Accounts AcctNo Type Balance 12345 savings 12000 23456 checking 1000 34567 25 Indicate the schema for each relation Indicate the database schema Specify a suitable domain for each attribute Customers 8. Accounts(AcctNo, Type, Balance) Customers(fname, lname, idNo, account) 9. Accounts(AcctNo, Type, Balance), Customers(fname, lname, idNo, account) 10. AcctNo: INT, Type: CHAR(8), Balance: INT, fname: VARCHAR(32), lname: VARCHAR(32), idNO: INT, account: INT fname lname idNo account Robbie Banks 901-222 12345 Lena Hand 805-333 23456

Exercise-4: Terminology Customers Indicate another equivalent way to represent this relation How many different ways to represent this relation? fname lname idNo account Robbie Banks 901-222 12345 Lena Hand 805-333 23456 11. For example, swap the 2nd and 3rd rows 12. 3! * 4! = 144

Keys Key = one (or multiple) attributes that uniquely identify a record AcctNo Type Balance 12345 savings 12000 23456 checking 1000 34567 25

Keys Key = one (or multiple) attributes that uniquely identify a record Key Not a key Not a key AcctNo Type Balance 12345 savings 12000 23456 checking 1000 34567 25

Multiple-attribute Key Multiple-attribute Key = multiple attributes that uniquely identify a record Key = fname, lname fname lname age salary Robbie Banks 20 10k Alice 30 8k Smith 25 12k

Multiple Keys Key Another Key SIN fname lname age salary 123-456-789 Robbie Banks 20 10k 222-111-709 Alice 30 8k 345-498-712 Smith 25 12k We can choose one key as primary key (e.g., SSN)

Foreign key to Accounts.acctNo Attribute(s) whose value is a key of a record in some other relation Accounts Customers acctNo type balance 12345 savings 12000 23456 checking 1000 34567 25 fname lname idNo account Robbie Banks 901-222 12345 Lena Hand 805-333 23456 Foreign key to Accounts.acctNo

Discussions Tables are NOT ordered They are sets or multisets (bags) Tables DO NOT prescribe how they are implemented/stored on disk This is called physical data independence

Outline An overview of data models Basics of the Relational Model Define a relational schema in SQL

SQL DDL SQL stands for Structured Query Language SQL is divided into two parts Data Manipulation Language (DML) which allows users to create, modify and query data Data Definition Language (DDL) which is used to define external and conceptual schemas The DDL supports the creation, deletion and modification of tables Including the specification of domain constraints and other constraints

Creating Tables To create a table use the CREATE TABLE statement Specify the table name, field names and domains CREATE TABLE Customer ( sin CHAR(11), firstName CHAR(20), lastName CHAR(20), age INTEGER, income REAL) Question – is SQL case sensitive? Answer – SQL keywords (create and table for example) are not case sensitive. Named objects (tables, columns etc.) may be.

Inserting Records To insert a record into an existing table use the INSERT statement The list of column names is optional If omitted the values must be in the same order as the columns INSERT INTO Customer(sin, firstName, lastName, age, income) VALUES ('111', 'Sam', 'Spade', 23, 65234)

Deleting Records To delete a record use the DELETE statement The WHERE clause specifies the record(s) to be deleted Be careful, the following SQL query deletes all the records in a table DELETE FROM Customer WHERE sin = '111' DELETE FROM Customer

Modifying Records Use the UPDATE statement to modify a record, or records, in a table Note that the WHERE statement is evaluated before the SET statement Like DELETE the WHERE clause specifies which records are to be updated UPDATE Customer SET age = 37 WHERE sin = '111'

Deleting Tables To delete a table use the DROP TABLE statement This not only deletes all of the records but also deletes the table schema DROP TABLE Customer

Modifying Tables Columns can be added or removed to tables using the ALTER TABLE statement ADD to add a column and DROP to remove a column ALTER TABLE Customer ADD height INTEGER ALTER TABLE Customer DROP height

Acknowledge Some lecture slides were copied from or inspired by the following course materials “W4111: Introduction to databases” by Eugene Wu at Columbia University “CSE344: Introduction to Data Management” by Dan Suciu at University of Washington “CMPT354: Database System I” by John Edgar at Simon Fraser University “CS186: Introduction to Database Systems” by Joe Hellerstein at UC Berkeley “CS145: Introduction to Databases” by Peter Bailis at Stanford “CS 348: Introduction to Database Management” by Grant Weddell at University of Waterloo