문서 보기역링크PDF로 내보내기맨 위로 이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요. ## RANDOM https://www.postgresql.org/docs/9.5/static/functions-math.html select random()* 10 ## 제약조건 변경 제약조건 생성 SQL ```sql ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (length(zipcode) = 6); ``` 수정이 바로 되지 않으므로 drop 후에 add 합니다. ```sql ALTER TABLE distributors DROP CONSTRAINT zipchk , ADD CONSTRAINT zipchk CHECK (length(zipcode) = 6); ``` 생성 SQL ```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[])); ``` ### 참고 - <http://dba.stackexchange.com/questions/122689/modify-table-constraint-in-postgresql> ## NOW - 현재시각 현재 시각을 출력할 때 `now()` 함수를 사용한다. <https://www.postgresql.org/docs/9.5/static/functions-formatting.html> 포맷 변환 ```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;` 구문을 실행합니다. 그 후 접속 테스트를 합니다. ```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 사용시에 해당 결과를 받을 수 있도록 타입을 모두 지정해 주어야 합니다. ```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)) ``` ### 참고 - <http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html> ## 외래키 제약조건 설정 쿼리 (Add foreign key constraint to existing table) http://www.postgresqltutorial.com/postgresql-foreign-key/ ```sql ALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY (c1) REFERENCES parent_table (p1); ``` open/postgresql-random.txt 마지막으로 수정됨: 2020/06/02 09:25저자 127.0.0.1