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

Lecture01 - DML DDL - Day - 1 - 2019 PDF

Uploaded by

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

Lecture01 - DML DDL - Day - 1 - 2019 PDF

Uploaded by

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

PL/SQL

by Jean Baptiste MINANI

Email:[email protected]

WhatsApp: 0788465590
DML(Covered under DBMS)
The data type in DBMS(Oracle):
Data types already Char(size),varchar(size),varchar2(size), number(p,s), date, int,
covered: integer,float
Types for Large objects
BLOB Used to store large binary objects in the database. 8 to 128
terabytes (TB)
CLOB Used to store large blocks of character data in the database. 8 to
128 TB
Timestamp

TIMESTAMP TIMESTAMP data type to store valid date (year, month, day) with
time (hour, minute, second).
CREATE TABLE COMMAND
INSERTION OF DATA INTO TABLES
INSERTION OF DATA INTO TABLES
UPDATE STATEMENT

7
DELETE STATEMENT

8
SELECT STATEMENT
oSQL SELECT Statement is used to viewing data from the table. This
statement return list of table format table data.
oWe use asterisk (*) sign to fetch all table columns instead of write all
column name.
SQL SELECT Statement is used this four type:
 SELECT All Rows and All Columns
 SELECT All Rows and Selected Columns
 Only Selected Rows and All Columns
 Selected Rows and Selected Columns

9 © Pearson Education Limited 1995, 2005


SELECT STATEMENT

10 © Pearson Education Limited 1995, 2005


SELECT STATEMENT

11 © Pearson Education Limited 1995, 2005


SELECT DISTINCT
oSELECT DISTINCT Statement is use for eliminate duplicates rows from
selected column in table. You can add DISTINCT statement in multiple
column:
 SELECT with DISTINCT on all columns
 SELECT with DISTINCT on two columns
 SELECT with DISTINCT on one columns

12 © Pearson Education Limited 1995, 2005


ALTER TABLE Statement
ALTER TABLE Statement to you can do following thing,
o SQL TABLE RENAME
o ADD NEW COLUMN IN TABLE
o MODIFY EXISTING COLUMN IN TABLE
o RENAME COLUMN IN TABLE
o DROP THE EXISTING COLUMN IN TABLE

13 © Pearson Education Limited 1995, 2005


ALTER TABLE Statement

14 © Pearson Education Limited 1995, 2005


WHERE CLAUSE
oWHERE clause is basically use for fetching specific criteria matched
data
oWHERE clause is optionally clause in DML statement. SQL WHERE
clause we can use with SELECT, UPDATE, DELETE statements
oSELECT statement with WHERE clause execute, fetching all table rows
and apply WHERE clause filtering and finally return the result data.

15 © Pearson Education Limited 1995, 2005


16
WHERECLAUSEOPERATORS
OPERATOR DESCRIPTION + EXAMPLE
= Equal to
SELECT * FROM users_info WHERE no = 5;
<>,!= Not equa
SELECT * FROM users_info WHERE no <> 5;
> Greater than
SELECT * FROM users_info WHERE no > 5;
< Less than
SELECT * FROM users_info WHERE no < 5;
>= Greater than or equal
SELECT * FROM users_info WHERE no >= 5;
<= Less than or equal
SELECT * FROM users_info WHERE no <= 5;
BETWEEN(NOT Fetching within range data
BETWEEN) SELECT * FROM users_info WHERE no BETWEEN 1 AND 5;
LIKE(NOT LIKE) Search for a pattern
SELECT * FROM users_info WHERE name LIKE 'Be%';
IN(NOT IN) Allows only specified values
SELECT * FROM users_info WHERE no NOT IN (1,3,5,7,9);
17 © Pearson Education Limited 1995, 2005
WHERE CLAUSE OPERATORS
Logical Operators DESCRIPTION + EXAMPLE
AND AND Condition use to test more then one conditions in INSERT, UPDATE,
DELETE, SELECT statement. AND operator work is test first condition if
true come to a second and so forth, otherwise not check next condition.

OR OR Condition use to test more then one conditions in INSERT, UPDATE,


DELETE, SELECT statement. OR operator test all condition even if
condition TRUE or FALSE. And return data when any one of the condition
TRUE.
SQL OR Operator same as AND operator, return the record base filtered
data. INSERT, UPDATE, DELETE, SELECT statement perform only one of
the specified condition TRUE.
WHERE condition_1 OR condition_2 OR condition_3 ... OR condtion_N;

18 © Pearson Education Limited 1995, 2005


Constraints
Constraints are the rules which are apply to table columns to store valid data and prevents the
user to storing/entering invalid data into table columns.
Constraints are part of a database schema definition.
We can create/define constraints on single or multiple columns of any table. It maintain the data
integrity of the table.

Type of Data constraints


