Table Command
테이블을 만들기 위한 명령어들을 알아보자.
CREATE TABLE
테이블을 만드는 명령어이다.
(account는 명령어로 사용한 것이 아니라 '계좌'를 영어로 표현한 것이다.)
#account table
create table account(
user_id serial primary key,
username varchar(50) unique not null,
password varchar(50) not null,
email varchar(256) unique not null,
create_on timestamp not null,
last_login timestamp
)
#job table
create table job(
job_id serial primary key,
job_name varchar(200) unique not null
)
#account_job table
create table account_job(
user_id integer REFERENCES account(user_id), #fk
job_id integer REFERENCES job(job_id), #fk
hire_date timestamp
)
UPDATE
Table 내 값을 변경할 수 있다.
- account Table의 last_login값이 현재 시간으로 값이 변경된다.
update account set last_login = current_timestamp
- last_login과 created_on의 값을 동일하게 한다.
update account set last_login = created_on
- 다른 Table로부터 레퍼런스할 수 있다.
account_job의 hire_date값을 account table의 created_on과 같게 만든다.
단, account_job의 user_id와 account의 user_id가 같은 경우에만 적용해보자.
update account_job set hire_date = account.created_on
from account where account_job.user_id = account.user_id
returnig을 사용해서 결과를 바로 확인할 수 있다.
update account set last_login = current_timestamp
returning email, created_on, last_login
DELETE
표의 값을 제거할 수 있다.
insert into job job_name values "doctor";
delete from job where job_name = "doctor" returning job_id, job_name
ALTER TABLE
이미 존재하는 테이블 구조를 바꾸어준다.
add, drop, change name, change data type등이 있다.
create table information(
info_id serial primary key,
title varchar(500) not null,
person varchar(50) not null unique
)
Table 이름과 변수명을 바꾸어보자.
#rename table
alter table information rename to new_info
#rename column
alter table new_info renale column person to people
컬럼에 존재하는 제약 조건을 바꾸어보자.
아래와 같이 입력을 하면 에러가 발생한다. new_info Table에 title은 NOT NULL로 제약조건을 두었기 때문이다.
그러니 값을 입력하거나 NOT NULL 제약 조건을 없애야 한다.
insert into new_info (title) values ("some not null")
제약 조건, NOT NULL을 지우자.
alter table new_info alter column people drop not null
추가로 alter table을 하고 싶다면 문서를 찾아서 바꾸어주면 된다.
DROP
ALTER TABLE과 함께 쓰이고 테이블에서 컬럼을 지운다.
- drop하려는 컬럼에 연결된 인덱스와 제약 조건도 자동으로 삭제한다.
- Check, not null, unique와 같은 제약 조건 등을 삭제한다.
- 비독립적인 다른 지점에 사용되는 컬럼은 제거하지 않는다.
- views, triggers, store precedures
- 이럴 때는 cascade 키워드를 활용해 컬럼 및 연관 관계를 삭제한다.
- views, triggers, store precedures
- 존재하지 않는 컬럼을 DROP하는 에러를 범하지 않기 위해 if exists 키워드를 사용한다.
#drop column
alter table new_info drop column people
#drop columns
alter table table_name
drop column col_one,
drop column col_two
CHECK
특정 조건에 맞춤화한 제약 조건을 줄 수 있다.
Table을 만들 때 check를 사용한다.
create table employees(
emp_id serial primary key,
first_name varchar(50) not null,
last_name varchar(50) not null,
birthdate date check (birthdate > '2000-01-01'),
hire_date date check (hire_date > birthdate),
salary integer check (salary > 0)
)
아래와 같이 입력을 하면 에러가 발생한다.
insert into employees (first_name, last_name, birthdate, hire_date, salary)
values ("Namjin", "Baek", "1998-06-02", "2022-12-13", 200)
'Coding Test' 카테고리의 다른 글
SQL - [IMPORT & EXPORT] (0) | 2022.12.17 |
---|---|
SQL - [CASE, COALESCE, CAST, NULLIF] (0) | 2022.12.13 |
SQL - [DB, Table] (0) | 2022.12.13 |
SQL - [고급 SQL] (2) | 2022.12.07 |
SQL - [AS, JOIN, UNION] (0) | 2022.12.07 |