You are on page 1of 162

ITE11

Database Applications
Programming 1
Information System
A database is a carefully designed and constructed
repository of facts. The database is part of a larger whole
known as information system, which provides for data
collection, storage, and retrieval.

A complete information system is composed of people,


hardware, software, the database(s), application programs,
and procedures.
Information System
Systems Analysis is the process that establishes the need
for an information system and its extent. The process of
creating an information system is known as systems
development.

The performance of an information system depends on


three factors: (1) Database design and implementation, (2)
Application design and implementation, (3) Administrative
procedures.
Information System
Creating a sound information system ensures that all of
the activities will interface each other, that they will
complement each other, and that they will be completed
on time.

Database development describes the process of database


design and implementation.
Information System
The primary objective in database design is to create
complete, normalized, nonredundant, and fully integrated
conceptual, logical, and physical database models.

The implementation phase includes creating the database


storage structure, loading data into the database, and
providing for data management.
Systems Development Life Cycle
Systems Development Life Cycle is a general framework
through which you can track and understand the activities
requires to develop and maintain information systems.

SDLC is divided into five phases: planning, analysis,


detailed systems design, implementation, and
maintenance. The SDLC is an iterative process rather than
a sequential process.
SDLC-Planning
The SDLC planning phase yields a general overview of the
company and its objectives. An initial assessment of the
information flow-and-extent requirements must be made
during this discovery portion of the SDLC and should
answer the following questions:
• Should the existing system be continued?
• Should the existing system be modified?
• Should the existing system be replaced?
SDLC-Planning
If a new system is necessary, the next question is whether
it is feasible. The feasibility study must address the
following:
• The technical aspects of hardware and software
requirements.
• The system cost.
• The operational cost.
SDLC-Analysis
Problems defined during the planning phase are
examined in greater detail during the analysis phase. A
macro analysis must be made for both individual and
organizational needs, addressing questions such as:
• What are the requirements of the current system’s end
users?
• Do those requirements fit into the overall information
requirements?
SDLC-Analysis
The analysis phase of the SDLC is, in effect, a thorough
audit of user requirements.

Along with a study of user requirements and the existing


systems, the analysis phase also includes the creation of a
logical system design. The logical design must specify the
appropriate conceptual data model, inputs, processes,
and expected output requirements.
SDLC-Detailed System Design
In detailed systems design phase, the designer completes
the design of the system’s processes. The design includes
all the necessary technical specifications for the screens,
menus, reports, and other devices that might help make
the system more efficient information generator.

The steps are laid out for conversion from old system to
the new system. Training principles and methodologies
are also planned and must be submitted for approval.
SDLC-Implementation
During the implementation phase, the hardware, DBMS
software, and application programs are installed, and the
database design is implemented. During the initial stages
of the implementation phase, the system enters into a
cycle of coding, testing, and debugging until it is ready to
be delivered. The actual database are created, and the
system is customized by the creation of tables and views,
user authorization, and so on.
SDLC-Implementation
The database contents might be loaded interactively or in
by batch mode, using a variety of methods and devices:

• Customized user programs


• Database interface programs
• Conversion programs that import the data from a
different file structure, using batch programs, a
database utility, or both.
SDLC-Maintenance
Almost as soon as the system is operational, end users
begin to request changes in it. Those changes generates
system maintenance activities, which can be grouped into
three ways:
• Corrective maintenance
• Adaptive maintenance
• Perfective maintenance
The Database Life Cycle
Within the larger information system, the database is
subject to a life cycle. The Database Life Cycle (DBLC)
contains six phases; database initial study, database
design, implementation and loading, testing and
evaluation, operation, and maintenance and evaluation.
DBLC-Database Initial Study
The overall purpose of the database initial study is to:
• Analyze the company situation.
• Define the problems and constraints.
• Define objectives.
• Define scope and boundaries.
DBLC-Database Initial Study
Analyze the Company Situation
The company situation describes the general condition in
which a company operates, its organizational structure,
and its mission.

To analyze the company situation, the database designer


must learn the company’s operational components, how
they function, and how they interact.
DBLC-Database Initial Study
The following issues must be resolved:
• What is the organization’s general operating
environment, and what is its mission within the
environment?
• What is the organization’s structure?
DBLC-Database Initial Study
Define Problems and Constraints
The Process of defining problems might initially appear to
be unstructured. Company end-users often cannot
precisely describe the larger scope of company operations
or identify the real problems.

Often the managerial view of a company’s operation and


it’s problem is different from that of the end-users, who
perform the actual work.
DBLC-Database Initial Study
Define Objectives
A proposed database system must be designed to help
solve at least the major problems identified during the
problem discovery process. As the list of problems unfolds,
several common sources are likely to be discovered. The
database designer must begin to address the following:
• What is the proposed system’s initial objectives?
• Will the system interface with other existing/future
systems in the company?
• Will the system share the data with other systems/users?
DBLC-Database Initial Study
Define Scope and Objectives
The system’s scopes defines the extent of the design
according to operational requirements. Knowing the scope
helps define the required data structures, the type and
number of entities, the physical size of the database and so
on.
DBLC-Database Initial Study
Define Scope and Objectives
The proposed system is also subject to limit known as
boundaries, which are external to the system. Boundaries
are also imposed by existing hardware and software.
DBLC-Database Design
The second phase of the DBLC focuses on the design of the
database model that will support company operations and
objectives. Arguably, the most critical DBLC phase: making
sure that the final product meets user and system
requirements.

