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

Chapter1_OverviewOfBasicSQLStatement_1

The document outlines a course on Advanced Database using PL/SQL, covering topics such as basic SQL statements, PL/SQL operations, database triggers, and error handling. It includes practical laboratory work and specifies tools like MS SQL Server and SSMS for hands-on experience. Key SQL concepts such as commands, joins, and views are also detailed with examples and syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter1_OverviewOfBasicSQLStatement_1

The document outlines a course on Advanced Database using PL/SQL, covering topics such as basic SQL statements, PL/SQL operations, database triggers, and error handling. It includes practical laboratory work and specifies tools like MS SQL Server and SSMS for hands-on experience. Key SQL concepts such as commands, joins, and views are also detailed with examples and syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Advance Database using PL/SQL

(Elective)
Advance Database using PL/SQL

Course Contents
1. Overview of Basic SQL statement
2. Overview to PL/SQL
3. Performing SQL Operations from PL/SQL
4. Using PL/SQL Subprogram
5. Using PL/SQL Package
6. Database Triggers
7. Handling PL/SQL Errors

-Need to perform Laboratory work in all units


Advance Database using PL/SQL

Tools
1. Ms SQL Server 2012 or Higher
2. SSMS (SQL Server Management Studio)

• Download Link
• (For SQL server 2018)
• https://www.microsoft.com/en-us/sql-server/sql-server-downloads

• (For SQL Server Management Studio)


• https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-
management-studio-ssms?view=sql-server-ver16
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL (Structured Query Language)


• SQL is a standard relational database language used for creation, deletion
and modification of database tables.
• It enables a user to create, read, update and delete relational databases
and tables
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Data types in SQL


• Numeric data types such as: INT, TINYINT, BIGINT, FLOAT, REAL, etc.
• Date and Time data types such as: DATE, TIME, DATETIME, etc.
• Character and String data types such as: CHAR, VARCHAR, TEXT, etc.
• Unicode character string data types such as: NCHAR, NVARCHAR, NTEXT, etc.
• Binary data types such as: BINARY, VARBINARY, etc.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands
• SQL commands are instructions. It is used to communicate with the database.
• We use suitable SQL commands to extract and modify information of the
database.
• There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands- DDL (Data Definition Language)


It is used to build and modify the structure of your tables and other objects in the
database.
Examples: CREATE TABLE, ALTER TABLE, DROP TABLE

Create : It is used to create a new table in the database.


Syntax : CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example : CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

DROP : It is used to delete both the structure and record stored in the table.
Syntax : DROP TABLE table_name;
Example : DROP TABLE EMPLOYEE;

ALTER : It is used to alter the structure of the database.


Syntax : ALTER TABLE table_name ADD column_name COLUMN-definition;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands - DML (Data Manipulation Language)


DML (Data Manipulation Language): It is used to work with the data in tables. It is used to retrieve, store, modify,
delete, insert and update data in database.
Examples: INSERT INTO, UPDATE, DELETE statements
INSERT: It is used to insert data into the row of a table.
Syntax : INSERT INTO TABLE_NAME VALUES (value1, value2, .... valueN);
Example : insert into EMPLOYEE(‘ram’,’[email protected]’,’2050-01-01’)

UPDATE: This command is used to update or modify the value of a column in the table.
Syntax : UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]
Example : UPDATE EMPLOYEE SET name = ‘Shyam' WHERE name = ‘ram‘

DELETE: It is used to remove one or more row from a table.


DELETE FROM table_name [WHERE condition];
Example : delete from EMPLOYEE where name=‘ram’
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands - DCL (Data Control Language)


It is used to control access rights. It is used to create roles, permissions, and referential integrity as well it is
used to control access to database by securing it.

Examples: GRANT, REVOKE statements

Grant: It is used to give user access privileges to a database.


Example : GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

Revoke: It is used to take back permissions from the user.


Example : REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands - DQL (Data query language)


