自定义关系更新:是否需要主键?
Update on custom relation: is primary key required?
UPDATE (
SELECT
o.order_id,
o.shipping_from
FROM
orders o,
items i
WHERE
o.item_id = i.item_id
AND o.shipping_from = 'foot'
AND i.type = 'ent'
) t
SET
t.shipping_from = 'car';
内部 SELECT
查询 returns 来自 orders
的 2 行。整个查询工作正常。 o.order_id
和i.item_id
是主键,o.item_id
是外键,其他列名不匹配。
当我运行以这种方式进行更新时,是否需要在我要更新的关系中包含一个主键?为什么?如果不是,DBMS 怎么知道一行位于另一个 table 中?当然,items
没有 shipping_from
字段,所以我 select 的哪一行并不模糊,但如果它有呢?
一些数据示例:
SELECT * FROM items WHERE type = 'ent';
ITEM_ID ITEM_SERIAL_CODE NAME BRAND TYPE DAILY_COST PURCHASE_DAT
---------- -------------------- -------------------- -------------------- ------------ ---------- ------------
1007 DC00755250 Dragon costume Branded ent 19000 14-DEC. -15
1010 SS01003632 Serpentine streamer Chinese ent 132500 10-MÁRC. -03
SELECT * FROM orders WHERE shipping_from = 'foot';
ORDER_ID ITEM_ID EVENT_ID LIABLE_PERSON SHIPPING_T SHIPPING_F ORDER_COMMENT
---------- ---------- ---------- --------------- ---------- ---------- -----------------------------------
3011 1006 2010 Géza Nagy car foot It will be a great party.
3018 1009 2011 Ferenc Nagy boat foot Multiple celebs expected.
3019 1010 2011 Ferenc Balázs bus foot Changing weather, changing seasons.
3020 1010 2012 Béci Patkó boat foot Bring the stuff to the first floor.
is it reuquired to include a primary key in the relation I want to
update? Why?
是
答案在文档中:24.1.5 DML Statements and Join Views
Updating a Join View
An updatable join view (also referred to as a modifiable join view) is a view that contains multiple tables in the
top-level FROM clause of the SELECT statement, and is not restricted
by the WITH READ ONLY clause.
The rules for updatable join views are shown in the following table.
Views that meet these criteria are said to be inherently updatable.
- General Rule: Any INSERT, UPDATE, or DELETE operation on a join view can modify only one underlying base table at a time.
- UPDATE Rule: All updatable columns of a join view must map to columns of a key-preserved table. See "Key-Preserved Tables" for a discussion of key-preserved tables. If the view is defined with the WITH CHECK OPTION clause, then all join columns and all columns of repeated tables are not updatable.
.......................
在上述文档的上下文中,UPDATE 语句 (UPDATE ( subquery ) SET ...
) 中的子查询被视为 视图 ,就像 UPDATE the_view SET ...
- 因为任何视图都只是一个(子)查询。
我想知道的答案是否定的。投影列表中不需要主键,即使 o.order_id
在子查询中没有 select,UPDATE
的效果也是一样的。然而,这并不意味着主键未被使用,这要归功于声明性技术。无论是否包含主键,都会创建相同的执行计划来获取将要更新的记录:
投影在这里没有太大变化,DBMS 可以在散列连接操作后识别每一行,请参阅 krokodilko 的回答。
如果你没有select来自table的任何东西(比如你输入SELECT 1 FROM ...
),你不能执行更新命令,子查询的结果只是一些数据的正常集合,它不能映射到 table.
我认为我们应该始终包含主键,以便 reader 清楚子查询的结果如何用于内部查询,table 我们计划更新内部查询。
UPDATE (
SELECT
o.order_id,
o.shipping_from
FROM
orders o,
items i
WHERE
o.item_id = i.item_id
AND o.shipping_from = 'foot'
AND i.type = 'ent'
) t
SET
t.shipping_from = 'car';
内部 SELECT
查询 returns 来自 orders
的 2 行。整个查询工作正常。 o.order_id
和i.item_id
是主键,o.item_id
是外键,其他列名不匹配。
当我运行以这种方式进行更新时,是否需要在我要更新的关系中包含一个主键?为什么?如果不是,DBMS 怎么知道一行位于另一个 table 中?当然,items
没有 shipping_from
字段,所以我 select 的哪一行并不模糊,但如果它有呢?
一些数据示例:
SELECT * FROM items WHERE type = 'ent';
ITEM_ID ITEM_SERIAL_CODE NAME BRAND TYPE DAILY_COST PURCHASE_DAT
---------- -------------------- -------------------- -------------------- ------------ ---------- ------------
1007 DC00755250 Dragon costume Branded ent 19000 14-DEC. -15
1010 SS01003632 Serpentine streamer Chinese ent 132500 10-MÁRC. -03
SELECT * FROM orders WHERE shipping_from = 'foot';
ORDER_ID ITEM_ID EVENT_ID LIABLE_PERSON SHIPPING_T SHIPPING_F ORDER_COMMENT
---------- ---------- ---------- --------------- ---------- ---------- -----------------------------------
3011 1006 2010 Géza Nagy car foot It will be a great party.
3018 1009 2011 Ferenc Nagy boat foot Multiple celebs expected.
3019 1010 2011 Ferenc Balázs bus foot Changing weather, changing seasons.
3020 1010 2012 Béci Patkó boat foot Bring the stuff to the first floor.
is it reuquired to include a primary key in the relation I want to update? Why?
是
答案在文档中:24.1.5 DML Statements and Join Views
Updating a Join View
An updatable join view (also referred to as a modifiable join view) is a view that contains multiple tables in the top-level FROM clause of the SELECT statement, and is not restricted by the WITH READ ONLY clause.
The rules for updatable join views are shown in the following table. Views that meet these criteria are said to be inherently updatable.
- General Rule: Any INSERT, UPDATE, or DELETE operation on a join view can modify only one underlying base table at a time.
- UPDATE Rule: All updatable columns of a join view must map to columns of a key-preserved table. See "Key-Preserved Tables" for a discussion of key-preserved tables. If the view is defined with the WITH CHECK OPTION clause, then all join columns and all columns of repeated tables are not updatable.
.......................
在上述文档的上下文中,UPDATE 语句 (UPDATE ( subquery ) SET ...
) 中的子查询被视为 视图 ,就像 UPDATE the_view SET ...
- 因为任何视图都只是一个(子)查询。
我想知道的答案是否定的。投影列表中不需要主键,即使 o.order_id
在子查询中没有 select,UPDATE
的效果也是一样的。然而,这并不意味着主键未被使用,这要归功于声明性技术。无论是否包含主键,都会创建相同的执行计划来获取将要更新的记录:
投影在这里没有太大变化,DBMS 可以在散列连接操作后识别每一行,请参阅 krokodilko 的回答。
如果你没有select来自table的任何东西(比如你输入SELECT 1 FROM ...
),你不能执行更新命令,子查询的结果只是一些数据的正常集合,它不能映射到 table.
我认为我们应该始终包含主键,以便 reader 清楚子查询的结果如何用于内部查询,table 我们计划更新内部查询。