Defining data is an integral part of the DBLC’s second


phase.
Manager’s View

• What are the problems?


• What are the solutions?
• What information is needed to implement the solutions?
• What data are required to generate the desired information?

Designer’s View

• How must the data be structured?


• How will the data be accessed?
• How are the data transformed into information?
DBLC-Implementation and Loading
The output of the database design phase is a series of
instructions detailing the creation of tables, attributes,
domains, views, indexes, security constraints, and storage
and performance guidelines. During this phase, actual
implementation of all design specifications.
DBLC-Implementation and Loading
Install the DBMS
This step is required only when a new dedicated instance of
the DBMS is necessary for the system. The DBMS maybe
installed on a new server or an existing one. One current
trend is called virtualization. Virtualization is a technique
that creates logical representations of computing resources
that are independent of the underlying physical computing
resources.
DBLC-Implementation and Loading
Create the Database(s)
In most modern relational DBMSs, a new database
implementation requires the creation of special storage-
related constructs to house the end-user tables.
DBLC-Implementation and Loading
Load or Convert the Data
After the database has been created, the data must be
loaded into the database tables. Typically, the data will have
to be migrated from the prior version of the system. Once
the data have been loaded, the DBA works with the
application developers to test and evaluate the database.
DBLC-Testing and Evaluation
In the design phase, decisions were made to ensure
integrity, security, performance, and recoverability of the
database. During implementation and loading, these plans
were put into place. In testing and evaluation, the DBA
tests and finetunes the database to ensure that it performs
as expected. This phase occurs in conjunction with
application programming.
DBLC-Testing and Evaluation
Test the Database
During this step, the DBA test the database to ensure that it
maintains the integrity and security of the data. Data
integrity is enforced by the DBMS through the proper use of
primary and foreign key. Data integrity is also the result of
properly implemented data management policies, which are
part of a comprehensive data administration framework.
DBLC-Testing and Evaluation
At least, a test must be conducted to the following:
• Physical security
• Password security
• Access rights
• Audit trails
• Data encryption
• Diskless workstations
DBLC-Testing and Evaluation
Finetune the Database
Database performance can be difficult to evaluate because
there are no standards for measuring it, but it is typically
one of the most important factors in database
implementation.
DBLC-Testing and Evaluation
Evaluate the Database and Its Applications Programs
Testing and evaluation of the individual components should
culminate in a variety of broader system test to ensure that
all of the components interact properly to meet the needs
of the users.

Timely data availability is crucial for almost every database.


Unfortunately, the database can lose data through
unintended deletions, power outrages, and other causes.
DBLC-Testing and Evaluation
Database backups can be performed at different levels:

• A full backup, or dump, of the entire database.


• A differential backup of the database, in which only the
objects that have been updated or modified since the
last full backup.
• A transaction backup, which backs up only the
transaction log operations that are not reflected in a
previous backup copy of the database
Source Description Example
Software Software-induced failures may be traceable The SQL Slammer worm affected many
to the operating system, the DBMS unpatched MS SQL Server systems in
software, application programs, or viruses. 2003 causing millions of dollars in
damages.
Hardware Hardware-induced failures may include A bad memory module or a multiple hard
memory chip errors, dick crashes, bad disk disk failure in a database system can bring
sectors, and full-disk errors. it to an abrupt stop.
Programming Application programs or end users may roll Hackers constantly search for ways to
Exemptions back transactions when certain conditions exploit unprotected Web database
are defined. Programming exemptions can systems.
also be caused by malicious or improperly
tested code that can be exploited by
hackers.
Transactions The system detects deadlocks and aborts Deadlocks occur when executing multiple
one of the transaction simultaneous transactions.
External factors Backups are especially important when a In 2005, Hurricane Katrina in New Orleans
system suffers complete destruction from caused data losses worth millions of
fire, earthquake, flood and other natural dollars.
disasters
DBLC-Testing and Evaluation
Depending on the type and extent of the failure, the
recovery process ranges from a minor short-term
inconvenience to a major long-term rebuild. Regardless of
the extent of the required recovery process, recovery is not
possible without a usable backup.
DBLC-Operation
Once the database has passed the evaluation stage, it is
considered operational. The beginning of the operational
phase invariably starts the process of system evolution. As
soon as all of the targeted end users have entered the
operations phase, problems could not have been foreseen
during the testing phase begin to surface. Some of the
problems are serious enough to warrant emergency
“patchwork”, while others are merely minor annoyances.
The demand for change is the designer’s constant concern.
DBLC-Maintenance and Evolution
Some of the periodic maintenance activities include:
• Preventive maintenance (backup)
• Corrective maintenance (recovery)
• Adaptive maintenance (enhancement)
• Assignment of access permissions and their maintenance
• Generation of database access statistics to improve the
efficiency and usefulness of the system
• Periodic security audits based on the system statistics
• Monthly, quarterly, or yearly system usage summaries.
Conceptual Design
DBLC database design comprise of three stages: conceptual
design, logical design, and physical design. Conceptual
Design is the first stage in the database design process. The
goal of this stage is to design a database that is
independent of database software and physical details.

