Sunteți pe pagina 1din 10

Topic1X Introduction

to SQL

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Explain the meaning and application of SQL and its importance.
2. Present the basic structure of SQL and transform-oriented language;
and
3. List and answer from queries before building an SQL.

X INTRODUCTION
In the previous course, you learnt that data model relations form the basis of
database technology. Data technology comprises a strong basic structure based on
the relation theory in mathematics. Currently, a simple and user-friendly query
language in commercial applications is high in demand.

Structured Query Language or SQL is a language derived from the


development of data model relations. It has become the standard language for
database relationship. This standard was defined by the American National
Standards Institute (ANSI) in 1986 and certified by the International Standards
Organization (ISO) in 1987. Currently, there are more than 100 database
management systems supporting the SQL and it is applicable in various computer
environments, from personal computers to mainframes.

This topic will explain what SQL is and the importance of learning the language
in building a current database application.

ACTIVITY 1.1
2 X TOPIC 1 INTRODUCTION TO SQL

1.1 BACKGROUND OF SQL

SQL is a query language for database. You have learnt that there are many
software applied in a database such as Access, Oracle and others. Is SQL suitable
for all types of database software?

Why do you need a query language? In reality, a structured and systematic query
system is required to minimise errors in interpreting the required space. We also
require a standard system which is applicable to various peripherals and database
systems from different suppliers. This enables users and developers to learn
techniques conforming to most database systems.

Ideally, a database language enables users to:


(i) Create the database and relation structure;
(ii) Perform insertion, modification and deletion of data from the relationship;
and
(iii) Perform both simple and complex queries.

SQL was introduced to fulfill the above requirements and it is the most widely
used database language.

In addition, a database language must perform these tasks with minimal user
effort. Its command structure and syntax must be relatively easy to learn and most
importantly the language must be portable, that is, it must conform to some
recognised standard so that the user can use the same command structure and
syntax when moving from one DBMS to another. SQL is intended to satisfy these
requirements.

As a language, SQL has two major components:

1. A Data Definition Language (DDL) for defining the database structure and
controlling access to data; and
2. A Data Manipulation Language (DML) for retrieving and updating data.

SQL is easy to learn because:

(i) It is a non-procedural language; users only specify what information is


required and not the access methods to data;
TOPIC 1 INTRODUCTION TO SQL W 3

(ii) It is essentially a free-format, meaning the format is easy to understand;

(iii) Its application is in the standard English language;


(iv) It is not dependent on capital and small alphabets but it must be typed as
per the content of the database; and

(v) It can be used by a range of users such as database administrators (DBA),


programmers and end users.

Information on SQL may be obtained from many books. Among those that can
enrich your knowledge is Database System; Principles, Design and
Implementation written by Catherine Ricardo, published by New York McMillan,
1990.

Before you proceed, attempt the following questions based on the basic concepts
you have just learnt.

SELF-CHECK 1.1

1. What is SQL and state its importance?

2. What are the two main components of SQL and state the function
of each component?

1.2 HISTORY OF SQL

SQL is a structured query language used to develop a database and to


retrieve important data in a database. How has SQL developed since its
inception?

The history of the relational model started with the publication of the seminal
paper by E.F. Codd in 1970 while he was with IBM. At that point in time,
"Structured English Query Language (SEQUEL) was introduced as an upgraded
4 X TOPIC 1 INTRODUCTION TO SQL

version of SEQUEL. A revised version, SEQUEL/2, was defined in 1976, but the
name was subsequently changed to SQL for legal reasons as SEQUEL was used

by other parties. SQL was then introduced in a DBMS command by the company
ORACLE.

Each implementation of SQL by particular companies is known as a dialect.


Currently, there are several SQL dialect languages in the market, each with
additional or variant versions of the standard required. In 1986, a standard
version of SQL was used by ANSI. The SQL version, according to this standard,
was known as SQL-86. An additional feature was introduced in the 1989
standards. The most current or updated version of DBMS supports SQL-89. A
revision on SQL standards was built in 1992 and it resulted in the birth of SQL-92
or SQL2. Recent development has shown that SQL 3 is underway.

SQL is the first standard database language to gain wide acceptance. It is an


important language in most commercial DBMS in various companies and
organisations as a standard global reference. The SQL ISO standard does not use
the formal terms of relations, attributes and tuples; instead it uses the terms table,
column and rows. It should also be noted that SQL does not adhere strictly to the
definition of the relational model and furthermore, SQL allows for repeated rows.
SQL permits users to order the rows of a table. It also allows the null value to state
non or unknown values.

Visit http://www.bayougroup.comltocsql.htm for further information on


SQL.

1.3 BASIC STRUCTURE OF SQL STATEMENTS

You know that SQL is a structured language; can you think of why it
is referred to as structured?

Each query in the SQL language consists of three basic clauses in the following
format
TOPIC 1 INTRODUCTION TO SQL W 5

SELECT (DISTINCT | ALL) column_name


FROM table name
WHERE condition

We can make several observations as follows:

1. SQL statements are words defined by SQL and words defined by users.
Words defined by SQL are known as reserved words with unchangeable
meaning. Words defined by users are normally for tables, column, views
and situations. All these must adhere to the fixed syntax.

2. Although SQL statements are case insensitive, to simplify your


understanding, we shall adhere to the following indentation and lineation.

(i) Upper case letters are used to represent reserved words and lower case
letters are used to represent users-defined words.

(ii) A vertical bar ( | ) indicates a choice, for example a choice between x, y


and z is written as X | y | and z.

