Top Banner
Instructor: Craig Duckett Lecture 02 : Thursday, October 1 st , 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)
48

Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

Jan 16, 2016

Download

Documents

David Higgins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

1

Instructor: Craig Duckett

Lecture 02: Thursday, October 1st, 2015SQL Basics and SELECT, FROM, WHERE

BIT275: Database Design (Fall 2015)

Page 2: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

2

Assignment 1 is due LECTURE 4, Thursday, October 8th, in StudentTracker by MIDNIGHT

• MID-TERM EXAM is LECTURE 10, Tuesday, November 3rd • Assignment 2 is due LECTURE 11, Thursday, November 5th,

in StudentTracker by MIDNIGHT

Page 3: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

Setting Up Cascadia Email Accounts Online

3

https://accounts.student.cascadia.edu/

Page 4: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

4

• http://databaseanswers.org/• http://databaseanswers.org/tutorials.htm• http://databaseanswers.org/data_models/index_all_models.htm

Suggested Resource: DatabaseAnswers.org*

*I have this listed on the Resources page, but wanted to call special attention to it.

Page 5: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

5

Tuesday (LECTURE 1)• Database Design for Mere Mortals: Chapter 1

Thursday (LECTURE 2)• The Language of SQL: • Chapter 1: Relational Databases and SQL• Chapter 2: Basic Data Retrieval

Page 6: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

6

• SQL Basics• SQL Syntax• SQL Keywords• The SELECT Statement• The FROM Clause• The WHERE Clause• BIT276: ICE 02

Page 7: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

7

SQL Basics

Page 8: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Basics

SQL, or Structured Query Language, is the common language that lies at the heart of every Relational Database Management System* (the application that actually gets the SQL to run and do something) that you'll likely to use. Now, you've probably noticed the different pronunciation already. Some people say S-Q-L, some people say sequel. I tend to say S-Q-L in general and when talking about MySQL (the MySQL official documentation says that’s how it should be pronounced), but I will say Microsoft Sequel Server (because that’s how they pronounce it on-campus AT Microsoft) although will sometimes revert to S-Q-L Server if I’ve been talking about SQL in general just before hand. In short, both ways are correct, but I would defer to what and how they are calling it where you work

Now SQL, the language, has been around since the 1970s, and that's one of the few languages I was writing 20 years ago that I'm still writing now. A lot of other programming languages have come and gone, become fashionable and unfashionable, but SQL has stuck around, and I expect it to stay for a long time to come. But SQL is a very small, very focused language, and the key to first learning it is to understand that it's a little different from other programming languages you might have come across, like C, C++, Java, or C#. SQL is what's considered a declarative query language, as opposed to procedural or imperative languages. What it means is you use SQL to describe what you want, and you let the Database Management System handle how that's actually done. You don't have to manually lay out the algorithm, the different steps of the procedure as you would do in other programming languages.

Page 9: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

*How Does SQL Run?

A .java files is simply a text file that needs a compiler like jGrasp to run. An .html or .css or .js file are also all text files that need a browser application to run. A C or C++ or C# file are simply text files as well that also need a compiler to run.

Like them, a .sql file is also a text file that needs a Database Management System (DBMS)* to run, although you can also enter your SQL queries directly into the DBMS without the need of a separate .sql file (as we shall see throughout the quarter, and starting with today’s ICE).

Different Types of Database Management Systems

• Access• FileMaker Pro• DB2• Sybase

• MySQL• MariaDB• SQL Server• Oracle

• dBase• InGres• PostgreSQL• Rocket U2

See: http://en.wikipedia.org/wiki/List_of_relational_database_management_systems

Page 10: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Basics: How Does It Work?

If I have a Books table with, perhaps, thousand different books in it, and I want to know which books have a list price of more than $40. Well, in a procedural or imperative language like C or Java, I'll have to write the steps to do this…

Page 11: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Basics

I would probably write some (pseudo) code that would start at the first book and then would loop through all of them one by one. And every time going through this loop you're asking the question, "Is this more than $40? If so, do one thing. If not, do something else." Even writing this in pseudo code, I'd be writing a loop. I'd be writing conditions. I'd be writing returns.

Page 12: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL BasicsWith SQL, you don't describe all the steps, you just describe the outcome. You describe what you want the same way you might do in English, where you just say:

or this written in SQL:

That's it. Select or go get everything from the Books table where the price is more than $40. The Database Management System will take this, it will look at all your data, figure it out, and return the result set, whether that would be one book, 500 books, or even none, based on your query.

Page 13: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL BasicsSo SQL can be used to select, to retrieve, or read data, and to ask questions of it.

Books Table

Page 14: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL BasicsIn fact, all of these things are referred to with the wonderful acronym of CRUD: Create, Read, Update, Delete.

An SQL can be used to create not just your data, but to define the databases themselves.

FYI: I will be discussing Data Manipulation Language (DML) and Data Definition Language (DDL) in Lecture 5

Page 15: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Syntax

http://www.w3schools.com/sql/sql_syntax.asp

Page 16: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Syntax1. Comment A comment is optional text that explains you program. Comments usually describe what a program does or why code

