Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Similar documents
Insertions, Deletions, and Updates

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Relational Databases

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

PHP Queries and HTML Forms Lecture 23

Relational Algebra Part I. CS 377: Database Systems

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

MySQL Creating a Database Lecture 3

XQuery FLOWR Expressions Lecture 35

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

The Critical-Path Algorithm

Database design process

Minimal Spanning Trees

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

Overview Relational data model

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Relational Algebra & Calculus. CS 377: Database Systems

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Part 1 on Table Function

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Unit 1 - Chapter 4,5

KINGDOM OF SAUDI ARABIA-JAZAN UNIVERSITY COLLEGE OF COMPUTER SCIENCE & INFORMATION SYSTEMS 221 INFS 3 DATABASE SYSTEMS-1 REVIEW QUESTIONS

Database Technology. Topic 3: SQL. Olaf Hartig.

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Introduction to Databases

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

SQL Introduction. CS 377: Database Systems

The Decreasing-Time Algorithm

Recursive Descent Parsers

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

CS2300: File Structures and Introduction to Database Systems

CIS611 Lab Assignment 1 SS Chung

Density Curves Sections

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

CS 377 Database Systems

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Relational Calculus: 1

SQL Structured Query Language Introduction

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Data Definition Language (DDL)

Implementing Linked Lists

LR Parsing - Conflicts

Slides by: Ms. Shree Jaswal

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Scope and Parameter Passing

Lab # 4. Data Definition Language (DDL)

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Database Management Systems,

Some different database system architectures. (a) Shared nothing architecture.

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

ECE 650 Systems Programming & Engineering. Spring 2018

Scheduling and Digraphs

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

CSIE30600 Database Systems Basic SQL 2. Outline

The Pairwise-Comparison Method

Introduction to Databases

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Relational Databases Lecture 2

Recognition of Tokens

The Traveling Salesman Problem Brute Force Method

Scope and Parameter Passing

Function Definition Syntax Tree

Building the Abstract Syntax Trees

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

SQL STRUCTURED QUERY LANGUAGE

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Solving Recursive Sequences by Iteration

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Introduction to Compiler Design

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

COSC Assignment 2

Displaying Distributions - Quantitative Variables

DTDs and XML Attributes

The Graphics Pipeline

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Table Joins and Indexes in SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

After completing this unit, you should be able to: Define the terms

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Transcription:

Selections Lecture 4 Sections 4.2-4.3 Robb T. Koether Hampden-Sydney College Wed, Jan 22, 2014 Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 1 / 38

1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 2 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 3 / 38

Date and Time Types Standard date and time types DATE - A date in the format YYYY-MM-DD. TIME - A time of day in the format HH:MM:SS. DATETIME - A date and time in the format YYYY-MM-DD HH:MM:SS. TIMESTAMP - A date and time in the format YYYY-MM-DD HH:MM:SS. YEAR(n) - A year as either YYYY when n = 4 or YY when n = 2. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 4 / 38

Date and Time Types The values of a DATE may range from 1000-01-01 to 9999-12-31. The values of a DATETIME may range from 1000-01-01 00:00:00 to 9999-12-31 23:59:59. The values of a TIMESTAMP may range from 1970-01-01 00:00:01 to 2038-01-19 03:14:07. A TIMESTAMP attribute may be assigned the value CURRENT_TIMESTAMP or NOW(). Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 5 / 38

DATETIME Format There are several formats available for date input. Format Example YYYY-MM-DD 2013-01-05 YYYY-M-DD 2013-1-05 YYYY-MM-D 2013-01-5 YYYY-M-D 2013-1-5 YY-MM-DD 13-01-05 YYYY/MM/DD 2013/01/05 YY/MM/DD 13/01/05 YYYY:MM:DD 2013:01:05 YYYYMMDD 20130105 YYMMDD 130105 YYYY 2013 YY 13 Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 6 / 38

DATETIME Format If the year is given as a four-digit number YYYY, it may be in the range 1901 to 2155. If the year is given as a two-digit number YY, then 00-69 represent 2000-2069. 70-99 represent 1970-1999. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 7 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 8 / 38

Default Values Default Values ssn CHAR(11) DEFAULT 000-00-0000 ; We may use the DEFAULT clause to specify a default value for an attribute when a value is not given. For example, in the employees relation, if the Social Security number is not given, then we may let 000-00-0000 be the default value. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 9 / 38

Null Values The NOT NULL Modifier lname CHAR(20) NOT NULL; If we designate no default value and do not assign a value to an attribute in a tuple, then that attribute is given the value NULL. We may specify that a value may not be NULL. This prevents the value from being assigned NULL or being changed to NULL. For example, in the employees relation, we may specify that the last name be non-null. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 10 / 38

The CHECK Clause The CHECK Clause dept TINYINT CHECK(dept >= 1 AND dept <= 5); Even though the value of the attribute is of the right type, we may want to check that it is in the right range. For example, in the employees relation, the department number must be between 1 and 5. This feature is currently not available in MySQL. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 11 / 38

