👨‍💻
Dokumentasi Golang (PT Phincon)
  • 🚀Dokumentasi Bootcamp (PT Phincon)
  • Command di Golang
  • Static Type di Golang
  • Variable and Constant
    • Variable daI Constant
    • Deklarasi Variabel
    • Iota (Auto Increment)
    • Blank Identifier (Variable Underscore)
    • Access Modifier (Public or Private)
  • Tipe Data Primitif
    • Integer dan Unsigned Integer
    • Float dan Complex
    • String
    • Boolean
  • Tipe Data Aggregate
    • Array
    • Struct
  • Tipe Data Reference
    • Slice
    • Map
    • Function in Golang
      • Function
      • Function vs Method
      • Function vs Procedure
      • Private vs Public Function
      • Function Init dan Main
      • Function dengan Return Multiple Value
      • Variadic Function
      • Function sebagai Parameter
      • Anonymous Function
      • Input Function dari CLI
      • Exercise (Function)
  • Default Value setiap Tipe Data
  • Type Declaration di Golang
    • Type Declaration
    • Type Declaration Built-In di Golang
  • Kebab Case, Camel Case, Snake Case & Pascal Case di Golang
  • Konversi Tipe Data
  • If-Else dan Switch
    • If Else
    • Switch
    • Exercise
  • Looping
    • Looping For
    • Looping for range
    • Infinite Looping
    • Penerapan Looping pada Sorting
  • Operator di Golang
    • Arithmetic Operator
    • Assignment Operator
    • Relational Operator
    • Logic Operator
  • Interface
  • Interface Kosong atau Any
  • Nil
  • Pointer
    • Pass By Value dan Pass By Reference
    • Pointer (Pass By Reference)
    • Operator Address (& dan *)
    • Default Value Pointer
    • Tricky Case
    • Pointer pada Parameter di Function
    • Pointer pada Method
  • Package
    • Fmt
    • Rand
    • Os
    • Strings
      • To Lower
      • Contains
      • Split
      • Trim
      • Atoi
      • Itoa
      • EqualFold
    • Random Generator
    • Time
      • Get Current Time By Location
      • Time Sleep
      • Time Since
      • Timer & After
      • AfterFunc
      • Ticker & Tick
  • Go dan JSON
    • JSON vs XML
    • Unmarshal vs Marshal
    • Marshal (Go Object -> JSON)
    • Unmarshal (JSON -> Go Object)
    • Streaming Decoder & Encoder
    • Tag
    • JSON Go Return Byte
  • Go dan CSV
    • Insert Data ke File CSV
    • Insert 1.000.000 Data ke File CSV
  • Goroutine
    • Concurrency vs Parrarel
    • Go Routine Sederhana
    • Go Routine vs Synchronous
    • Wait Group
    • Defer
    • Channel
    • Buffered Channel
    • Select Channel
    • Deadlock - All goroutines are asleep
    • Race Condition
    • Mutex (Mutual Exclusion)
    • RW Mutex vs Mutex
    • Once
    • Pool
    • Atomic
    • Go Max Procs
    • Exit
    • Exercise 1 : Go Routine + Context + Channel
    • Exercise 2 : Worker (Go Routine + Channel + Context)
    • Exercise 3 : Random Worker (Go Routine + Channel + Context)
    • Exercise : Implementasi Goroutine dan Channel pada File CSV
  • Go Context
    • Pengenalan Context
    • Context Background & TODO
    • Context With Value
    • Context WithDeadline dan Context WithTimeout
    • Context WithCancel dan Context Done
  • Pengenalan HTTP
  • Go Native HTTP
    • HTTP Server
    • HTTP Server Multi Handler
    • HTTP Server dengan Serve Mux
    • HTTP Response Writer
    • HTTP Test
    • Routing
    • Konsep Middleware
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
    • Hit Endpoint dengan Curl di Terminal
  • Go Gin Framework
    • HTTP Server
    • Router
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
  • Golang dan Database
    • Instalasi MySQL dengan Docker Desktop
    • Instalasi PostgreSQL dengan Docker
    • Basic SQL
    • SQL Join
    • SQL Relation
    • Golang Database Driver
    • Golang dan SQL
  • Go Unit Test
    • Method di Package Testing
    • Package Assert & Require
    • Running Sub Test
    • Table Test
    • Generate Coverage Unit Testing dalam Bentuk HTML
  • Sonar Qube dan Sonar Scanner
  • Logging
  • Golang dan Redis
  • Golang dan RabbitMQ
    • Instalasi RabbitMQ dengan Docker
    • Instalasi Package RabbitMQ di Golang
    • Publisher dan Consumer
    • Publisher dan Multi Consumer
    • Setting RabbitMQ (Durable, Auto Delete dan Ack)
  • Git Command
  • Git Clone dengan SSH
  • Anotasi dan Package di Java Spring Boot