The output of this process is a conceptual data model that


describes the main data entities, attributes, relationships,
and constraints of a given problem domain.
Conceptual Design
In this stage, data modelling is used to create an abstract
database structure that represents real-world objects in the
most realistic way possible. The conceptual model must
embody a clear understanding of the business and its
functional areas.
Conceptual Design
The conceptual design has four steps:
• Data analysis and requirements
• Entity relationship modelling and normalization
• Data model verification
• Distributed database design
Conceptual Design
Data Analysis and Requirements
The first step in conceptual design is to discover the
characteristic of the data elements. An effective database is
an information factory that produces key ingredients for
successful decision making.
Conceptual Design
Designer’s efforts are focused on:
• Information needs. What kind of information is needed?
What output must be generated by the system, what
information does the current system generate, and to
what extent is that information adequate?
• Information users. Who will use the information? How is
the information to be used? What are the various end-
user data views?
Conceptual Design
• Information sources. Where is the information to be
found? How is the information to be extracted once it is
found?
• Information constitution. What data elements are
needed to produce the information? What are the data
attributes? What relationships exists among the data?
What is data volume? How frequently are the data used?
What data transformations will be used to generate the
required information?
Conceptual Design
How designers will obtain answers to questions are by the
following:
• Developing and gathering end-user data views.
• Directly observing the current system: existing and
desired output.
• Interfacing with the systems design group.
Conceptual Design
To develop an accurate data model, the designer must have
a thorough understanding of the company’s data types and
their extent and uses. But data do not, by themselves, yield
the required understanding of the total business. The
collection of data becomes meaningful only when business
rules are defined.
Conceptual Design
Business rules is a brief and precise description of a policy,
procedure, or principle within a specific organization’s
environment. Business rules, derived from a detailed
description of an organization’s operations, help to create
and enforce actions within that organization’s environment.

To be effective, business rules must be easy to understand


and they must be widely disseminated to ensure that every
person in the organization shares a common interpretation.
Conceptual Design
Examples of business rules are as follows:
• A customer may make many payments on an account.
• Each payment on an account is credited to only one
customer.
• A customer may generate many invoices.
• Each invoice is generated by only one customer.
Conceptual Design
Business rules are derived from a formal description of
operations, which is a document that provides a precise,
up-to-date, and thoroughly reviewed description of the
activities. The main sources of information for the
description of operations are company managers, policy
makers, department managers, and written documentation
such as company procedures, standards, and operations
manuals. A faster and more direct source of business rules
is direct interviews with end users.
Conceptual Design
Business rules yield several important benefits in the design
of the new system:
• They help standardized the company’s view of data
• They constitute a communications tool between users
and designers
• They allow the designer to understand the nature, role,
and scope of the data
• They allow the designer to understand business process
• They allow the designer to develop appropriate rules and
foreign key constraints.
Conceptual Design
Entity Relationship Modelling and Normalization

Steps in developing the Conceptual Model using ER


Diagrams:
• Identify, analyze, and refine the business rules.
• Identify the main entities, using the results of STEP 1.
• Define the relationships among the entities, using results
of STEP 1 and STEP 2.
• Define the attributes, primary keys, and foreign keys for
each of the entities.
Conceptual Design
• Normalize the entities.
• Complete the initial ER diagram.
• Validate the ER model against the end users’ information
and processing requirements.
• Modify the ER model, using the results of STEP 7.
Conceptual Design
Example:
Suppose the you are creating a conceptual model for
JollyGood Movie Rental, whose end users want to track
customers’ movie rental. As for the company’s business
rule, customers are not required to check out a video. A
video need not to be checked out in order to exist on the
shelf. A customer may rent many videos, and a video may
be rented by many customers.
Conceptual Design
Data Model Verification
Data model verification is one of the last steps in conceptual
design stage, and it is one of the most critical. In this step,
the ER model must be verified against the proposed system
processes to corroborate that they can be supported by the
database model.
Conceptual Design
Verification requires that the model be run through a series
of tests against:
• End-user data views
• All required transactions: SELECT, INSERT, UPDATE, and
DELETE operations.
• Access rights and security.
• Business-imposed data requirements and contraints.
Conceptual Design
Because real-world database design is generally done by
teams, the database design is probably divided into major
components known as modules. A module is an
information system component that handles a specific
business function, such as inventory, orders, or payroll.
Conceptual Design
Working with modules accomplishes several important
ends:
• The modules can be delegated to design groups within
teams, greatly speeding up the development work.
• The modules simplify the design work.
• The modules can be prototyped quickly.
• Even if the entire system cannot be brought online
quickly, the implementation of one or more modules will
demonstrate the progress is being made.
Conceptual Design
The ER model verification process:
• Identify the ER model’s central entity.
• Identify each module and its components.
• Identify each module’s transaction requirements:
(internal: updates/inserts/deletes/queries/reports
external: module interfaces)
• Verify all processes against system requirements
• Make all necessary changes suggested in STEP 4.
• Repeat STEPS 2-5 for all modules.
Conceptual Design
Distributed Database Design
Distributed database defines the optimum allocation
strategy for database fragments to ensure database
integrity, security, and performance. The allocation strategy
determines how to partition the database and where to
store each fragment. A database fragment is a subset of a
database that is stored at a given location.
DBMS Software Selection
Although the factors that affect the purchasing decision
vary from company to company, some of the most
common are:
• Cost. This includes the original purchase price, along
with maintenance, operational, license, installation,
training, and conversion cost.
• DBMS feature tools.
• Underlying model.
• Portability
• DBMS hardware requirments
Logical Design
Logical design is the second stage in the database design
process. The logical design goal is to design an enterprise-
wide database that is based on specific data model. Logical
design steps is generally performed in the following:
• Map the conceptual model to logical model components.
• Validate the logical model using normalization.
• Validate the logical model integrity constraints.
• Validate the logical model against user requirements.
Logical Design
Map the Conceptual Model to the Logical Model

