Refresher
Refresher
ADVANCE SQL
SQL
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
TCS
SCS
REFERRENTIAL INTEGRITY
FOREIGN KEY
TRANSACTION LOGGING
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
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
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
PRACTICES
-- DDL
create table
alter table employee add column age VARCHAR(50) REFERENCES employee (eno);
-- DML
union
-------------------------------------------------------------------------------------------------
from employee
group by e_name
AS $$
DECLARE
total_rows INT;
BEGIN
-- Count the number of rows in the employee table and store it in total_rows
END;
$$ LANGUAGE plpgsql;
call count_rows();
--------------------------------------------------------------------------increment
RETURNS INTEGER AS $$
DECLARE
next_id INTEGER;
BEGIN
RETURN next_id;
END;
$$ LANGUAGE plpgsql;
RETURNS INTEGER AS $$
DECLARE
next_id INTEGER;
BEGIN
next_id := next_id + 1;
END IF;
RETURN next_id;
END;
$$ LANGUAGE plpgsql;
------------------------------------------------------------------------------------
RETURNS TRIGGER AS $$
BEGIN
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------
AS $$
BEGIN
update student
END;
$$ LANGUAGE plpgsql
call upd();
IN employee_id VARCHAR(10),
IN new_manager_id VARCHAR(10))
AS $$
DECLARE
manager_count INT;
BEGIN
FROM employee
END IF;
END;
$$ LANGUAGE plpgsql;
RETURNS VARCHAR(10)
AS $$
DECLARE
year_hired VARCHAR(4);
seriesno VARCHAR(3);
new_empno VARCHAR(10);
BEGIN
SELECT
FROM
employee
WHERE
RETURN new_empno;
END
$$ LANGUAGE plpgsql;
ON payslip (total_rec);
EXPLAIN SELECT
emp_id,
payslip_no,
total_rec
FROM
payslip
WHERE
QUERY OPTS
Edgar Codd
PRACTICES
ON payslip (total_rec);
EXPLAIN SELECT
emp_id,
payslip_no,
total_rec
FROM
payslip
WHERE
SELECT
emp_name,
manager_id
FROM
employee
WHERE
manager_id = 103;
LIMIT
3;