gpt4 book ai didi

mysql - 如何在不提供参数的情况下增加值

转载 作者:可可西里 更新时间:2023-11-01 08:49:00 26 4
gpt4 key购买 nike

如何在创建新 session 时增加 session 和子 session 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
);

表:“ session ”

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;

插入表“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;

创建新 session

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;

创建子 session

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;

将新参与者添加到同一 session

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;

最佳答案

您需要使列类型为 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)
);

关于mysql - 如何在不提供参数的情况下增加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19399895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com