The process of translating the conceptual model into a set of


relations are the following:
• Map strong entities.
• Map supertype/subtype relationships.
• Map weak entities.
• Map binary relationships.
• Map higher-degree relationships.
EMPLOYEE PROFESSOR CLASS

emp_num (PK) emp_num (PK) (FK) class_code (PK)


emp_lname prof_specialty emp_num (FK)
emp_fname prof_rank crs_code (FK)
emp_initial class_time
emp_e_mail class_days

COURSE

crs_code (PK)
crs_title
crs_descript
crs_credit
Logical Design
Validate the Logical Model Using Normalization
The logical design should contain only properly normalized
tables. The process of mapping the conceptual model to
the logical model may unveil some new attributes or the
discovery of new multivalued or composite attributes.
Logical Design
Validate Logical Model Integrity Constraints

CLASS_CODE CLASS_DAYS CLASS_TIME


is a valid code is a valid code is a valid code
Type: numeric Type: character Type: character
1000-9999 XXX 99:99
9999 MWF, TR, M, T, W 06:00-22:00
R, F, S
Length: 4 Length: 3 Length: 5
Logical Design
Validate the Logical Model Against User Requirements
The final step in the logical design process is to validate all
logical model definitions against all end-user data,
transaction, and security requirements.
Physical Design
Physical design is the process of determining the data storage
organization and data access characteristics of the database
to ensure its integrity, security, and performance. Physical
design can become a very technical job that affects not only
the accessibility of the data but also the performance of the
system. The physical design consists of the following:
• Define data storage organization
• Define integrity and security measures
• Determine performance measurements
Physical Design
Define Data Storage Organization
Before defining the data storage organization, it must be
determined first the volume of data to be managed and the
data usage patterns.
• Knowing the data volume will help in determining how
much storage space to reserve for the database.
• Knowing how frequently the new data are inserted,
updated, and retrieved will help the designer determine
the data usage patterns.
Physical Design
Equipped with the previous information, the designer must:
• Determine the location and physical storage organization
for each table.
• Identify indexes and the type of indexes to be used for
each table.
• Identify the views and the type of views to be used on
each table.
Physical Design
Define Integrity and Security Measures
Once the physical organization of the tables, indexes, and
views are defined, the database is ready for the end users. In
this step of physical design, two tasks must be addressed:
• Define user and security and roles.
• Assign security controls.
Physical Design
Determine Performance Measures
Physical design becomes more complex when data are
distributed at different locations because the performance is
affected by the communication media’s throughput. Physical
design performance measurement deals with fine-tuning the
DBMS and queries to ensure that they will meet end-user
performance requirements.
Data Model
The basic building blocks of all data models are entities,
attributes, relationships, and constraints. An entity is a
person, place, thing, or event about which data will be
collected and stored. An entity represents a particular type of
object in the real world, which means an entity is
“distinguishable” – that is, each entity occurrence is unique
and distinct.
Data Model
An attribute is a characteristic of an entity. For example, a
CUSTOMER entity would be described by attributes such as
last name, first name, phone number, etc. Attributes are the
equivalent of fields in file systems.
Data Model
A relationship describes an association among entities. For
example, a relationship exists between customers and agents
that can be described as follows: an agent can serve many
customers, and each customer may be served by one agent.
Data Model
A constraint is a restriction placed on the data. Constraints
are important because they help to ensure data integrity.
Constraints are normally expressed in the form of rules.
Data Model
The data dictionary provides a detailed description of all
tables in the database created by the user and designer. The
data dictionary contains at least all of the attribute names and
characteristics for each table in the system.

The data dictionary is sometimes described as “the database


designer’s database” because it records the design decisions
about tables and their structures.
Attribute FK Referenced
Table Name Contents Type Format Range Required PK/FK
Name Table
CUSTOMER CUS_CODE Customer account code CHAR(5) 99999 10000-99999 Y PK

CUS_LNAME Customer last name VARCHAR(20) Xxxxxxx Y FK AGENT_CODE

CUS_FNAME Customer first name VARCHAR(20) Xxxxxxx Y

CUS_INITIAL Customer initial CHAR(1) X

CUS_RENEW_D Customer insurance renewal DATE dd-mmm-yyyy


ATE data
AGENT_CODE Agent code CHAR(3) 999

AGENT AGENT_CODE Agent code CHAR(3) 999 Y PK

AGENT_AREAC Agent area code CHAR(3) 999 Y


ODE
AGENT_PHONE Agent telephone number CHAR(8) 999-9999 Y

