0% found this document useful (0 votes)
13 views10 pages

Refresher

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)
13 views10 pages

Refresher

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/ 10

CHAPTER 1

ADVANCE SQL

 Created during late 1970s by Donald Chamberlin and Raymond Boyce

SQL

 standard query language referred to as sequel


 non procedural and is used for accessing and manipulating databases

DDL

 data definition language is used for defining the structures of databases or tables
 create, alter, drop, truncate, rename

DML

 data manipulation language is used for managing data within the table object
 select, insert, update, delete, merge, lock table

DCL

 data control language is used to give privileges to access limited data


 grant, revoke, analyze, comment, audit

TCS

 transaction control statement apply changes permanently saved into database

SCS

 session control statement dynamically manage the properties of a user session

REFERRENTIAL INTEGRITY

 relationship between table

FOREIGN KEY

 when a primary key appears in another table

TRANSACTION LOGGING

 are keeping of transactions known as logical records stored in logical logs

JOIN – join two table with common domain, relational operation

EQUI JOIN – known as inner join, returns values that has matches from both tables

NATURAL JOIN – joins tables that has matching column name, attributes and data type then eliminates
duplicated values
OUTER JOIN – opposite of inner join, this does not need any matches just to be included in the result

FULL/UNION JOIN – joins both tables includes all data’s

LEFT JOIN – returns values that is matched with tb1 and tb2 and also all on left table

RIGHT JOIN returns values that is matched with tb1 and tb2 and also all on right table

On delete cascade

 once a row in the parent column is deleted, the matching row from the child table will also be
automatically be deleted
 this works only on foreign key and primary key relations
 syntax: child_column DATA TYPE REFERENCES tbl_of_parent (parent_column);

group by – mag return siyag group rows nga same ug value, for example e count niya tanan e_name if
nay 2 ka emp na same ug name ang return niya adto nga number of row is 2

order by – used to arrange the values either desc or asc

having – mura ra japog where clause maka butang ug condition

SUBQUERY – a query within a query

Non correlated subquery – executed once for the entire outer query

Correlated subquery – executed once for each row for the outer entire query

Function – pre written code, reusable invoked through inserting or selecting or select

Procedure – same with ano function but ma invoke through calling

Trigger – basta trigger ma invoke through insert

PRACTICES

-- DDL

create table

alter table employee add column age VARCHAR(50) REFERENCES employee (eno);

alter table payslipt add column age INT;


alter table employee drop column age;

truncate table employee;

alter table employee rename column ageno to age;

alter table payslip rename to payslipt;

-- DML

select * from employee;

insert into payslipt(age) values (19);

alter table payslipt delete column age;

select e_name from employee

union

select eno from payslipt; -- joins both tables

inner join employee on eno; --

-------------------------------------------------------------------------------------------------

select e_name, COUNT(*) AS nofemployees

from employee

group by e_name

having COUNT(*) > 1; -- less than 2

order by nofemployees ASC; -- retrun niya pila ka employee

CREATE or replace PROCEDURE count_rows()

AS $$

DECLARE

total_rows INT;

BEGIN

-- Count the number of rows in the employee table and store it in total_rows

SELECT COUNT(e_name) into total_rows FROM employee; -- simple count


-- Print the total count of rows

RAISE NOTICE 'Total number of rows in employee table: %', total_rows;

END;

$$ LANGUAGE plpgsql;

call count_rows();

select count(e_name) as nood from employee;

--------------------------------------------------------------------------increment

create sequence emp_id_seq START 1;

CREATE OR REPLACE FUNCTION generate_emp_id()

RETURNS INTEGER AS $$

DECLARE

next_id INTEGER;

BEGIN

SELECT nextval('emp_id_seq') INTO next_id;

RETURN next_id;

END;

$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION generate_emp()

RETURNS INTEGER AS $$

DECLARE

next_id INTEGER;

BEGIN

SELECT nextval('emp_id_seq') INTO next_id;

-- Increment next_id by 1 if it's odd

IF next_id % 2 <> 0 THEN

next_id := next_id + 1;
END IF;

RETURN next_id;

END;

$$ LANGUAGE plpgsql;

insert into employee values (generate_emp(), 'May', NULL);

------------------------------------------------------------------------------------

CREATE OR REPLACE FUNCTION insert_order()

RETURNS TRIGGER AS $$

BEGIN

-- Check if the maximum limit has been reached

IF (SELECT COUNT(*) FROM orders) >= 100 THEN

RAISE EXCEPTION 'Maximum limit reached. Cannot insert more orders.';

END IF;

-- If limit is not reached, proceed with the insertion

RETURN NEW;

END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER before_insert_order

BEFORE INSERT ON orders

FOR EACH ROW

EXECUTE FUNCTION insert_order();

-----------------------------------------------------------------------------

create procedure upd (new_grade INT, s_id INT)

AS $$

BEGIN

update student

set grae = new_grade


where courseid = s_id

END;

$$ LANGUAGE plpgsql

call upd();

select * from employee;

select * from payslipt;

CREATE OR REPLACE PROCEDURE update_manager(

IN employee_id VARCHAR(10),

IN new_manager_id VARCHAR(10))

AS $$

DECLARE

manager_count INT;

BEGIN

SELECT COUNT(*) INTO manager_count

FROM employee

WHERE manager = new_manager_id;

UPDATE employee SET manager = new_manager_id WHERE emp_no = employee_id;

IF manager_count < 5 THEN

RAISE NOTICE 'Each manager should handle at least 5 employees.';

ELSIF manager_count >= 15 THEN

RAISE NOTICE 'Maximum number of employees for this manager reached.';

END IF;
END;

$$ LANGUAGE plpgsql;

Call update_manager ('2021-002', '2022-001');

Call update_manager ('2022-001','2026-001');

Call update_manager ('2022-001','2027-001');

CREATE OR REPLACE FUNCTION generate_empno(date_hired TIMESTAMP)

RETURNS VARCHAR(10)

AS $$

DECLARE

year_hired VARCHAR(4);

seriesno VARCHAR(3);

new_empno VARCHAR(10);

BEGIN

SELECT EXTRACT(YEAR FROM date_hired)::VARCHAR(4) INTO year_hired;

SELECT

RIGHT('0000' || CAST(COUNT(*) + 1 AS VARCHAR), 3) INTO seriesno

FROM

employee

WHERE

SUBSTRING(emp_no FROM 1 FOR 4) = year_hired;

new_empno := year_hired || '-' || seriesno;

RETURN new_empno;
END

$$ LANGUAGE plpgsql;

CREATE INDEX idx_payslip_total_rec

ON payslip (total_rec);

EXPLAIN SELECT

emp_id,

payslip_no,

total_rec

FROM

payslip

WHERE

total_rec > 5000 AND total_rec < 7000;


CHAPTER 2

QUERY OPTS

Edgar Codd

PRACTICES

CREATE INDEX idx_payslip_total_rec

ON payslip (total_rec);

EXPLAIN SELECT

emp_id,

payslip_no,

total_rec

FROM

payslip

WHERE

total_rec > 5000 AND total_rec < 7000;

PREPARE get_employee (text) AS SELECT * FROM employee WHERE emp_name = $1;

EXECUTE get_employee ('Joel');

EXPLAIN SELECT * FROM employee WHERE emp_name = 'Joel';

EXECUTE get_employee ('Robert');

CREATE INDEX idx_employee_manager_id ON employee (manager_id);

SELECT

emp_name,

manager_id

FROM
employee

WHERE

manager_id = 103;

LIMIT

3;

You might also like