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

lecture-3

The document provides an overview of SQL (Structured Query Language), detailing its history, importance, and fundamental components including Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). It explains the syntax and usage of various SQL commands such as SELECT, INSERT, UPDATE, and DELETE, along with the roles of SQL clauses. The content is aimed at individuals looking to understand and utilize SQL for database management and data manipulation.

Uploaded by

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

lecture-3

The document provides an overview of SQL (Structured Query Language), detailing its history, importance, and fundamental components including Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). It explains the syntax and usage of various SQL commands such as SELECT, INSERT, UPDATE, and DELETE, along with the roles of SQL clauses. The content is aimed at individuals looking to understand and utilize SQL for database management and data manipulation.

Uploaded by

pekogroup2017
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Database Systems and Security

SQL Fundamentals

Evrad KAMTCHOUM

CENTER FOR CYBERSECURITY AND MATHEMATICAL CRYPTOLOGY - THE


UNIVERSITY OF BAMENDA

November 6, 2024

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 1 / 50


Contents

1 Introduction

2 Data Definition Language (DDL)

3 Data Manipulation Language (DML)

4 Data Control Language (DCL)

5 SQL Operators

6 Aggregation Functions

7 Writing Functions with PostgreSQL

8 Conclusion

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 2 / 50


Introduction

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 3 / 50


What is SQL?

What is SQL?
SQL (Structured Query Language) is a standard language for accessing and
manipulating databases. It is used to perform tasks such as querying data,
updating data, and defining the structure of databases. SQL is a powerful
language for working with databases. Understanding its basic syntax and
common data types is essential for working effectively with databases.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 4 / 50


History of SQL
SQL (Structured Query Language) has a rich history that dates back several
decades:
Early Development (1970s):
SQL was initially developed by IBM researchers Donald D. Chamberlin and
Raymond F. Boyce in the early 1970s.
The initial version of SQL, known as SEQUEL (Structured English Query
Language), was developed as part of the System R project at IBM.

Standardization (1980s):
SQL gained popularity and was adopted by various database vendors.
In 1986, SQL became an ANSI (American National Standards Institute) standard,
known as ANSI SQL-86.
Subsequent versions of the standard were released, including ANSI SQL-89 and
ANSI SQL-92, which introduced new features and enhancements.

ISO Standardization (1980s-1990s):


SQL also became an ISO (International Organization for Standardization) standard,
with ISO/IEC 9075 defining the SQL language.
ISO SQL standards continued to evolve, with new versions released over the years
to address emerging needs and technologies.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 5 / 50


History of SQL (2)

Vendor Extensions and Implementations:


While SQL standards provide a common foundation, many database
vendors introduced their own extensions and implementations of SQL.
These vendor-specific extensions added additional functionality and
features beyond the standard SQL language.
Current Status:
SQL remains a widely used and standardized language for accessing
and manipulating databases.
Various versions of SQL are supported by different database
management systems (DBMS), each with its own level of compliance
with SQL standards and additional features.
The history of SQL reflects its evolution from a research project to a
standardized language that plays a central role in database management
and data processing.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 6 / 50


Importance of SQL
1 Universal Language: SQL is a standardized language that is widely used across different
database management systems (DBMS) and platforms. Learning SQL allows individuals
to work with various databases regardless of the specific DBMS being used.
2 Data Querying and Retrieval: SQL provides powerful capabilities for querying and
retrieving data from databases. With SQL, users can perform complex searches, filter
data, and retrieve specific information from large datasets efficiently.
3 Data Manipulation: SQL enables users to manipulate data stored in databases, including
inserting, updating, and deleting records. This is essential for managing and maintaining
the integrity of data within a database.
4 Database Administration: SQL is crucial for database administrators (DBAs) who are
responsible for managing databases. Tasks such as creating and modifying database
structures, setting up user permissions, and optimizing database performance are
performed using SQL commands.
5 Data Analysis and Reporting: SQL is often used in conjunction with other tools and
languages for data analysis and reporting purposes. By querying databases with SQL,
analysts can extract insights, generate reports, and make informed decisions based on
data.
6 Integration with Applications: Many applications and systems rely on databases to store
and manage data. SQL is used to interact with databases from within applications,
allowing developers to build database-driven applications and integrate them seamlessly
with backend data stores.
Overall, SQL plays a critical role in managing, querying, and manipulating data, making it an
essential skill for anyone working with databases or involved in data-related tasks.
Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 7 / 50
Basic SQL Syntax