AGENT_LNAME Agent last name VARCHAR(20) Xxxxxxx Y

AGENT_YTD_SL Agent year-to-date sales NUMBER(9,2) 9,9999,999.99


S
Data Model
Relationships are classified as one-to-one (1:1), one-to-many
(1:M), and many-to-many (M:N). The 1:M relationship is the
norm for relational databases. The 1:1 relationship implies
that one entity in a 1:1 relationship can be related to only one
other entity, and vice versa. A many-to-many relationship is
not supported directly in the relational environment.
However, M:N relationships can be implemented by creating a
new entity 1:M relationships with the original entities.
Introduction to SQL
A database language allows you to create database and table
structures, perform basic data management chores (add,
delete, and modify), and perform complex queries designed
to transform the raw data into useful information. A database
language must perform basic functions with minimal user
effort, and its command structure and syntax must be easy to
learn.
Introduction to SQL
SQL functions fit into two broad categories:
• It is a data definition language (DDL). SQL includes
commands to create database such as tables, indexes, and
views, as well as commands to define access rights to
database objects.
Command or Option Description
CREATE SCHEMA AUTHORIZATION Creates a database schema
CREATE TABLE Creates a new table in the user’s database schema
NOT NULL Ensures that a column will not have null values
UNIQUE Ensures that a column will not have duplicate values
PRIMARY KEY Defines a primary key for a table
FOREIGN KEY Defines a foreign key for a table
DEFAULT Defines a default value for a column (when no value is given)
CHECK Validates data in an attribute
CREATE INDEX Creates an index for a table
CREATE VIEW Creates a dynamic subset of rows and columns from one of more tables
ALTER TABLE Modifies a table’s definition
CREATE TABLE AS Creates a new table based on a query in the user’s database schema
DROP TABLE Permanently deletes a table
DROP INDEX Permanently deletes an index
DROP VIEW Permanently deletes views
Create Schema
CREATE SCHEMA <new schema name>
AUTHORIZATION <new schema owner>;
Create Table
CREATE TABLE (column1 datatype,
column2 datatype, ..column3
datatype);
CREATE TABLE Persons (PersonID int, LastName
varchar(255), FirstName varchar(255), Address
varchar(255), City varchar(255));

CREATE TABLE Persons AS SELECT PersonID,


LastName, FirstName, Address, City FROM
Personnel;

CREATE TABLE Persons LIKE Personnel;


Not Null
By default, a column can hold NULL values. The NOT NULL
constraint enforces a column to NOT accept NULL values.

CREATE TABLE (ID int NOT NULL


UNIQUE, LastName varchar(255) NOT
NULL, FirstName varchar(255) NOT
NULL);
SELECT ProductName, UnitPrice * (UnitsInStock
+ COALESCE(UnitsOnOrder,0)) FROM
tbl_nullvalues;

SELECT ProductName, UnitPrice * (UnitsInStock


+ IFNULL(UnitsOnOrder,0)) FROM tbl_nullvalues;
Unique
The UNIQUE constraint ensures that all values in a column are
different.

CREATE TABLE (ID int NOT NULL


UNIQUE, LastName varchar(255) NOT
NULL, FirstName varchar(255) NOT
NULL);
Primary Key
The PRIMARY KEY constraint uniquely identifies each record
in a table.

CREATE TABLE (ID int NOT NULL,


LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
PRIMARY KEY (ID) );
Foreign Key
The FOREIGN KEY is a key used to link two tables together.

CREATE TABLE Orders (OrderID


int NOT NULL, OrderNumber
int NOT NULL, PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Pe
rsons(PersonID))
Default
The DEFAULT constraint is used to provide a default value for
a column.

CREATE TABLE Persons(ID


int NOT NULL, LastName
varchar(255)NOT NULL, FirstName
varchar(255), Age int, City
varchar()255 DEFAULT ‘Sandnes’)
Check
The CHECK constraint is used to limit the value range that can
be replaced in a column.

CREATE TABLE Persons(ID


int NOT NULL, LastName
varchar(255)NOT NULL, FirstName
varchar(255), Age int, CHECK
(Age>=18));
Create Index
The CREATE INDEX constraint is used to create indexes in
table. Indexes are used to retrieve data from the database
very fast .
CREATE INDEX idx_lastname ON
Persons(LastName);
Create View
In SQL, a view is a virtual table based on the result-set of an
SQL statement. A view contains rows and columns , just like a
real table.
Alter Table
The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table. It also used to add and drop
various constraints on an existing table.

ALTER TABLE Persons ADD COLUMN


PlaceofBirth date;
Introduction to SQL
• It is a data manipulation language (DML). SQL includes
commands to insert, update, delete, and retrieve data
within the database tables.
Command or Option Description
INSERT Inserts row(s) into a table
SELECT Selects attributes from rows in one or more tables or views
WHERE Restricts the selection of rows based on a conditional expression
GROUP BY Groups the selected rows based on one or more attributes
HAVING Restricts the selection of grouped rows based on a condition
ORDER BY Orders the selected rows based on one or more attributes
UPDATE Modifies an attribute’s values in one or more table’s rows
DELETE Deletes one or more rows from a table

