0% found this document useful (0 votes)
12 views6 pages

Database Design Week 5

The first thing that comes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Database Design Week 5

The first thing that comes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

5.

0 STRUCTURED QUERY LANGUAGE (SQL) FUNDAMENTALS


For some time now, the Structured Query Language (SQL) has been in use and has
gradually evolved to become the standard relational database language.

Structural Components of SQL


A typical SQL consists of several parts which include:
Data definition language (DDL) - provides commands to define relation schemes, delete
relations, create indices, modify schemes.
Interactive data manipulation language (DML) - a query language based on both relational
algebra and tuple relational calculus, plus commands to insert, delete and modify tuples.
Embedded data manipulation language - for use within programming languages like C, PL/1,
Cobol, Pascal, etc.
View Definition - commands for defining views
Authorization – this specifies the access rights to relations and views.
Integrity - a limited form of integrity checking.
Transaction control - specifying beginning and end of transactions.

5.1 DATABASE QUERIES


A query is a view of your data showing information from one or more tables. For instance,
using the sample database we used in the previous Unit 7 when describing normalization, you
could query the Students database asking "Show me the first and last names of the students who
take both history and geography and have Alice Hernandez as their advisor" Such a query displays
information from the Students table (firstname, lastname), Courses table (course description) and
Advisor table (advisor name), using the keys (Student_ID, course ID, advisor ID) to find matching
information. The query below brings together the fields from the agent info tables that are needed
for mailing labels.

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 1


Figure 1: Query Design View Window
The SQL code for this query looks a lot different from the design view above. No wonder
a drag-and-drop method was created.

Figure 2: Query Code View Window.

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 2


5.2 QUERY LANGUAGE FEATURES
A query language normally comprises a Data Descriptive Language (DDL) and a Data
Manipulative Language (DML). There are two types of queries:
i. Query By Example (QBE) and
ii. Structural Query Language (SQL).
Queries languages have two basic modes of operation:
Terminal Monitor Mode: The user is able to use query language at a terminal; in the same way
a command language interpreter is used. Ad-hoc queries are formulated to obtain data form
databases.
Embedded Query Language: The query language statements are included within the code of
programs written in some other programming language, e.g. C or Oracle, and effectively because
part of the program, hence the term “embedded query language”.

5.3 STRUCTURED QUERY LANGUAGE (SQL)


Structured Query Language (pronounced sequel in the US; ess-queue-ell elsewhere). A
computer language designed to organise and simplify the process of getting information out of a
database in a usable form, and also used to reorganise data within databases. SQL is most often
used on larger databases on minicomputers, mainframes and corporate servers. This is an
international standard for database query languages and has been adopted by many computer
manufacturers and database product suppliers, e.g. IBM, INGRES, ORACLE, SYBASE, and
INFORMIX.

5.3.1 Tables
Data in a relational database are stored in tables sometimes called “relations”. A row in a
table corresponds to a single record in a file. Another name for row in a table is “tuple”.
Table 1: A table called “STUDENT”
Student_ID Name Address date of birth Boarding Fees
FPA/SC/98/01 Binta Abaka K9 Abidjan Str. 22 Dec 1966 800
FPA /SC/98/02 Dulen Kpenetese 63 Aba Rd P/H 15 May 1975 1000
FPA /SC/98/03 Ugoma Ifeanyi 3 Police Station Rd 22 Jan 1970 300
FPA /SC/98/04 Ademola Rufai 61b Bishop Oluwole Str 22 Jun 1965 450

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 3


Each row should be uniquely identifiable by a suitable key. The key may either be a simple
key consisting of the value in one column or a composite key consisting of multiple columns. The
example of the table above of the “Student_ID” field is a simple key.
To create a table similar like the one above using SQL statement we use the following syntax;
Create table student
(
Student_ID char (13) not null unique,
Name char (20) not null,
Address char (40) not null,
date of birth date,
Boarding Fees smallint (13)
)

The layout above has been chosen for readability, with the definition of each column in the
table set out on a separate line in the order in which it is to appear in the table.
The explanations of the above statements are as follows;
“Student_ID char (13) not null unique”, states that the first column is to be called “Student_ID”,
it can be up to 13 characters long, it must not be empty “null” and its value must be unique.
“date of birth”, states that the fourth column is to be called “date of birth”, and its value must be a
date.

5.3.2 Select Statements


Every select statement yields a table of values as output and Sometimes there’s only one
row in the table, depending on how you use the command.
select columns and/or expressions
from tables
where conditions on the rows
group by group rows together
having conditions on the groups
order by order the rows
into temp save results of query in a temporary table

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 4


This is used to retrieve values form a table. The following statement retrieves rows from
the table “Student” displaying just the three columns “Student_ID”, “Name” and “Boarding Fees”.
The expression after the word “where” defines what values in a row must have for the row to be
selected.
Select
Student_ID, Name, Boarding Fees
From
Student
Where
Boarding Fees > 400
And
Boarding Fees <1000

Another short syntax is:


Select * from Table where Boarding Fees > 400 and Boarding Fees <1000
This “*” implies ‘everything’ or ‘all records’.
In the above statements the values are Boarding Fees between 400 and
1000 will produces the table below; Table 9.2 Result of the SELECT
Statement

Student_ID Name Boarding Fees


FPA/SC/98/01 Binta Abaka 800
FPA/SC/98/04 Ademola Rufai 450

5.3.3 Insert Statement


Insert statement is used to add a record into a table, in performing the operation care is
taking not to insert a null value. It has the following syntax;
Insert into Table (Field 1, Field 2, …) Values (‘Value 1’, ‘Value 2’, …)
For example:
Insert into Student (Student_ID, Name, Boarding Fees) Value (‘FPA/SC/98/01’, ‘Binta
Abaka’, ‘800’)

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 5


5.3.4 Update Statement
The update statement is used to modify or change values in one or more rows within a
table. It has the following syntax;
Update Table Set computation where condition

Update Student
Set Boarding Fess = Boarding Fess + 250
where Student_ID = ‘FPA/SC/98/01’
5.3.5 Delete Statement
The delete statement is used to remove one or more rows from a table. It has the following
syntax;
Delete From Table Where condition

Delete from Student where Student_ID = ‘FPA/SC/98/01’


The table means the name of the table in the database such as “Student”, while the condition
states the parameters to consider during execution of the command such as “Student_ID =
‘FPA/SC/98/01’”.

DATABASE DESIGN (COM 312) FELE, TAIWO © 2016 6

You might also like