SQL statements are typically written as text queries.


Each SQL statement usually ends with a semicolon (‘;’).
SQL is not case-sensitive, but conventionally keywords are written in
uppercase.
Whitespace characters (spaces, tabs, newlines) are generally ignored,
except within string literals.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 8 / 50


Common Data Types in SQL

SQL supports various data types, including:


Integer
Decimal
Char
Varchar
Date
Time
Blob (Binary Large Object)
Boolean
The specific data types available may vary depending on the database
system being used.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 9 / 50


Examples of Basic SQL Queries
1 Select Statement:
SELECT ∗ FROM e m p l o y e e s ;
This query retrieves all columns from the employees table.
2 Where Clause:
SELECT ∗ FROM o r d e r s WHERE o r d e r s t a t u s = ’ S h i p p e d ’ ;
This query selects all columns from the orders table where the order status is ’Shipped’.
3 Insert Statement:
INSERT INTO c u s t o m e r s ( c u s t o m e r n a m e , c i t y , c o u n t r y )
VALUES ( ’ John Doe ’ , ’ New York ’ , ’USA ’ ) ;
This query inserts a new record into the customers table.
4 Update Statement:
UPDATE p r o d u c t s SET p r i c e = 20 WHERE c a t e g o r y = ’ E l e c t r o n i c s ’ ;
This query updates the price of products in the ’Electronics’ category.
5 Delete Statement:
DELETE FROM e m p l o y e e s WHERE d e p a r t m e n t = ’HR ’ ;
This query deletes records from the employees table where the department is ’HR’.
Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 10 / 50
Types of SQL Language
SQL (Structured Query Language) can be categorized into several types
based on its functionality and purpose:
1 Data Definition Language (DDL):

Used to define the structure of the database schema.


Includes commands like CREATE, ALTER, DROP, and TRUNCATE.
2 Data Manipulation Language (DML):
Used to manipulate data stored in the database.
Includes commands like SELECT, INSERT, UPDATE, and DELETE.
3 Data Control Language (DCL):
Used to control access to data within the database.
Includes commands like GRANT and REVOKE.
4 Transaction Control Language (TCL):
Used to manage transactions within the database.
Includes commands like COMMIT, ROLLBACK, and SAVEPOINT.
These types of SQL language provide different capabilities for working with
databases, from defining their structure to manipulating data and
controlling access and transactions.
Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 11 / 50
Full Syntax of SQL Queries

The full syntax of SQL queries can include various clauses and options,
such as:
SELECT Statement:
SELECT column1 , column2 , . . .
FROM t a b l e n a m e
WHERE c o n d i t i o n
GROUP BY column1
HAVING c o n d i t i o n
ORDER BY column1 [ASC | DESC]
LIMIT o f f s e t , count ;

These clauses and options provide flexibility and control when querying
and manipulating data in SQL databases.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 12 / 50


Roles of SQL Clauses

When constructing SQL queries, various clauses are used to define the
desired operations and conditions. Let’s explore the roles of some common
SQL clauses:
1 SELECT: Specifies the columns to be retrieved from the database
tables.
2 FROM: Specifies the tables from which to retrieve data.
3 WHERE: Filters the rows based on specified conditions.
4 GROUP BY: Groups the rows that have the same values into
summary rows.
5 HAVING: Filters the grouped rows based on specified conditions.
6 ORDER BY: Specifies the order in which the rows are returned.
7 LIMIT: Limits the number of rows returned by the query.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 13 / 50