COMPARISON OPERATORS
=, <, >, <=, >=, <> Used in conditional expressions
LOGICAL OPERATORS
AND/OR/NOT Used in conditional expressions
Command or Option Description
SPECIAL OPERATORS
BETWEEN Checks whether an attribute value is within a range
IS NULL Checks whether an attribute value is null
LIKE Checks whether an attribute value matches a given string pattern
IN Checks whether an attribute value matches any value within a value list
EXISTS Checks whether a subquery returns any rows
DISTINCT Limits values to unique values

AGGREGATE FUNCTIONS
COUNT Returns the number of rows with non-null values for a given condition
MIN Returns the minimum attribute value found in a given column
MAX Returns the maximum attribute value found in a given column
SUM Returns the sum of all values for a given column
AVG Returns the average of all values for a given column
Insert Into
The INSERT INTO statement is used to insert new records in a
table.

INSERT INTO table_name (column1,


column 2...) VALUES (value1, value2,
...);
INSERT INTO table_name SET
column1=value1, column 2=value2;
INSERT INTO table_name SET column1=value1,
column2=value2, column3=value3;

INSERT INTO table_name SELECT * FROM


table_name;
Select
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-
set.

SELECT column1, column2 FROM


table_name;

SELECT * FROM table_name;


Select - where
The WHERE clause is used to filter records. The WHERE
clause is used to extract only those records that fulfill a
specified condition.

SELECT column1, column2 FROM


table_name WHERE condition;

SELECT * FROM table_name;


Operator Description
= Equal
<> Not equal (!=)
> 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
Select – group by
The GROUP BY statement is often used with aggregate
functions (COUNT, MAX, MIN, SUM, AVG) to group the result-
set by one or more columns.

SELECT colum_name(s) FROM table_name


WHERE condition GROUP BY
column_name(s) ORDER BY
column_name(s);
Select – having
The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.

SELECT colum_name(s) FROM table_name


WHERE condition GROUP BY
column_name(s) HAVING condition
ORDER BY column_name(s);
Select – order by
The ORDER BY keyword is used to sort the result-set in
ascending or descending order.

SELECT colum1, column2 FROM


table_name ORDER BY column1,
column2... ASC|DESC;
Update
The UPDATE statement is used to modify the existing records
in a table.
UPDATE table_name SET
column1=value1, column2=value2 WHERE
condition;
UPDATE table1, table2, ... SET
column1 = expression1, column2 =
expression2, ... WHERE table1.column
= table2.column AND conditions;
Delete
The DELETE statement is used to delete existing records in a
table.

DELETE FROM table_name WHERE


condition;
Between
The BETWEEN operator selects values within a give range.
The values can be numbers, texts, or dates.

SELECT column_name(s) FROM


table_name WHERE condition BETWEEN
value1 AND value2;
Is Null
SELECT column_name(s) FROM
table_name WHERE column_name IS
NULL;

SELECT column_name(s) FROM


table_name WHERE column_name IS NOT
NULL;
Like
The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column. There are two wildcards used in
conjunction with the LIKE operator (%, _).

SELECT column1, column2 FROM


table_name WHERE column LIKE
pattern;
In
The IN operator allows you to specify multiple values in a
WHERE clause. The IN operator is a shorthand for multiple
OR conditions.

SELECT column_name(s) FROM table_name


WHERE column_name IN (value1,
value2,... / SELECT STATEMENT);
Exists
The EXISTS operator is used to test for the existence of any
record in a subquery.

SELECT column_name(s) FROM table_name


WHERE EXISTS (SELECT column_name FROM
table_name WHERE condition);
Count, Avg, Sum
The Count() function returns the number of rows that
matches a specified criteria, the AVG() function returns the
average value of a numeric column, and SUM() function
returns the total sum of numeric column.

SELECT COUNT/AVG/SUM(column_name)FROM
table_name WHERE condition;
Min, Max
The Min() Function returns the smallest value of the selected
column. The Max() function returns the largest value of the
selected column.

SELECT MIN/MAX(column_name)FROM
table_name WHERE condition;
Introduction to SQL
• Transactional Control Language (TCL). These SQL
commands are used for managing changes affecting the
data. These command are commit, rollback, and savepoint.
Command or Option Description
COMMIT Commit command is used to permanently save any transaction into
database.
ROLLBACK This command restores the database to last committed state. It is also use
with savepoint command to jump to a savepoint in a transaction.
SAVEPOINT This command is used to temporarily save a transaction so that you can
rollback to that point whenever necessary.
Introduction to SQL
• Data Control Language (DCL). These SQL commands are
used for providing security to database objects. These
commands are grant, revoke, and deny.
Command or Option Description
GRANT This command is used by administrators to add new permissions to a
database user.
REVOKE This command is used to remove database access from a user previously
granted such access.
DENY This command is used to explicitly prevent a user from receiving a particular
permission.
SQL Join Operators
The relational join operation merges rows from two tables
and returns the rows with one of the following conditions:
• Have common values in common columns (natural join)
• Meet a given join condition (equality or inequality)
• Have common values in common columns or have no
matching values (outer join)
SQL Join Operators
SQL Join Operators
Join operations can be classified as inner joins and outer joins.
The inner join is the traditional join in which only rows that
meet a given criterion are selected. An outer join returns not
only the matching rows but the rows with unmatched
attribute values for one table or both tables to be joined.
Join Classification Join Type SQL Syntax Example Description

