Our world depends on data in order to thrive. There are many different methods for storing data but the idea of relational database technology has proved the most advantageous. At the heart of all major relational database approach is the SQL, standing for Structured Query Language. SQL is based on set theory or relational principles.
1. 1
Database Systems
Introduction to the Structured Query
Language
Harmony Kwawu
hkwawu@aol.com
Course Outline
Introduction
Use of database applications
Database Management System
Relational operators
Purpose of relational database query
SQL Commands:
Data manipulation command
Data definition command
SQL Joins
Summary
2. 3
Introduction
Our world depends on data and increasingly so, in order to thrive.
There are many different methods used for storing data but the idea
of relational database technology has proved the most advantageous
A key benefit of relational database approach is the capacity to allow
users to retrieve data from or interact with database in an intuitive
and variant ways.
At the heart of all major relational database approach is the SQL,
standing for Structured Query Language. SQL is based on set theory
or relational principles.
Relational operators help developers write complex procedures to
enforce business rules and enhance database interactivity
There are six different types of relational operators used in major
DBMS as illustrated in this brief introduction to the Structured Query
Language
But first ….
Uses of Database Applications
4. Relational Database Systems
There are different ways to store data. Data can be stored in
files, documents, image and sound forms etc
But relational database approach is the most versatile and
convenient
Relational database approach is one in which data is
organised into sets or tables
Where each table is linked to other tables to form a relational
model.
STUDENT
Student_ID (PK)
StudentName
StudentAddress
Email
Telephone
Comment
Course_Code(FK)
ModuleModule
Module_Id(PK)
Module Name
Aim
Outcome
Course_Code (FK)
Course_Code(PK)
CourseName
CourseDescription
CourseCourse
A simple illustrated example of relational data model.
5. Relations
Tables in a relational database are technically
referred to as entities or relations.
Key Components
The main components of relational database systems are:
The database or data repository consisting of:
Individual tables linked to form a logical model
Attributes of stored data
Specific values or records
Database Manager (BM)
The command base for retrieving and managing the
database (SQL)
Applications for interacting with the content of the data
base
User interface
6. Major benefits of Relational database systems
Relational database approach also
allow data from different parts of
an organisation to be stored in
one place
This approach makes it easy to
protect and control access to the
database
Also, storing data in a logically
related tables can be accessed by
different applications and users
according to their needs
SQL
What is Database Management System
7. 13
Database Management System (DBMS)
A database management system (DBMS) is a large piece of
software that is used in defining, creating, updating and
deleting a database records.
A database management system is also known as database
manager
It supports the operation of database applications within an
integrated, controlled and accessible framework.
Examples of DBMS are:
MS Access
Oracle PL/SQL
MySQL
13
A simple Infographics of Database
Management Systems
8. Data Base Query
Relational Database Operators
Operators are generally used in programming and application
development to build expressions.
There are generally four categories of operators, namely:
Arithmetic operators
Relational or Boolean Operators
Logical Operator
Assignment Operator
Relational operators are the most common type of operators
used in relational database
They are used to help write complex procedures to enforce
business rules and construct query statements
9. Data Base Query
Database Relational Operators
Common Database Relational Operators
OperatorOperatorOperatorOperator MeaningMeaningMeaningMeaning ExampleExampleExampleExample
= Equal to Gives exact value in a database
<> Not equal to List values other than the one
specified
> Greater than List values that are greater
than the one specified
< Less than Shows values that are less
than the one stated
<= Less than or equal to Shows values stated and all
values less than the one stated
>= Greater than or equal to Shows values specified and all
values greater than the one
stated
10. Other Database Relational Operators
Cont..
OperatorOperatorOperatorOperator MeaningMeaningMeaningMeaning ExampleExampleExampleExample
Between ......> And <..... Retrieve all values within the
given range excluding the two
values stated
Between Between Retrieve all values within the
given range including the two
values stated
OR ......OR ...... List one set of value or
another
The SQL Command
11. 21
The SQL command
We shall examine in future posts how the above operators are
used to programme relational database,
But first let’s discuss other aspect of database query-the SQL
command.
As noted above, SQL allow database users to retrieve data,
update existing records, delete records, create index, create
new tables and delete tables
SQL statements can be interactive and embedded in third
party programming languages, such as ASP.NET or PHP
And SQL statement may be simple or complex, more on this in
future
22
SQL Command
In MS Access database management systems for example,
queries are created using what is known as Access Query
Design view.
The queries created in design view can be viewed and edited
to reflect changes
It’s equally possible to write new query statements directly in
SQL view
Note that SQL Stand for structured query language and that
SQL is the defector programming language for relational
databases.
12. 23
Illustrated Example: Query Design View
A very simple query
A local bank require the design of a system to store record of its clients
and the loan they borrow. The system should be able to store details of
the clients, details of loan and loan types and the branch for Each
customer. A preliminary entity relation model has been produced by one
of your database development team member as shown below: use the
ERD to produce the following queries
Bank customer loan processing system challenge
13. 25
Illustrated Example: Query Design View
Bank Database
Model
A slightly complex query
SQL View in MS Access
SQL Query statement
14. SQL Command Types
28
SQL Command Types
There are many categories of structured Query Language
statements.
For our purpose, we shall focus on two key categories as
follows:
The data definition language (DDL) commands, and
Data manipulation language (DML) commands
15. DDL and DML
Data Definition Language (DDL)
Are used to define and model the structure of data
e.g. Create the database, Alter (make changes to the
database) and Drop (delete database object)
Data Manipulation Language (DML)
Used mainly for interacting and manipulating stored data
e.g. Select, Update, Delete and Insert new records into
database
Data Definition Language (DDL)
Create statement
Alter statement, and
Drop statement
16. 31
Example 1: SQL Create Table Command
Create Table Staff(
Staff_Id Number (10) NOT Null Staff_FirstName Varchar(30)
NOT Null,
Staff_SurName Varchar(30) NOT Null,
Staff_Position Varchar(10) NOT Null,
Staff_EmailVarchar(30) NOT Null,
Staff_Telephone Varchar(30) NOT Null,
Room_Number Varchar(30) NOT Null,
Staff_Speciality Varchar(30) NOT Null,
Constraints Staff_PK Primary Key (Staff_id));
The Result
See if you can guess the result by sketching it
17. 33
Example2: SQL Create Table Cont..
Create Table Course (
Course_Code Number (10) NOT Null,
Course_Name Varchar(30) NOT Null,
Course_Aim Varchar(30) NOT Null,
Course_Description Varchar(100) NOT Null,
Staff_Id Number (30),
Constraints Course_PK Primary Key (Course_Code),
CONSTRAINTS Staff_FK FOREIGN (Staff_Id),
REFERENCES Staff(Staff));
The Result
Again, see if you can guess the result by
sketching it
18. Other important DDL Statements
Other important DDL Statements
Alter statement and Drop statement
Find out more from the link below:
http://www.w3schools.com/sql/sql_alter.asp
19. Data Manipulating Language
Data Manipulating Language
Select statement is perhaps the most important and
frequently used data manipulation statement.
Other equally important commands are:
Update command
Delete command
Insert command
The main purpose of DML, is to allow database users to
retrieve (select) and or manipulate stored data
20. Structure of select statement
Below are what could be descried as the building blocks of
simple select command:
SELECT
FROM
Where
Or
Quiz
Briefly describe what Each of the following select statement
keywords means in practice?
SELECT
FROM
Where
Or
21. Example of select statement:
List the names and email address of all staff
in room 123
SELECT (Staff_ID, First Name, Surname,
Email, Telephone, Room
FROM Staff,
WHERE ((Room)=123));
The Result
See if you can guess the result by sketching it
22. 43
Data Manipulating Language
Example of DML select statement-2:
List Name and detail of students
SELECT Student_ID, First_Name, Surname,
Email, Mobile, Home_Tel
FROM Student;
The Result
See if you can guess the result by sketching it
23. 45
DML SQL Command
List names of students and their course
SELECT Student_ID, First_Name, Surname, D_O_B,
Email,Course_Name, Description
FROM Course INNER JOIN Student ON
tblCourse.Course_ID=TblStudent.Course_ID;
The Result
Congratulation if you were able to guess
the result correctly by sketching it
24. 47
Other Types of DML Query
Update Query in MS Access:
Example 1:
UPDATE TblStudent SET TblStudent.First_Name = "Null",
TblStudent.Surname = “Null”, TblStudent.D_O_B = “Null”;
Example 2:
UPDATE TblJob_Type SET TblJob_Type.Charges = “£90";
Log on to the URL below to find out more:
http://www.w3schools.com/sql/sql_update.asp
25. 49
Delete Query in MS Access
Delete all record from the Module Table
DELETE *
FROM TblModules;
50
Insert data into table
INSERT INTO Student
(Student_Name,Student_Address,Email)
Values (Joe Williams, London Road, JW@Yahoo.com);
INSERT INTO Student
(Student_Name,Student_Address,Email)
Values (Henry Champion, West Kent Road,
HC@Yahoo.com);
INSERT INTO Student
(Student_Name,Student_Address,Email) Values (Mark
Williams, China Town, MW@Yahoo.com);
26. 51
Project Report 1
52
SQL Report1
SELECT Projects_Tbl.Project_Id,Projects_Tbl.Project_Name,
Employees_Tbl.Employee_No,Employees_Tbl.Employee_Na
me, Job_Type_Tbl.Job_Class, Job_Type_Tbl.Charges,
Employee_Project_Tbl.Hours
FROM (Projects_Tbl INNER JOIN (Employees_Tbl INNER
JOIN Employee_Project_Tbl ON
Employees_Tbl.Employee_No=Employee_Project_Tbl.Emply
ee_Number) ON
Projects_Tbl.Project_Id=Employee_Project_Tbl.Project_id)
INNER JOIN Job_Type_Tbl ON
Projects_Tbl.Project_Id=Job_Type_Tbl.Project_Id;
27. Output report
Try and sketch what you think the output is likely
to be
54
Project Report 2
Fig: Select Query to generate the report below
Fig: Result of the query
28. 55
SQL Report 2
SELECT Projects_Tbl.Project_Id,
Projects_Tbl.Project_Name, Employees_Tbl.Employee_No,
Employees_Tbl.Employee_Name, Job_Type_Tbl.Job_Class,
Job_Type_Tbl.Charges, Employee_Project_Tbl.Hours,
[Charges]*[hours] AS Total
FROM (Projects_Tbl INNER JOIN (Employees_Tbl INNER
JOIN Employee_Project_Tbl ON
Employees_Tbl.Employee_No =
Employee_Project_Tbl.Emplyee_Number) ON
Projects_Tbl.Project_Id = Employee_Project_Tbl.Project_id)
INNER JOIN Job_Type_Tbl ON Projects_Tbl.Project_Id =
Job_Type_Tbl.Project_Id;
56
Write SQL Query to implement the following two Tables. You
may use MS Access Query design to help you complete this
task. The tables are based on the Project/Employee database
Table: 1
Table: 2
Try this revision task
29. 57
Joins Statements
SQL join commands are useful when querying data from
multiple tables that are linked together
58
Joins Statements Cont’..
Types of joins:
INNER JOIN (JOIN): Return rows when there is at least one match
in both tables
LEFT JOIN: Return all rows from the left table, even if there are no
matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are
no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
See the sites below for further details on joins:
http://infogoal.com/sql/sql-outer-join.htm
http://www.w3schools.com/sql/sql_join_inner.asp
30. 59
Summary
In this post, we’ve learnt that SQL is a command based language
which allow database developers to create and manipulate stored
record
SQL can be used to create the database, make changes to the
database and delete database objects
SQL also help users to interact and manipulate stored data.
Users can make a request (declare) for the data they require without
the need to specify how the request should be processed
SQL is a powerful language to learn as it is the most popular
programming language for relational database applications
Next Post
Advance SQL
31. Additional SQL online tutorials you!
Introduction to SQL:
http://www.w3schools.com/sql/sql_intro.asp
End
Feel free to get in touch if you
would like to share your
thought on this topic. You can
contact me by email at:
hkwawu@aol.com