0% found this document useful (0 votes)
103 views

Structured Query Language

SQL is a standard language used to communicate with relational database management systems like MySQL. It allows users to query, insert, update and delete data. SQL commands are classified into data definition language, data manipulation language, and data control language. The data definition language is used to define database structures like tables and views. SQL supports various data types including numeric, date/time, string, and enumeration types to define columns in tables.

Uploaded by

dextermartins608
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Structured Query Language

SQL is a standard language used to communicate with relational database management systems like MySQL. It allows users to query, insert, update and delete data. SQL commands are classified into data definition language, data manipulation language, and data control language. The data definition language is used to define database structures like tables and views. SQL supports various data types including numeric, date/time, string, and enumeration types to define columns in tables.

Uploaded by

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

5.

SQL
Introduction
SQL stands for “Structured Query Language”. SQL is a relational database language. By itself,
SQL does not make a DBMS but it’s a medium which is used to communicate to the DBMS. SQL
commands consist of English-like statements which are used to query, insert, update and delete data.
English-like statements mean that SQL commands resemble English language sentences in their
construct and use, and therefore, SQL is easy to learn and understand.
SQL is referred to as nonprocedural database language because whenever we want to retrieve
data from the database, it is enough to tell SQL what data to retrieve rather than how to retrieve it. The
DBMS provides means of locating information in the database.
SQL, particularly in commercial DBMS, can be used in two distinct ways:
1. As Interactive SQL – SQL commands can be typed at the command line directly where DBMS
interprets and processes those commands immediately, and displays the results.
2. As programmatic SQL - here SQL statements are embedded in a host language such as COBOL,
JAVA, C++ and C etc. SQL needs a host language because it is not a really complete computer
programming language because it has no statements or constructs that allow branch or loop. The
host language provides necessary looping and branching structures and the user interfaces while
SQL provides the statements to communicate with the DBMS.
Features of SQL
SQL possess some of the following features;
 It is a language used to interact with the database.
 It is a data access language
 It is based on relational tuple calculus
 It is a standard relational database management language
 It is a nonprocedural or declarative language
Benefits of Standardized Relational Language
SQL language is standardized by ANSI and ISO as a relational language, hence bringing forth the
following benefits;
 Reduced training cost
 Enhanced productivity

1
 Application portability – means applications can be moved from machine to machine when each
machine uses SQL.
 Application longevity – a standard language tends to remain unchanged for a long time, hence
there will be little pressure to rewrite old applications.
 Reduces dependence on single vendor – IBM, Oracle, Sybase etc. all uses SQL.
Commands in SQL
SQL commands can be classified in to three types:
1. Data Definition Language commands (DDL) – commands used to define a database, including
creating, altering, and dropping tables, and establishing constraints.
2. Data manipulation Language commands (DML) – commands used to maintain and query a
database, including updating, inserting, modifying, and querying data.
3. Data Control Language commands (DCL) – commands used to control database including
administering privileges and saving of data. They are used to determine whether a user is
allowed to carry out a particular operation or not.
This classification can be represented diagrammatically as shown below.

CREATE
DDL
ALTER
DROP

INSERT
SELECT
SQL DML UPDATE
DELETE

GRANT
DCL REVOKE
Datatypes in SQL
In relational model, data are stored in form of tables. When a table is created, datatype for each column
in the table must be specified. These datatypes define the domain of values that each column can take.
There are a number of built-in datatypes as well as several categories of user-defined types that can be
used as datatypes in MySQL. They include:
1. Numeric data types

2
 INT – a normal-sized integer that can be signed or unsigned. If signed, the allowable
range is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to
4294967295. One can specify a width of up to 11 digits.
 TINYINT – a very small integer than can be signed or unsigned. If signed, allowable
range is -128 to 127 and if unsigned is from 0 to 255. One can specify a width of up to 4
digits.
 SMALLINT - A small integer that can be signed or unsigned. If signed, the allowable
range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You
can specify a width of up to 5 digits.
 MEDIUMINT - A medium-sized integer that can be signed or unsigned. If signed, the
allowable range is from -8388608 to 8388607. If unsigned, the allowable range is from 0
to 16777215. You can specify a width of up to 9 digits.
 BIGINT - A large integer that can be signed or unsigned. If signed, the allowable range
is from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable
range is from 0 to 18446744073709551615. You can specify a width of up to 11 digits.
 FLOAT (M, D) - A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). This is not required and will default
to 10, 2, where 2 is the number of decimals and 10 is the total number of digits (including
decimals). Decimal precision can go to 24 places for a FLOAT.
 DOUBLE (M, D) - A double precision floating-point number that cannot be unsigned.
