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.

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.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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.
Representing Data Elements Gayatri Gopalakrishnan.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Basic SQL types String –Char(n): fixed length. Padded –Varchar(n): variable length Number –Integer: 32 bit –Decimal(5,2): –Real, Double: 32 bit,
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.
Chapter 12 Representing Data Elements By Yue Lu CS257 Spring 2008 Instructor: Dr.Lin.
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.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Database Systems Lecture 5 Natasha Alechina
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Copyright © Curt Hill SQL The Data Definition Language.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
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.
BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
1 CS1368 Introduction* Relational Model, Schemas, SQL Semistructured Model, XML * The slides in this lecture are adapted from slides used in Standford's.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
SQL: DDL John Ortiz Cs.utsa.edu.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
1 Database Systems Defining Database Schema Views.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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.
©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.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
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.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
Jennifer Widom Relational Databases The Relational Model.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
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.
Database Design and Programming Jan Baumbach Adopted from previous slides of Peter Schneider-Kamp.
1. 2 Design of databases. E/R model (entity-relationship model) Relational model (tables) UML (unified modeling language) Database programming. SQL, Relational.
CPSC-310 Database Systems
Relational Data Model Lu Chaojun, SJTU.
Constraints and Triggers
Database Models Relational Model
Instructor: Mohamed Eltabakh
Relational Databases The Relational Model.
Relational Databases The Relational Model.
SQL OVERVIEW DEFINING A SCHEMA
CMPT 354: Database System I
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Instructor: Mohamed Eltabakh
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

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 by the user. –Structure of data (math representation) Examples: relational model = relations(tables); semistructured model = trees/graphs. –Operations on data. –Constraints on data. 2

Lu Chaojun, SJTU Data Model: An Analogy Data Structure Operation Constraint 3 站得住 不能煮破 18 个褶

Lu Chaojun, SJTU Some Data Models Widely used today: –Relational, OR –Semistructured Others –OO History: –Hierarchical –Network 4

Lu Chaojun, SJTU Comparison Relational model is preferred, because of its –simple and limited, yet versatile, approach to structuring data –limited, yet useful, collection of operations –allow implementation of VHL language: SQL ease of use: just state what to do efficiency: optimization possible Semistructured models have more flexibility, but less preferred in DBMS 5

Lu Chaojun, SJTU Relational Model: Basics Relation = table Attribute = column ( header) tuple = row 6

Lu Chaojun, SJTU Relation: an example 7 codename name 007James Bond 008Steven Chow Spy Attributes (column headers) Tuples (rows) Relation name

Lu Chaojun, SJTU Relational Model: Basics(cont.) Relation schema = relation_name(attribute_list) –usually not change over time –SET of attributes, not a LIST. But take the “list” as standard order. –Optionally: types, other structure info –Example: Spy(codename,name) Relation instance = current set of tuples –change over time –Not a list Relational DB schema = set of relation schemas RDB instance 8

Lu Chaojun, SJTU Table Def. in SQL:2003 A table has an ordered collection of one or more columns and an unordered collection of zero or more rows. –Each column has a name and a data type. –Each row has, for each column, exactly one value in the data type of that column. 9

Lu Chaojun, SJTU Relational Model: Basics(cont.) Domains –atomic values for each attributes –each attribute is associated with a domain (type) –E.g. R(A1:D1,A2:D2) Key –attribute(s) to distinguish tuples in a relation –E.g. R(A1, A2, A3) –Semantic constraint –Create artificial keys 10

Lu Chaojun, SJTU Why Relational Model? Very simple model –single data-modeling concept -- relation Very High-level prog. language -- SQL –simple yet powerful Design theory –mathematically strict 11

Lu Chaojun, SJTU SQL Standard DB language –SQL:1999 –SQL:2003 SQL: DDL + DML Relations in SQL –Stored relation: table –View: not stored, constructed when needed –Temporary table: constructed by SQL processor SJTU Lu Chaojun12

Lu Chaojun, SJTU SQL Data Types Data Types –CHAR(n), VARCHAR(n), clob –BIT(n), BIT VARYING(n), blob –BOOLEAN –SMALLINT, INT/INTEGER, BIGINT –DECIMAL(n,d)/NUMERIC(n,d), FLOAT/REAL, DOUBLE PRECISION –TIME WITH/WITHOUT TIME ZONE, TIMESTAMP WITH/WITHOUT TIME ZONE, DATE, INTERVAL Implicit type coercions

Lu Chaojun, SJTU SQL Values Integers and reals: as expected Strings: single quotes! –Two single quotes = real quote e.g., ’I’’m Back.’ Any value can be NULL. Date: DATE ’yyyy-mm-dd’ –Example: DATE ’ ’ Time: TIME ’hh:mm:ss’, with optional fractions of a second –Example: TIME ’15:30:02.5’ SJTU Lu Chaojun14

Lu Chaojun, SJTU SQL:2003 Data Types A data type is a set of representable values. Every data value belongs to some data type. Data type –Predefined: specified by ISO/IEC 9075, and provided by the SQL-implementation. Atomic –Constructed Atomic Composite –User-defined

Lu Chaojun, SJTU Defining Relation Schema CREATE TABLE name ( column_1 type_1,..., column_n type_n ); –Most simple table declaration.

Lu Chaojun, SJTU Example CREATE TABLE Student ( sno CHAR(4), name CHAR(10), age INTEGER, dept VARCHAR(40) );

Lu Chaojun, SJTU Drop Table DROP TABLE relation; –No longer part of DB schema –Tuples also removed

Lu Chaojun, SJTU Modifying Relation Schema ALTER TABLE relation ADD column type | DROP column; –ADD: use NULL values as default.

Lu Chaojun, SJTU Default Values CREATE TABLE Student ( sno CHAR(4), name CHAR(10), age INT DEFAULT 18, dept VARCHAR(40) DEFAULT ‘CS’ ); –Default to NULL, if not designated.

Lu Chaojun, SJTU 21 Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Either says that no two tuples of the relation may agree in all the attribute(s) on the list. There are a few distinctions to be mentioned later.

Lu Chaojun, SJTU Declaring Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Student ( sno CHAR(10) PRIMARY KEY, name VARCHAR(20), age INTEGER, dept CHAR(8) ); 22

Lu Chaojun, SJTU 23 Declaring Multiattribute Keys A key declaration can also be another element in the list of elements of a CREATE TABLE statement. This form is essential if the key consists of more than one attribute. –May be used even for one-attribute keys.

Lu Chaojun, SJTU Example: Multiattribute Key The student number and course number together are the key for SC: CREATE TABLE SC ( sno char(10), cno char(20), grade integer, PRIMARY KEY (sno, cno) ); 24

Lu Chaojun, SJTU PRIMARY KEY vs. UNIQUE 1.There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes. 2.No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL. 25

Lu Chaojun, SJTU End