Email          : ali.19arifin@gmail.com
RPubs         : https://rpubs.com/aliciaarifin/
Jurusan      :
Statistika
Address     : ARA Center, Matana University Tower
   Â
         Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua,
Tangerang, Banten 15810.
library(RSQLite)
library(dplyr)
library(dbplyr)
conn <- src_memdb() # create a SQLite database in memory
copy_to(conn,
storms, # this is a dataset built into dplyr
overwrite = TRUE)
tbl(conn, sql("SELECT * FROM storms LIMIT 5"))
{#sql connection= conn_isolated, eval = FALSE} select * FROM storms LIMIT 5
Kita akan melakukan normalisasi data buku online seperti foto di
bawah ini.
Data buku tersebut memuat nama buku,
penulus, tema, penerbit, dan tahun terbit. Dalam menormalisasikan data
kita akan membuat tabel-tabel biasa, dan nantinya akan digabungkan
(joint table). Untuk sekarang kita akan mencoba menormalisasikan table
terlebih dahulu sebelum digabungkan ke dalam joint table. Pertama-tama
kita akan connect RStudio ke Posgree dengan codingan di bawah ini.
# connect R ke Posgree
library(RPostgres)## Warning: package 'RPostgres' was built under R version 4.2.3
Posgree15 <- dbConnect(Postgres(),
user='postgres',
password='bunbun',
dbname='bunbun_prak2',
host='localhost')Setelah itu kita akan membuat table Author (penulis), Genre (tema),
Publisher (penerbit), dan publication Year (tahun terbit). untuk
menambahkan isi tabel, kita akan menggunakan
dbExecute(nama_connect, "INSERT INTO nama_table(kolom1 ..., ....) VALUES(isi_kolom1, ...)"
ketika kita menggunakan codingan ini, kita akan menambahkan satu per
satu, dan codingan ini kalau misalnya di-run 2x akan menyebabkan data
duplikat. sehingga saya sebelumnya sudah menambahkan isinya sebelum file
ini diknit.
Kita akan menggunakan IF NOT EXISTS pada
coding CREATE TABLE agar codingan tersebut tidak error saat
dijalankan. Codingan tersebut untuk membuat table dan jika table
tersebut sudah ada dalam data base, maka codingan tersebut tidak akan
menambahkan lagi table yang sama.
# author
dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS Author(
Author_ID int,
Author varchar(255))")## NOTICE: relation "author" already exists, skipping
## [1] 0
# masukin satu per satu
# dbExecute(Posgree15,"INSERT INTO Author(Author_ID, Author)
# VALUES(2, 'Author Y')")
# Genre
dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS Genre(
Genre_ID int,
Genre varchar(255))")## NOTICE: relation "genre" already exists, skipping
## [1] 0
# dbExecute(Posgree15,"INSERT INTO Genre(Genre_ID, Genre)
# VALUES(3, 'Romance')")
# judul kalau mau
# dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS Title(
# Title_ID int,
# Title varchar(255),
# Author varchar(225))")
# dbExecute(Posgree15,"INSERT INTO Title(Title_ID, Title, Author)
# VALUES(3, 'Book B', 'Author X')")
# penerbit
dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS Publisher(
Publisher_ID int,
Publisher varchar(255))")## NOTICE: relation "publisher" already exists, skipping
## [1] 0
# dbExecute(Posgree15,"INSERT INTO Publisher(Publisher_ID, Publisher)
# VALUES(2, 'Matana')")
# tahun keluar
dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS PublicationYear(
Year_ID int,
Year int)")## NOTICE: relation "publicationyear" already exists, skipping
## [1] 0
# dbExecute(Posgree15,"INSERT INTO PublicationYear(Year_ID, Year)
# VALUES(3, 2023)")
# tabel full
dbExecute(Posgree15, "CREATE TABLE IF NOT EXISTS Books(
BookID int,
Title varchar(255),
Author int,
Genre int,
Publisher int,
PublicationYear int)")## NOTICE: relation "books" already exists, skipping
## [1] 0
# isi tabel full
#dbExecute(Posgree15,"INSERT INTO Books(BookID, Title, Author, Genre, Publisher, #PublicationYear)
# VALUES(1, 'Book A', 1, 1, 1, 1)")
#
#dbExecute(Posgree15,"INSERT INTO Books(BookID, Title, Author, Genre, Publisher, #PublicationYear)
# VALUES(2, 'Book B', 2, 2, 2, 2)")
#
#dbExecute(Posgree15,"INSERT INTO Books(BookID, Title, Author, Genre, Publisher, #PublicationYear)
# VALUES(3, 'Book B', 1, 3, 1, 3)")Setelah kita membuat table dan mengisikan baris/field table sesuai dengan foto contoh soal di atas, kita akan mencoba membuka table yang sudah kita buat.
# cek tabel apa saja yang sudah dimasukkan ke dalam database
dbListTables(Posgree15) ## [1] "Customers" "Categories" "OrderDetails" "Orders"
## [5] "Products" "Shippers" "Suppliers" "hellowwww"
## [9] "persons" "Employees" "genre" "publisher"
## [13] "author" "publicationyear" "books"
# memunculkan data table yang sudah dibuat
dbReadTable(Posgree15, 'persons')## [1] personid lastname firstname address city
## <0 rows> (or 0-length row.names)
dbReadTable(Posgree15, 'genre')## genre_id genre
## 1 1 Fiction
## 2 2 Mystery
## 3 3 Romance
dbReadTable(Posgree15, 'publisher')## publisher_id publisher
## 1 1 dsciencelabs
## 2 2 Matana
dbReadTable(Posgree15, 'author')## author_id author
## 1 1 Author X
## 2 2 Author Y
dbReadTable(Posgree15, 'publicationyear')## year_id year
## 1 1 2021
## 2 2 2022
## 3 3 2023
dbReadTable(Posgree15, 'books')## bookid title author genre publisher publicationyear
## 1 1 Book A 1 1 1 1
## 2 2 Book B 2 2 2 2
## 3 3 Book B 1 3 1 3
Dari hasil table yang dibuat, tabel books adalah tabel yang terbentuk setelah normalisasi data. dari hasil ini nanti kita bisa menggabungkan tabel-tabel yang lain agar bentuknya seperti foto contoh soal di atas.
dbReadTable(Posgree15, 'books')## bookid title author genre publisher publicationyear
## 1 1 Book A 1 1 1 1
## 2 2 Book B 2 2 2 2
## 3 3 Book B 1 3 1 3
# menutup koneksi Posgree
dbDisconnect(Posgree15)Referensi + https://bookdown.org/dsciencelabs/sql_in_r/_book/database-normalization-in-sql.html#your-job-1