Database(Upd)......
Database(Upd)......
Information
communication
Technology
1
Querying a Database
A query is an inquiry into the database using the SELECT
statement.
A query is used to extract data from the database in
a readable format according to the user's request.
A query language consists of simple, English-like
statements that allow users to specify the data to
display, print, or store.
Each query language has its own grammar and
vocabulary
Uses of querying
Find data
Calculate values per record
Delete records
Most important DBMS skill 2
Why Querying is the Most Important Skill in
Database Management
Query is the main way you interact with the data. It
can be used to
Find the Data Easily:
When you need information from a database, you write a
query to get it.
To make decision:
Businesses use data to make decisions. If you can write
good queries, you can get the right data, which helps the
company make better choices.
Improving Database Performance
Good queries can make your database work faster. If
queries are poorly written, it can slow down the system
and make everything take longer.
3
Keeping Data Safe and Accurate:
Queries also allow you to update, delete, or add
data. It’s important to know how to do this carefully
so you don’t accidentally mess up or lose valuable
data.
Job Requirement:
Almost any job that involves working with data—like
in sales, marketing, or IT—requires you to know
how to query a database. It’s a basic skill needed
for many roles.
4
Query Languages
All DBMS use a query language
Most DBMS modify the language
Structured Query Language (SQL)
Most common query language
xBase
Query language for dBase systems
Query by example (QBE)
Interface to SQL or xBase
Interactive query design
5
Query Examples
SQL
Select FirstName, LastName, Phone
From tblPhoneNumbers
Where LastName=“Norton”;
6
Database Tables
A database most often contains one or more
tables.
Each table is identified by a name (e.g.
"Customers" or "Orders").
Tables contain records (rows) with data.
we are using the sample database
Below is a selection from the "Customers"
table:
7
Sample Database
The table above contains five records (one for each customer) and seven
columns (CustomerID, CustomerName, ContactName, Address, City,
PostalCode, and Country).
8
SQL Statements
10
SELECT STATMENT
There are four keywords or clauses that are
valuable part of SELECT statement.
13
SAMPLE DATABASE
(CUSTOMERS)
14
SELECT Column Example
The following SQL statement selects the "CustomerName"
and "City" columns from the "Customers" table:
15
16
The SQL SELECT DISTINCT
Statement
Inside a table, a column often contains many
duplicate values; and sometimes you only want
to list the different (distinct) values.
The SELECT DISTINCT statement is used to
return only distinct (different) values.
SELECT DISTINCT Syntax
17
SELECT DISTINCT
Examples
The following SQL statement selects only the
18
Continued…
The following SQL statement lists the number
of different (distinct) customer countries:
SELECT COUNT(DISTINCT Country) FROM
Customers;
Result:
Number of Records: 1
COUNT(DISTINCT Country)
21
19
FOR MS ACCESS
SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM
Customers);
Number of Records: 1
COUNT(DISTINCT Country)
21
20
The SQL WHERE Clause
The WHERE clause is used to filter records.
The WHERE clause is used to extract only those
records that fulfill a specified condition.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
21
SAMPLE DATABASE
RESULT ON NEXT SLIDE
CustomerID CustomerNa ContactNam Address City PostalCode Country
me e
1 Alfreds Maria Obere Str. Berlin 12209 Germany
Futterkiste Anders 57
22
RESULT
23
Operators in The WHERE
Clause
The following operators can be used in the
WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
24
Text Fields vs. Numeric
Fields
SQL requires single quotes around text values (most
database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes:
SELECT * FROM Customers WHERE CustomerID=1;
Number of Records: 1
25
SQL AND, OR and
NOT Operators
The WHERE clause can be combined with
26
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condi
tion3 ...;
OR Syntax
Number of Records: 2
28
AND Example
The following SQL statement selects all fields
from "Customers" where country is "Germany"
AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
Number of Records: 1
CustomerID Customer ContactN Address City PostalCo Country
Name ame de
1 Alfreds Maria Obere Berlin 12209 Germany
Futterkist Anders Str. 57
e
29
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
CustomerID CustomerNa ContactNam Address City PostalCode Country
me e
1 Alfreds Maria Obere Str. Berlin 12209 Germany
Futterkiste Anders 57
31
Combining AND, OR and
NOT
You can also combine the AND, OR and NOT
operators.
The following SQL statement selects all fields
from "Customers" where country is "Germany"
AND city must be "Berlin" OR "München" (use
parenthesis to form complex expressions):
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin'
OR City='München');
32
RESULT
Number of Records: 2
33
(DIY) Do it Yourself
The following SQL statement selects all fields
from "Customers" where country is NOT
"Germany" and NOT "USA":
35
SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the
result-set in ascending or descending order.
To sort the records in descending order, use
the DESC keyword.
The ORDER BY keyword sorts the records in
ascending order by default.
36
ORDER BY SYNTAX
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
37
RESULT
CustomerID CustomerNam ContactName Address City PostalCode Country
e
12 Cactus Patricio Cerrito 333 Buenos Aires 1010 Argentina
Comidas para Simpson
llevar
38
ORDER BY DESC EXAMPLE
SELECT * FROM Customers ORDER BY Country DESC;
33 GROSELLA- Manuel Pereira 5ª Ave. Los Caracas 1081 Venezuela
Restaurante Palos Grandes
35 HILARIÓN- Carlos Carrera 22 con San Cristóbal 5022 Venezuela
Abastos Hernández Ave. Carlos
Soublette #8-
35
40
ORDER BY Several Columns
Example
SELECT 2 ORDER BY Country ASC, CustomerName DESC;
* FROM Customers
41
Database Query
Query by example (QBE) provides a GUI to
assist users with retrieving data
Program retrieves records that match criteria
entered in form fields.
criteria
Query results
42
Use of Simple Query Wizard
43
Data Security
A DBMS provides means to ensure that only
authorized users access data at permitted times
Access privileges define activities that specific
user or group of users can perform
Read-only privileges - user can view data, but
cannot change it
Full-update privileges -user can view and change
data
principle of least privilege policy, where users’
access privileges are limited to the lowest level
necessary to perform required tasks
44
Backup and Recovery
A DMBS provides a variety of techniques to
restore the database to a usable form in case it
is damaged or destroyed
Backup Log
Recovery Continuous
utility backup
45
Backup and Log
Backup is a copy of the before image
entire database
Log is a listing of activities
that change database
contents
DBMS places three items in change
log:
before image after image
actual change, and
after image
also, might store who made
the change, when it was
made, and from which
computer it was made.
46
Recovery Utility
Uses logs and/or backups to restore database when it
is damaged or destroyed
Rollforward— DBMS uses log to re-enter changes
made to data-base since last save or backup
Also called forward recovery
Rollback— DBMS uses log to undo any changes made
to database during a certain period of time
Also called backward recovery
Continuous backup is a backup plan in which all data
is backed up whenever a change is made.
Can cost more than other backup strategies but is growing
in popularity because of its benefits
Provides recovery of damaged data in a matter of seconds.
47
Relational Databases
stores data in tables that
consist of rows and
columns
Each row has a primary
key
Each column has a unique
name
Stores data relationships
A relationship is a link
within the data
can set up a relationship
between tables at any time
48
Data Terminology
A relational database uses terms different from
a file processing system.
49
Data Warehouse
A data warehouse is a huge database that stores and
manages the data required to analyze historical and
current transactions
Uses multidimensional databases
Quick and efficient way to access large amounts of data
typically has a user-friendly interface, so that users easily
can interact with its data
Often, the database is distributed.
Data in a distributed database exists in many separate locations
throughout a network or the Internet
Data is accessible through a single server
Data’s location is transparent to the user, who usually is unaware that
the data is stored in multiple servers.
Often uses a process called data mining to find patterns and
relationships among data
50
Web Databases
Databases on the Web allow you to:
Some Web databases are collaborative databases, where
users store and share photos, videos, recordings, and other
personal media with other registered users
Shop for
Buy or sell Search for a
products or
stocks job
services
52
Common Corporate DBMS
Oracle
Most popular enterprise-level DBMS
Very flexible storage system
Can be very complex
Platform independent
Offers a wide range of solutions
DB2
Suit of applications to assist with data mining,
analysis and integration, data warehousing
Platform independent
Only database using pure SQL
53
Common Corporate DBMS
Microsoft SQL Server
Fastest growing DBMS
Only runs on Microsoft platforms
Eight different versions exist
Extremely scalable architecture
Software can grow with the data
MySQL
Leading DBMS for Linux
Very inexpensive
Features are those needed in business
Often faster than other DBMS
Platform independent 54
Database Administration
Database analysts and administrators are responsible
for managing and coordinating all database activities
55
Role of Employee as User
Employees should learn how to use the data in
the database effectively
Interact with database
Identify new data for the database
Maintain the database
Users can take part in designing the database that will
help them achieve those goals
56