CMPT 258 Database Systems The Relationship Model (Chapter 3)

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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Database Design -- Basic SQL
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.
D ATABASE S YSTEMS I T HE R ELATIONAL D ATA M ODEL.
The Relational Model Ramakrishnan & Gehrke Chapter 3.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 3.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model Ramakrishnan & Gehrke, Chap. 3.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
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.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model These slides are based on the slides of your text book.
The Relational Model Chapter 3
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
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.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
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 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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (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 Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
COP Introduction to Database Structures
CS 186, Fall 2006, Lecture 2 R & G, Chap. 3
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
The Relational Model Relational Data Model
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
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
Presentation transcript:

CMPT 258 Database Systems The Relationship Model (Chapter 3)

Relational Database: Definitions Relational database: a set of relations with distinct relation names ▫Database: a set of tables with distinct table names

Relational Database: Definitions Relation (table): made up of 2 parts: ▫Schema : specifies name of relation (table), plus name and type of each column.  E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real). ▫Instance : a table, with rows and columns.  Can think of a relation as a set of rows or tuples (i.e., all rows are distinct). #Rows = cardinality, #fields = degree

Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct  Do all columns in a relation instance have to be distinct? TUPLES (RECORDS, ROWS) FIELDS (ATTRIBUTES, COLUMNS)

Relational Query Languages A major strength of the relational model ▫supports simple, powerful querying of data. Structured query language (SQL) Developed by IBM in the 1970s SQL Standard (ANSI) since it is used by many vendors

6 The SQL Query Language DDL (Data Definition Language) ▫A subset of SQL that supports the creation, deletion, and modification of tables ▫Commands CREATE, DROP, and ALTER are used for data definition DML (Data Manipulate Language) ▫The SELECT-FROM-WHERE structure of SQL queries SELECT FROM WHERE

DDL Create database Create table Alter table Drop table 7

Creating Databases Create database db_name; ▫A new database will be created if not exists. ▫e.g. Create database db_university; Use db_name; ▫e.g. Use db_university;

Creating tables

Creating Tables 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. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR (2))

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

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

DML: Adding Tuples Can insert a single tuple using: ▫ INSERT INTO TABLE_NAME [ (col1, col2, col3,...colN)] VALUES (value1, value2,value3,...valueN); INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, 18, 3.2) We can optionally omit the list of column name in the INTO clause and list the values in the appropriate order.

DML: Adding Tuples INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, 18, 3.2) INSERT INTO Students VALUES (53688, ‘Smith’, 18, 3.2)

Altering Tables ALTER TABLE tbl_name ADD COLUMN col_name domain DROP COLUMN col_name e.g., ALTER TABLE Students ADD COLUMN firstYear integer 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.

DML: updating data within a table Modify attribute values of one or more selected tuples Additional SET clause in the UPDATE command ▫Specifies attributes to be modified and new values UPDATE table_name SET column_name1 = value1, column_name2 = value2,... [WHERE condition]

The WHERE Clause Logical comparison operators ▫ =,, >=, and <> Projection attributes ▫Attributes whose values are to be retrieved Selection condition ▫Boolean condition that must be true for any retrieved tuple

DML: updating data within a table UPDATE table_name SET column_name1 = value1, column_name2 = value2,... [WHERE condition]

DML: updating data within a table

Exercises Use database university Create another table called Enrolled. ▫Enrolled(sid, cname) Add two tuples each for Joe and Paul Add another column called grade in Enrolled with a default value of ‘B’ Change Joe’s databases grade to ‘A-’ Change Paul’s data structures grade to ‘A’

Use AND to combine two or more conditions in the WHERE clause Is it case sensitive in MySQL when comparing strings? What if we want everyone in the databases class to get an ‘A’? What happens when we omit the WHERE clause? Joe dropped Data Structures and he took Algorithms instead and he got a D :( What is S.sid?

DML: Deleting Tuples Removes tuples from a relation ▫Includes a WHERE clause to select the tuples to be deleted DELETE FROM table_name [WHERE condition]; What happens when we omit the where clause?

DML: Deleting Tuples Can delete all tuples satisfying some condition (e.g., name = Smith): ▫ DELETE FROM Students WHERE name = ‘Smith’ What if nothing satisfies the condition?

Destroying Tables DROP TABLE tbl_name; ▫ e.g., DROP TABLE Students; ▫ e.g., DROP TABLE Enrolled; Destroys the relation Students. The schema information and the tuples are deleted.

Basic SQL Queries SELECT statement ▫One basic statement for retrieving information from a database

Basic SQL Queries To find all 18 year old students, we can write: SELECT * FROM Students WHERE age=18 Specify an asterisk (*) Retrieve all the attribute values of the selected tuples

Basic SQL Queries SELECT * FROM Students Missing WHERE clause Indicates no condition on tuple selection

Basic SQL Queries To find the names and logins of all 18 year old students : Find all the ids of students and courses in which they got an A. SELECT name, login FROM Students WHERE age=18

Querying Multiple Relations Find all the names of students and courses in which they got an A. SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=‘A’ Given the following instances of Enrolled and Students: we get:

Ambiguous Attribute Names Same name can be used for two (or more) attributes ▫As long as the attributes are in different relations ▫Must qualify the attribute name with the relation name to prevent ambiguity SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=‘A’

CROSS PRODUCT ▫All possible tuple combinations

SQL Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: ▫CREATE - to create objects in the database ▫ALTER - alters the structure of the database ▫DROP - delete objects from the database 33

SQL Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: ▫SELECT - retrieve data from the a database ▫INSERT - insert data into a table ▫UPDATE - updates existing data within a table ▫DELETE - deletes all records from a table, the space for the records remain 34

Readings Chapter 3