was changed. Compilers are for humans—the compiler ignores them. A comment is introduced by two consecutive hypens and continues until the end of the line. -- This is a commentSome DBMS (like Microsoft SQL Server) support the /* ... */ (forward slash-asterisk character pairs) multiline comments (those who've taken Java or C# should recognize these).

2. SQL Statement An SQL statement is a valid combination of tokens introduced by a keyword. Tokens are the basic indivisible parts of the SQL language; they can’t be reduced grammatically. Tokens include keywords, identifiers, operators, constants, and punctuation symbols.

3. Clauses An SQL statement has one or more clauses. In general, a clause is a fragment of an SQL statement introduced by a keyword, is required or optional, and must be given in a particular order. SELECT, FROM, WHERE, and ORDER BY are examples of clauses.

4. Keywords Keywords are words that SQL reserves because they have special meaning in the language. Using a keyword outside its specific context (as an identifier, for example)) causes an error. DBMSs use a mix of standard and nonstandard (proprietary) keywords. Search your DBMS documentation for keywords or reserved words.

5. Identifiers Identifiers are words that you (or the database designer) use to name database objects such as tables, columns, aliases, indexes, and views.

6. Terminating Semicolon AN SQL statement ends with a semicolon.

Page 17: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

Authors Table

Page 18: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL SyntaxSQL is a "freeform" language whose statements can:

• Be in uppercase or lowercase (SELECT, select, or SeLECt are considered to be identical keywords)• Continue on the next line as long as you don't split words, tokens, or quoted strings in two.• Be on the same line as other statements• Start in any column

Despite its flexibility, you should adopt a consistent style. I prefer to use uppercase keywords (e.g., SELECT) and an uppercase/lowercase mix for identifiers using Pascal case (e.g., FirstName).

Although I don't do it myself, some SQL coders also like to indent each clause on its own line:

Page 19: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL Syntax

Common Errors

• Omitting the terminating semicolon when using multiple statements (or Transact-SQL)

• Misspelling a keyword or identifier• Mismatched or unmatched parentheses or quotes (e.g., double quotes instead of

single quotes)• Listing clauses out of order• Not surrounding a string or datetime literal with single quotes• Surrounding a numeric constant (literal) or the keyword NULL with quotes• Mismatching a table and column name• Creating a table with a bad name

Page 20: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

20

SQL Keywords

Page 21: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SQL KeywordsStructured Query Language is the shared vocabulary of relational databases. That's not for general purpose programming. It's a small language focused purely on working with databases and the first handful of keywords that you'll learn (maybe a dozen) will take care of 90% of everything you'd ever do in SQL.

Page 22: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)
Page 24: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

The SELECT StatementIn terms of frequency, there's one that's head and shoulders above all the others. By far the most common word you'll ever write in SQL is SELECT. This is the word we start with when we want to SELECT or read information from one of the tables in one of our databases. We're using it to ask the database a question, and we expect a reply. Actually, the better word is not question but query. A query as in the Q in SQL: Structured Query Language.

if I want to SELECT some data out of my database, I'm going to have multiple tables and multiple columns in each table. So, I need to say what specific part of this database am I interested in selecting. The general format of a SELECT statement is the word SELECT, what columns you're interested in, then the word FROM what table? SELECT and FROM are your SQL keywords. They're part of the SQL language itself. Now, the columns and table names are up to you. It's whatever you named your columns and your tables in your database.

See: http://www.w3schools.com/sql/sql_select.asp

Page 25: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

25

The FROM Clause

Page 26: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SELECT Statement, FROM Clause

Page 27: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SELECT Statement, FROM Clause

Page 28: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SELECT Statement, FROM Clause

Page 29: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SELECT Statement, FROM Clause

Instead of selecting all the columns in a table using the *, you can specify only the columns you want by name (separated by a comma ,).

This example will return only the first names and last names of all employees.

Page 30: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

SELECT Statement, FROM Clause

Page 31: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

31

The WHERE Clause

Page 34: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

Uppercase, Lowercase, Semicolon?

Page 35: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

Database Name with Dot Notation

Dot notation allows you to prefix a table name to a column name to avoid ambiguity and to query multiple tables.

Page 36: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

Page 37: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

Page 38: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

Single ‘=‘ sign

‘<>’ NOT equal

Page 39: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

AND

OR

See: http://www.w3schools.com/sql/sql_and_or.asp

Page 40: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

IN

(note the single quotes, and comma)

See: http://www.w3schools.com/sql/sql_in.asp

Page 41: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

‘%’ wildcard

See: http://www.w3schools.com/sql/sql_wildcards.asp A substitute for 0 or more characters

Page 42: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

‘%’ wildcards

See: http://www.w3schools.com/sql/sql_wildcards.asp

This will return any last names that contain the ‘een’ characters anywhere in the name. For example:

Green, Greenburg, Springsteen, Deener

Page 43: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

‘_’ wildcard

See: http://www.w3schools.com/sql/sql_wildcards.asp

A substitute for a single character

Page 44: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

<> is not equal

See: http://www.w3schools.com/sql/sql_where.asp

This will return all employees whose last name is not equal to ‘Winkus’.

Page 45: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

IS NULL

See: http://www.w3schools.com/sql/sql_null_values.asp

Page 46: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

More About the WHERE Clause

IS NOT NULL

See: http://www.w3schools.com/sql/sql_notnull.asp

Page 47: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

47

BIT 275 ICE 02

Page 48: Instructor: Craig Duckett Lecture 02: Thursday, October 1 st, 2015 SQL Basics and SELECT, FROM, WHERE 1 BIT275: Database Design (Fall 2015)

BIT275: In-Class Exercises (ICE)

Using what you've learned today about SQL, use the remainder of the class time to write SQL queries as part of the BIT275 ICE 02 exercise. Feel free to utilize whatever you might finduseful in building these queries (a book, the PowerPoint slides, Google, etc).

SQL Query Exercise (solo, no partner )

http://faculty.cascadia.edu/cduckett/bit275/lecture_02/ice02.html