You can define the display length (M) and the number of decimals (D). This is not
required and will default to 16, 4, where 4 is the number of decimals. Decimal precision
can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE.
 DECIMAL (M, D) - An unpacked floating-point number that cannot be unsigned. In
unpacked decimals, each decimal corresponds to one byte. Defining the display length
(M) and the number of decimals (D) is required. NUMERIC is a synonym for
DECIMAL.
2. Date and Time types
 DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For
example, December 30th, 1973 would be stored as 1973-12-30.

3
 DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format,
between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the
afternoon on December 30th, 1973 would be stored as 1973-12-30 15:30:00.
 TIMESTAMP - This looks like the previous DATETIME format, only without the
hyphens between numbers; e.g. 3:30 in the afternoon on December 30th, 1973 would be
stored as 19731230153000 (YYYYMMDDHHMMSS ).
 TIME - Stores the time in HH:MM:SS format.
 YEAR (M) - Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for
example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4,
YEAR can be 1901 to 2155. The default length is 4.
3. String Types
 CHAR (M) - A fixed-length string between 1 and 255 characters in length (for example
CHAR (5)), right-padded with spaces to the specified length when stored. Defining a
length is not required, but the default is 1.
 VARCHAR (M) - A variable-length string between 1 and 255 characters in length; for
example VARCHAR (25). You must define a length when creating a VARCHAR field.
 BLOB or TEXT - A field with a maximum length of 65535 characters. BLOBs are
"Binary Large Objects" and are used to store large amounts of binary data, such as
images or other types of files. Fields defined as TEXT also hold large amounts of data;
the difference between the two is that sorts and comparisons on stored data are case
sensitive on BLOBs and are not case sensitive in TEXT fields. You do not specify a
length with BLOB or TEXT.
 TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum length of
255 characters. You do not specify a length with TINYBLOB or TINYTEXT.
 MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximum
length of 16777215 characters. You do not specify a length with MEDIUMBLOB or
MEDIUMTEXT.
 LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximum length of
4294967295 characters. You do not specify a length with LONGBLOB or LONGTEXT.
 ENUM - An enumeration, which is a fancy term for list. When defining an ENUM, you
are creating a list of items from which the value must be selected (or it can be NULL).

4
For example, if you wanted your field to contain "A" or "B" or "C", you would define
your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever
populate that field.

DATA DEFINITION LANGUAGE


The DDL is used to define schemas, relations, and other database structures. It is also used to
update these structures as the database evolves.
There are different structures that are created by DDL. They include Tables, Views, Sequences,
Triggers, Indexes, among others including the Database itself.
1. Database – a collection of interrelated data. It is created using CREATE DATABASE statement.
2. Tables – this is a relation that stores record of related data. A base table is created with the
CREATE TABLE statement and is used to hold persistent user data.
3. Views – a view is a “Pseudo table” or a “Virtual table”. It provides an alternative way of looking
at data in one or more tables.
4. Sequences – a sequence is an integer that varies by a given constant value. It is typically used for
unique ID assignment
5. Triggers – Trigger automatically executes certain commands when given conditions are met.
6. Indexes – index is basically used for performance tuning. Indexes play a crucial role in fast data
retrieval.
Create Database Command
The CREATE DATABASE command is used to implement a database that will hold schemas of
individual relations.
Syntax of creating a database
The general syntax to create a database is given below. The key words are shown in capital letters.
CREATE DATABASE database_name;
For example;
To create a database called “my_database”, you supply the following SQL statement.
CREATE DATABASE my_database.
NB: In MySQL, ‘IF NOT EXISTS’ key words are given together with the CREATE DATABASE
command. The idea is to first check the existing database before creating new database object. Thus, the
Syntax is; CREATE DATABASE IF NOT EXISTS database_name.

5
For example; CREATE DATABASE IF NOT EXISTS my_database; this similarly
creates a database called my_database.
To see a list of the available databases in a DBMS, the SHOW DATABASES command is used.
Drop Database command
The DROP DATABASE statement is used to delete a database.
The syntax is DROP DATABASE database_name;
For example, to delete database “my_database”, the command should be
DROP DATABASE my_database;
Create Table Command
The CREATE TABLE command is used to implement the schemas of individual relations. To create a
table, the following steps are followed;
 Identify datatypes for attributes
 Identify columns that can and cannot be null
 Identify columns that must be unique
 Identify primary key –foreign key mates
 Determine default values
 Identify constraints on columns (domain specifications)
 Create table