Primary Keys The PRIMARY KEY Clause PRIMARY KEY (ssn, dep_name) Every table should have a primary key. The primary key may be a single attribute or a set of attributes. A table s primary key must be unique for each tuple. For example, in the dependents relation, the Social Security number and name together must be unique. That would be a natural choice for the primary key. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 12 / 38

Unique Values The UNIQUE Modifier dept_name VARCHAR(64) UNIQUE; We may use the UNIQUE modifier to specify that a given attribute will have a unique value for each tuple, even though it may not be a primary key. For example, in the departments relation, we may assume that each department has a unique name. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 13 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 14 / 38

Storage Engines MySQL uses a storage engine to process queries. There are two main storage engines for MySQL. MyISAM (My Indexed Sequential Access Method) innodb (Derived from Innobase, a Finnish company) The features available in MySQL and their performance depend on the storage engine. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 15 / 38

MyISAM Advantages of MyISAM over innodb Easier to design databases. Faster execution. Especially fast at reading tables. Advantages of innodb over MyISAM Supports data integrity constraints. Supports transactions. Faster for inserting data and updating tables. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 16 / 38

Designating the Storage Engine Designating the Storage Engine CREATE TABLE table_name (attribute_list) ENGINE = engine_name; When creating a table, after describing its attributes, specify the storage engine. If the storage engine is omitted, then the default is MyISAM. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 17 / 38

