SlideShare a Scribd company logo
1 of 27
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.pdfFederico Razzoli
 
SESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdfSESI 2 DATA DEFINITION LANGUAGE.pdf
SESI 2 DATA DEFINITION LANGUAGE.pdfbudiman
 
dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.pptMARasheed3
 
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 releasesGeorgi 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 ModelerDataStax
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQLEDB
 
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
 
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 TableAhmed Elshayeb
 
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 cheatsMikalai Sitsko
 

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

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

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