> Tesilio's Blog
기록하는 공간
기록하는 공간
나쁜 예:
'FirstName'혹은"All Employees"
나쁜 예:
text혹은timestamp
_)를 사용하여 단어를 분할한다.좋은 예:
word_count혹은team_member_id나쁜 예:wordcount혹은wordCount
snake_case.몇몇 긴 단어에서는 그 단어 자체보다 약어가 흔히 사용된다.
- "Internationalization" and "localization" 좋은 예:
Internationalization>i18n좋은 예:localization>l10n
하지만 약어의 의미전달이 의심스럽다면 전체 단어를 사용한다.
나쁜 예:
user,lock,table등.
데이터를 보유하는 테이블, 뷰 및 기타 객체는 복수가 아니라 단수형이어야 한다.
좋은 예:
team나쁜 예:teams
자바의 클래스나 파이썬의 변수명등으로 쉽게 변환된다.
id 로 한다.CREATE TABLE person (
id bigint PRIMARY KEY,
full_name text NOT NULL,
birth_date date NOT NULL);
CREATE TABLE team_member (
team_id bigint NOT NULL REFERENCES team(id),
person_id bigint NOT NULL REFERENCES person(id),
CONSTRAINT team_member_pkey PRIMARY KEY (team_id, person_id));
인덱스명은 명시적으로 지정해줘야 하며, 테이블명과 컬럼명을 모두 포함해야 한다.
CREATE TABLE person (
id bigserial PRIMARY KEY,
email text NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
CONSTRAINT person_ck_email_lower_case CHECK (email = LOWER(email)));
CREATE INDEX person_ix_first_name_last_name ON person (first_name, last_name);
이름과 성에 대한 index person_ix_first_name_last_name가 사용 된다.
=# EXPLAIN SELECT * FROM person WHERE first_name = 'alice' AND last_name = 'smith';
QUERY PLAN
----------------------------------------------------------------------------------------------
Index Scan using person_ix_first_name_last_name on person (cost=0.15..8.17 rows=1 width=72)
Index Cond: ((first_name = 'alice'::text) AND (last_name = 'smith'::text))
(2 rows)
제약조건에 대한 체크나 디버깅시 용이성을 위해 설명을 지정한다.
CREATE TABLE team (
id bigserial PRIMARY KEY,
name text NOT NULL);
CREATE TABLE team_member (
team_id bigint REFERENCES team(id),
person_id bigint REFERENCES person(id),
CONSTRAINT team_member_pkey PRIMARY KEY (team_id, person_id));
PostgreSQL에서 외래키 작업 제한 설명 이름 지정
=# \d team_member
Table "public.team_member"
Column | Type | Modifiers
-----------+--------+-----------
team_id | bigint | not null
person_id | bigint | not null
Indexes:
"team_member_pkey" PRIMARY KEY, btree (team_id, person_id)
Foreign-key constraints:
"team_member_person_id_fkey" FOREIGN KEY (person_id) REFERENCES person(id)
"team_member_team_id_fkey" FOREIGN KEY (team_id) REFERENCES team(id)
제약 조건을 위반하는 행을 삽입하려고 시도할 경우 제약조건 이름으로 파악하기 쉽다.
INSERT INTO team_member(team_id, person_id) VALUES (1234, 5678);
ERROR: insert or update on table "team_member" violates foreign key constraint "team_member_team_id_fkey"
DETAIL: Key (team_id)=(1234) is not present in table "team".
마찬가지로, 3.1. 에서 생성한 person 테이블에 새로운 행을 추가할 때, 무엇이 잘못되었는지 알려주는 제약 조건 위반 오류가 표시된다.
-- This insert will work:
=> INSERT INTO person (email, first_name, last_name) VALUES ('alice@example.com', 'Alice', 'Anderson');
INSERT 0 1
-- This insert will not work:
=> INSERT INTO person (email, first_name, last_name) VALUES ('bob@EXAMPLE.com', 'Bob', 'Barker');
ERROR: new row for relation "person" violates check constraint "person_ck_email_lower_case"
DETAIL: Failing row contains (2, bob@EXAMPLE.com, Bob, Barker).
잘못된 명명 규칙보다 더 나쁜 것은 다중 명명 규칙이다. 기존에 사용하던 방식이 있다면 계속 사용해야 한다.