SQL。我如何根据其值设置数据不可用并在使用时更新 ir? // 餐厅系统模拟
SQL. How could i set data unavailable depending of its value and update ir if used? // Restaurant system simulation
我是 SQL 的新手,正在构建用于测试和学习的数据库。
我的示例来自一家餐厅,那里有 5 tables:Customer/Table/Order/Sale/dishes
这些列:
CUSTOMER
: 客户ID,表ID。
TABLE
: TableID, OrderID, available(boolean)
Order
:食物编号
Sale
: OrderID/TotalPrice/customerID/TableID
dishes
: foodID/Price
我想做的是:
A table SALE
未清算不能分配给新客户。
无单不能清仓
顾客不能点table道菜中不存在的菜品。
所有订单和销售必须在客户来访当天结算。
我该怎么做?
提前致谢
编辑:
Darwin von Corax 想出了一个完整的解决方案。您可以在答案中看到他的作品,并随时加入聊天。
这是我目前所知道的。
表格:
Table "public.orders"
Column | Type | Modifiers
-----------------+--------------+-----------------------------------------------------
id | integer | not null default nextval('orders_id_seq'::regclass)
discount | numeric(5,2) |
tax | numeric(5,2) |
tip | numeric(5,2) |
amount_tendered | numeric(6,2) |
closed | boolean | default false
party_size | integer |
Indexes:
"order_pk" PRIMARY KEY, btree (id)
Referenced by:
TABLE "order_items" CONSTRAINT "order_item_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
TABLE "tables" CONSTRAINT "table_order_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
Table "public.tables"
Column | Type | Modifiers
-----------+---------+-----------------------------------------------------
id | integer | not null default nextval('tables_id_seq'::regclass)
places | integer |
available | boolean |
order_id | integer |
Indexes:
"table_pk" PRIMARY KEY, btree (id)
"fki_table_order_fk" btree (order_id)
Foreign-key constraints:
"table_order_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
Table "public.order_items"
Column | Type | Modifiers
-----------+---------+---------------------------------------------------------------
order_id | integer | not null
item_id | integer | not null default nextval('order_items_item_id_seq'::regclass)
dish_id | integer |
delivered | boolean |
Indexes:
"ord_item_pk" PRIMARY KEY, btree (order_id, item_id)
Foreign-key constraints:
"order_item_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
"orditem_dish_fk" FOREIGN KEY (dish_id) REFERENCES dishes(id)
Table "public.dishes"
Column | Type | Modifiers
-------------+-------------------------+-----------------------------------------------------
id | integer | not null default nextval('dishes_id_seq'::regclass)
price | numeric(5,2) |
description | character varying(1024) |
Indexes:
"dish_pk" PRIMARY KEY, btree (id)
Referenced by:
TABLE "order_items" CONSTRAINT "orditem_dish_fk" FOREIGN KEY (dish_id) REFERENCES dishes(id)
我还有两个功能:
-- Function: seat_party(integer, integer)
-- DROP FUNCTION seat_party(integer, integer);
CREATE OR REPLACE FUNCTION seat_party(party_size integer DEFAULT 1, preferred_table integer DEFAULT 1)
RETURNS integer AS
$BODY$
DECLARE
assigned_table tables.id%TYPE := NULL;
new_order orders.id%TYPE;
BEGIN
IF ((preferred_table IS NOT NULL) AND (table_is_available(preferred_table, party_size))) THEN
assigned_table := preferred_table;
END IF;
IF (assigned_table IS NULL) THEN
SELECT INTO assigned_table
tables.id
FROM tables
WHERE order_id IS NULL
AND places >= party_size
LIMIT 1;
END IF;
IF (assigned_table IS NOT NULL) THEN
INSERT INTO orders (party_size)
VALUES (party_size)
RETURNING id AS new_order;
UPDATE tables
SET order_id = new_order
WHERE tables.id = assigned_table;
RETURN assigned_table;
ELSE
RETURN NULL;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION seat_party(integer, integer)
OWNER TO dave;
和
-- Function: table_is_available(integer, integer)
-- DROP FUNCTION table_is_available(integer, integer);
CREATE OR REPLACE FUNCTION table_is_available(table_id integer, party_size integer)
RETURNS boolean AS
$BODY$
DECLARE
ord_id tables.order_id%TYPE;
places tables.places%TYPE;
BEGIN
SELECT INTO ord_id, places
tables.order_id
FROM tables
WHERE tables.id = table_id;
RETURN ((avail IS NULL) AND (places >= party_size));
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION table_is_available(integer, integer)
OWNER TO dave;
要完成该解决方案,您将需要接受订单、服务订单、支付账单和结束当天业务的程序。我已经为任何想质疑我的推理或讨论修改或扩展的人创建了一个聊天:Extended discussion
我是 SQL 的新手,正在构建用于测试和学习的数据库。
我的示例来自一家餐厅,那里有 5 tables:Customer/Table/Order/Sale/dishes
这些列:
CUSTOMER
: 客户ID,表ID。TABLE
: TableID, OrderID, available(boolean)Order
:食物编号Sale
: OrderID/TotalPrice/customerID/TableIDdishes
: foodID/Price
我想做的是:
A table
SALE
未清算不能分配给新客户。无单不能清仓
顾客不能点table道菜中不存在的菜品。
所有订单和销售必须在客户来访当天结算。
我该怎么做?
提前致谢
编辑:
Darwin von Corax 想出了一个完整的解决方案。您可以在答案中看到他的作品,并随时加入聊天。
这是我目前所知道的。 表格:
Table "public.orders"
Column | Type | Modifiers
-----------------+--------------+-----------------------------------------------------
id | integer | not null default nextval('orders_id_seq'::regclass)
discount | numeric(5,2) |
tax | numeric(5,2) |
tip | numeric(5,2) |
amount_tendered | numeric(6,2) |
closed | boolean | default false
party_size | integer |
Indexes:
"order_pk" PRIMARY KEY, btree (id)
Referenced by:
TABLE "order_items" CONSTRAINT "order_item_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
TABLE "tables" CONSTRAINT "table_order_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
Table "public.tables"
Column | Type | Modifiers
-----------+---------+-----------------------------------------------------
id | integer | not null default nextval('tables_id_seq'::regclass)
places | integer |
available | boolean |
order_id | integer |
Indexes:
"table_pk" PRIMARY KEY, btree (id)
"fki_table_order_fk" btree (order_id)
Foreign-key constraints:
"table_order_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
Table "public.order_items"
Column | Type | Modifiers
-----------+---------+---------------------------------------------------------------
order_id | integer | not null
item_id | integer | not null default nextval('order_items_item_id_seq'::regclass)
dish_id | integer |
delivered | boolean |
Indexes:
"ord_item_pk" PRIMARY KEY, btree (order_id, item_id)
Foreign-key constraints:
"order_item_fk" FOREIGN KEY (order_id) REFERENCES orders(id)
"orditem_dish_fk" FOREIGN KEY (dish_id) REFERENCES dishes(id)
Table "public.dishes"
Column | Type | Modifiers
-------------+-------------------------+-----------------------------------------------------
id | integer | not null default nextval('dishes_id_seq'::regclass)
price | numeric(5,2) |
description | character varying(1024) |
Indexes:
"dish_pk" PRIMARY KEY, btree (id)
Referenced by:
TABLE "order_items" CONSTRAINT "orditem_dish_fk" FOREIGN KEY (dish_id) REFERENCES dishes(id)
我还有两个功能:
-- Function: seat_party(integer, integer)
-- DROP FUNCTION seat_party(integer, integer);
CREATE OR REPLACE FUNCTION seat_party(party_size integer DEFAULT 1, preferred_table integer DEFAULT 1)
RETURNS integer AS
$BODY$
DECLARE
assigned_table tables.id%TYPE := NULL;
new_order orders.id%TYPE;
BEGIN
IF ((preferred_table IS NOT NULL) AND (table_is_available(preferred_table, party_size))) THEN
assigned_table := preferred_table;
END IF;
IF (assigned_table IS NULL) THEN
SELECT INTO assigned_table
tables.id
FROM tables
WHERE order_id IS NULL
AND places >= party_size
LIMIT 1;
END IF;
IF (assigned_table IS NOT NULL) THEN
INSERT INTO orders (party_size)
VALUES (party_size)
RETURNING id AS new_order;
UPDATE tables
SET order_id = new_order
WHERE tables.id = assigned_table;
RETURN assigned_table;
ELSE
RETURN NULL;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION seat_party(integer, integer)
OWNER TO dave;
和
-- Function: table_is_available(integer, integer)
-- DROP FUNCTION table_is_available(integer, integer);
CREATE OR REPLACE FUNCTION table_is_available(table_id integer, party_size integer)
RETURNS boolean AS
$BODY$
DECLARE
ord_id tables.order_id%TYPE;
places tables.places%TYPE;
BEGIN
SELECT INTO ord_id, places
tables.order_id
FROM tables
WHERE tables.id = table_id;
RETURN ((avail IS NULL) AND (places >= party_size));
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION table_is_available(integer, integer)
OWNER TO dave;
要完成该解决方案,您将需要接受订单、服务订单、支付账单和结束当天业务的程序。我已经为任何想质疑我的推理或讨论修改或扩展的人创建了一个聊天:Extended discussion