RANDOM
https://www.postgresql.org/docs/9.5/static/functions-math.html
select random()* 10
제약조건 변경
제약조건 생성 SQL
- snippet.sql
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (LENGTH(zipcode) = 6);
수정이 바로 되지 않으므로 drop 후에 add 합니다.
- snippet.sql
ALTER TABLE distributors DROP CONSTRAINT zipchk , ADD CONSTRAINT zipchk CHECK (LENGTH(zipcode) = 6);
생성 SQL
- snippet.sql
ALTER TABLE public.event_info_ps DROP CONSTRAINT event_info_ps_ck1 , ADD CONSTRAINT event_info_ps_ck1 CHECK (event_state_type::text = ANY (ARRAY['0001'::CHARACTER VARYING, '0002'::CHARACTER VARYING, '0003'::CHARACTER VARYING]::text[]));
참고
NOW - 현재시각
현재 시각을 출력할 때 now()
함수를 사용한다.
https://www.postgresql.org/docs/9.5/static/functions-formatting.html
포맷 변환
- snippet.sql
SELECT to_char(now(), 'YYYYMMDDHH24MISS');
날짜변환
SELECT now() + interval '-30 days'
https://www.postgresql.org/docs/9.0/static/functions-datetime.html
Postgresql - DBLINK
ERROR - function dblink(unknown, unknown) does not exist
위와 같은 에러가 나오면 CREATE EXTENSION dblink;
구문을 실행합니다.
그 후 접속 테스트를 합니다.
- snippet.sql
SELECT dblink_connect('host=localhost user=postgresuser password=enjoy dbname=postgres');
각각, host, user, password, dbname 을 정확히 입력합니다.
ERROR - a column definition list is required for functions returning "record"
위의 에러가 발생하면, dblink 사용시에 해당 결과를 받을 수 있도록 타입을 모두 지정해 주어야 합니다.
- snippet.sql
SELECT p.* FROM dblink('host=localhost user=postgresuser password=postgrespassword dbname=mydb', 'select code_type from my.common_code') AS p(code_type VARCHAR(10))
참고
외래키 제약조건 설정 쿼리 (Add foreign key constraint to existing table)
http://www.postgresqltutorial.com/postgresql-foreign-key/
- snippet.sql
ALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY (c1) REFERENCES parent_table (p1);
관련 문서
Plugin Backlinks: 아무 것도 없습니다.