3

新しい会議が作成されるたびに、会議およびサブ会議 ID の値を増やすにはどうすればよいですか

表:「従業員」

CREATE TABLE employee 
(
  sno SERIAL,
  emp_id INTEGER,
  emp_name TEXT,
  supervisor TEXT,
  designation TEXT,
  department TEXT,
  dob DATE,
  off_ph BIGINT,
  mob_ph BIGINT,
  home_ph BIGINT,
  off_mail TEXT,
  pers_mail TEXT
);

表:「会議」

CREATE TABLE meeting
(
  sno SERIAL NOT NULL,
  meet_id INTEGER,
  submeet_id INTEGER,
  title TEXT,
  agenda TEXT,
  venue TEXT,
  date DATE,
  list TEXT,
  list_of_role TEXT,
  CONSTRAINT meeting_pk PRIMARY KEY (sno)
);

表:「再発」

CREATE TABLE recurrence
(
  sno SERIAL,
  meet_id INTEGER,
  submeet_id INTEGER,
  start_date DATE,
  start_time TIME WITH TIME ZONE,
  end_time TIME WITH TIME ZONE,
  repeat NUMERIC,
  remainder NUMERIC,
  range CHARACTER VARYING (30)
);

表:「出席者」

CREATE TABLE attendee
(
  sno SERIAL,
  meet_id INTEGER,
  submeet_id INTEGER,
  emp_name TEXT,
  status TEXT DEFAULT 'Pending' :: TEXT,
  role TEXT,
  reason TEXT DEFAULT ' ' :: TEXT
 );

表:「アクション」

CREATE TABLE actions
(
  sno SERIAL,
  emp_id INTEGER,
  emp_name TEXT,
  action TEXT,
  action_status TEXT DEFAULT 'Pending' :: TEXT
);

テーブル「従業員」に挿入

CREATE OR REPLACE FUNCTION insert_into_employee(_emp_id INTEGER, _emp_name TEXT, _supervisor TEXT, _designation TEXT, _department TEXT, _dob DATE, _off_ph BIGINT, _mob_ph BIGINT, _home_ph BIGINT, _off_mail TEXT, _pers_mail TEXT)
  RETURNS void AS
$BODY$
BEGIN

    INSERT INTO employee(emp_id,emp_name,supervisor,designation,department,dob,off_ph,mob_ph,home_ph,off_mail,pers_mail)
      VALUES (_emp_id,_emp_name,_supervisor,_designation,_department,_dob,_off_ph,_mob_ph,_home_ph,_off_mail,_pers_mail);


    PERFORM insert_into_actions_and_attendee(_emp_id, _emp_name);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

INSERT INTO TABLES "actions" および "attendee" emp_id および emp_name (この関数は "insert_employee" から呼び出されます)

CREATE OR REPLACE FUNCTION insert_into_actions_and_attendee(_emp_id INTEGER, _emp_name TEXT)
  RETURNS void AS
$BODY$
BEGIN

  INSERT INTO actions (emp_id) VALUES (_emp_id);
  INSERT INTO attendee (emp_name) VALUES (_emp_name);

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

新しい会議を作成するには

CREATE OR REPLACE FUNCTION create_meet(_meet_id INTEGER, _submeet_id INTEGER, _title TEXT, _agenda TEXT, _venue TEXT, _date DATE, _list TEXT, _list_of_role TEXT, _start_date DATE, _start_time TIME WITH TIME ZONE, _end_time TIME WITH TIME ZONE, _repeat NUMERIC, _remainder NUMERIC, _range CHARACTER VARYING(30))
  RETURNS void AS
$BODY$
DECLARE

    _meet_id INTEGER;
    _submeet_id INTEGER := 1;

BEGIN

    _meet_id := ((SELECT MAX (meet_id) FROM meeting));
    _meet_id := coalesce (_meet_id, 0) + 1;

    INSERT INTO meeting(meet_id, submeet_id, title, agenda, venue, date, list, list_of_role)
      VALUES(_meet_id, _submeet_id, _title, _agenda, _venue, _date, _list, _list_of_role);

    WITH cte AS(
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, regexp_split_to_table(_list, ';') AS participant_name, regexp_split_to_table(_list_of_role,';') AS participant_role
      )

    INSERT INTO attendee(meet_id,submeet_id,emp_name, role)
    SELECT meet_id, submeet_id, participant_name, participant_role
    FROM cte
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id AND participant_name IS NOT NULL AND participant_name <> '' AND participant_role IS NOT NULL AND participant_role <> '' GROUP BY meet_id,submeet_id,participant_name,participant_role;

    INSERT INTO recurrence(meet_id, submeet_id, start_date, start_time, end_time, repeat, remainder, range)
    VALUES(_meet_id, _submeet_id, _start_date, _start_time, _end_time, _repeat, _remainder, _range);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

