Database & SQL Engineering / SQL & RELATIONAL FUNDAMENTALS / SQL-001

SQL Workbench

Explore concept graph

Latihan soal

Latihan langsung dengan psql

File yang bisa dijalankan tersedia di repositori:

  • labs/SQL-001/init.sql — DDL + DML + transaksi dalam schema cs101_sql001.
  • labs/SQL-001/inspect.sql — enam SELECT query untuk memeriksa data dan metadata.
  • labs/SQL-001/verify-postgres.sh — integration proof terhadap disposable PostgreSQL 16.

Dengan database sandbox yang kamu kontrol dan PostgreSQL client terpasang:

psql -X -v ON_ERROR_STOP=1 -d your_sandbox_db -f labs/SQL-001/init.sql
psql -X -v ON_ERROR_STOP=1 -At -d your_sandbox_db -f labs/SQL-001/inspect.sql

Jalankan init.sql dua kali. Periksa bahwa kedua run menghasilkan jumlah row dan foreign key yang sama. Perintah di atas bergantung pada koneksi/akun PostgreSQL milik environment kamu; jangan arahkan ke database production.

Untuk menjalankan proof end-to-end menggunakan Docker lokal:

bash labs/SQL-001/verify-postgres.sh

Script CI tersebut membuat container PostgreSQL 16 terisolasi tanpa port yang dipublikasikan, menjalankan query, mengulang init, memeriksa fresh database kedua, dan menghapus container setelah selesai.

Batas generator: SQL belum didukung oleh runner executable V3 Node/Java. Bundle SQL-001 memakai alasan not executable in generator yang eksplisit. Hasil SQL hanya dinyatakan teruji oleh PostgreSQL integration test terpisah, bukan oleh validator authoring atau fixture palsu.

Konsep 1 / 3

Recognize relational tables, rows, primary keys and foreign keys before issuing queries.

Coba prediksi atau perbaiki

A transfer row stores source_account_id='A-99' but no account with this ID exists. Which database constraint rejects it?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Assuming a text column named account_id automatically enforces referential integrity.

Mengapa tidak bekerja: Names alone do not declare a constraint; the database will accept orphan references unless a foreign key exists.

Coba perbaiki: Declare explicit PRIMARY KEY and REFERENCES constraints, then inspect schema metadata.

Periksa hasil sendiri

Hasil yang diharapkan: Both transfer account IDs point to rows in accounts, and the database metadata shows two foreign keys.

Kalau berbeda: Query information_schema.table_constraints for the schema and inspect the exact DDL rather than relying on diagram notation.

Konsep 2 / 3

Run a bounded DDL/DML/TCL script via psql twice without duplicating sample data.

Coba prediksi atau perbaiki

Why is CREATE TABLE IF NOT EXISTS insufficient to prove a previously created accounts table has the intended columns?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Treating ON CONFLICT DO NOTHING as a general-purpose update or correct schema migration.

Mengapa tidak bekerja: It can silently retain old payloads and does not validate column definitions when an existing table is reused.

Coba perbaiki: Use deterministic fixture IDs and assert exact row/schema outputs; reserve real migrations for later units.

Periksa hasil sendiri

Hasil yang diharapkan: After replaying init.sql, the sandbox still contains exactly two accounts and two transfers.

Kalau berbeda: Inspect schema/search_path and error output; use -v ON_ERROR_STOP=1 so a failed statement aborts the script.

Konsep 3 / 3

Run six bounded SQL inspection queries and classify the observed schema/data results.

Coba prediksi atau perbaiki

The two table names are correct, but transfer count is four after rerunning init.sql. Which property has failed?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Assuming six query strings by themselves are enough evidence of populated data.

Mengapa tidak bekerja: A textual query does not establish a PostgreSQL result; the DB instance, schema, and target data matter.

Coba perbaiki: Execute with psql against a disposable database and capture the returned values.

Periksa hasil sendiri

Hasil yang diharapkan: The unmodified fixture produces schema, tables, accounts, transfers, FK count and exact IDR total observations.

Kalau berbeda: Check current_schema(), schema-qualified table names and psql error status; ensure you are connected to the intended database.

Lima soal inspeksi SQL

Jawab tanpa melihat pembahasan dulu. Kamu bisa memperbaiki jawaban dan mencoba lagi. Kuis ini latihan mandiri dan tidak menyimpan skor atau progres.

Di schema sandbox, transfer.source_account_id harus menunjuk ke accounts.account_id yang ada. Constraint yang tepat?
Query SELECT * FROM accounts menjalankan tabel mana ketika ada sandbox.accounts dan public.accounts?
Apa batas CREATE TABLE IF NOT EXISTS untuk fixture yang bisa dijalankan ulang?
Saat menyisipkan transfer dengan transfer_id duplikat menggunakan ON CONFLICT (transfer_id) DO NOTHING, apa yang terjadi?
Enam query inspeksi sudah ditulis, tetapi belum pernah dijalankan ke PostgreSQL. Klaim bukti yang tepat?