CROSS CROSS SELECT * Returns the Cartesian product of T1 and T2 (old style)
JOIN FROM T1, T2
SELECT * Returns the Cartesian product of T1 and T2
FROM T1 CROSS JOIN T2
INNER Old-style JOIN SELECT * Returns only the rows that meet the join condition in
FROM T1, T2 the WHERE clause (old style); only rows with matching
WHERE T1.C1 = T2.C1 values are selected
NATURAL JOIN SELECT * Returns only the rows with matching values in the
FROM T1 NATURAL JOIN T2 matching columns; the matching columns must have
the same names and similar data types
JOIN SELECT * Returns only the rows with matching values in the
USING FROM T1 JOIN T2 USING (C1) columns indicated in the USING clause
JOIN SELECT * Returns only the rows that meet the join condition
ON FROM T1 JOIN T2 ON T1.C1=T2.C1 indicated in the ON
OUTER LEFT SELECT * Returns rows with matching values and includes all
JOIN FROM T1 LEFT OUTER JOIN T2 rows from the left table (T1) with unmatched values
ON T1.C1=T2.C1
RIGHT SELECT * Returns rows with matching values and includes all
JOIN FROM T1 LEFT OUTER JOIN T2 rows from the right table (T2) with unmatched values
ON T1.C1=T2.C1
FULL SELECT * Returns rows with matching values and includes all
JOIN FROM T1 FULL OUTER JOIN T2 rows from both tables (T1 and T2) with unmatched
ON T1.C1=T2.C1 values
Cross join
A CROSS join performs a relational product of two tables.
The CROSS join syntax is:

SELECT column_list FROM table1 CROSS JOIN


table2;
Natural join
NATURAL join returns all rows with matching values in the
matching columns and eliminates duplicate columns.
NATURAL join syntax is:

SELECT column_list FROM table1 NATURAL JOIN


table2;
Join Using Clause
The query returns only when the rows with matching values in
the column indicated in the USING clause – and that column
must exists in both tables. The syntax is:

SELECT column_list FROM table1 JOIN table2


USING(common-column);
Join On Clause
The query will return only the rows that meet the indicated
join condition. The join condition will typically include an
equality comparison expression of two columns.

SELECT column_list FROM table1 JOIN table2


ON join-condition;
Outer Joins
An OUTER join returns not only the rows matching the join
condition, it return the rows with unmatched values. The
ANSI standard defines three types of OUTER joins: left, right,
and full.

SELECT column_list FROM table1 LEFT JOIN


table2 ON join-condition;
Outer Joins
An OUTER join returns not only the rows matching the join
condition, it return the rows with unmatched values. The
ANSI standard defines three types of OUTER joins: left, right,
and full. The LEFT OUTER join returns not only the rows
matching the join condition, it returns the rows in the left with
unmatched values in the right table. The syntax is:

SELECT column_list FROM table1 LEFT JOIN


table2 ON join-condition;
Outer Joins
The LEFT OUTER join returns not only the rows matching the
join condition, it returns the rows in the left with unmatched
values in the left table. The syntax is:

SELECT column_list FROM table1 RIGHT JOIN


table2 ON join-condition;
Outer Joins
The FULL OUTER join returns not only the rows matching the
join condition, it returns the rows in the left with unmatched
values in the table on either side. The syntax is:

SELECT column_list FROM table1 FULL JOIN


table2 ON join-condition;
Subqueries
Subqueries have the following characteristics:
• A subquery is a query (SELECT statement) inside a query.
• A subquery is normally expressed inside parentheses.
• The first query in the SQL statement is known as the outer
query.
• The query inside the SQL statement is known as the inner
query.
Subqueries
Subqueries have the following characteristics:
• The inner query is executed first.
• The output of an inner query is used as the input for the outer
query.
• The entire SQL statement is sometimes referred to as a nested
query.
SELECT Subquery Examples Explanation
INSERT INTO PRODUCT Inserts all rows from Table P into the PRODUCT table. Both
SELECT * FROM P; tables must have the same attributes. The subquery returns all
rows from Table P.
UPDATE PRODUCT Updates the product price to the average product price, but only
SET P_PRICE=(SELECT AVG(P_PRICE) for products provided by vendors who have an area code equal
FROM PRODUCT) to 615. The first subquery returns the average price; the second
WHERE V_CODE IN (SELECT V_CODE subquery returns the list of vendors with an area code equal to
FROM VENDOR 615.
WHERE V_AREACODE=‘615’)
DELETE FROM PRODUCT Deletes the PRODUCT table rows provided by vendors with an
WHERE V_CODE IN (SELECT V_CODE area code equal to 615. The subquery returns the list of vendor
FROM VENDOR codes with an area code equal to 615.
WHERE V_AREACODE=‘615’)
Subqueries
The subquery can return the following:
• One single value (one column and one row). This query is used
anywhere a single value is expected, as in the right side of a
comparison expression.
• A list of values (one column and multiple rows). This type of
subquery is used anywhere a list of values is expected, such as
IN clause.
• A virtual table (multicolumn, multirow set of values). This type
of subquery can be used anywhere a table is expected such as
when using the FROM clause.
Where Subqueries
The common type of subquery uses an inner SELECT subquery on
the right side of WHERE comparison expression.