Syntax for creating a table


The general syntax to create a table is shown below;
CREATE TABLE table_name
(column_name1 datatype (size),
column_name2 datatype (size),
column_name3 datatype (size),
.
.
Column_name (n) datatype (size));
The Column_name parameters specify the names of attributes (columns) of the table.
The datatype parameter specify what type of data that column can hold (e.g. varchar, integer, date, etc.).
The size parameter specifies the maximum length of the column table.

6
NB: If constraints are to be enforced at column level, then the constraint definition is added after the
datatype. The syntax then follows as shown
CREATE TABLE table_name
(column_name1 datatype (size) [constraint],
column_name2 datatype (size) [constraint],
column_name(n) datatype (size) [constraint]);

For example;
Now let’s create a table called “Student” that contains five columns: Regno, LastName, FirstName,
Address and Date_of_Birth.
We use the following statement;
CREATE TABLE Student
( RegNo varchar(255),
LastName char (255),
FirstName char (255),
Address varchar (255),
Date_of_Birth date);
Suppose we want to enforce a NOT NULL constraint on RegNo such that, a value must be entered in
this column. Then we modify the first column to RegNo varchar (255) NOT NULL,
To see a list of tables in a database we use SHOW TABLES command.
To see the description of the table we have created, then DESC command is used. DESC here means
description of the table. The syntax for DESC command is:
DESC table_name;
This command returns the attributes (columns) of the table, the datatype associated with the column, and
also any constraint (if any) imposed on the column.
For example;
DESC Student;
Dropping table
To delete definition of a table as well as the contents of the table, DROP TABLE command is used. The
syntax is:
DROP TABLE table_name;

7
However, one may want to remove the contents of a table (i.e., all rows of a table) without deleting the
table definition. This operation is called Table Truncation and TRUNCATE TABLE command is used.
This command also releases the storage space used by table. The syntax of this command is;
TRUNCATE TABLE table_name;
NB: another way to delete all the rows of the table is to use DELETE command. The syntax is:
DELETE FROM table_name;
Altering Table Command (Modifying the Structure of a Table)
We can use ALTER command to alter the structure of a table, i.e., we can add a column, delete a
column and even modify a datatype of an attribute.
Adding a Column to a Table
To add a column to an already existing table, ADD command is used. The syntax is;
ALTER TABLE table_name
ADD column_name datatype;
For example
Suppose in the Student table created above, we would like to add phone number attribute. Then the
ADD command used would be;
ALTER TABLE Student
ADD phonenumber int;
Modifying the Column of the Table
We can modify the datatype of a column by using ALTER and MODIFY command. The syntax to
change the datatype of the column is:
ALTER TABLE table_name
MODIFY column_name datatype;
For example
Suppose we modify the datatype of column Date_of_Birth from date to datetime. Then the command to
use would be;
ALTER TABLE Student
MODIFY Date_of_Birth datetime;
Deleting a Column of a Table
To delete a column of a table, we use DROP COLUMN command along with the ALTER TABLE
command. The syntax to delete a column is;
8
ALTER TABLE table_name
DROP COLUMN column_name;
For example,
Suppose we delete the column phonenumber that we added in the table Student. The command to use is;
ALTER TABLE Student
DROP COLUMN phonenumber;
Imposition of Constraints
Constraints are basically used to impose rules on the table, whenever a row is inserted, updated, or
deleted from a table. They prevent the deletion of a table if there are dependencies. The different types
of constraint that can be imposed on a table are NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN
KEY, and CHECK.
i. NOT NULL Constraint
Whenever an attribute is declared as NOT NULL, then that attribute cannot contain a NULL value. The
syntax of this constraint is:
CREATE TABLE table_name
(column_name1 datatype NOT NULL,
Column_nameN datatype);
This syntax indicates that column1 is declared as NOT NULL.
ii. UNIQUE Constraint
The UNIQUE constraint imposes that every value in a column or set of columns be unique. It implies
that no two rows of a table can have duplicate values in a specified column or set of columns. The
syntax is:
CREATE TABLE table_name
(column_name1 datatype UNIQUE,
Column_nameN datatype);
iii. PRIMARY KEY Constraint
When an attribute or set of attributes is declared as primary key, then the attribute will not accept NULL
values neither duplicate values. The syntax is:
CREATE TABLE table_name
(column_name1 datatype PRIMARY KEY ,
Column_nameN datatype);