Input/Output constraints : This constraints determines the speed of which data are inserted or
extracted from database table. For example Primary key, Foreign key constraints.
Business Rule constraints : This rules are applied to data prior(first) the data being inserted into
the table columns. For example Unique, Not NULL, Default constraints.
Oracle SQL allows programmers to define constraints at:
 Column level
 Table level

19 © Pearson Education Limited 1995, 2005


Constraints

20 © Pearson Education Limited 1995, 2005


Constraints

21 © Pearson Education Limited 1995, 2005


Constraints

22 © Pearson Education Limited 1995, 2005


Constraints
You can also specifies CONSTRAINT keyword to specify the constraint name.

23 © Pearson Education Limited 1995, 2005


Constraints

24 © Pearson Education Limited 1995, 2005


Constraints

25 © Pearson Education Limited 1995, 2005


Constraints

26 © Pearson Education Limited 1995, 2005


Constraints
You can also specifies CONSTRAINT keyword to specify the constraint name.

27 © Pearson Education Limited 1995, 2005


Constraints

28 © Pearson Education Limited 1995, 2005


Constraints

29 © Pearson Education Limited 1995, 2005


Constraints

30 © Pearson Education Limited 1995, 2005


Constraints

31 © Pearson Education Limited 1995, 2005


Constraints

32 © Pearson Education Limited 1995, 2005


Constraints

33 © Pearson Education Limited 1995, 2005


Constraints

34 © Pearson Education Limited 1995, 2005


Constraints

35 © Pearson Education Limited 1995, 2005


Constraints

36 © Pearson Education Limited 1995, 2005


Constraints

37 © Pearson Education Limited 1995, 2005


SELECT Statement - Aggregates

ISO standard defines five aggregate functions:

COUNT returns number of values in specified column.


SUM returns sum of values in specified column.
AVG returns average of values in specified column.
MIN returns smallest value in specified column.
MAX returns largest value in specified column.

38
SELECT Statement - Aggregates

Each operates on a single column of a table and returns a single


value.
COUNT, MIN, and MAX apply to numeric and non-numeric fields,
but SUM and AVG may be used on numeric fields only.
Apart from COUNT(*), each function eliminates nulls first and
operates only on remaining non-null values.

39
SELECT Statement - Aggregates

COUNT(*) counts all rows of a table, regardless of whether nulls


or duplicate values occur.
Can use DISTINCT before column name to eliminate duplicates.
DISTINCT has no effect with MIN/MAX, but may have with
SUM/AVG.

40
SELECT Statement - Aggregates

Aggregate functions can be used only in SELECT list and in


HAVING clause.

If SELECT list includes an aggregate function and there is no


GROUP BY clause, SELECT list cannot reference a column out
with an aggregate function. For example, the following is illegal:

SELECT staffNo, COUNT(salary)


FROM Staff;

41
Use of COUNT(*)

How many properties cost more than £350 per month to rent?

SELECT COUNT(*) AS myCount


FROM PropertyForRent
WHERE rent > 350;

42
Use of COUNT(DISTINCT)
How many different properties viewed in May ‘04?

SELECT COUNT(DISTINCT propertyNo) AS myCount


FROM Viewing
WHERE viewDate BETWEEN ‘1-May-04’
AND ‘31-May-04’;

© Pearson Education Limited 1995, 2005 43


Use of COUNT and SUM
Find number of Managers and sum of their salaries.

SELECT COUNT(staffNo) AS myCount,


SUM(salary) AS mySum
FROM Staff
WHERE position = ‘Manager’;

44
Use of MIN, MAX, AVG

Find minimum, maximum, and average staff salary.

SELECT MIN(salary) AS myMin,


MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM Staff;

45
SELECT Statement - Grouping

Use GROUP BY clause to get sub-totals.


SELECT and GROUP BY closely integrated: each item in SELECT
list must be single-valued per group, and SELECT clause may only
contain:
column names
aggregate functions
constants
expression involving combinations of the above.

46
SELECT Statement - Grouping
All column names in SELECT list must appear in GROUP BY clause
unless name is used only in an aggregate function.
If WHERE is used with GROUP BY, WHERE is applied first, then
groups are formed from remaining rows satisfying predicate.
ISO considers two nulls to be equal for purposes of GROUP BY.

47
Use of GROUP BY

Find number of staff in each branch and their total salaries.

SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;

48
Restricted Groupings – HAVING clause

HAVING clause is designed for use with GROUP BY to restrict


groups that appear in final result table.
Similar to WHERE, but WHERE filters individual rows whereas
HAVING filters groups.
Column names in HAVING clause must also appear in the GROUP
BY list or be contained within an aggregate function.

49
Use of HAVING

For each branch with more than 1 member of staff, find


number of staff in each branch and sum of their salaries.

SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;

50

You might also like