Download Selection condition

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Extensible Storage Engine wikipedia , lookup

Microsoft Access wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Clusterpoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database model wikipedia , lookup

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
COMP 1100
Basic SQL
David J. Stucki
1
Outline







SQL Overview
Retrievals
Schema creation
Table creation
Constraints
Inserts
Updates
2
SQL

Structured Query Language

High-level, declarative programming language


User specifies what he wants, not how to do it
Comprehensive database language



Statements for data definition, queries and updates
Defining views, specifying authorization
Setting up integrity constraints and transaction controls
3
SQL Terminology

Table


Row


Equivalent to tuple
Column


Equivalent to relational model’s relation
Equivalent to attribute
CREATE statement

Used for data definition
4
Schemas


Identified by a schema name
Can be thought of as a grouping of elements
into one database idea





Tables
Constraints
Views
Domains
Authorizations (Roles)
5
Sample Schema
6
SQL Retrieval

SQL retrieval operation: SELECT statement

SELECT
FROM
WHERE

<attributes> - Projection attributes

<condition> - Selection condition
<attributes>
<tables>
<condition>;
7
SQL Retrieval

Example SQL Query


SELECT
FROM
WHERE
BIRTH_DATE, ADDRESS
EMPLOYEE
FIRST_NAME=‘John’ AND
LAST_NAME=‘Smith’;
Retrieves every row from the table where the selection
condition holds true

Anyone with the name “John Smith” will have their birth date
and address returned
8
SQL Retrieval

Example select-project-join SQL Query


SELECT
FROM
WHERE
Fname, Lname
EMPLOYEE, DEPARTMENT
Dname=‘Research’
AND Dnumber=Dno;
Here will retrieve first name and last name of all
employees who work in the ‘Research’ department



Join condition – combines multiple tables in the WHERE
Finds the row in the table DEPARTMENT where DNAME is
‘Reseach’
Matches the DNUMBER for that row to the DNO in the
EMPLOYEE table

Only retrieves results where these values match
9
SQL Retrieval

Another example select-project-join SQL Query

SELECT
FROM
WHERE

Here we find:



Pname, Lname
EMPLOYEE, DEPARTMENT, PROJECT
Dnum=Dnumber
AND Mgr_ssn=Ssn
AND Plocation = ‘Houston’;
Names of projects located in Houston
Last names of the managers of the departments that these
projects are associated with
Note that this query joins across 3 tables:



Need PROJECT table to find the location
Relate PROJECT to DEPARTMENT via DNUMBER to get
Department Manager’s SSN
Use Dpt. Manager’s SSN to find last name in EMPLOYEE table
10
SQL Retrieval

Suppose we had chosen different names for our
attributes




What if we wanted to use “Dnumber” everywhere for the
department number?
This would no longer work:
SELECT
Fname, Lname
FROM
EMPLOYEE, DEPARTMENT
WHERE
Dname=‘Research’
AND Dnumber=Dnumber;
In this case, what do we mean by
Dnumber=Dnumber?

Ambiguous - we have two tables with the same attribute in
them
11
SQL Retrieval

We can specify exactly what we mean


SELECT
FROM
WHERE
Fname, Lname
EMPLOYEE, DEPARTMENT
Dname=‘Research’
AND EMPLOYEE.Dnumber
=DEPARTMENT.Dnumber;
Can also use aliases to make our lives simpler:

SELECT
FROM
WHERE
Fname, Lname
EMPLOYEE AS E, DEPARTMENT AS D
Dname=‘Research’
AND E.Dnumber=D.Dnumber;
12
SQL Retrieval

Aliases can also help us with circular references:

SELECT
FROM
WHERE

E.Fname, E.Lname
S.Fname, S.Lname
EMPLOYEE AS E, EMPLOYEE AS S
E.Super_ssn = S.Ssn;
This gets us back a list of employees and their supervisors

Without aliasing, this query would not work

Best practice is to always use aliases, even when you don’t need them

Usually improves understandability of code:

SELECT
FROM
WHERE
Fname, Lname
EMPLOYEE AS EMP, DEPARTMENT AS DEPT
DEPT.Dname=‘Research’
AND DEPT.Dnumber=EMP.Dno;
13
SQL Retrieval

Missing “WHERE” clauses


Where clauses are not required
When missing, ALL results are returned:

SELECT
FROM
Fname, Lname
EMPLOYEE;

This would return all employee names in the table
14
SQL Retrieval


Need to be careful with results
When multiple tables involved, you get back the cross
product of your tuples:

SELECT
FROM

This actually returns ALL employees crossed with ALL
Department names

Fname, Lname, Dname
EMPLOYEE, DEPARTMENT
So if you have two employees and two deparments, ‘John Smith’
in Research and ‘Bob Jones’ in ‘Accounting’, you would get:
John Smith Accounting
John Smith Research
Bob
Jones Accounting
Bob
Jones Research
15
SQL Retrieval

Wildcards in the select clause

Retrieve all of the attributes

SELECT
FROM
*
EMPLOYEE;

This would return all employee data from the EMPLOYEE
table
16
SQL Retrieval

Duplicate elimination


In a pure relational model, no relation would have
duplicate tuples
But SQL does allow duplicate tuples in a relation


So long as there is no conflict in their primary keys
…but if there’s no primary key defined in a table, it can have
duplicate entries
17
SQL Retrieval

Even on tables that have a primary key, sometimes
queries will return duplicates


If we’re only getting a limited number of attributes, there could
be overlap in those attributes
If we want to remove duplicates, we use the DISTINCT
keyword

SELECT
FROM
DISTINCT Salary
EMPLOYEE;

This would return only the unique SALARY entries from the
EMPLOYEE table, removing duplicate entries
18
SQL Retrieval

Wildcards in the WHERE clause

Sometimes we don’t want to match a whole attribute
completely





A partial match is enough
We can use a wildcard in the WHERE clause to accomplish
this
SELECT
Fname, Lname
FROM
EMPLOYEE
WHERE
Address LIKE ‘%Houston%’;
If we really need a ‘%’ literal character, we need to escape it:
SELECT
Product_name
FROM
PRODUCTS
WHERE
Discount LIKE ‘%5\%’;
19
SQL Retrieval

Arithmetic in queries

SELECT
FROM

This would give us back all of the employee last names with
their salary increased by 10%


Lname, 1.1*Salary AS Increase
EMPLOYEE;
Note that it doesn’t change what’s in the database – just what is
reported by the query
Ranges

SELECT
FROM
WHERE
*
EMPLOYEE
Salary >= 30000
AND Salary <= 400000;
20
SQL Retrieval

Ordering

We can force the query to come back in a particular
order


Very useful for reports and for debugging
SELECT
*
FROM
EMPLOYEE
ORDER BY
Lname, Fname, Super_ssn;
21