Professional Documents
Culture Documents
• Databases and database technology have had a major impact on the growing
use of computers.
• Databases play a critical role in almost all areas where computers are used
• RDBMS is the basis for SQL and for all modern database systems
08/11/2022 2
Araya Mesfin (MPH-HI) UoG
Introduction
SQL RDBMS Concepts cont’d…cont’d…
What is the difference between table and relation?
A table is the most common and simplest form of data storage in a relational
database
08/11/2022 3
Araya Mesfin (MPH-HI) UoG
SQL RDBMS Concepts cont’d…
CUSTOMERS table
08/11/2022 4
Araya Mesfin (MPH-HI) UoG
Structured Query Language (SQL)
It is the standard language for relational database management systems
It is mainly used to create, insert, search, update, delete database records
SQL uses terms such as: table, row, and column for the formal relational
model terms relation, tuple, and attribute, respectively
08/11/2022 5
Araya Mesfin (MPH-HI) UoG
What can SQL do?
SQL can:
execute queries against a database
08/11/2022 7
Araya Mesfin (MPH-HI) UoG
Tips on SQL
• Use one line per column (attribute) definition
• i.e. we can mix uppercase and lowercase when referencing SQL keywords
(such as SELECT and INSERT)
• Primary key attribute names contain both NOT NULL and a UNIQUE
specification
08/11/2022 8
Araya Mesfin (MPH-HI) UoG
Tips on SQL cont’d…
• The basic structure of an SQL expression consists of three clauses: select, from,
and where
• SQL uses the terms table, row, and column for the formal relational model
terms relation, tuple, and attribute respectively
• The main SQL command for data definition is the CREATE statement
• SQL can also be used to create tables (relations) and domains (as well as other
constructs such as views)
Data type selection is usually dictated by nature of data and by intended use
Pay close attention to expected use of attributes for sorting and data retrieval
purposes
When defining a column, you should always set its name and associated
domain or data type
The domain and data type limit the information that the column can accept
08/11/2022 10
Araya Mesfin (MPH-HI) UoG
Data types cont’d…
08/11/2022 11
Araya Mesfin (MPH-HI) UoG
Basic SQL constraints
Constraints are used to enforce valid data in columns
SQL commands are divided into four subgroups, DDL, DML, DCL, and TCL
08/11/2022 14
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
1. Data Definition Language (DDL)
DDL deals with database schemas and descriptions, of how the data should
reside in the database
CREATE: used for creating a database and its objects (Like: table, index, views, store)
TRUNCATE TABLE: removing all records from a table, including all spaces
allocated for the records are removed
RENAME: rename database objects
08/11/2022 15
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
• CREATE TABLE is the keyword telling the DB system what you want to do
• It can be used to create schemas, tables, types, views, and domains
Syntax
• ColumnName
• Data type
DOB date,
Allows users to add, modify, delete or retrieve information stored inside your
database entries
INSERT: Insert one or more rows from a table or views (creates a record)
DELETE: removes one or more rows from a table or view (deletes a record)
08/11/2022 18
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
• The INSERT INTO statement is used to add new data to a database.
o The first way specifies both the column names and the values to be
inserted
08/11/2022 19
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
The second way, if you are adding values for all the columns of the table, you
do not need to specify the column names in the SQL query
• However, make sure the order of the values is in the same order as the
columns in the table.
08/11/2022 20
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
• SELECT: the SELECT statement is used to select data from a database.
SELECT Syntax
• Here, column1, column2, ... are the field names of the table you want to select
data from.
• If you want to select all the fields available in the table, use the following
syntax:
08/11/2022 21
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d…
Example1:
Select StudID, Sname, Avg_Score
from STUDENT
The query displays StudID, Sname, Avg_Score from STUDENT table
Example1:
Select *
from STUDENT
where sex= ‘F’
The query display only female students with all the fields name from STUDENT
table
08/11/2022 22
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
3. Data Control Language (DCL)
08/11/2022 23
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
4. Transaction Control Language (TCL)
It is used to perform some operations (transactions like insert, delete and
update). i.e. it is used to manage the changes made by DML statements
The DML operations are used to control the data in the table.
ROLL BACK: the command restores the database to last committed state
08/11/2022 24
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
Note 1: Saving table changes made to table contents are not physically saved
on disk until, one of the following occurs:
• Database is closed
• Program is closed
• Will permanently save any changes made to any table in the database
08/11/2022 25
Araya Mesfin (MPH-HI) UoG
Classifications of SQL commands cont’d….
Note 2: Restoring table contents
• ROLLBACK
Syntax:
• ROLLBACK;
08/11/2022 26
Araya Mesfin (MPH-HI) UoG
08/11/2022 Araya Mesfin (MPH-HI) UoG 27
N.B: 1) The create database statement is used to create a new database
1. Arithmetic operators
2. Comparison operators
3. Logical Operators
With the exception of the COUNT aggregate function, all other aggregate
functions ignore NULL values
Are frequently used with the GROUP BY clause of the SELECT statement
08/11/2022 62
Araya Mesfin (MPH-HI) UoG
Aggregate Function cont’d….
The functions SUM, MAX, MIN, and AVG can be applied to a set or multi
set of numeric values and return, respectively, the sum, maximum value,
minimum value, and average (mean) of those values.
The functions MAX and MIN can also be used with attributes that have
nonnumeric domains if the domain values have a total ordering among
one another.
SELECT SUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary)
FROM EMPLOYEE;
08/11/2022 64
Araya Mesfin (MPH-HI) UoG
Aggregate Function cont’d…..
Retrieve the total number of employees in the company and the number of
employees in the ‘Research’ department.
SELECT COUNT(*)
FROM EMPLOYEE;
SELECT COUNT(*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND DNAME=‘Research’;
• However, any tuples with NULL for SALARY will not be counted. In general,
NULL values are discarded when aggregate functions are applied to a particular
column (attribute)
Produces summary values for the selected columns, which should also appear
in the SELECT clause,
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
• SELECT MAX(column_name)
FROM table_name
WHERE condition;
COUNT ( ) Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Count (Distinct field) displays the number of unique values in the specified
field, taken only from the records returned
08/11/2022 72
Araya Mesfin (MPH-HI) UoG
Thank You!
Thank You!!!