Powered by GitBook
On this page
  • Create Table
  • Alter
  • Select
  • Select Distinct
  • Insert
  • Update
  • Delete
  1. Golang dan Database

Basic SQL

Create Table, Alter Table, Select, Insert, Update, Delete dan SQL Injection

Create Table

Contoh SQL untuk membuat tabel baru bernama pelanggan dan comments di MySQL:

create table pelanggan (
	id int not null auto_increment,
	name varchar(100) not null,
	no_telp varchar(100) not null,
	created_at datetime,
	updated_at datetime,
	deleted_at datetime,
	primary key (id)
)

Sementara itu PostgreSQL memiliki SQL yang sedikit berbeda seperti di bawah ini :

create table pelanggan (
	id serial not null,
	name varchar(100) not null,
	pelanggan varchar(100) not null,
	created_at timestamp,
	updated_at timestamp,
	deleted_at timestamp,
	primary key (id)
)

Alter

Contoh alter untuk menambah kolom :

alter table pelanggan 
add no_telp varchar(100) not null

Contoh alter untuk modifikasi kolom :

alter table pelanggan 
modify column id int not null auto_increment;

Contoh alter untuk menambahkan kolom foreign key bernama id_2 di tabel_A yang nilainya dari kolom id di tabel_B :

alter table table_A
add constaint table_B
foregn key id_2 references table_B(id)

Select

Asumsikan tabel pelanggan telah memiliki data sebagai berikut :

Untuk mendapatkan data kolom id, name dan no_telp dari tabel pelanggan dapat menggunakan query berikut :

select id, name, no_telp from pelanggan

Select Distinct

Untuk mendapatkan data-data yang tidak duplikat dari kolom name tabel pelanggan dapat menggunakan query berikut :

select distinct name from pelanggan;

Untuk mendapatkan data-data yang tidak duplikat dari kolom name dan no_telp tabel pelanggan dapat menggunakan query berikut :

select distinct name, no_telp from pelanggan;

Perlu di ingat, jika misal di kolom name terdapat 20 data distinct (data tidak duplikat) dan kolom no_telp terdapat 30 data distinct. Maka jumlah data yang terpilih akan menjadi 30, menyesuaikan kolom dengan jumlah data distinct terbanyak.

Insert

Untuk insert data baru ke tabel pelanggan dapat menggunakan query berikut :

insert into pelanggan (name, no_telp) values ("andi", "088227867533")

Update

Untuk update data dari tabel pelanggan dapat menggunakan query berikut :

update pelanggan SET name = "umar" , no_telp = "089932943287" where id = 1

Delete

Untuk menghapus data 1 baris dari tabel pelanggan dapat menggunakan query berikut :

delete FROM contact where id = 1

Reference :

PreviousInstalasi PostgreSQL dengan DockerNextSQL Join

Last updated 1 year ago

LogoSQL Tutorial
LogoSQL Tryit Editor v1.6