SlideShare a Scribd company logo
PostgreSQL
author : kang dian
Requirement Tool
• PostgreSQL Database,
• Link https://www.postgresql.org/download/windows/
• Link https://www.enterprisedb.com/download-postgresql-binaries (PostreSQL Prior)
• Pg Admin 4 or Latest
• Link https://www.postgresql.org/ftp/pgadmin/pgadmin4/v4.10/windows/ (Desktop)
Objective
• Peserta diharapkan bisa Create Database
• Mampu melakukan proses DDL (Data Definition Language) & DML (Data
Manipulation Language)
• Mampu melakukan analisa schema desain database, query, subquery, sum,
count, group by, order by.
Roadmap
Create
Database
DDL DML
1. Create Database
• Start Database Postgres ~ Pastikan service postgresql sudah running
• Open PGAdmin
• Create server database.
• Create User baru atau bisa gunakan user existing seperti postgres
• Create Database for schema Human Resource
1. Create Database Server
2.1. Schema Database Human Resource
Notation Entity Relation (ER) Diagram
• Primary Key ditandai dengan tanda *
• Kotak orange primary key, kotak hijau foreign
key
Relasi ke dirinya sendiri, biasa
digunakan untuk query leveling
Satu region memiliki
banyak countries
2.2. How To Create Tables
Urutan Create Table
• Mulai dari table master (parent table)
atau table referensi terlebih dahulu,
contoh:
• Regions > Countries > Locations
• Departments > Employees > Jobs
atau Dependents
• Create table with scripts
2.3. Data Definition Language (DDL)
TABLES
CREATE
ALTER
DROP
ADD
RENAME
DROP
MODIFY
Add new column
Rename column
Drop column
Modify data type or length data type column
Create tables
Drop Table
2.3.1. CREATE TABLE
CREATE TABLE REGIONS(
REGION_ID SERIAL PRIMARY KEY,
REGION_NAME CHARACTER VARYING(25)
)
CREATE SEQUENCE REGION_ID_SEQ START 1;
CREATE TABLE REGIONS(
REGION_ID INT CONSTRAINT REGION_ID_PK PRIMARY KEY DEFAULT
NEXTVAL('REGION_ID_SEQ'),
REGION_NAME CHARACTER VARYING(25)
)
TABLES
CREATE
Method
#1
Method
#2
1
2
Tipe data Serial digunakan
untuk autoincrement value
Kita juga bisa gunakan autoincrement
menggunakan sequence.
Steps :
2.3.2. ALTER TABLE
ALTER TABLE regions ADD COLUMN region_x VARCHAR(25)
ALTER TABLE regions DROP COLUMN region_x
ALTER TABLE regions RENAME COLUMN region_x TO region_xx
ALTER TABLE regions ALTER COLUMN region_xx TYPE
VARCHAR(30)
ALTER TABLE regions ADD CONSTRAINT region_id_pk PRIMARY KEY
(region_id)
ALTER TABLE regions DROP CONSTRAINT region_id_pk
ALTER TABLE country ADD CONSTRAINT country_region_id_fk
FOREIGN KEY (region_id)
REFERENCES regions(region_id);
TABLES
ALTER
ADD
RENAME
DROP
MODIFY
ADD CONSTRAINT
PK
DROP CONSTRAINT
ADD FOREIGN KEY
Table name Column name
New column name
Primary key name
Foreign key name
Master table
2.3.3 DROP TABLE
TABLES
DROP
DROP TABLE regions
DROP TABLE regions CASCADE;
Method
#1
Method
#2
2.3 SCRIPT CREATE TABLE(1)
CREATE TABLE regions (
region_id SERIAL PRIMARY KEY,
region_name VARCHAR (25)
);
CREATE TABLE countries (
country_id CHARACTER (2) PRIMARY KEY,
country_name CHARACTER VARYING (40),
region_id INTEGER NOT NULL,
FOREIGN KEY (region_id) REFERENCES regions (region_id) ON UPDATE CASCADE ON
DELETE CASCADE
);
CREATE TABLE locations (
location_id SERIAL PRIMARY KEY,
street_address CHARACTER VARYING (40),
postal_code CHARACTER VARYING (12),
city CHARACTER VARYING (30) NOT NULL,
state_province VARCHAR(25),
country_id CHARACTER (2) NOT NULL,
FOREIGN KEY (country_id) REFERENCES countries (country_id) ON UPDATE CASCADE
ON DELETE CASCADE
);
CHARACTER VARYING
=
VARCHAR
Jika parent table column value di update,
maka otomatis child table column value
diupdate
Jika parent table column value di delete
child column table value di delete
2.3 SCRIPT CREATE TABLE(2)
CREATE TABLE departments (
department_id SERIAL PRIMARY KEY,
department_name CHARACTER VARYING (30) NOT NULL,
location_id INTEGER,
FOREIGN KEY (location_id) REFERENCES locations (location_id) ON UPDATE C
ASCADE ON DELETE CASCADE
);
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name CHARACTER VARYING (20),
last_name CHARACTER VARYING (25) NOT NULL,
email CHARACTER VARYING (100) NOT NULL,
phone_number CHARACTER VARYING (20),
hire_date DATE NOT NULL,
job_id INTEGER NOT NULL,
salary NUMERIC (8, 2) NOT NULL,
manager_id INTEGER,
department_id INTEGER,
FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON UPDATE CASCADE ON D
ELETE CASCADE,
FOREIGN KEY (department_id) REFERENCES departments (department_id) ON U
PDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (manager_id) REFERENCES employees (employee_id) ON UPDA
TE CASCADE ON DELETE CASCADE
);
2.3. SCRIPT CREATE TABLE(3)
CREATE TABLE dependents (
dependent_id SERIAL PRIMARY KEY,
first_name CHARACTER VARYING (50) NOT NULL,
last_name CHARACTER VARYING (50) NOT NULL,
relationship CHARACTER VARYING (25) NOT NULL,
employee_id INTEGER NOT NULL,
FOREIGN KEY (employee_id) REFERENCES employees (employee_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE jobs (
job_id SERIAL PRIMARY KEY,
job_title CHARACTER VARYING (35) NOT NULL,
min_salary NUMERIC (8, 2),
max_salary NUMERIC (8, 2)
);
3.1. Insert, Update, Delete
TABLES
INSERT
UPDATE
DELETE
insert regions (region_id,region_name) values (5,'Antartic')
update regions
set region_name ='Antartika'
where region_id=5
delete from regions where region_id=5
insert regions (region_name) values ('Antartic')
3.2. SQL JOINS
3.3 Inner Join
select a.region_id,a.region_name,country_name
from regions a, countries b
where a.region_id=b.region_id
and a.region_id=1
order by a.region_id
Method #1 ~ Paralel Execution method #1
Method #2 (Always do this method)
select a.region_id,a.region_name,country_name
from regions a inner join countries b
on a.region_id=b.region_id
where a.region_id=1
order by a.region_id
3.4 Left & Right Join
Step #1
INSERT INTO
employees(employee_id,first_name,last_name,email,phone_number,hire
_date,job_id,salary,manager_id,department_id) VALUES
(99,'xsis','aca','xa@xsis.com','515.123.4567',DATE '1987-06-
17',4,24000.00,NULL,null);
select first_name,last_name,a.department_id,b.department_name
from employees a
left join departments b
on a.department_id= b.department_id
Left Join
select first_name,last_name,a.department_id,a.department_name
from departments a
right join employees b
on a.department_id= b.department_id
Right Join
3.5 Outer Join
Query
SELECT
first_name,
department_name
FROM
employees e
FULL OUTER JOIN departments d
ON d.department_id = e.department_id
WHERE
department_name IS NULL;
3.6 Count
SQL
select manager_id,count(employee_id)
from employees
group by manager_id
Practise
Tampilkan nama manager dan department
name
3.7 Having Sum & Group By
Before
select department_id, sum(salary)salary
from employees
group by department_id
After
select department_id, sum(salary)salary
from employees
group by department_id
having sum(salary) <= 6500
3.8 Like %
Query
select employee_id,first_name,last_name,salary
from employees
where first_name like 'Da%'
3.9 SubQuery
Query
select *
from departments
where location_id in
(select location_id from locations a,countries b
where a.country_id= b.country_id
and b.region_id=1)
3.10. Practise (1)
1. Tampilkan data pegawai seperti dibawah ini :
select first_name,last_name, first_name||’ ‘||last_name fullname, to_char(hire_date, 'DD/MON/YYYY')
hire_date,age(now(),hire_date) masa_kerja
from employees
3.10. Practise (2)
1. Tampilkan data total pegawai, lalu verifikasi apakah data tiap country & department sesuai :
3.10. Create View
Create View
create or replace view total_employee_by_country as
select d.country_name,d.city,a.department_id,b.department_name,count(employee_id)total_employee
from employees a, departments b,
(select a.region_id,region_name,b.country_id,country_name,city,c.location_id
from regions a, countries b, locations c
where a.region_id=b.region_id
and b.country_id=c.country_id)d
where a.department_id = b.department_id
and b.location_id = d.location_id
group by d.country_name,d.city,a.department_id,b.department_name
drop view if exists total_employee_by_country
select * from total_employee_by_country
where city='London'
Select View
Drop View

More Related Content

Similar to database.pptx

Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdf
Federico Razzoli
 
SESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdfSESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdf
budiman
 
dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.ppt
MARasheed3
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Data Con LA
 
New SQL features in latest MySQL releases
New SQL features in latest MySQL releasesNew SQL features in latest MySQL releases
New SQL features in latest MySQL releases
Georgi Sotirov
 
Cassandra Community Webinar | Become a Super Modeler
Cassandra Community Webinar | Become a Super ModelerCassandra Community Webinar | Become a Super Modeler
Cassandra Community Webinar | Become a Super Modeler
DataStax
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQL
EDB
 
Les09
Les09Les09
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
Lab
LabLab
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
Karthick Panneerselvam
 
Database
Database Database
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableProcedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Ahmed Elshayeb
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tablesMind The Firebird
 
SQL: tricks and cheats
SQL: tricks and cheatsSQL: tricks and cheats
SQL: tricks and cheats
Mikalai Sitsko
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
ChryslerPanaguiton
 

Similar to database.pptx (20)

MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdf
 
SESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdfSESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdf
 
dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.ppt
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
 
New SQL features in latest MySQL releases
New SQL features in latest MySQL releasesNew SQL features in latest MySQL releases
New SQL features in latest MySQL releases
 
Cassandra Community Webinar | Become a Super Modeler
Cassandra Community Webinar | Become a Super ModelerCassandra Community Webinar | Become a Super Modeler
Cassandra Community Webinar | Become a Super Modeler
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQL
 
Les09
Les09Les09
Les09
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
Lab
LabLab
Lab
 
DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
 
Database
Database Database
Database
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableProcedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom Table
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 
Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
 
SQL: tricks and cheats
SQL: tricks and cheatsSQL: tricks and cheats
SQL: tricks and cheats
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
 

Recently uploaded

Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 

database.pptx

  • 2. Requirement Tool • PostgreSQL Database, • Link https://www.postgresql.org/download/windows/ • Link https://www.enterprisedb.com/download-postgresql-binaries (PostreSQL Prior) • Pg Admin 4 or Latest • Link https://www.postgresql.org/ftp/pgadmin/pgadmin4/v4.10/windows/ (Desktop)
  • 3. Objective • Peserta diharapkan bisa Create Database • Mampu melakukan proses DDL (Data Definition Language) & DML (Data Manipulation Language) • Mampu melakukan analisa schema desain database, query, subquery, sum, count, group by, order by.
  • 5. 1. Create Database • Start Database Postgres ~ Pastikan service postgresql sudah running • Open PGAdmin • Create server database. • Create User baru atau bisa gunakan user existing seperti postgres • Create Database for schema Human Resource
  • 7. 2.1. Schema Database Human Resource Notation Entity Relation (ER) Diagram • Primary Key ditandai dengan tanda * • Kotak orange primary key, kotak hijau foreign key Relasi ke dirinya sendiri, biasa digunakan untuk query leveling Satu region memiliki banyak countries
  • 8. 2.2. How To Create Tables Urutan Create Table • Mulai dari table master (parent table) atau table referensi terlebih dahulu, contoh: • Regions > Countries > Locations • Departments > Employees > Jobs atau Dependents • Create table with scripts
  • 9. 2.3. Data Definition Language (DDL) TABLES CREATE ALTER DROP ADD RENAME DROP MODIFY Add new column Rename column Drop column Modify data type or length data type column Create tables Drop Table
  • 10. 2.3.1. CREATE TABLE CREATE TABLE REGIONS( REGION_ID SERIAL PRIMARY KEY, REGION_NAME CHARACTER VARYING(25) ) CREATE SEQUENCE REGION_ID_SEQ START 1; CREATE TABLE REGIONS( REGION_ID INT CONSTRAINT REGION_ID_PK PRIMARY KEY DEFAULT NEXTVAL('REGION_ID_SEQ'), REGION_NAME CHARACTER VARYING(25) ) TABLES CREATE Method #1 Method #2 1 2 Tipe data Serial digunakan untuk autoincrement value Kita juga bisa gunakan autoincrement menggunakan sequence. Steps :
  • 11. 2.3.2. ALTER TABLE ALTER TABLE regions ADD COLUMN region_x VARCHAR(25) ALTER TABLE regions DROP COLUMN region_x ALTER TABLE regions RENAME COLUMN region_x TO region_xx ALTER TABLE regions ALTER COLUMN region_xx TYPE VARCHAR(30) ALTER TABLE regions ADD CONSTRAINT region_id_pk PRIMARY KEY (region_id) ALTER TABLE regions DROP CONSTRAINT region_id_pk ALTER TABLE country ADD CONSTRAINT country_region_id_fk FOREIGN KEY (region_id) REFERENCES regions(region_id); TABLES ALTER ADD RENAME DROP MODIFY ADD CONSTRAINT PK DROP CONSTRAINT ADD FOREIGN KEY Table name Column name New column name Primary key name Foreign key name Master table
  • 12. 2.3.3 DROP TABLE TABLES DROP DROP TABLE regions DROP TABLE regions CASCADE; Method #1 Method #2
  • 13. 2.3 SCRIPT CREATE TABLE(1) CREATE TABLE regions ( region_id SERIAL PRIMARY KEY, region_name VARCHAR (25) ); CREATE TABLE countries ( country_id CHARACTER (2) PRIMARY KEY, country_name CHARACTER VARYING (40), region_id INTEGER NOT NULL, FOREIGN KEY (region_id) REFERENCES regions (region_id) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE locations ( location_id SERIAL PRIMARY KEY, street_address CHARACTER VARYING (40), postal_code CHARACTER VARYING (12), city CHARACTER VARYING (30) NOT NULL, state_province VARCHAR(25), country_id CHARACTER (2) NOT NULL, FOREIGN KEY (country_id) REFERENCES countries (country_id) ON UPDATE CASCADE ON DELETE CASCADE ); CHARACTER VARYING = VARCHAR Jika parent table column value di update, maka otomatis child table column value diupdate Jika parent table column value di delete child column table value di delete
  • 14. 2.3 SCRIPT CREATE TABLE(2) CREATE TABLE departments ( department_id SERIAL PRIMARY KEY, department_name CHARACTER VARYING (30) NOT NULL, location_id INTEGER, FOREIGN KEY (location_id) REFERENCES locations (location_id) ON UPDATE C ASCADE ON DELETE CASCADE ); CREATE TABLE employees ( employee_id SERIAL PRIMARY KEY, first_name CHARACTER VARYING (20), last_name CHARACTER VARYING (25) NOT NULL, email CHARACTER VARYING (100) NOT NULL, phone_number CHARACTER VARYING (20), hire_date DATE NOT NULL, job_id INTEGER NOT NULL, salary NUMERIC (8, 2) NOT NULL, manager_id INTEGER, department_id INTEGER, FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON UPDATE CASCADE ON D ELETE CASCADE, FOREIGN KEY (department_id) REFERENCES departments (department_id) ON U PDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (manager_id) REFERENCES employees (employee_id) ON UPDA TE CASCADE ON DELETE CASCADE );
  • 15. 2.3. SCRIPT CREATE TABLE(3) CREATE TABLE dependents ( dependent_id SERIAL PRIMARY KEY, first_name CHARACTER VARYING (50) NOT NULL, last_name CHARACTER VARYING (50) NOT NULL, relationship CHARACTER VARYING (25) NOT NULL, employee_id INTEGER NOT NULL, FOREIGN KEY (employee_id) REFERENCES employees (employee_id) ON DELETE CASCADE ON UPDATE CASCADE ); CREATE TABLE jobs ( job_id SERIAL PRIMARY KEY, job_title CHARACTER VARYING (35) NOT NULL, min_salary NUMERIC (8, 2), max_salary NUMERIC (8, 2) );
  • 16. 3.1. Insert, Update, Delete TABLES INSERT UPDATE DELETE insert regions (region_id,region_name) values (5,'Antartic') update regions set region_name ='Antartika' where region_id=5 delete from regions where region_id=5 insert regions (region_name) values ('Antartic')
  • 18. 3.3 Inner Join select a.region_id,a.region_name,country_name from regions a, countries b where a.region_id=b.region_id and a.region_id=1 order by a.region_id Method #1 ~ Paralel Execution method #1 Method #2 (Always do this method) select a.region_id,a.region_name,country_name from regions a inner join countries b on a.region_id=b.region_id where a.region_id=1 order by a.region_id
  • 19. 3.4 Left & Right Join Step #1 INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire _date,job_id,salary,manager_id,department_id) VALUES (99,'xsis','aca','xa@xsis.com','515.123.4567',DATE '1987-06- 17',4,24000.00,NULL,null); select first_name,last_name,a.department_id,b.department_name from employees a left join departments b on a.department_id= b.department_id Left Join select first_name,last_name,a.department_id,a.department_name from departments a right join employees b on a.department_id= b.department_id Right Join
  • 20. 3.5 Outer Join Query SELECT first_name, department_name FROM employees e FULL OUTER JOIN departments d ON d.department_id = e.department_id WHERE department_name IS NULL;
  • 21. 3.6 Count SQL select manager_id,count(employee_id) from employees group by manager_id Practise Tampilkan nama manager dan department name
  • 22. 3.7 Having Sum & Group By Before select department_id, sum(salary)salary from employees group by department_id After select department_id, sum(salary)salary from employees group by department_id having sum(salary) <= 6500
  • 23. 3.8 Like % Query select employee_id,first_name,last_name,salary from employees where first_name like 'Da%'
  • 24. 3.9 SubQuery Query select * from departments where location_id in (select location_id from locations a,countries b where a.country_id= b.country_id and b.region_id=1)
  • 25. 3.10. Practise (1) 1. Tampilkan data pegawai seperti dibawah ini : select first_name,last_name, first_name||’ ‘||last_name fullname, to_char(hire_date, 'DD/MON/YYYY') hire_date,age(now(),hire_date) masa_kerja from employees
  • 26. 3.10. Practise (2) 1. Tampilkan data total pegawai, lalu verifikasi apakah data tiap country & department sesuai :
  • 27. 3.10. Create View Create View create or replace view total_employee_by_country as select d.country_name,d.city,a.department_id,b.department_name,count(employee_id)total_employee from employees a, departments b, (select a.region_id,region_name,b.country_id,country_name,city,c.location_id from regions a, countries b, locations c where a.region_id=b.region_id and b.country_id=c.country_id)d where a.department_id = b.department_id and b.location_id = d.location_id group by d.country_name,d.city,a.department_id,b.department_name drop view if exists total_employee_by_country select * from total_employee_by_country where city='London' Select View Drop View