Data Definition Language (DDL)

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 14 / 50


Introduction to DDL in SQL

Data Definition Language (DDL) in SQL is used to define and


manage the structure of database objects, such as tables, indexes,
views, and constraints.
DDL statements are used to create, modify, and delete these objects,
allowing users to define the logical schema of a database.
Hereafter, we will explore the key concepts and commands of DDL in
SQL, including creating tables, altering table structures, and dropping
objects.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 15 / 50


CREATE Statement

The CREATE statement is used to create new database objects. Here’s the
syntax for creating a table:
CREATE TABLE t a b l e n a m e (
column1 d a t a t y p e [ c o n s t r a i n t ] ,
column2 d a t a t y p e [ c o n s t r a i n t ] ,
...
PRIMARY KEY ( column1 ) ,
FOREIGN KEY ( column2 ) REFERENCES o t h e r t a b l e ( column ) ,
UNIQUE ( column3 ) ,
...
);
This statement creates a new table with specified columns and constraints, such
as primary keys, foreign keys, and uniqueness constraints.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 16 / 50


ALTER Statement

The ALTER statement is used to modify the structure of existing database


objects. Here’s an example of adding a new column to a table:
ALTER TABLE t a b l e n a m e
ADD column name d a t a t y p e [ c o n s t r a i n t ] ;
This statement adds a new column with the specified data type and
optional constraints to an existing table.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 17 / 50


DROP Statement

The DROP statement is used to delete existing database objects. Here’s an


example of dropping a table:
DROP TABLE t a b l e n a m e ;
This statement deletes the specified table from the database, along with
all its data and associated objects.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 18 / 50


Data Manipulation Language (DML)

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 19 / 50


Introduction to DML in SQL

Data Manipulation Language (DML) in SQL is used to manipulate


data stored in the database. DML statements are used to query,
insert, update, and delete data in database tables.
DML provides the means to perform actions such as retrieving
specific data, adding new records, modifying existing records, and
removing unwanted records.
The key concepts and commands of DML in SQL, including SELECT,
INSERT, UPDATE, and DELETE statements are explored hereafter.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 20 / 50


SELECT Statement

The SELECT statement is used to retrieve data from one or more tables.
Here’s the syntax for a basic SELECT statement:
SELECT column1 , column2 , ...
FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement selects specific columns from a table based on the specified
conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 21 / 50


INSERT Statement

The INSERT statement is used to insert new records into a table. Here’s
the syntax for an INSERT statement:
INSERT INTO t a b l e n a m e ( column1 , column2 , . . . )
VALUES ( v a l u e 1 , v a l u e 2 , . . . ) ;
This statement adds a new record with specified values into the specified
columns of a table.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 22 / 50


UPDATE Statement

The UPDATE statement is used to modify existing records in a table.


Here’s the syntax for an UPDATE statement:
UPDATE t a b l e n a m e
SET column1 = v a l u e 1 , column2 = v a l u e 2 , ...
WHERE c o n d i t i o n ;
This statement updates the values of specified columns in the table based
on the specified conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 23 / 50


DELETE Statement

The DELETE statement is used to remove records from a table. Here’s the
syntax for a DELETE statement:
DELETE FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement deletes records from the table based on the specified
conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 24 / 50


INSERT Statement

The INSERT statement is used to insert new records into a table. Here’s
the syntax for an INSERT statement:
INSERT INTO t a b l e n a m e ( column1 , column2 , . . . )
VALUES ( v a l u e 1 , v a l u e 2 , . . . ) ;
This statement adds a new record with specified values into the specified
columns of a table.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 25 / 50


UPDATE Statement

The UPDATE statement is used to modify existing records in a table.