9
CREATE TABLE table_name
(column_name1 datatype ,
Column_nameN datatype
PRIMARY KEY (column-name1));
NB: both syntax sets column_name1 as the PRIMARY KEY.
iv. FOREIGN KEY Constraint
According to referential integrity constraint, when a foreign key in one relation references
primary key in another relation, then foreign key value must match with primary key value. In other
words, the referential integrity says “pointed to” information must exist. To allow naming of a
FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, the
following syntax:
CREATE TABLE table_name
(column1 datatype,
ColumnN datatype,
CONSTRAINT fkconstraintname FOREIGN KEY (column_name) REFERENCES
table2name (column_name)).
v. CHECK Constraint
CHECK Constraint is used to limit the value of range that can be placed in a column. If a CHECK
constraint is defined on a single column, it allows only certain values for that column. If defined on a
table, it can limit the values in certain columns based on values in other columns in the row. It is added
to the declaration of the attribute. The attribute value check is checked only when the value of the
attribute is inserted or updated. The syntax is:
CREATE TABLE table_name
(Column1 datatype,
Column datatype,
CHECK (Column (condition)));
Example;
CREATE TABLE Voter
(name char(25),
Age int,
CHECK (age>18)); this ensures that only value greater than 18 are accepted in attribute age.

10
DATA MANIPULATION LANGUAGE
Data Manipulation language is used to add, update, and delete data in the database. The SQL commands
used in this category are INSERT, UPDATE and DELETE. The SELECT command is also used in this
category to retrieve data before and after manipulation.
Adding a New Row to a Table
Adding a new row to a table, the INSERT command is used. The syntax of this command varies on
parameters depending on the order in which data is added. To add an entirely new row to a table, the
following syntax is used;
INSERT INTO table_name
VALUES (‘column1_name’, ‘column2_name’, …… , ‘columnN_name’);
To add several rows at once, the following command is used;
INSERT INTO table_name
VALUES (‘column1_name’, ‘column2_name’, …… , ‘columnN_name’),
(‘column1_name’, ‘column2_name’, …… , ‘columnN_name’),
(‘column1_name’, ‘column2_name’, …… , ‘columnN_name’);
This adds three rows to a table. Note, the last row is delimited with a semi-colon but subsequent rows by
commas.
NB: It is to be noted that apostrophe is not required for numeric datatype.
However, one can add values of specific attributes to a table and thus it’s important to specify the
attribute names to which values are added. In this case, the order is insignificant as long as values
maintain domain integrity. The syntax is;
INSERT INTO table_name (column1_name, column2_name, ….., column_name)
VALUES (‘column1_name’, ‘column2_name’, …… , ‘columnN_name’);
Updating Data in a Table
The already existing data in a table can be updated using UPDATE command. The syntax of the
UPDATE command is:
UPDATE table_name
SET attribute name= new value, attribute2 name=new value …attributeN=new value
WHERE condition;

11
Deleting Row from a Table
To delete row(s) from a table, the DELETE command is used. The syntax is;
DELETE FROM table_name
WHERE condition;

DATA SELECTION AND PROJECTION


Selection operation can be considered as row wise filtering in which, we can select specific row(s) using
condition. The operation which filters the row of the relation is called SELECTION. The projection
operation performs column wise filtering. Specific columns are selected in PROJECTION operation.
A typical SQL selection query is of the form:
SELECT
A1, A2, A3,…….An
FROM
R1, R2, R3, ……….Rn
WHERE
P
Ai represents an attribute, Ri a relation and P a predicate.
To select the entire data in a relation, the following syntax is used;
SELECT * FROM table_name
WHERE condition
NB:The * (asterick) means all.
To select specific columns, i.e a PROJECTION, the following syntax is used;
SELECT column_name1, column_name2, ……column_nameN
FROM table name;
NB: if all columns of the table are selected, then it cannot be considered as PROJECTION.
The SELECT clause can also contain arithmetic expressions involving operations +,-,*,/ and operating
on constants or attributes of tables.
For example, to select annual salary of employees who are paid on monthly basis from table employee,
the following SELECT statement can be used;
SELECT name, idno, salary *12 FROM Employee.

