CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010

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

Introduction to MySQL. 2 Road Map  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
How to Use MySQL CS430 March 18, SQL: “Structured Query Language”—the most common standardized language used to access databases. SQL has several.
Representing Data Elements Gayatri Gopalakrishnan.
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.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 3 Single-Table Queries
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) SQL and MySQL James Wang.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
CpSc 462/662: Database Management Systems (DBMS) MySQL.
SQL has several parts: Major ones: DDL – Data Definition Language {Defining, Deleting, Modifying relation schemas} DML – Data Manipulation Language {Inserting,
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (MySQL)
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
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.
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.
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
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
CCT395, Week 5 Translating ER into Relations; Normalization This presentation is licensed under Creative Commons Attribution License, v To view a.
CCT395, Week 7 SQL with PHP This presentation is licensed under Creative Commons Attribution License, v To view a copy of this license, visit
INF1343, Winter 2011 Data Modeling and Database Design Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
3 A Guide to MySQL.
Database Design and Implementation
Translating ER into Relations; Normalization
Review Databases and Objects
MySQL: Part II.
Data Definition and Data Types
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Special Topics in CCIT: Databases
CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
Introduction to MySQL.
Introduction to MySQL.
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
JDBC – Java Database Connectivity
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
Chapter 4 Introduction to MySQL.
Database Instructor: Bei Kang.
Introduction to MySQL.
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010 University of Toronto September 15, 2010 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/.

The Relational Model implements SQL controls

The Relational Model SQL controls

an SSH client (aka “telnet”)

1. Use the “Terminal” app: MacOS 1. Use the “Terminal” app: 2. Type in: ssh <utorid>@yoda.ischool.utoronto.ca It’s pre-installed! 3. Provide your username (utorid) and password (to be emailed); pick a new password.

Windows 1. Use PuTTY: Get it at: http:www.chiark.greenend.org.uk/~sgtatham/putty/

Connecting with PuTTY yoda.ischool.utoronto.ca click to connect

Login 1. Enter your utorid 2. Enter your password (to be emailed) 3. Pick a new password

1. Type: mysql -u <username> -p e.g.: mysql -u kenobio7 -p Once Connected 1. Type: mysql -u <username> -p e.g.: mysql -u kenobio7 -p 2. Enter your password (to be emailed)

mysql> show databases; Available Databases mysql> show databases; +--------------------+ | Database | | information_schema | | diveshop | | kenobio7 | | menagerie | 7 rows in set (0.00 sec) There is no database called “monkeys”!

mysql> use menagerie; Selecting a Database mysql> use menagerie; Database changed

mysql> show tables; Listing Tables mysql> show tables; +---------------------+ | Tables_in_menagerie | | event | | pet | 2 rows in set (0.00 sec)

mysql> describe pet; Describing a Table 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)

Relation “name” “species” “birth” “owner” “sex” (Fluffy, Harold, cat, f, 1993-02-04) (Buffy, Harold, dog, f, 1989-05-13) (Chirpy, Gwen, bird, f, 1998-09-11) “name” “species” “birth” “owner” “sex”

Table name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog bird sex f birth 1993-02-04 1989-05-13 1998-09-11

Order Doesn’t Matter birth 1993-02-04 1989-05-13 1998-09-11 owner Harold Gwen name Fluffy Bluffy Chirpy sex f species cat dog bird

Projection name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog bird sex f birth 1993-02-04 1989-05-13 1998-09-11

Projection name Fluffy Bluffy Chirpy species cat dog bird sex f

select <fields> from <table>; Projection in SQL select <fields> from <table>; For instance: select name, species, sex from pet;

mysql> select name, species, -> sex from pet; Projection in SQL mysql> select name, species, -> sex from pet; +----------+---------+------+ | name | species | sex | | Fluffy | cat | f | | Claws | cat | m | | Buffy | dog | f | | Fang | dog | m | | Bowser | dog | m | | Chirpy | bird | f | | Whistler | bird | NULL | | Slim | snake | m | | Puffball | hamster | f | 9 rows in set (0.00 sec)