Here’s the syntax for an UPDATE statement:
UPDATE t a b l e n a m e
SET column1 = v a l u e 1 , column2 = v a l u e 2 , ...
WHERE c o n d i t i o n ;
This statement updates the values of specified columns in the table based
on the specified conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 26 / 50


DELETE Statement

The DELETE statement is used to remove records from a table. Here’s the
syntax for a DELETE statement:
DELETE FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement deletes records from the table based on the specified
conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 27 / 50


Data Control Language (DCL)

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 28 / 50


Introduction to DCL in SQL

Data Control Language (DCL) in SQL is used to control access to


data within the database. DCL statements are used to grant and
revoke privileges or permissions on database objects, such as tables,
views, and procedures.
DCL provides the means to manage the security and access control of
the database.
The key concepts and commands of DCL in SQL, namely GRANT
and REVOKE statements are explored hereafter.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 29 / 50


GRANT Statement

The GRANT statement is used to grant privileges or permissions on database


objects to users or roles. Here’s the syntax for a GRANT statement:
GRANT p r i v i l e g e 1 , p r i v i l e g e 2 , ...
ON o b j e c t n a m e
TO u s e r 1 , u s e r 2 , . . . ;
This statement grants specified privileges on the specified object to the
specified users or roles.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 30 / 50


REVOKE Statement

The REVOKE statement is used to revoke privileges or permissions on


database objects from users or roles. Here’s the syntax for a REVOKE
statement:
REVOKE p r i v i l e g e 1 , p r i v i l e g e 2 , ...
ON o b j e c t n a m e
FROM u s e r 1 , u s e r 2 , . . . ;
This statement revokes specified privileges on the specified object from the
specified users or roles.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 31 / 50


SQL Operators

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 32 / 50


Introduction to SQL Operators

SQL operators are special symbols or keywords used to perform operations


on data in SQL queries. Operators are used in conditions, expressions, and
comparisons to manipulate and filter data in the database.

We distinguish namely:
Arithmetic Operators
Comparison Operators
Logical Operators

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 33 / 50


Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on


numeric data. Here are the commonly used arithmetic operators in SQL:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulo)
These operators can be used in SQL queries to perform calculations on
numeric columns or values.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 34 / 50


Comparison Operators

Comparison operators are used to compare values in SQL queries. Here are
the commonly used comparison operators in SQL:
= (Equal to)
<> or ! = (Not equal to)
< (Less than)
> (Greater than)
<= (Less than or equal to)
>= (Greater than or equal to)
These operators are used in conditions to filter data based on specified
criteria.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 35 / 50


Logical Operators

Logical operators are used to combine multiple conditions in SQL queries.


Here are the commonly used logical operators in SQL:
AND
OR
NOT
These operators are used to create complex conditions by combining
multiple simple conditions.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 36 / 50


Aggregation Functions

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 37 / 50


Introduction to Aggregation Functions

Aggregation functions, also known as aggregate functions, are SQL


functions used to perform calculations on sets of values and return a
single result.
These functions are commonly used to compute summary statistics or
derive aggregated data from tables in the database.
In this section, we will explore the different types of aggregation
functions in SQL and how they are used in SQL queries.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 38 / 50


COUNT Function

The COUNT function is used to count the number of rows in a result set or
the number of non-null values in a column. Here’s the syntax for the
COUNT function:
SELECT COUNT( column name )
FROM t a b l e n a m e ;
This function returns the number of rows or non-null values in the
specified column.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 39 / 50


SUM Function

The SUM function is used to calculate the sum of values in a column.


Here’s the syntax for the SUM function:
SELECT SUM( column name )
FROM t a b l e n a m e ;
This function returns the sum of values in the specified column.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 40 / 50


AVG Function

The AVG function is used to calculate the average (mean) of values in a


column. Here’s the syntax for the AVG function:
SELECT AVG( column name )
FROM t a b l e n a m e ;
This function returns the average of values in the specified column.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 41 / 50


