Sie sind auf Seite 1von 31

BN 204 / BN204D DATABASE TECHNOLOGIES

Lecture 3-
The Relational Model 2: SQL

Acknowledgement: Concepts of Database Management, 8th Edition


Philip J. Pratt Grand Valley State University Mary Z. Last -Cengage Learning

Oct 2016 Compiled by - Dr. Deepani Guruge 1


Lecture 2 -Review
Relation: two-dimensional table
A tables primary key uniquely identify a row
Query-By-Example (QBE) -visual tool
Simple Queries
To indicate AND, OR criteria in an Access query,
Place criteria on separate Criteria rows
To create a computed field in Access, enter expression
in the desired column of design grid
To use functions to perform calculations
To sort query results in Ascending /Descending order
Oct 2016 Compiled by - Dr. Deepani Guruge 2
Lecture 3- Overview

QBE
Join tables in QBE
Update data using QBE
SQL
Introduce Structured Query Language (SQL)
Use simple and compound conditions in SQL
Use computed fields in SQL
Sorting

Oct 2016 Compiled by - Dr.3Deepani Guruge


QBE
Joining Tables

Queries to select data from more than one


table
Join the tables based on matching fields in
corresponding columns
Join line
Line drawn by Access between matching fields in
the two tables
Indicates that the tables are related

Compiled by - Dr. Deepani Guruge


4
Oct 2016
Joining Tables (continued)

FIGURE 2-29: Query design to join two tables


5
Oct 2016
Compiled by - Dr. Deepani Guruge
Joining Multiple Tables

Joining three or more tables is similar to


joining two tables
To join three or more tables:
Add the field lists for all tables in the join to upper
pane
Add the fields to appear in query results to design
grid in the desired order

Compiled by - Dr. Deepani Guruge


Oct 2016 6
Using an Update Query

Update query: a query that changes data


Makes a specified change to all records satisfying the
criteria in the query
To change a query to an update query:
Click Update button in the Query Type group on the
QUERY TOOLS DESIGN tab
Update To row is added when an update query is
created
Used to indicate how to update data selected by the
query

Oct 2016
Compiled by - Dr. Deepani Guruge 7
Using an Update Query
(continued)

FIGURE 2-35: Query design to update data


Oct 2016 8
Compiled by - Dr. Deepani Guruge
Using a Delete Query

Delete query: permanently deletes all records


satisfying the criteria entered in the query
To change query type to a delete query:
Click Delete button in the Query Type group on
the QUERY TOOLS DESIGN tab
Delete row is added
Indicates this is a delete query

Oct 2016 9
Compiled by - Dr. Deepani Guruge
Using a Delete Query
(continued)

FIGURE 2-36: Query design to delete records


Oct 2016 Compiled by - Dr. Deepani Guruge 10
Relational Algebra

Theoretical way of manipulating a relational


database
Includes operations that act on existing tables
to produce new tables
Each command ends with a GIVING clause,
followed by a table name
Clause requests the result of the command to be
placed in a temporary table with the specified
name

Compiled by - Dr. Deepani Guruge


Oct 2016
11
Select

Takes a horizontal subset of a table


Retrieves certain rows from an existing table
(based on criteria) and saves them as a new table
Includes the word WHERE followed by a
condition
Example:
SELECT Customer WHERE
CustomerNum=586
GIVING Answer

Oct 2016 Compiled by - Dr. Deepani Guruge


12
Join

Allows extraction of data from more than one


table
Two tables being joined
Join column: common column on which two tables
are joined
Rows in new table will be the concatenation
(combination) of rows from each original table
Natural join: joins records from each original
table that is common to both tables
Outer join: joins records from each original table
including records not common to both tables

Oct 2016 Compiled by - Dr. Deepani Guruge 13


SQL (STRUCTURED QUERY LANGUAGE)
Allows users to query a relational database
Must enter commands to obtain the desired
results
Standard language for relational database
manipulation

Oct 2016 Compiled by - Dr. Deepani Guruge 14


Getting Started with Microsoft
Office Access 2010 or 2013
TAL Distributors database is provided with Data Files
To execute SQL commands shown in the figures in
Access 2010 or Access 2013:
Open the TAL Distributors database
Click the CREATE tab on the ribbon
Click the Query Design button in the Queries group
Click the Close button in the Show Table dialog box
Click the View button arrow in the Results group on the
QUERY DESIGN TOOLS tab, then click SQL View
The Query1 tab displays the query in SQL view, ready for
you to type your SQL commands
Oct 2016 Compiled by - Dr. Deepani Guruge
15
Table Creation

SQL CREATE TABLE command


Creates a table by describing its layout
Typical restrictions placed on table and
column names by DBMS
Names cannot exceed 18 characters
Names must start with a letter
Names can contain only letters, numbers, and
underscores (_)
Names cannot contain spaces
Compiled by - Dr. Deepani Guruge
Oct 2016 16
Table Creation (continued)