mysql> select name, owner -> from pet; Projection in SQL mysql> select name, owner -> from pet; +----------+--------+ | name | owner | | Fluffy | Harold | | Claws | Gwen | | Buffy | Harold | | Fang | Benny | | Bowser | Diane | | Chirpy | Gwen | | Whistler | Gwen | | Slim | Benny | | Puffball | Diane | 9 rows in set (0.00 sec)

(“Restriction” in Harrington) Selection (“Restriction” in Harrington) name Fluffy Bluffy Chirpy owner Harold Gwen species cat dog bird sex f birth 1993-02-04 1989-05-13 1998-09-11

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

select <fields> from <table> where <condition>; Selection in SQL select <fields> from <table> where <condition>; For instance: select name from pet where owner="Harold";

mysql> select name from pet -> where owner="Harold"; Selection in SQL mysql> select name from pet -> where owner="Harold"; +--------+ | name | | Fluffy | | Buffy | 2 rows in set (0.00 sec)

Complex Conditions select <fields> from <table> where <condition1> and <condition2>; For instance: select name, owner, species from pet where owner="Harold" and species="dog";

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

Or We Can Use “OR" select <fields> from <table> where <condition1> or <condition2>; For instance: 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>; For instance: 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)

where <column>=<value>; Tests: Equality where <column>=<value>; For instance: select name from pet where owner="Harold";

where <column>!=<value>; Tests: Inequality where <column>!=<value>; For instance: 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)

More: >, <, <=, >= where <column><<value>; For instance: select name, birth from pet where birth<"1980-01-01";

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

where <column> between <value1> and <value2>; For instance: select name, birth from pet where birth between "1980-01-01" and "1990-01-01";

+-------+------------+ | name | birth | | Buffy | 1989-05-13 | 1 row in set (0.00 sec)

where <column> like <pattern>; For instance: select name from pet where name like "%uff%";

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

where <column> in (<values>); For instance: select name, species from pet where species in ("cat", "dog");

+--------+---------+ | name | species | | Fluffy | cat | | Claws | cat | | Buffy | dog | | Fang | dog | | Bowser | dog | 5 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.)

where <column> is null; For instance: 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)

where <column> is not null; For instance: 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

select distinct <columns> ... Removing Duplicates select distinct <columns> ... For instance: select distinct species from pet where owner="Gwen";

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

select ... from ... where ... order by <column>; Sorting the Results select ... from ... where ... order by <column>; For instance: select name, birth from pet order by birth;

+----------+------------+ | name | birth | | Bowser | 1979-08-31 | | Buffy | 1989-05-13 | | Fang | 1990-08-27 | | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Chirpy | 1998-09-11 | | Puffball | 1999-03-30 | 9 rows in set (0.01 sec)

select ... from ... limit <N>; For instance: select name, birth from pet order by birth limit 5;

+--------+------------+ | name | birth | | Bowser | 1979-08-31 | | Buffy | 1989-05-13 | | Fang | 1990-08-27 | | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | 5 rows in set (0.00 sec)

select count(<columns>) from ...; Aggregating: Count select count(<columns>) from ...; For instance: 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 For instance: select min(weight) from pet;

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

Grouping ... group by <columns>; For instance: select species, sum(weight) from pet group by species;

+---------+-------------------+ | species | sum(weight) | | bird | 0.453000005334616 | | cat | 13.1300001144409 | | dog | 52.3299984931946 | | hamster | 0.127000004053116 | | snake | 0.209999993443489 | 5 rows in set (0.02 sec)

“Having” select species, sum(weight) from pet group by species having sum(weight)<1; Similar to “where” conditions, but can refer to the aggregate values.

+---------+-------------------+ | species | sum(weight) | | bird | 0.453000005334616 | | hamster | 0.127000004053116 | | snake | 0.209999993443489 | 3 rows in set (0.00 sec)

mysql> describe pet; Data Types 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)

name owner species sex birth death weight Data Types name owner species sex birth death weight text (“string”) date numbers

String Characters or bytes What character set? How many? - char(n) - varchar(n) and text - blob, varbinary - enum and set

Numeric Numbers How precise? What is the range? - int(n), int(n) unsigned, bigint(n) - float(n), double(n) - boolean (or “bool")

Date and Time Date, time, or both - date - time - datetime - timestamp

Questions?