(iii) Square brackets [ ] indicate optional elements; for example [ X ].

(iv) Curly braces { } indicate a required element, for example { X } means X


is required.

(v) Literals must be typed as stores in the database. Literals are constants
in SQL statements. All literals must be enclosed in single quotes, for
example "Database", unless it is numeric.

Although SQL is a free format for standardising and easy reading we shall follow
the following formats:

(a) Each clause in a statement should begin with a new line.


(b) The beginning of each clause should be lined up with the beginning of other
clauses.

Let us look at the syntax and meaning of each SQL clause.

(a) SELECT column_name


The SELECT clause is followed by one or more column names required in
an output. Column name refers to tables defined in FROM clause. The
comma (,) is located between column names if more than one column is
listed.
6 X TOPIC 1 INTRODUCTION TO SQL

(b) FROM table name


The FROM clause is followed by one or more table name. This table will be
scanned by the system. The comma (,) is located between table names if
more than one table is listed.

(c) WHERE condition


The WHERE commands is followed by situation K, which is a BOOLEAN
phrase. K is a combination of sequences connected by AND, OR and NOT.
The sequence is as follows:

X operator Y

where X and Y represent the column name, constants or arithmetic statement. An


operator is a comparison element in SQL statement:

Operator Meaning
= equals to
< less than
> more than
<= less or equals to
>= more or equals to
<> not equal to

In short, K states the condition of how a column name is in SELECT command.


The command WHERE is a choice and if it is not stated, K will always be true.

(d) DISTINCT I ALL


Distinct or ALL are words defined by SQL according to the SELECT clause.
The purpose of DISTINCT is to delete repeatable rows from the table
produced. Instead, ALL defines all repeated rows and is maintained. ALL is
a default value, DISTINCT is a choice and if it is left, SQL will allow
repeatable rows in tabling output.

The following exercise will assist you in better understanding the topic you have
just learnt.
TOPIC 1 INTRODUCTION TO SQL W 7

SELF-CHECK 1.2

1. What is meant by these statements?


(a) SQL language dialect; and
(b) SQL reserved language.

2. Write an example of an SQL command using three main clauses.

Once you have learnt the basic SQL structure, let us rest our mind before
proceeding to the next section.

1.4 FOUR QUERIES FOR SQL DEVELOPMENT


Before we try accessing a relations database, ask yourself four more questions:

1. What is the output required?


2. What have you known (what are the restrictions involved)?
3. What are the tables involved?
4. How are tables linked?

You must remember these four questions without thinking of the SQL structure.
In creating an SQL, develop a habit of answering questions. You may be able to
answer simple questions automatically but for complex questions, you may need
to answer the question part by part. Proceed to the other questions until you are
able to comprehend each question as a whole.

1.4.1 What is the Required Output?


Normally, this question is answered by selecting columns from several tables
stored in the database. You need to identify the column names to answer this
question. The most difficult part is to see the entire table list and identify the
required column. It will be even more difficult if the database has hundreds of
tables and thousands of columns to choose from.
8 X TOPIC 1 INTRODUCTION TO SQL

You can also command a system to calculate arithmetic equations such as average
amount, plus, minus, multiply and divide.

1.4.2 What Have You Learnt?


In many instances, you are required to limit your search based on a particular
character. For example, you may be interested in sales from several departments
or sales derived from one department only. A search must be changed to a
standard BOOLEAN notation (AND or OR statement). The most important aspect
in this move is to rewrite all situations/occurrences that can assist you in
understanding the purposes of statement required.

1.4.3 What are the Tables Involved?


With minimal number of tables, quarters are easy to handle but with hundreds of
tables, you may require more time in determining which table is required. With
the help of a data dictionary, you should be able to create queries. It is advised
that tables be given names that correspond to the content and purpose of the
development.

1.4.4 How are Tables Linked?


This question is related to the issue of modeling data that is most important in
relations database. Each table is linked by the data in the same column. For
example, the table STUDENT is linked to the column MatricNum. The data in the
table COURSE is also linked to the column name MatricNum. In many cases, a
column linked to the table has the same name (for example, MatricNum).
However, in others situation, not all columns have same names. For example, the
table GRADE may have the column name StudentNum in order to refer to the
column MatricNum in the table STUDENT.
TOPIC 1 INTRODUCTION TO SQL W 9

ACTIVITY 1.1

The following queries are related to a university environment. Most of


the queries are columns as they involve only one table. These are
common queries by an administrator or dean of a particular faculty. Give
your opinion on four of the queries you below.

List all A grade students in the TM30202 course.


State the percentage of students according to race in each academic
year.
State the percentage of male students compared to the female
students in one academic year.
List all students with CGPA 2.0 and below.
List all students graduated with first class honours.

SELF-CHECK 1.3

If you are a manager of a computer peripheral sales company, what


are the questions that may occur to ensure the management of a
company is safeguarded? State three queries.

You have learnt what an SQL statement is and the importance of learning this
language. Two components involved in the SQL language are the Data Definition
Language and Data Manipulation Language. These languages are important in
enabling us to identify and update a database.

Before any queries in the SQL language are developed, we have to ask four (4)
important questions:
What is the output required?
What have you learnt? What are the restrictions?
What are the tables involved?
How are the tables linked?
10 X TOPIC 1 INTRODUCTION TO SQL

By answering all the questions involved, you will be able to construct the SQL
query language. It is hoped that you will be able to comprehend a higher level of
SQL language in the next topic.

Structured Query Language

S-ar putea să vă placă și