INTEGER
Number without a decimal point
SMALLINT
Uses less space than INTEGER
DECIMAL(p,q)
P number of digits; q number of decimal places
CHAR(n)
Character string n places long
DATE
Dates in DD-MON-YYYY or MM/DD/YYYY form

Oct 2016 17
Compiled by - Dr. Deepani Guruge
Simple Retrieval

SELECT-FROM-WHERE: SQL retrieval command


SELECT clause: lists fields to display
FROM clause: lists table or tables that contain data
to display in query results
WHERE clause (optional): lists any conditions to be
applied to the data to retrieve
Simple condition: field name, a comparison
operator, and either another field name or a
value
Oct 2016 Compiled by - Dr. Deepani Guruge 18
Simple Retrieval (continued)

FIGURE 3-1: SQL query to select customer data (Access)

Compiled by - Dr. Deepani Guruge


Oct 2016 19
Simple Retrieval (continued)

FIGURE 3-3: SQL query to list the complete Item table

FIGURE 3-5: SQL query with WHERE condition


Oct 2016 20
Compiled by - Dr. Deepani Guruge
Simple Retrieval (continued)

FIGURE 3-6: Query results

FIGURE 3-7: Comparison operators used in SQL commands


Oct 2016
Compiled by - Dr. Deepani Guruge 21
Compound Conditions

Compound condition
Connecting two or more simple conditions using one or
both of the following operators: AND and OR
Preceding a single condition with the NOT operator
Connecting simple conditions using AND operator
All of the simple conditions must be true for the
compound condition to be true
Connecting simple conditions using OR operator
Any of the simple conditions must be true for the
compound condition to be true

Oct 2016 Compiled by - Dr. Deepani Guruge 22


Compound Conditions (continued)

FIGURE 3-14: Compound condition that uses the AND operator

FIGURE 3-15: Query results


23
Compiled by - Dr. Deepani Guruge
Compound Conditions (continued)

FIGURE 3-16: Compound condition that uses the OR operator

FIGURE 3-17: Query results


Compiled by - Dr. Deepani Guruge 24
Compound Conditions
(continued)
Preceding a condition by NOT operator
Reverses the truth or falsity of the original condition
BETWEEN operator
Value must be between the listed numbers
Computed field or calculated field
Field whose values you derive from existing fields
Can involve:
Addition (+), Subtraction (-)
Multiplication (*), Division (/)

Oct 2016
Compiled by - Dr. Deepani Guruge 25
Computed Fields (continued)

FIGURE 3-24: SQL query with a computed field and condition

FIGURE 3-25: Query results


Compiled by - Dr. Deepani Guruge 26
Using Special Operators (LIKE and
IN)

Wildcards in Access SQL


Asterisk (*): collection of characters
Question mark (?): any individual character
Wildcards in other versions of SQL
Percent sign (%): any collection of characters
Underscore (_): any individual character
To use a wildcard, include the LIKE operator in the WHERE
clause
IN operator provides a concise way of phrasing certain
conditions

Oct 2016
Compiled by - Dr. Deepani Guruge 27
Using Special Operators (LIKE and IN)
(continued)

FIGURE 3-26: SQL query with a LIKE operator

FIGURE 3-27: Query results


Compiled by - Dr. Deepani Guruge 28
Using Special Operators (LIKE and IN)
(continued)

FIGURE 3-28: SQL query with an IN operator

FIGURE 3-29: Query results


Compiled by - Dr. Deepani Guruge 29
Summary
QBE
To join tables (QBE), place field lists for both tables
To use an update query
To delete all records, use a delete query
To save the results of a query as a table, make-table
SELECT command, JOIN command to combine data
SQL
SQL CREATE TABLE
Basic form of an SQL query: SELECT-FROM-WHERE
In conditions, how to enter character values - enclosed in
single quotation marks
Compound conditions which are formed by combining
simple conditions using either or both of the following
operators: AND and OR.
Compiled by - Dr. Deepani Guruge
Oct 2016 30
Lecture 3 Questions
1. What is the basic form of an SQL statement?
2. Is the WHERE clause required or optional?
3. What is the built in function that you would use to
determine
1. number of records in a table?
2. the total of all customer balances,
3. calculate the smallest value in a column
4. Write SQL command use to create a table called student?
5. Write outputs of the following queries?
SELECT CustomerNum, SELECT CustomerNum, CustomerName
FROM Customer
CustomerName WHERE RepNum='15'
FROM Customer OR CreditLimit=10000;
WHERE RepNum='15';
Oct 2016 Compiled by - Dr. Deepani Guruge 31

Das könnte Ihnen auch gefallen