Overview

Structured Query Language

  • A declarative language designed to define, manipulate, and control data in a Relational Database (RDB).
  • Now an ANSI/ISO standard → universal across RDBMSs
  • DBs that use SQL: SQL server, PostgreSQL, Oracle, etc
  • versatile
    • Works with APIs, BI tools, analytics, etc.
    • DBMS may extend SQL with Stored Procedures, custom functions, etc.
  • Purpose
    • Manages structured data in tables.
    • Declarative: You say what you want, the db handles how.
    • Easily embedded in programs for data access.
  • Best practices
    • ; at the end
    • ALL CAPS for query commands
  • Dialects

SQL Categories

분류설명주요 키워드
DDL (Data Definition Language)데이터 구조 정의 (CRUD the table structure)CREATE, ALTER, DROP, TRUNCATE
DML (Data Manipulation Language)테이블 내 데이터 조회 및 조작 (CRUD the data)SELECT, INSERT, UPDATE, DELETE
DCL (Data Control Language)사용자 권한 제어GRANT, REVOKE
TCL (Transaction Control Language)트랜잭션 단위 제어COMMIT, ROLLBACK, SAVEPOINT
  • Most important are DDL and DML

Multi-Table Processing

Primary Key

Role

The PRIMARY KEY is assigned to a column (or a combination of columns) that can uniquely identify each row.

  • A single table can have only one PRIMARY KEY.
CREATE TABLE products (
    product_id INTEGER PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);
 
	-- Including a FOREIGN KEY
CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    member_id INTEGER NOT NULL,
    order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (member_id) REFERENCES members(member_id)  -- Referential Integrity
);
  • Declaring a foreign key will cause an error if you try to delete from the referenced table. Consider using the ON DELETE option.
  • When setting up a composite key, you need to consider how it integrates with jpa, such as using @Embeddable and @EmbeddedId.

Composite PRIMARY KEY

  • Two or more columns can be combined to be designated as the primary key.

Types of Constraints

ConstraintDescription
PRIMARY KEYUnique and cannot be NULL.
UNIQUECannot be duplicated.
NOT NULLProhibits NULL input.
DEFAULTSpecifies a default value.
CHECKAllows input only if a condition is met.
FOREIGN KEYForeign key constraint (references another table).