A SELECT statement can be single-table (selecting records from one table only) or
multitable (selecting rows from more than one table, usually using some kind of join).

It is comprised of SELECT statements only.

Syntax : SELECT expressions FROM TABLES WHERE conditions;


Example : SELECT emp_name FROM employee WHERE age > 20;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL Commands - TCL (Transactional Control Language)


It is used to manage different transactions occurring within a database.
Examples: COMMIT, ROLLBACK statements

Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example : DELETE FROM CUSTOMERS WHERE AGE = 25; COMMIT;

Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax : ROLLBACK;
Example : DELETE FROM CUSTOMERS WHERE AGE = 25; ROLLBACK;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Select Statement
The SELECT statement is used to select data
from a database.
Apart from information retrieval, this statement also gives query capability. It means that when
select command runs the information present in table will be displayed on the screen.
Syntax: The three common elements of SELECT command is SELECT, FROM and WHERE. These
elements can retrieve information from more than one table. The syntax is:
SELECT <column_list>
FROM <table_list>
WHERE <search_criteria>

Where,
<column_list> defines the list attributes whose value is to be extracted
<table list> defines the list of relation names
<Condition> defines the conditional expression that recognizes the tuple.
Example : SELECT CustomerName, City FROM Customers;
** Tuple: Tuple is a term which refers to a collection of one or more attributes
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

In SQL, basic logical comparison operators are used on the WHERE clause.
Comparison operators and their meanings are
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Clauses of the SELECT statement


• SELECT – the columns in the result set
• FROM – names the base table(s) from which results will be
retrieved
• WHERE – specifies any conditions for the results set (filter)
• ORDER BY – sets how the result set will be ordered
• LIMIT – sets the number of rows to be returned
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Select * FROM Users SELECT first_name, last_name FROM Users
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Subquery of SELECT statement


With the help of WHERE and HAVING commands it is possible to embed a SQL
statement into another. In this situation the query is known as sub query and
the entire select statement is known as nested query.
The structure is:

SELECT “column_name1” FROM “table_name1” WHERE “column_name2”


[Comparison Operator]
(SELECT “column_name3” FROM “table_name2” WHERE [Condition])
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Select Statement-Example’s
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Select Statement-Example’s
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT * FROM Users WHERE status = 'Single';
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT * FROM Users WHERE age > 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the AND Operator
SELECT *FROM Users WHERE status = 'Single' AND age > 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the BETWEEN Operator
SELECT * FROM Users WHERE age BETWEEN 20 AND 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the IN Operator
SELECT * FROM Users WHERE age IN (56,33,10);
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SQL Select ORDER BY Clause
SELECT * FROM Users ORDER BY first_name ASC;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

The percent sign % represents zero, one, or multiple characters


Example :

SELECT * FROM Customers WHERE city LIKE '%L%';


Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Display the employees whose DEPTNO is the same as that of employee 1821 :

Select ENAME, DEPTNO FROM EMP Where DEPTNO = (SELECT DEPTNO FROM EMP WHERE EMPNO =
1821);

** In the example above you can see that the inner query is executed firstly and then the result is followed by the
outer query.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Function on SELECT statement


Subprogram that returns a value is known as functions. SQL supports various aggregate functions shown
below.
(a) Count: COUNT function contains a column name and returns the count of tupple in that column.
COUNT (*) displays all the tuples of the column.

Example: Write a query to List the number of employee in the company from a table employee
SELECT COUNT (*) FROM EMPLOYEE

(b) SUM: SUM function is written with column name and gives the sum of all tupples present in that column.

(c) AVG: AVG function or Average function is written with column name and returns the AVG value of that
column.
(d) MAX: MAX function or Maximum value function written with column name returns the maximum value
present in that column.
(e) MIN: MIN function or Minimum value function written with column name returns the minimum value
present in that column.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Examples of Queries Based on Aggregate Functions Queries

Find the sum of salaries of all the employees and also the minimum, maximum and average
salary.

