DBAtlas · 데이터베이스 운영 아틀라스

뷰 생성 / 수정

Oracle

CREATE OR REPLACE VIEW active_customer AS
SELECT id, name, email FROM customer WHERE status = 'ACTIVE';

-- Drop
DROP VIEW active_customer;

CREATE OR REPLACE VIEW는 생성과 수정을 모두 수행한다. 특정 조건에서 갱신 가능한 뷰에는 WITH CHECK OPTION을 추가할 수 있다.

PostgreSQL

CREATE OR REPLACE VIEW active_customer AS
SELECT id, name, email FROM customer WHERE status = 'ACTIVE';

DROP VIEW active_customer;

CREATE OR REPLACE에서는 새 컬럼을 끝에만 추가할 수 있다. 컬럼 타입/순서를 바꾸려면 먼저 DROP해야 한다. 단순 뷰는 자동으로 갱신 가능하다.

MySQL

CREATE OR REPLACE VIEW active_customer AS
SELECT id, name, email FROM customer WHERE status = 'ACTIVE';

-- Modify
ALTER VIEW active_customer AS SELECT id, name FROM customer;
DROP VIEW active_customer;

정의는 CREATE OR REPLACE VIEW 또는 ALTER VIEW로 갱신한다. 실행 방식은 ALGORITHM = MERGE/TEMPTABLE로 지정할 수 있으며, WITH CHECK OPTION은 갱신 가능한 뷰를 보호한다.

SQL Server

CREATE OR ALTER VIEW active_customer AS
SELECT id, name, email FROM customer WHERE status = 'ACTIVE';

DROP VIEW active_customer;

CREATE OR ALTER VIEW(2016 SP1+)는 단일 문으로 생성/수정한다. 이전 버전에서는 먼저 CREATE한 뒤 ALTER VIEW를 사용한다. 뷰 문은 배치의 첫 문이어야 한다.

MongoDB

// Read-only view (on top of an aggregation pipeline)
db.createView("active_customer", "customer", [ { $match: { status: "ACTIVE" } }, { $project: { name: 1, email: 1 } } ]);

db.active_customer.drop();

db.createView는 소스 컬렉션과 aggregation 파이프라인 위에 읽기 전용 뷰를 생성한다. 뷰에는 쓸 수 없으며, 정의를 바꾸려면 삭제 후 다시 생성한다(또는 collMod 사용).

Redis

# Not applicable: Redis has no view concept; derived views are provided on the application
# side or with RedisJSON/RediSearch indexes.

쿼리 기반으로 정의되는 뷰를 지원하지 않는다. 비슷한 용도에는 미리 계산된 키를 유지하거나 RediSearch로 검색 가능한 인덱스를 구축한다.

Elasticsearch

# Filtered logical view with an alias
POST _aliases
{ "actions": [ { "add": { "index": "customer", "alias": "active_customer",
  "filter": { "term": { "status": "ACTIVE" } } } } ] }

고전적인 SQL 뷰는 없다. 필터링된 alias가 가장 가까운 대안이며 쿼리에 필터를 자동 적용한다. 컬럼 선택은 쿼리에서 _source 필터링을 사용한다.

DBAtlas에서 열기 →