The employees Table The employees Table CREATE TABLE employees ( fname char(20) DEFAULT NULL, lname char(20) NOT NULL, ssn char(11) NOT NULL DEFAULT 000-00-0000, bdate date DEFAULT NULL, sex char(1) DEFAULT NULL, salary decimal(10,2) DEFAULT NULL, dept int(11) DEFAULT NULL, PRIMARY KEY (ssn) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 18 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 19 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 20 / 38

Selections The SELECT Statement SELECT attribute_list FROM table_name WHERE condition ORDER BY attribute_list; The SELECT command will select tuples from a relation. The attribute list can be *, which means all attributes. The WHERE and ORDER BY clauses are optional. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 21 / 38

Selections SELECT Example SELECT * FROM employees; +----------+---------+-----------+------------+------+-----------+------+ fname lname ssn bdate sex salary dept +----------+---------+-----------+------------+------+-----------+------+ Alice Smith 123456789 1968-05-22 F 35000.00 2 Barbara Brown 135792468 1985-10-12 F 40000.00 3 James Green 246813579 1974-02-15 M 100000.00 1 Jennifer Wallace 321549876 1985-12-02 F 50000.00 2 Frank Gilbert 369147258 1966-08-21 M 30000.00 3 Joyce English 456789012 1983-05-07 F 25000.00 3 Richard Johnson 531978642 1955-03-17 M 35000.00 3 Ernest Roth 642189753 1986-06-12 M 60000.00 2 Susan Lane 654872109 1959-03-31 F 70000.00 3 John Kohler 789012345 1966-11-24 M 40000.00 2 Raymond Jones 963418527 1974-08-30 M 80000.00 2 Amy Ford 987105432 1968-01-21 F 30000.00 3 +----------+---------+-----------+------------+------+-----------+------+ Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 22 / 38

Selections SELECT Example SELECT fname, lname FROM employees; +----------+---------+ fname lname +----------+---------+ Alice Smith Barbara Brown James Green Jennifer Wallace Frank Gilbert Joyce English Richard Johnson Ernest Roth Susan Lane John Kohler Raymond Jones Amy Ford +----------+---------+ We can specify only those attributes that we want to see. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 23 / 38

Selections SELECT Example SELECT CONCAT(fname,, lname) FROM employees; +---------------------------+ CONCAT(fname,, lname) +---------------------------+ Alice Smith Barbara Brown James Green Jennifer Wallace Frank Gilbert Joyce English Richard Johnson Ernest Roth Susan Lane John Kohler Raymond Jones Amy Ford +---------------------------+ In the attribute list, we may write expressions as well as simple attributes. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 24 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 25 / 38

Renaming Attributes SELECT Example SELECT CONCAT(fname,, lname) AS emp_name FROM employees; +------------------+ emp_name +------------------+ Alice Smith Barbara Brown James Green Jennifer Wallace Frank Gilbert Joyce English Richard Johnson Ernest Roth Susan Lane John Kohler Raymond Jones Amy Ford +------------------+ Using the AS clause, we can rename the attribute. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 26 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 27 / 38

The WHERE Clause WHERE Example SELECT fname, lname, salary FROM employees WHERE salary > 40000; +----------+---------+-----------+ fname lname salary +----------+---------+-----------+ James Green 100000.00 Jennifer Wallace 50000.00 Ernest Roth 60000.00 Susan Lane 70000.00 Raymond Jones 80000.00 +----------+---------+-----------+ In the WHERE clause, we write a boolean expression that will filter out some of the tuples. Use AND, OR, and NOT for compound expressions. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 28 / 38

The WHERE Clause WHERE Example SELECT fname, lname, sex, bdate, salary FROM employees WHERE (bdate >= 1982-01-01 AND sex = M ) OR (bdate >= 1976-01-01 AND sex = F ); +----------+---------+------+------------+----------+ fname lname sex bdate salary +----------+---------+------+------------+----------+ Barbara Brown F 1985-10-12 40000.00 Jennifer Wallace F 1985-12-02 50000.00 Joyce English F 1983-05-07 25000.00 Ernest Roth M 1986-06-12 60000.00 +----------+---------+------+------------+----------+ Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 29 / 38

The WHERE Clause WHERE Example SELECT fname, lname, sex, bdate, salary FROM employees WHERE NOT((bdate >= 1982-01-01 AND sex = M ) OR (bdate >= 1976-01-01 AND sex = F )); +---------+---------+------+------------+-----------+ fname lname sex bdate salary +---------+---------+------+------------+-----------+ Alice Smith F 1968-05-22 35000.00 James Green M 1974-02-15 100000.00 Frank Gilbert M 1966-08-21 30000.00 Richard Johnson M 1955-03-17 35000.00 Susan Lane F 1959-03-31 70000.00 John Kohler M 1966-11-24 40000.00 Raymond Jones M 1974-08-30 80000.00 Amy Ford F 1968-01-21 30000.00 +---------+---------+------+------------+-----------+ Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 30 / 38

The WHERE Clause WHERE Example SELECT fname, lname, sex, bdate, salary FROM employees WHERE (bdate < 1982-01-01 OR sex = F ) AND (bdate < 1976-01-01 OR sex = M ); +---------+---------+------+------------+-----------+ fname lname sex bdate salary +---------+---------+------+------------+-----------+ Alice Smith F 1968-05-22 35000.00 James Green M 1974-02-15 100000.00 Frank Gilbert M 1966-08-21 30000.00 Richard Johnson M 1955-03-17 35000.00 Susan Lane F 1959-03-31 70000.00 John Kohler M 1966-11-24 40000.00 Raymond Jones M 1974-08-30 80000.00 Amy Ford F 1968-01-21 30000.00 +---------+---------+------+------------+-----------+ Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 31 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 32 / 38

The ORDER BY Clause ORDER BY Example SELECT fname, lname, salary FROM employees ORDER BY salary; +----------+---------+-----------+ fname lname salary +----------+---------+-----------+ Joyce English 25000.00 Amy Ford 30000.00 Frank Gilbert 30000.00 Richard Johnson 35000.00 Alice Smith 35000.00 Barbara Brown 40000.00 John Kohler 40000.00 Jennifer Wallace 50000.00 Ernest Roth 60000.00 Susan Lane 70000.00 Raymond Jones 80000.00 James Green 100000.00 +----------+---------+-----------+ We can use the ORDER BY clause to sort the tuples by specified fields. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 33 / 38

The ORDER BY Clause ORDER BY Example SELECT fname, lname, salary FROM employees WHERE sex = F ORDER BY salary; +----------+---------+----------+ fname lname salary +----------+---------+----------+ Joyce English 25000.00 Amy Ford 30000.00 Alice Smith 35000.00 Barbara Brown 40000.00 Jennifer Wallace 50000.00 Susan Lane 70000.00 +----------+---------+----------+ The ORDER BY clause comes after the WHERE clause. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 34 / 38

The ORDER BY Clause ORDER BY Example SELECT fname, lname, sex FROM employees ORDER BY sex, lname; +----------+---------+------+ fname lname sex +----------+---------+------+ Barbara Brown F Joyce English F Amy Ford F Susan Lane F Alice Smith F Jennifer Wallace F Frank Gilbert M James Green M Richard Johnson M Raymond Jones M John Kohler M Ernest Roth M +----------+---------+------+ If we specify more than one attribute, then they are treated as major and minor sort fields. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 35 / 38

The ORDER BY Clause ORDER BY Example SELECT fname, lname, sex, salary FROM employees ORDER BY sex, salary DESC; +----------+---------+------+-----------+ fname lname sex salary +----------+---------+------+-----------+ Susan Lane F 70000.00 Jennifer Wallace F 50000.00 Barbara Brown F 40000.00 Alice Smith F 35000.00 Amy Ford F 30000.00 Joyce English F 25000.00 James Green M 100000.00 Raymond Jones M 80000.00 Ernest Roth M 60000.00 John Kohler M 40000.00 Richard Johnson M 35000.00 Frank Gilbert M 30000.00 +----------+---------+------+-----------+ We can specify that any sort field be sorted in descending order. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 36 / 38

Outline 1 Datatypes 2 Constraints 3 Storage Engines 4 Selections The SELECT Statement Renaming Attributes The WHERE Clause The ORDER BY Clause 5 Assignment Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 37 / 38

Assignment Assignment Read Sections 4.2-4.3. Robb T. Koether (Hampden-Sydney College) Selections Wed, Jan 22, 2014 38 / 38