MIN and MAX Functions

The MIN and MAX functions are used to find the minimum and maximum
values in a column, respectively. Here’s the syntax for the MIN and MAX
functions:
SELECT MIN( column name )
FROM t a b l e n a m e ;

SELECT MAX( column name )


FROM t a b l e n a m e ;
These functions return the minimum and maximum values in the specified
column, respectively.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 42 / 50


Writing Functions with PostgreSQL

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 43 / 50


Introduction to Writing Functions with PostgreSQL

PostgreSQL allows you to define custom functions using various


procedural languages, such as PL/pgSQL, PL/Python, PL/Perl, etc.

These functions enable you to encapsulate complex logic and perform


specific tasks within the database.

In this section, we will explore the process of writing functions with


PostgreSQL, focusing on the PL/pgSQL language.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 44 / 50


PL/pgSQL Overview

PL/pgSQL is a procedural language provided by PostgreSQL for


writing functions and stored procedures.

It is similar to the procedural languages used in other databases and


supports features like variables, control structures, exception handling,
and SQL commands.

You can use PL/pgSQL to write functions that perform calculations,


data transformations, and other tasks within the database.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 45 / 50


Writing Functions with PL/pgSQL
To write a function with PL/pgSQL, you use the CREATE FUNCTION statement followed by the
function definition. Here’s an example of writing a simple function to calculate the total salary
of employees in a specified department:
CREATE OR REPLACE FUNCTION c a l c u l a t e t o t a l s a l a r y (
d e p a r t m e n t i d INT
) RETURNS NUMERIC AS
$$
DECLARE
t o t a l s a l a r y NUMERIC ;
BEGIN
SELECT SUM( s a l a r y ) INTO t o t a l s a l a r y
FROM e m p l o y e e s
WHERE d e p a r t m e n t i d = d e p a r t m e n t i d ;

RETURN t o t a l s a l a r y ;
END;
$$
LANGUAGE p l p g s q l ;

This function calculates the total salary of employees in the specified department and returns
the result.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 46 / 50


Calling Functions

Once you’ve defined a function, you can call it from your SQL queries or
other functions just like any built-in function. Here’s how you can call the
calculate total salary function we defined earlier:
SELECT c a l c u l a t e t o t a l s a l a r y ( 1 2 3 ) AS t o t a l s a l a r y ;
This query will invoke the function with the specified department ID (123)
and return the total salary of employees in that department.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 47 / 50


Conclusion

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 48 / 50


Conclusion

Key Takeaways
Data Definition Language (DDL) in SQL is essential for defining and
managing the structure of database objects. By using DDL
statements such as CREATE, ALTER, and DROP, users can create and
modify database objects according to their requirements.
Data Manipulation Language (DML) in SQL provides the means to
query, insert, update, and delete data in database tables. By using
DML statements such as SELECT, INSERT, UPDATE, and DELETE,
users can perform various data manipulation tasks to manage the
contents of the database.
Data Control Language (DCL) in SQL provides the means to control
access to data within the database by granting and revoking privileges
or permissions on database objects. By using DCL statements such as
GRANT and REVOKE, database administrators can manage the security
and access control of the database.
Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 49 / 50
Conclusion (2)

Key Takeaways
SQL operators are essential for performing various operations and
comparisons in SQL queries. By using arithmetic, comparison, and
logical operators, users can manipulate and filter data in the database
to retrieve the desired results.
Aggregation functions are powerful tools for summarizing and
analyzing data in SQL queries. By using functions such as COUNT,
SUM, AVG, MIN, and MAX, users can derive valuable insights from
database tables and compute summary statistics as needed.
Writing functions with PostgreSQL using PL/pgSQL allows you to
extend the functionality of the database and perform custom tasks
within the database environment. By encapsulating logic in functions,
you can improve code organization, reusability, and maintainability.

Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 50 / 50

You might also like