12
WHERE CLAUSE
The WHERE clause in a SELECT statement is used to specify a condition that must be met. SQL uses
logical connectives AND, OR, NOT, and BETWEEN in the where clause. It uses operands of logical
connectives <, <=, >,>=, =, and < >.
For example, to select name and IdNo of employees who live in “Thika” and their salary ranges between
10,000 and 15,000, the following statement is used.
SELECT name, IdNo
FROM Employee
WHERE town = “Thika” and salary between 10,000 and 15,000
SQL also provides a mechanism for renaming both relations and attributes by use of AS clause. It is
usually of the form,
Old_name AS new_name.
For example, to change attribute name to Employee_name and compute annual salary and select it as
annual salary from Employee, the following statement is used.
SELECT name AS Employee_name, salary * 12 AS Annual_salary
FROM Employee.
Similarly, relations can be renamed using AS clause.
For example, selecting name, salary, city and street of an employee from two tables, Employee and
Branch, the following statement can be used. NB: the table names are changed to E and B respectively.
SELECT E.name, E.salary, B.city, B.street
FROM Employee as E, Branch as B
WHERE B.branchno = E.branchno.
String operations
SQL also provide operations on strings. Most commonly used operation on string is pattern matching
using “LIKE”.
Two characters are used;
 Percent (%) – matches any substring
 Underscore (_) – matches any character.
NB: patterns are case sensitive, uppercase do not match lowercase characters.
Examples
i. “Mary%” – matches any string beginning with “Mary”.

13
ii. “%ry” –matches any string containing “ry” as a sub-string e.g. very, mary, arry etc.
iii. “---” - matches any string of exactly three characters.
iv. “---%” – matches any string of atleast three characters.
For example, to select all details of employees whose name include substring “wa”, the SQL statement
would be;
SELECT * FROM Employee
WHERE name LIKE “%wa%”.
Aggregate Functions
SQL provides a number of built-in functions to facilitate query processing. These functions include
COUNT, MAX, MIN, SUM, AVG, STDDEV and VARIANCE. Each of these functions performs a
specific task.
 COUNT Function
This function returns the number of rows in a table. There are variations of this function
depending on the characteristics of attributes to be counted. To count all rows in a table, the following
syntax is used;
SELECT COUNT (*) FROM table name;
A slight variation of COUNT (*) function is COUNT (attribute name) function. This counts entries of a
specific attribute in a table. The syntax is;
SELECT COUNT (attribute name) FROM table name;
NB: this counts all entries within an attribute including duplicate values. However, to return the number
of rows of a relation and eliminate duplicate values, the DISTINCT keyword is used. The syntax is;
COUNT (DISTINCT attribute name) FROM table name.
 MAX Function
The MAX command, which stands for maximum value, returns the maximum value of an attribute.
The syntax is:
SELECT MAX (attribute name) FROM table name;
 MIN Function
The MIN Function is used to return the minimum value of an attribute. The syntax is;
SELECT MIN (attribute name) FROM table name;
 SUM Function
This function finds the sum of values of an attribute provided the datatype of the attribute is number.
The syntax is;

14
SELECT SUM (attribute name) FROM table name;
 AVG Function
This command is used to compute the average value of an attribute. The syntax of this command
is;
SELECT AVG (attribute name) FROM table name;
 STDDEV Function
This function is used to compute the standard deviation of the attribute values. The syntax is;
SELECT STDDEV (attribute name) FROM table name;
 VARIANCE Function
This function is used to get the variance of the attribute values. The syntax is;
SELECT VARIANCE (attribute name) FROM table name;
Sorting of Results
SQL also provides other functions that are used to sort values of relations. These functions include
GROUP BY, HAVING and ORDER BY functions.
 GROUP BY Function
This clause is used to group rows to compute group-statistics. It is to be noted that when the GROUP
BY clause is present, then the SELECT clause may include only the columns that appear in the GROUP
BY clause and aggregate functions. The syntax is;
SELECT attribute name
FROM table name
GROUP BY attribute name;
If an aggregate function is included, then the syntax is;
SELECT attribute name, aggregate function
FROM table name
GROUP BY attribute name;
NB: attribute name after SELECT command should match with the attribute name after GROUP BY
command.
 HAVING Command
This clause is used to select the group, i.e., it restricts the groups according to a specified condition. The
syntax is;

15
SELECT attribute name, aggregate function
FROM table name
GROUP BY attribute name
HAVING condition;
 ORDER BY Clause
This clause is used to sort the results in ascending or descending order. The syntax of ORDER BY
command to arrange the result in ascending order is;
SELECT *
FROM table name
ORDER BY attribute name ASC;
Here ASC stands for ascending order.
The syntax to arrange the results in descending order is;
SELECT *
FROM table name
ORDER BY attribute name DESC;
Here DESC stands for descending order.
NB: if we do not specify order as ASC or DESC after ORDER BY keyword, by default, the results will
be arranged in ascending order.

Assignment
 Read and make notes on Join Operation
 Sub-queries (nested select)

16

You might also like