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

Structured Query Language

This document provides an introduction to Structured Query Language (SQL) including its components (DDL, DML, DCL), how it is used to manage data in relational database management systems (RDBMS), and examples of common SQL statements. Key points covered include creating and modifying database objects and tables, inserting, querying, updating, and deleting data from tables. Popular RDBMS like MySQL are also introduced.

Uploaded by

charles mmari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Structured Query Language

This document provides an introduction to Structured Query Language (SQL) including its components (DDL, DML, DCL), how it is used to manage data in relational database management systems (RDBMS), and examples of common SQL statements. Key points covered include creating and modifying database objects and tables, inserting, querying, updating, and deleting data from tables. Popular RDBMS like MySQL are also introduced.

Uploaded by

charles mmari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Structured

Query
Language
Introduction
• Structured Query Language (SQL) is a
database language designed for managing
data in relational database management
systems (RDBMS).
• SQL has three components:
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language (DCL)

© K.S. Mbise SQL Slide 2


Introduction cont…
• A Data Definition Language (DDL) is a
database language for defining the
structure of database objects in a
database.
• It is a language for creating, altering
and dropping database objects such as
create table, drop table, alter table, etc.
• DDL changes database schema.
© K.S. Mbise SQL Slide 3
Introduction cont…
• Data Manipulation Language (DML) is
a database language used to insert,
delete, select and update data in a
database.
• DML reads or changes the content of
the database.

© K.S. Mbise SQL Slide 4


Introduction cont…
• Data Control Language (DCL) is also a
database language used to control
access (privilege) to data stored in a
database.
• DCL has two main SQL statements:
• GRANT – to grant a privilege to a user.
• REVOKE – to revoke (remove) a
privilege from a user.
© K.S. Mbise SQL Slide 5
Database management system
• DBMS provides mechanisms for storing,
organizing, retrieving and modifying data.
• DBMSs allow for the access and storage of
data without concern for the internal
representation of the data in the database.
• Some popular RDBMSs are Microsoft SQL
Server, Oracle, Sybase, IBM DB2, Informix,
PostgreSQL and MySQL.

© K.S. Mbise SQL Slide 6


Relational Databases
• A relational database is a logical representation
of data that allows the data to be accessed
without consideration of its physical structure.
• A relational database stores data in tables.
• Tables are composed of rows, and rows are
composed of columns.
• Each row has a primary key which uniquely
identifies it and cannot be duplicated in other
rows.
© K.S. Mbise SQL Slide 7
Introduction to MySQL
• MySQL is a multiuser, multithreaded (i.e.,
allows multiple simultaneous connections)
RDBMS server that uses SQL to interact with
and manipulate data.
• MySQL is an open source, relational database
software, based on SQL for data query and
retrieval.
• Many tools and applications installed on
websites rely on MySQL as the back end data
repository. E.g., Joomla, WordPress, FB, etc.
© K.S. Mbise SQL Slide 8
Benefits of MySQL
• Scalability – can be embedded in any
application
• Performance – can be optimized
• Support for many programming
languages.
• Implementations of MySQL for Windows,
Mac OS, Linux and UNIX.
• Handling large databases (e.g., tens of
thousands of tables with millions of rows).
© K.S. Mbise SQL Slide 9
MySQL Installation
• The following are steps that are
required for the installation of WAMP
Server:
• Download the latest release/version of
the LAMP (for Linux OS)/WAMP (for
windows)/XAMPP (platform
independent) from
http://www.wampserver.com/en/downl
oad.php
© K.S. Mbise SQL Slide 10
MySQL Installation cont...
• After installation is completed, you
will see the WAMP server icon on a
task bar.
• Make sure that all MySQL services
are running before you start using
them.

© K.S. Mbise SQL Slide 11


Getting started with MySQL
• On a taskbar, click on WAMP server
icon  MySQL  Service 
Start/Resume Service.
• This starts the MySQL services.
• Once MySQL is working correctly, now
you can start MySQL console window.

© K.S. Mbise SQL Slide 12


Getting started with MySQL
cont…
• On a taskbar, click on WAMP server
icon  MySQL  MySQL console. A
MySQL console window looks like this:

© K.S. Mbise SQL Slide 13


SQL query keywords

© K.S. Mbise SQL Slide 14


Data types in SQL
• A data type defines what kind of value a
column can contain.
• Each column in a database table is
required to have a name and a data
type.
• SQL developers have to decide what
types of data will be stored in each
column when creating a SQL table.
© K.S. Mbise SQL Slide 15
Data types in SQL cont…
• Characters:
• CHAR(20) – fixed length
• VARCHAR(40) – variable length
• Numbers:
• BIGINT, INT, SMALLINT, TINYINT
• REAL, FLOAT – differ in precision
• DECIMAL
© K.S. Mbise SQL Slide 16
Data types in SQL cont…
• Times and dates:
• DATE
• DATETIME
• Note:
• Data types might have different names in
different database.
• And even if the name is the same, the size
and other details may be different! Always
check the documentation!
© K.S. Mbise SQL Slide 17
SQL statements
• To login (from unix shell) use -h only if
needed.
• # [mysql dir]/bin/mysql -h hostname -u
root -p
• Create a database.
• create database databaseName;
• List all databases on the MySQL server.
• show databases;
© K.S. Mbise SQL Slide 18
SQL statements cont…
• How to select a database to use.

use databaseName;
• To see all the tables in the database.

show tables;
• To see database's field formats.

describe tableName;

© K.S. Mbise SQL Slide 19


SQL statements cont…
• To drop a database.
• drop database databaseName;
• To drop a table in a database.
• drop table tableName;
• Insert data into a table.
• insert into tableName values
('value1','value2',...,'valueN');
© K.S. Mbise SQL Slide 20
SQL statements cont…
• Show all data in a table.
• SELECT * FROM tableName;
• Table columns' information
pertaining to the designated
table.
• show columns from tableName;

