Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev

Slides:



Advertisements
Similar presentations
Introduction to MySQL. 2 Road Map  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database.
Advertisements

M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
How to Use MySQL CS430 March 18, SQL: “Structured Query Language”—the most common standardized language used to access databases. SQL has several.
Basic SQL types String –Char(n): fixed length. Padded –Varchar(n): variable length Number –Integer: 32 bit –Decimal(5,2): –Real, Double: 32 bit,
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Copyright © Curt Hill SQL The Data Definition Language.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
CSC 2720 Building Web Applications Database and SQL.
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
CpSc 462/662: Database Management Systems (DBMS) MySQL.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL has several parts: Major ones: DDL – Data Definition Language {Defining, Deleting, Modifying relation schemas} DML – Data Manipulation Language {Inserting,
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
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.
Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (MySQL)
Mysql YUN YEO JOONG. 1 Connecting to and Disconnecting from the Server 1 Connecting to and Disconnecting from the Server shell> mysql – h host -u user.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: Week 3 – MySQL – Statements.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT1343, Week 3 SQL Queries Using Multiple Tables This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
INF1343, Week 6 Implementing a Database with SQL This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
Introduction to SQL. Relational databases  Practical level: A data storage that can effectively been queried with SQL query language  A database consists.
CCT395, Week 11 Review, Permissions Physical Storage and Performance This presentation is licensed under Creative Commons Attribution License, v. 3.0.
CCT395, Week 6 Implementing a Database with SQL and a Case Study Exercise This presentation is licensed under Creative Commons Attribution License, v.
Database Design and Implementation
CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
Chapter 3 Introduction to SQL
Database Design and Implementation
Translating ER into Relations; Normalization
Managing Tables, Data Integrity, Constraints by Adrienne Watt
MySQL: Part II.
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
CS 480: Database Systems Lecture 13 February 13,2013.
Introduction to MySQL.
Introduction to MySQL.
Introduction to Structured Query Language (SQL)
Database Design and Implementation
Data Modeling and Database Design
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Database Design and Implementation
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Database systems Lecture 2 – Data Types
JDBC – Java Database Connectivity
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Section 4 - Sorting/Functions
SQL: Structured Query Language
Database Instructor: Bei Kang.
Introduction to MySQL.
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under Creative Commons Attribution License, v. 3.0. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/. This presentation incorporates images from the Crystal Clear icon collection by Everaldo Coelho, available under LGPL from http://everaldo.com/crystal/.

Week 2 One Table SQL

Answers to the Quiz select id, name from spare_parts where number_in_stock<=0; select number_in_stock where name="Type 2 Holoprojector";

Relational Algebra persona name occupation species Obi-Wan Kenobi Jedi Master Human Yoda NULL Jabba crime lord Hutt Chewbacca co-pilot Wookiee Luke Skywalker Jedi Knight Padmé Amidala queen

Projection persona name occupation species Obi-Wan Kenobi Jedi Master Human Yoda NULL Jabba crime lord Hutt Chewbacca co-pilot Wookiee Luke Skywalker Jedi Knight Padmé Amidala queen

Projection persona name species Obi-Wan Kenobi Human Yoda NULL Jabba Hutt Chewbacca Wookiee Luke Skywalker Padmé Amidala

Selection persona name occupation species Obi-Wan Kenobi Jedi Master Human Yoda NULL Jabba crime lord Hutt Chewbacca co-pilot Wookiee Luke Skywalker Jedi Knight Padmé Amidala queen (AKA “restriction”.)

Selection persona name occupation species Obi-Wan Kenobi Jedi Master Human Luke Skywalker Jedi Knight Padmé Amidala queen (AKA “restriction”.)

Columns vs Rows Projection: choosing columns (fields) by name Selection: choosing rows with a condition

selection followed by projection Back to SQL use starwars; select name, occupation from persona where species="Wookiee"; projection selection selection followed by projection

select * from «table» where «condition»; Skipping Projection select * from «table» where «condition»; select * from persona where species="Human";

select * from «table» where «condition»; Skipping Projection select * from «table» where «condition»; to be substituted general pattern the new parts select * from persona where species="Human"; specific example

Skipping Selection select * from «table»; select * from persona;

LIMIT select ... from ... limit «N»; select name from persona limit 5;

select ... from ... where ... order by «expression»; Sorting the Results select ... from ... where ... order by «expression»; select name from persona order by size;

select ... from ... where ... order by «expression» desc; Descending Sort select ... from ... where ... order by «expression» desc; select name from persona order by size desc;

Putting It Together select name, size from persona Who are the 3 tallest human characters (species="Human")? select name, size from persona where species="Human" order by size desc limit 3;

Putting It Together select name, size from persona Who are the 3 shortest human characters? select name, size from persona where species="Human" order by size desc limit 3;

Putting It Together select species from persona order by size limit 1; What is the species of the shortest character? select species from persona order by size limit 1;

Putting It Together select name from persona What which Jedi Master character comes first alphabetically? select name from persona where occupation="Jedi Master" order by name limit 1;

Moving On What is the name of the world that Humans are originally from? Hmm. Where is this information?

show tables; describe «table»; Browsing show tables; describe «table»; describe species;

select «fields» from «table» where «condition1» and «condition2»; Complex Conditions select «fields» from «table» where «condition1» and «condition2»; use menagerie; select name, weight from pet where owner="Harold" and species="dog";

+-------+--------+ | name | weight | | Buffy | 2.3 | 1 row in set (0.00 sec)

select «fields» from «table» where «condition1» or «condition2»; Or We Can Use “OR" select «fields» from «table» where «condition1» or «condition2»; select name, owner, species from pet where owner="Harold" or species="dog";

+--------+--------+---------+ | name | owner | species | | Fluffy | Harold | cat | | Buffy | Harold | dog | | Fang | Benny | dog | | Bowser | Diane | dog | 4 rows in set (0.00 sec)

select «fields» from «table» where not «condition»; And Why Not “NOT”? select «fields» from «table» where not «condition»; select name, owner from pet where not owner="Harold";

+----------+-------+ | name | owner | | Claws | Gwen | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | 7 rows in set (0.00 sec)

«condition1» and not («condition2» or «condition3») Combinations «condition1» and not («condition2» or «condition3») select name from pet where species="dog" and not (owner="Harold" or owner="Gwen");

+--------+ | name | | Fang | | Bowser | 2 rows in set (0.00 sec)

Things to Compare «field» = «constant» where name="Harald"; vs. «field1» = «field2» where name=owner;

where «field1» = «field2» Combinations where «field1» = «field2» select name from pet where name=owner;

+--------+--------+ | name | owner | | Margie | Margie | 1 row in set (0.00 sec)

An Odd Case "Harold"="Harold" What will we get back? select name, owner from pet where "Harold"="Harold";

+----------+--------+ | name | owner | | Fluffy | Harold | | Claws | Gwen | | Buffy | Harold | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | | Margie | Margie | 10 rows in set (0.00 sec)

Another Odd Case "Harold"="Gwen" What will we get back? select name, owner from pet where "Harold"="Gwen";

Empty set (0.00 sec)

«value» = «function»(«value») Functions «value» = «function»(«value») select name, owner from pet where upper(name)="buffy";

+-------+--------+ | name | owner | | Buffy | Harold | 1 row in set (0.00 sec)

where «value»=«value»; Tests: Equality where «value»=«value»; select name from pet where owner="Harold";

where «value»!=«value»; Tests: Inequality where «value»!=«value»; select name, owner from pet where owner!="Harold";

+----------+-------+ | name | owner | | Claws | Gwen | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | 7 rows in set (0.00 sec)

where «field» like «pattern»; Tests: LIKE where «field» like «pattern»; select name from pet where name like "%uff%";

+----------+ | name | | Fluffy | | Buffy | | Puffball | 3 rows in set (0.00 sec)

More: >, <, <=, >= where «value»<«value»; select name, birth from pet where birth<"1980-01-01";

+--------+------------+ | name | birth | | Bowser | 1979-08-31 | 1 row in set (0.00 sec)

«expression» > «expression» Expressions «expression» > «expression» select name from pet where weight>birth_weight*50;

+--------+ | name | | Fluffy | | Fang | | Bowser | 3 rows in set (0.01 sec)

Combinations select name from pet where species="dog" and not (owner="Harold" or owner="Gwen") and weight>birth_weight*50;

Combinations select name from pet where species="dog" and not (owner="Harold" or owner="Gwen") and weight>birth_weight*50;

+--------+ | name | | Fang | | Bowser | 2 rows in set (0.00 sec)

Complex Projection select upper(name), weight/1000 from pet where species="dog";

Complex Projection select upper(name), round(weight/1000,3) from pet where species="dog";

+-------------+----------------------+ | upper(name) | round(weight/1000,3) | | BUFFY | 0.002 | | FANG | 0.013 | | BOWSER | 0.037 | 3 rows in set (0.00 sec)

mysql> describe pet; The 6th Column mysql> describe pet; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | | weight | float | YES | | NULL | | 7 rows in set (0.00 sec)

mysql> select name, death > from pet; What’s in it? mysql> select name, death > from pet; +----------+------------+ | name | death | | Fluffy | NULL | | Claws | NULL | | Buffy | NULL | | Fang | NULL | | Bowser | 1995-07-29 | | Chirpy | NULL | | Whistler | NULL | | Slim | NULL | | Puffball | NULL | 9 rows in set (0.00 sec) NULL = no value provided (unknown, does not apply, etc.)

IS NULL where «field» is null; select name, death from pet where death is null;

+----------+-------+ | name | death | | Fluffy | NULL | | Claws | NULL | | Buffy | NULL | | Fang | NULL | | Chirpy | NULL | | Whistler | NULL | | Slim | NULL | | Puffball | NULL | 8 rows in set (0.00 sec)

“IS NULL” vs “= NULL” death="NULL" death=NULL true if the value of death is equal to the four-letter string “NULL” death=NULL always evaluates to NULL (the condition fails) death is NULL evaluates to TRUE is death is null and to FALSE if death is not null

where «field» is not null; select name, death from pet where death is not null;

+--------+------------+ | name | death | | Bowser | 1995-07-29 | 1 row in set (0.00 sec)

Null with And and Or Null and True → Null Null and False → False Null or True → True Null or False → Null

Duplicates select species from pet where owner="Gwen";

+---------+ | species | | cat | | bird | 3 rows in set (0.00 sec)

select distinct «fields» ... Removing Duplicates select distinct «fields» ... select distinct species from pet where owner="Gwen";

vs +---------+ | species | | cat | | bird | 2 rows in set (0.00 sec)

getting back less then one row per matched record Aggregation getting back less then one row per matched record

select upper(name) from pet where birth<"1994-12-31"; A “Normal” Function select upper(name) from pet where birth<"1994-12-31";

+-------------+ | upper(name) | | FLUFFY | | CLAWS | | BUFFY | | FANG | | BOWSER | 5 rows in set (0.00 sec)

select count(<fields>) from ...; Aggregating: COUNT select count(<fields>) from ...; select count(name) from pet where birth<"1994-12-31";

+-------------+ | count(name) | | 5 | 1 row in set (0.00 sec)

Counting Distinct select count(distinct owner) from pet where birth<"1994-12-31";

+-----------------------+ | count(distinct owner) | | 4 | 1 row in set (0.00 sec)

Summation select sum(weight) from pet where species="cat";

+-----------------+ | sum(weight) | | 13.130000114441 | 1 row in set (0.00 sec)

More Aggregation AVG, MIN, MAX select min(weight) from pet;

+-------------------+ | min(weight) | | 0.032000001519918 | 1 row in set (0.00 sec)

Data Modification INSERT add a new record UPDATE modify existing record DELETE remove a record

INSERT insert into my_pet values ( "Lilly", "Margie", "pony","f", "2001-04-19", NULL, 72, 2); here order matters!

INSERT insert into my_pet values ( "Frank", "Margie", "dog", "m", "2011-09-21", NULL, 6, 2), ( "Zé", "Margie", "fish", NULL, "2011-12-17", NULL, .1, 2);

INSERT insert into my_pet (name, owner) values ( "Minnie", "Margie" );

INSERT with SELECT insert into my_pet select * from pet;

UPDATE update my_pet set weight=100 where name="Buffy";

DELETE delete from my_pet where name="Buffy";

DELETE delete from my_pet;

CREATE TABLE use okenobi; create table «name» ( «field» «type» );

Domains / Data Types 01101001011110000011111100001101000110111001010110010010000 01000001110110101001000110010011110010000010000001011100110 00011100000001101000111101011100111110001110100110011010101 00111111011010011101100101100011011110100001111011101111001 10010000010110011100110001100011111011011000011000110011011 00111011101110110011101000110001001111100110000110010011011 00001100100001011001111111011011000010010101110101000110101 110110101100100100000110000100111001000110111100110110000110010000 0000000010110110011011111000001011011111000110100100 11100110010110110010100111111111001010000001111101101000010 00110111011010000010111100111011001001111111111100001111111 00001001100111111101100110011010101101111111101111001010101 10101111111001111101111010101001111111111011100111100010110 10010001101010011101001011100111011001111010101000110010100 10111100100010000000001001011001101000110000101111100100010 00110001000000000000101000011111011011010011010001110011100 01100110100111100001100000100011111011111011010111100100011 00001001000001001001001110111111001110001000000000100000010 00111011101101100010001111010011111011001000010100100010100

Domains / Data Types 01101001011110000011111100001101000110111001010110010010000 01000001110110101001000110010011110010000010000001011100110 00011100000001101000111101011100111110001110100110011010101 00111111011010011101100101100011011110100001111011101111001 10010000010110011100110001100011111011011000011000110011011 00111011101110110011101000110001001111100110000110010011011 00001100100001011001111111011011000010010101110101000110101 110110101100100100000110000100111001000110111100110110000110010000 0000000010110110011011111000001011011111000110100100 11100110010110110010100111111111001010000001111101101000010 00110111011010000010111100111011001001111111111100001111111 00001001100111111101100110011010101101111111101111001010101 10101111111001111101111010101001111111111011100111100010110 10010001101010011101001011100111011001111010101000110010100 10111100100010000000001001011001101000110000101111100100010 00110001000000000000101000011111011011010011010001110011100 01100110100111100001100000100011111011111011010111100100011 00001001000001001001001110111111001110001000000000100000010 00111011101101100010001111010011111011001000010100100010100

Domains / Data Types “Harold\n”? 1300457425722198016? 110110101100100100000110000100111001000110111100110110000110010000 0000000010110110011011111000001011011111000110100100 “Harold\n”? 1300457425722198016? 130045742.5722198016? 0.5237304801548 * 10-36?

CHAR(n) VARCHAR(n) TEXT String (Text) CHAR(n) VARCHAR(n) TEXT BINARY(n) VARBINARY(n) BLOB ENUM

INTEGER UNSIGNED SMALLINT BIGINT Numeric INTEGER UNSIGNED SMALLINT BIGINT NUMERIC(n,m) DECIMAL(n,m) BOOLEAN REAL DOUBLE PRECISION FLOAT(m,d)

DATE TIME DATETIME TIMESTAMP Date and Time DATE TIME DATETIME TIMESTAMP

Spatial: POINT POLYGON LINESTRING GEOMETRY Extensions Spatial: POINT POLYGON LINESTRING GEOMETRY

Choosing a Type table student name student_no date_of_birth no_of_credits gpa amount_owed

CREATE TABLE use okenobi; create table student ( name varchar(100) ); describe student;

DROP TABLE drop table student; describe student;

CREATE TABLE create table student ( name varchar(100), student_no integer, date_of_birth date, no_of_credits integer, gpa decimal(3,2), amount_owed decimal(10,2) );

An Exercise 1. Create a table to store info about films: title, year, country, director, duration. 2. Insert several movies. 3. Find the duration of the most recent movie.