postgresql

Need to create a trigger that increments a value in a table after insertion

孤人 提交于 2021-01-29 02:17:53
问题 I'm currently having a problem with triggers on a current prototype of a game database i'm working on So, I have these two tables CREATE TABLE public.hunters ( id integer NOT NULL, name character varying(30) COLLATE pg_catalog."default" NOT NULL, weapon character varying(30) COLLATE pg_catalog."default" NOT NULL, ranking character varying(30) COLLATE pg_catalog."default" NOT NULL, nhunts integer NOT NULL, sex character(1) COLLATE pg_catalog."default" NOT NULL, title character varying(30)

Need to create a trigger that increments a value in a table after insertion

倾然丶 夕夏残阳落幕 提交于 2021-01-29 02:17:51
问题 I'm currently having a problem with triggers on a current prototype of a game database i'm working on So, I have these two tables CREATE TABLE public.hunters ( id integer NOT NULL, name character varying(30) COLLATE pg_catalog."default" NOT NULL, weapon character varying(30) COLLATE pg_catalog."default" NOT NULL, ranking character varying(30) COLLATE pg_catalog."default" NOT NULL, nhunts integer NOT NULL, sex character(1) COLLATE pg_catalog."default" NOT NULL, title character varying(30)

Postgres selecting current hour data

回眸只為那壹抹淺笑 提交于 2021-01-29 02:06:08
问题 I have a postgres database with a table 'token' and it has 'token_id' and its generated time token_id | generated_time 196618 | 2016-10-15 01:02:48.963 196619 | 2016-10-15 01:02:50.569 196620 | 2016-10-15 01:03:12.931 196621 | 2016-10-15 02:03:17.037 196622 | 2016-10-15 02:22:55.782 196623 | 2016-10-15 02:24:57.477 196624 | 2016-10-15 03:23:00 What I want to do is selecting current hour data from the table for example if current datetime is 2016-10-15 01:30:15 then i want to select all the

Postgres selecting current hour data

寵の児 提交于 2021-01-29 02:03:54
问题 I have a postgres database with a table 'token' and it has 'token_id' and its generated time token_id | generated_time 196618 | 2016-10-15 01:02:48.963 196619 | 2016-10-15 01:02:50.569 196620 | 2016-10-15 01:03:12.931 196621 | 2016-10-15 02:03:17.037 196622 | 2016-10-15 02:22:55.782 196623 | 2016-10-15 02:24:57.477 196624 | 2016-10-15 03:23:00 What I want to do is selecting current hour data from the table for example if current datetime is 2016-10-15 01:30:15 then i want to select all the

Postgres selecting current hour data

你离开我真会死。 提交于 2021-01-29 02:02:06
问题 I have a postgres database with a table 'token' and it has 'token_id' and its generated time token_id | generated_time 196618 | 2016-10-15 01:02:48.963 196619 | 2016-10-15 01:02:50.569 196620 | 2016-10-15 01:03:12.931 196621 | 2016-10-15 02:03:17.037 196622 | 2016-10-15 02:22:55.782 196623 | 2016-10-15 02:24:57.477 196624 | 2016-10-15 03:23:00 What I want to do is selecting current hour data from the table for example if current datetime is 2016-10-15 01:30:15 then i want to select all the

PostgreSQL: Find permission for element, traverse up to root

瘦欲@ 提交于 2021-01-29 01:50:04
问题 Here is the DB Structure I'm using Item ---- ID [PK] Name Desc Links ----- ID [FK] LID [FK] -- Link ID LType -- Link Type (Parent, Alias) Permission ---------- ID [FK] CanRead CanWrite CanDelete Let's assume, we have the below data in the table Item Table ----------- ID Name Desc ================= 0 Root Base Item 1 One First 2 Two Second 3 Three Third 4 Four Forth 5 Five Fifth 6 Six Sixth Links Table ----------- ID LID LType ================== 1 0 Parent 2 0 Parent 3 1 Parent 4 2 Parent 5 4

PostgreSQL: Find permission for element, traverse up to root

≯℡__Kan透↙ 提交于 2021-01-29 01:46:40
问题 Here is the DB Structure I'm using Item ---- ID [PK] Name Desc Links ----- ID [FK] LID [FK] -- Link ID LType -- Link Type (Parent, Alias) Permission ---------- ID [FK] CanRead CanWrite CanDelete Let's assume, we have the below data in the table Item Table ----------- ID Name Desc ================= 0 Root Base Item 1 One First 2 Two Second 3 Three Third 4 Four Forth 5 Five Fifth 6 Six Sixth Links Table ----------- ID LID LType ================== 1 0 Parent 2 0 Parent 3 1 Parent 4 2 Parent 5 4

Alter timezone constraint PostgreSQL

不想你离开。 提交于 2021-01-28 23:07:51
问题 I have an attribute in a table like this: created_date timestamp without timezone Now my requirement is to change the created_date field to the timestamp with timezone on the production database but I don't know how to alter this constraint. 回答1: To change a column type use ALTER TABLE command: ALTER TABLE yourtable ALTER COLUMN column_name TYPE timestamptz; You can change more than 1 column type in one operation by delimiting ALTER COLUMN statements with a comma: ALTER TABLE yourtable ALTER

Get dates of a day of week in a date range

我与影子孤独终老i 提交于 2021-01-28 22:05:36
问题 I need a function in PostgreSQL that accepts a date range and returns the dates inside the date range that are Mondays. Anybody have an idea how this could be done? 回答1: The most efficient way should be to find the first Monday and generate a series in steps of 7 days: CREATE OR REPLACE FUNCTION f_mondays(dr daterange) RETURNS TABLE (day date) AS $func$ SELECT generate_series(a + (8 - EXTRACT(ISODOW FROM a)::int) % 7 , z , interval '7 days')::date FROM ( SELECT CASE WHEN lower_inc(dr) THEN

Get dates of a day of week in a date range

百般思念 提交于 2021-01-28 22:02:11
问题 I need a function in PostgreSQL that accepts a date range and returns the dates inside the date range that are Mondays. Anybody have an idea how this could be done? 回答1: The most efficient way should be to find the first Monday and generate a series in steps of 7 days: CREATE OR REPLACE FUNCTION f_mondays(dr daterange) RETURNS TABLE (day date) AS $func$ SELECT generate_series(a + (8 - EXTRACT(ISODOW FROM a)::int) % 7 , z , interval '7 days')::date FROM ( SELECT CASE WHEN lower_inc(dr) THEN