Solution:
SELECT SUM (E.ESAL) AS SUM_SALARY,
MAX (E.ESAL) AS MAX_SALARY,
MIN (E.ESAL) AS MIN_SALARY,
AVG ([DISTINCT] E.ESAL) AS AVERAGE_SALARY
FROM EMPLOYEE

This query calculates the total, minimum, maximum and average salaries
and also renames the column names.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

GROUP BY on SELECT statement


Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.

Syntax :
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3,...)
Or
INSERT INTO table_name VALUES (value1, value2, value3, ...)
Example :
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway')
Insert Multiple Rows
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country) VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL UPDATE Statement


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

Syntax :
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example :
UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

**WHERE clause that determines how many records will be updated.


Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL DELETE Statement


The DELETE statement is used to delete existing records in a table.
Syntax :
DELETE FROM table_name WHERE condition;
Example :
DELETE FROM Customers WHERE CustomerName='Ram';
To Delete All Records
Syntax :
DELETE FROM table_name;
Example :
DELETE FROM Customers;

Delete a Table we use drop statement


To delete the table completely, use the DROP TABLE statement:
DROP TABLE Customers;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL JOINS
The SQL JOIN joins two tables based on a common column and selects records that have matching values in
these columns.
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Types of SQL JOIN


INNER JOIN: Returns records that have matching values in both tables
LEFT JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT JOIN: Returns all records from the right table, and the matched records from the left table
FULL JOIN: Returns all records when there is a match in either left or right table
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

INNER JOIN
INNER JOIN selects records that have matching
values in both tables as long as the condition
is satisfied.
It returns the combination of all rows from both the
tables where the condition satisfies.

Example :
-- join the Customers and Orders tables with matching fields
customer_id and customer

SELECT Customers.customer_id, Customers.first_name,


Orders.amount FROM Customers INNER JOIN Orders
ON Customers.customer_id = Orders.customer;
Here, the SQL command selects rows from both tables if the
values of customer_id (of the Customers table) and customer
(of the Orders table) are a match.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL LEFT JOIN


The LEFT JOIN keyword returns all records
from the left table (table1), and the matching
records from the right table (table2). The
result is 0 records from the right side, if there
is no match.

Example :
-- left join the Customers and Orders tables

SELECT Customers.customer_id, Customers.first_name,


Orders.amount
FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer;
The result set will contain those rows where there is a match between
customer_id (of the Customers table) and customer (of the Orders table),
along with all the remaining rows from the Customers table.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

SQL FULL OUTER JOIN


The FULL OUTER JOIN keyword returns all
records when there is a match in left (table1) or
right (table2) table records.

Example :
-- full join Customers and Orders tables
-- based on their shared customer_id columns
-- Customers is the left table
-- Orders is the right table

SELECT Customers.customer_id, Customers.first_name,


Orders.item
FROM Customers
FULL OUTER JOIN Orders The result set will contain all rows of both the tables, regardless of whether
ON Customers.customer_id = Orders.customer_id; there is a match between customer_id (of the Customers table) and
customer (of the Orders table).
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Views
A view is a subschema in which logical tables are generated from more than one base table.
View is stored as a query as it does not contain its own data. During the query execution contents are taken from
other tables.

When the table content gets modified or changed then the view will change dynamically

A view is created with the CREATE VIEW statement.

Syntax :
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Views Example
CREATE VIEW employee_view AS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department = 'IT';

Using a View:
SELECT * FROM employee_view;

Modifying a View:

ALTER VIEW view_name AS


SELECT new_columns
FROM new_table
WHERE new_condition;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement

Views Example
CREATE VIEW vwEmployeesByDepartment
AS
SELECT emp.ID,
emp.Name,
emp.Salary,
CAST(emp.DOB AS Date) AS DOB,
emp.Gender,
dept.Name AS DepartmentName
FROM Employee emp
INNER JOIN Department dept
Now execute the following query which will return the data as expected. ON emp.DeptID = dept.ID
SELECT * FROM vwEmployeesByDepartment

You might also like