サブミーティングを作成するには

CREATE OR REPLACE FUNCTION create_sub_meet(_meet_id INTEGER, _submeet_id INTEGER, _title TEXT, _agenda TEXT, _venue TEXT, _date DATE, _list TEXT, _list_of_role TEXT, _start_date DATE, _start_time TIME WITH TIME ZONE, _end_time TIME WITH TIME ZONE, _repeat NUMERIC, _remainder NUMERIC, _range CHARACTER VARYING(30))
  RETURNS void AS
$BODY$
DECLARE

    _meet_id INTEGER  := (SELECT MAX(meet_id) FROM meeting);
    _submeet_id INTEGER;

BEGIN

    _submeet_id := ((SELECT MAX (submeet_id) FROM meeting WHERE meet_id=_meet_id));
    _submeet_id := coalesce (_submeet_id, 0) + 1;

    INSERT INTO meeting (meet_id, submeet_id, title, agenda, venue, date, list, list_of_role)
      VALUES (_meet_id, _submeet_id, _title, _agenda, _venue, _date, _list, _list_of_role);

    WITH cte AS (
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, regexp_split_to_table (_list, ';') AS participant_name, regexp_split_to_table (_list_of_role,';') AS participant_role
      )

    INSERT INTO attendee (meet_id, submeet_id, emp_name, role)
    SELECT meet_id, submeet_id, participant_name, participant_role
    FROM cte
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id AND participant_name IS NOT NULL AND participant_name <> '' AND participant_role IS NOT NULL AND participant_role <> '' GROUP BY meet_id,submeet_id,participant_name,participant_role;

    INSERT INTO recurrence (meet_id, submeet_id, start_date, start_time, end_time, repeat, remainder, range)
    VALUES (_meet_id, _submeet_id, _start_date, _start_time, _end_time, _repeat, _remainder, _range);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

新しい参加者を同じミーティングに追加するには

CREATE OR REPLACE FUNCTION add_participant_to_same_meet(_meet_id INTEGER, _submeet_id INTEGER, _emp_id INTEGER, _emp_name TEXT, _role TEXT, _action TEXT)
  RETURNS void AS
$BODY$
BEGIN
    UPDATE meeting SET list=CONCAT (list, _emp_name) WHERE meet_id=_meet_id AND submeet_id=_submeet_id;
    UPDATE meeting SET list_of_role=CONCAT (list_of_role, _role) WHERE meet_id=_meet_id AND submeet_id=_submeet_id;

    WITH abc AS (
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, _emp_name AS emp_name, _role AS role, _action AS action)

    INSERT INTO attendee (meet_id, submeet_id, emp_name, role)
    SELECT meet_id, submeet_id, emp_name, role
    FROM abc
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id;


    WITH def AS (
       SELECT _emp_id AS emp_id, _emp_name AS emp_name, _action AS action)

    INSERT INTO actions(emp_id, emp_name, action)
    SELECT emp_id, emp_name, action
    FROM def
    WHERE emp_id=_emp_id;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
4

2 に答える 2

1

列のタイプをSERIALにする必要があります。これは、postgresの自動インクリメント整数フィールドです。

SQLFiddleの例を次に示します。

CREATE TABLE meeting
(
  sno SERIAL NOT NULL,
  meet_id serial, --note serial rather than int
  submeet_id serial, --note serial rather than int
  title TEXT,
  agenda TEXT,
  venue TEXT,
  date DATE,
  list TEXT,
  list_of_role TEXT,
  CONSTRAINT meeting_pk PRIMARY KEY (sno)
);
于 2013-10-16T10:19:30.163 に答える
0

私はあなたの質問を理解するべきではありません.完全に説明されていないようですが、私はポケットテレパシーといくつかの議論を行っており、トリガーを使用するようアドバイスすることにしました. トリガーは、レコードの挿入、削除、更新などのイベントに反応し、操作の実行前または実行後にいくつかのアクションを実行できます。役に立てば幸いですが、信じられません。繰り返しますが、私はあなたの質問を完全には理解できませんでした。

于 2013-10-16T10:16:05.050 に答える