Subqueries can also be used in combination with joins.


In Subqueries
When comparing a single attribute to a list of values, use the IN
operator.
Having Subqueries
The HAVING clause is used to restrict the output of a GROUP BY
query by applying conditional criteria to the grouped rows.
SQL Functions
Date and Time Functions
Function Explanation
Year Returns a four-digit year. Syntax: YEAR(date_value)
Month Returns a two-digit month code. Syntax: MONTH(date_value)
Day Returns the number of the day. Syntax: DAY(date_value)
DATE()/NOW() Returns the today’s date/current date.
DATEADD Adds a number of selected time periods to a date. Syntax:
DATEADD(datepart, number, date)
DATEDIFF Subtracts two dates. Syntax: DATEDIFF(datepart, startdate,
enddate)
SQL Functions
Numeric Functions
Function Explanation
ABS Returns the absolute value of a number. Syntax:
ABS(numeric_number)
ROUND Rounds a value to a specified precision (number of digits).
Syntax: ROUND(numeric_value,p) p=precision
CEIL/CEILING/FLOOR Returns the smallest integer greater than or equal to a number
or returns the largest integer equal to or less than a number,
respectively. Syntax:
CEILING(numeric_value)
FLOOR(numeric_value)
SQL Functions
String Functions
Function Explanation
Concatenation Concatenates data from two different character columns and
returns a single column. Syntax: strg_value || strg_value
UPPER and LOWER Returns a string in all capital or all lowercase letters. Syntax:
UPPER(strg_value)
LOWER(strg_value)
SUBSTRING Returns a substring or part of a given parameter. Syntax:
SUBSTR(strg_value,p,l)
p=start position, l=length of characters
LENGTH Returns the number of characters in a string value. Syntax:
LEN(strg_value)
CASE Compares an attribute or expression with a series of values and
returns an associated value or default value if no match is found.
CASE WHEN condition THEN value1 ELSE value2 END
Stored Procedure
A stored procedure is a segment of declarative SQL code which is
stored in the database catalog and can be invoked later by a
program, a trigger or even a stored procedure.

Stored procedure is reusable and transparent to any application


which wants to use it.
Stored Procedure
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
Stored Procedure
DELIMITER // - is used to change the standard delimeter
(semicolon) to another. Hence, DELIMITER ; changes the
delimiter back to the standard one.
CREATE PROCEDURE // - is used to create a new stored
procedure. The name of the new stored procedure comes after
the statement.
BEGIN and END – the body part of the stored procedure
where declarative SQL and being coded.
Stored Procedure
In order to invoke a stored procedure the following command is
used:

CALL stored_procedure_name();
Stored Procedure
Declaring Variables
Variable are used in stored procedure to store immediate result.
The following syntax is used:

DECLARE variable_name datatype(size) DEFAULT


defaut_value;

Data type can be any primitive type which SQL supports such as
INT, VARCHAR, and DATETIME, along with the data type is the size
of the variable.
Stored Procedure
To assign other value to a variable use SET statement, such as:

DECLARE total_count INT DEFAULT 0


SET total_count = 10;

Beside SET statement, a SELECT..INTO can be used to assign a


query result to a variable.

DECLARE total_products INT DEFAULT 0


SELECT COUNT(*) INTO total_products FROM
products
Stored Procedure
A variable has its own scope. If a variable was declared inside a
stored procedure, it will be out of scope when the END of stored
procedure reached.
Stored Procedure
Stored Procedure Parameters
Parameters make stored procedure more flexible and useful. A
parameter has three modes IN, OUT, and INOUT. IN is the default
mode. It indicates that a parameter can be passed into stored
procedure but any modification inside stored procedure does not
change the parameter. OUT mode indicates that stored
procedure can change the parameter and pass back to the calling
program. INOUT pass parameter into stored procedure and get it
back with the new value from calling program.
Stored Procedure
The syntax of defining a parameter in stored procedure is as
follows:

MODE param_name param_type(param_size)


Stored Procedure
DELIMITER //
CREATE PROCEDURE GetOfficeByCountry(IN
countryName VARCHAR(255))
BEGIN
SELECT city, phone
FROM offices
WHERE country = countryName;
END //
DELIMITER ;
Stored Procedure
Conditional Control in Stored Procedure
Conditional control enables user to execute the code based on
the value of an expression or a combination of expression using
logical operators. MySQL supports two conditional control
statement such as IF and CASE.
Stored Procedure
IF Statement syntax is as follows:

IF expression THEN commands


END IF;
IF expression THEN commands
ELSE commands
END IF;
IF expression THEN commands
ELSEIF expression THEN commands
ELSE commands
END IF;
Stored Procedure
CASE Statement syntax is as follows:

CASE
WHEN expression THEN commands

WHEN expression THEN commands
ELSE commands
END CASE;
Stored Procedure
Loop in Stored Procedure
Allows user to process commands iteratively.

The syntax for WHILE Loop is as follows:

WHILE expression DO
Statements
END WHILE
Stored Procedure
The syntax for REPEAT Loop is as follows:

REPEAT
Statements;
UNTIL expression
END REPEAT

You might also like