PostgreSQL-기초 문법 총정리

개미는뚠뚠·2023년 11월 26일
0

PostgerSQL

목록 보기
4/4
post-thumbnail

🎈목차

11월 26일

1-1. 테이블 생성(create)
1-2. 테이블 삭제(drop)
1-3. 데이터 삽입(insert)
1-4. 데이터 제거(delete)
1-5. 데이터 수정(update)

2-1. 데이터 조회1(select)
2-2. 데이터 조회2(where)
2-3. 날짜 비교1(>, =, <)
2-4. 날짜 비교2(function)
2-5. 날짜 비교3(to_char, to_timestamp)

12월 03일

3-1. 테이블 합치기 (join)
3-2. 조건문 (case, when-then, end)
3-3. 반복문 (in)
3-4. 날짜 비교4(between, and)


🎈1

1-1. 테이블 생성(create)

create table public.dh_test 
    (
    idx SERIAL PRIMARY KEY,
    user_id VARCHAR(255) NOT NULL,
    user_pw VARCHAR(255) NOT NULL,
    user_name VARCHAR(255) NOT NULL,
    phone_number VARCHAR(20),
    first_date DATE,
    last_update TIMESTAMP
);

1-2. 테이블 삭제(drop)

drop table public.dh_test ;

1-3. 데이터 삽입(insert)

INSERT INTO public.dh_test 
(
	idx, 
    user_id,
    user_pw, 
    user_name, 
    phone_number, 
    first_date, 
    last_update
)
VALUES
(
	1, 
	'abc123', 
    '123456', 
    'John Doe', 
    '010-1234-5678', 
    '2021-01-01', 
    '2023-11-26'
);

1-4. 데이터 제거(delete)

delete from public.dh_test 
where 
	user_id = 'abc123'

1-5. 데이터 수정(update)

update public.dh_test
set
	user_id = 'eogh773',
	phone_number = '010-3633-3190'
where 
	user_id = 'abc123'

🎈2

2-1. 데이터 조회1(select)

select * from public.dh_test;
select
	idx,
	user_id,
	user_pw,
	user_name,
	phone_number,
	first_date,
	last_update
from
	public.dh_test;

2-2. 데이터 조회2(where)

select * from public.dh_test where 1=1;

where 1=1 이후 and ...; 을 통해 이후 쿼리문에 조건을 걸거나 쿼리를 수정할 때 편리하다.

2-3. 날짜비교1(>, =, <)

select
	*
from
	dh_test
where
	last_update <= '2023-11-26';

'> = <' 등호, 부등호를 사용한 날짜 비교

select
	*
from
	dh_test
where
	first_date  = DATE'2021-02-15';

DATE을 사용한 날짜 비교

select
	*
from
	dh_test
where
	first_date between '2021-02-15' and '2021-05-15';

BETWEEEN 을 사용한 날짜 비교(범위 지정)

select
	*
from
	dh_test
where
	last_update = TIMESTAMP '2023-11-26 02:00:00';

TIMESTAMP을 사용한 날짜 비교

2-4. 날짜비교2(function)

-- 오늘
	select current_date; 
-- 현재시각
	select current_timestamp;
-- 어제/오늘/내일
    current_date - 1, 
    current_date / current_date - 0,  
    current_date + 1
-- 두 날짜 사이의 날수
	select '2023-11-24'::date - '2023-11-26'::date;

postgreSql 제공 함수

select
	*
from
	dh_test
where
	extract(month from first_date) = 03;

extract 함수를 통하면 year/month/day/이 외에도 시,분,초도 날짜비교가 가능하다.

2-5. 날짜 비교3(to_char, to_timestamp)

2-5-1. select 부분에 to_char를 이용하여 원하는 날짜형식 출력

SELECT 
	idx,
	user_id,
	user_name,
	to_char(first_date, 'YY-MM-DD') 
FROM dh_test

2-5-2. to_timestamp 활용 날짜 형식 출력

select
	*
from
	dh_test
where
	first_date <= to_timestamp('20210310','YYYYMMDD') ;
select
	*
from
	dh_test
where
	first_date <= to_timestamp('2021-03-10','YYYY-MM-DD') ;

🎈3

3-1. 테이블 합치기 (join)

3-1-1. join을 통해 합칠 테이블을 지정하고 on을 통해 조건을 지정한다.
3-1-2. as를 통해 합칠 테이블들을 특정 변수로 지정하고 표출할 컬럼 앞에 사용한다.
3-1-3. join의 종류는 여러가지가 있는데 자세한 내용은 아래 이미지를 참고한다.

select 
	main.idx,
	main.user_id,
	main.user_name,
	sub.cash,
	sub.point,
	main.first_Date,
	main.last_update
from 
	public.main_table as main
join 
	public.sub_table as sub
on
	main.user_id = sub.user_id;

3-2. 조건문 (case, when-then, end)

3-2-1. case를 통해 조건문 시작을 알린다.
3-2-2. when을 선언하여 컬럼과 조건을 작성하고, then을 통해 표출할 결과 값을 작성한다.
3-2-3. else를 통해 조건문 마침을 알리고, as를 통해 결과값을 표출할 컬럼을 나타낸다.

select
	case 
		when usr_info.user_id = 'abc123' then 'red_team'
		when usr_info.user_id = 'jkl012' then 'blue_team'
		when usr_info.user_id = 'stu901' then 'green_team'
		else 'yellow_team'
	end as team,
	main.user_id,
	main.user_name,
	sub.cash,
	sub.point,
	case 
		when sub.cash < '20000' then '0'
		else '1'
	end as buy
from
	public.main_table as main 
inner join 
	public.sub_table as sub
on
	main.user_id = sub.user_id;

3-3. 반복문 (in)

in ('조회내용1', '조회내용2', '조회내용3') 형식을 통해 포함되는 내용을 전부 조회할 수 있다.

select 
	idx,
	user_id,
	user_name,
	first_date,
	last_update
from 
	public.main_table 
where 
	user_id in ('abc123','def456','ghi789')

3-4. 날짜 비교4(between, and)

where문에 비교하려는 날짜 컬럼 between 범위1 and 범위2 형식으로 작성(나는 to_date로 자주 사용)

select
	idx,
	user_id,
	user_pw,
	user_name,
	phone_number,
	first_date,
	last_update
from
	public.main_table
where
	first_date 
    	between TO_DATE('2021-02-01','YYYY-MM-DD') 
		and TO_DATE('2021-05-28','YYYY-MM-DD');

0개의 댓글