© K.S. Mbise SQL Slide 21


SQL statements cont…
• How to rename table in MySQL
• USE databaseName;
• RENAME TABLE old_table_name TO
new_table_name;
• Query OK, 0 rows affected (0.00
sec)

© K.S. Mbise SQL Slide 22


SQL statements cont…
• SQL SELECT statement is used to
fetch the data from a database table
which returns data in the form of
result table.
• Show certain selected rows with the
value "whatever".
• SELECT * FROM tableName WHERE
columnName = "whatever";
© K.S. Mbise SQL Slide 23
SQL statements cont…
• Show all records containing the
name "Allan Shearer" AND the phone
number '3444444'.
• SELECT * FROM tableName
WHERE name = "Allan Shearer"
AND phone_number = '3444444';

© K.S. Mbise SQL Slide 24


SQL statements cont…
• Show all records not containing the
name "Bob" AND the phone number
'3444444' order by the phone_number
field.
• SELECT * FROM tableName WHERE
name != "Bob" AND phone_number
= '3444444' order by
phone_number;
© K.S. Mbise SQL Slide 25
SQL statements cont…
• Show all records starting with the
letters 'bob' AND the phone number
'3444444' limit to records 1 through
5.
• SELECT * FROM [table name]
WHERE name like "Bob%" AND
phone_number = '3444444' limit
1,5;
© K.S. Mbise SQL Slide 26
SQL statements cont…
• Show unique records.
• SELECT DISTINCT columnName
FROM tableName;
• Show selected records sorted in an
ascending (asc) or descending (desc).
• SELECT [col1],[col2] FROM
tableName ORDER BY col1 DESC;

© K.S. Mbise SQL Slide 27


SQL statements cont…
• To update info already in a table.
• UPDATE tableName set
columnNameToModify= 'newValue'
where differentColumnName =
'whatever';
• Delete a row(s) from a table.
• DELETE from tableName where
columnName = 'whatever';
© K.S. Mbise SQL Slide 28
SQL statements cont…
• How to add a constraint to a column
e.g. foreign key or primary key
• alter table tableName add constraint
foreign key(columnName)
references
tableName1(columnName);
• alter table tableName add constraint
primary key(columnName);
© K.S. Mbise SQL Slide 29
SQL statements cont…
• Update database permissions or
privileges.
• flush privileges;
• Delete a column.
• alter table tableName drop column
columnName;

© K.S. Mbise SQL Slide 30


SQL statements cont…
• Add a new column to database.
• alter table tableName add column
columnName datatype(size);
• Change column name.
• alter table tableName change
oldColumnName newColumnName
datatype(size);

© K.S. Mbise SQL Slide 31


SQL statements cont…
• Make a unique column so you get no
dupes.
• alter table tableName add unique
columnName;
• Make a column bigger.
• alter table tableName modify
columnName datatype(size);

© K.S. Mbise SQL Slide 32


SQL statements cont…
• Dump all databases for backup.
• # [mysql dir]/bin/mysqldump -u root -
ppassword –all-databases
>/tmp/alldatabases.sql
• Dump one database for backup.
• # [mysql dir]/bin/mysqldump -u
username -ppassword --databases
databasename >/tmp/databasename.sql

© K.S. Mbise SQL Slide 33


SQL statements cont…
• Dump a table from a database.
• [mysql dir]/bin/mysqldump -c -u
username -ppassword databasename
tablename >
/tmp/databasename.tablename.sql
• Restore database (or database table)
from backup.
• [mysql dir]/bin/mysql -u username -
ppassword databasename <
/tmp/databasename.sql
© K.S. Mbise SQL Slide 34
SQL statements cont...
• The SQL statement to grant a privilege
on a table is:
• GRANT SELECT, INSERT, UPDATE, DELETE
ON tablename TO username;
• The SQL statement to revoke a
privilege on a table is:
• REVOKE SELECT, INSERT, UPDATE, DELETE
ON tablename FROM username;

© K.S. Mbise SQL Slide 35


SQL statements cont...
• WHERE clause is a general boolean expression
• Boolean operators:
AND, OR, NOT
• Comparison operators:
=, <, <=, >, >=, <>
• String comparison operators: LIKE
• Parentheses can be used to set precedence
• String literals can be enclosed in "…" or '…'

© K.S. Mbise SQL Slide 36


SQL statements cont...
• The LIKE comparison operator is
used to compare partial string
• Two wildcard characters are used:
• '%' replaces an arbitrary number
of characters
• '_' replaces a single arbitrary
character

© K.S. Mbise SQL Slide 37


Aggregate functions
• Aggregate functions are applied to result
attributes.
• These are COUNT, SUM, MAX, MIN, and
AVG.
• Find the maximum salary, the minimum
salary, and the average salary among all
employees.
• SELECT MAX(Salary), MIN(Salary),
AVG(Salary) FROM EMPLOYEE;
© K.S. Mbise SQL Slide 38
Aggregate functions cont…
• Find the total salary paid to employees
who work for the 'Research'
department.
• SELECT SUM(Salary) FROM
EMPLOYEE, DEPARTMENT WHERE
Dno=Dnumber AND
Dname='Research';

© K.S. Mbise SQL Slide 39


Set comparison operators
• IN – set membership (“is in”, ∈)
• EXISTS – set not empty (∃)
• ALL – applies to all set members (∀)
• ANY – applies to any set member
• CONTAINS – proper superset

© K.S. Mbise SQL Slide 40


Thank you for listening!

© K.S. Mbise SQL Slide 41

You might also like