sqlite 生成的列依赖表达式
sqlite generated column dependent expression
我遇到过这种情况,我打算插入一个生成的列,其中表达式(要设计的表达式在 (???..我也需要这个建议表达式..???) 中引用 table 下面。输出是一个计算,取决于 broker_id 的值和生成的列名 brokage 的位置。
非常感谢您对场景的分享和指导。
谢谢您,并致以诚挚的问候
新的 sqlite 学习者。
sqlite> .schema buy
CREATE TABLE buy(
buy_id INTEGER,
stock_id INTEGER,
investor_id INTEGER,
broker_id INTEGER,
unit INTEGER,
price REAL,
date TEXT,
cost REAL GENERATED ALWAYS AS (unit*price),
brokage REAL GENERATED ALWAYS AS (??? depend on broker_id for expression ???),
PRIMARY KEY (buy_id, stock_id, investor_id, broker_id),
FOREIGN KEY (stock_id) REFERENCES stock(stock_id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (investor_id) REFERENCES stock(investor_id) ON DELETE CASCADE ON UPDATE NO ACTION
FOREIGN KEY (broker_id) REFERENCES stock(broker_id) ON DELETE CASCADE ON UPDATE NO ACTION
);
sqlite> .mode column
sqlite> .header on
sqlite> SELECT * FROM buy;
buy_id stock_id investor_id broker_id unit price date cost brokage
------ -------- ----------- --------- ----- ----- ---------- ------ --------
1 7 1 1 2000 0.68 2020-06-24 1360.0
2 25 1 3 2000 0.88 2020-10-22 1760.0
您可以使用 CASE 表达式来完成:
CREATE TABLE buy(
buy_id INTEGER,
stock_id INTEGER,
investor_id INTEGER,
broker_id INTEGER,
unit INTEGER,
price REAL,
date TEXT,
cost REAL GENERATED ALWAYS AS (unit*price),
brokage REAL GENERATED ALWAYS AS (CASE broker_id WHEN 1 THEN 9 WHEN 3 THEN 28 END),
PRIMARY KEY (buy_id, stock_id, investor_id, broker_id),
FOREIGN KEY (stock_id) REFERENCES stock(stock_id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (investor_id) REFERENCES stock(investor_id) ON DELETE CASCADE ON UPDATE NO ACTION
FOREIGN KEY (broker_id) REFERENCES stock(broker_id) ON DELETE CASCADE ON UPDATE NO ACTION
);
我遇到过这种情况,我打算插入一个生成的列,其中表达式(要设计的表达式在 (???..我也需要这个建议表达式..???) 中引用 table 下面。输出是一个计算,取决于 broker_id 的值和生成的列名 brokage 的位置。
非常感谢您对场景的分享和指导。
谢谢您,并致以诚挚的问候
新的 sqlite 学习者。
sqlite> .schema buy
CREATE TABLE buy(
buy_id INTEGER,
stock_id INTEGER,
investor_id INTEGER,
broker_id INTEGER,
unit INTEGER,
price REAL,
date TEXT,
cost REAL GENERATED ALWAYS AS (unit*price),
brokage REAL GENERATED ALWAYS AS (??? depend on broker_id for expression ???),
PRIMARY KEY (buy_id, stock_id, investor_id, broker_id),
FOREIGN KEY (stock_id) REFERENCES stock(stock_id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (investor_id) REFERENCES stock(investor_id) ON DELETE CASCADE ON UPDATE NO ACTION
FOREIGN KEY (broker_id) REFERENCES stock(broker_id) ON DELETE CASCADE ON UPDATE NO ACTION
);
sqlite> .mode column
sqlite> .header on
sqlite> SELECT * FROM buy;
buy_id stock_id investor_id broker_id unit price date cost brokage
------ -------- ----------- --------- ----- ----- ---------- ------ --------
1 7 1 1 2000 0.68 2020-06-24 1360.0
2 25 1 3 2000 0.88 2020-10-22 1760.0
您可以使用 CASE 表达式来完成:
CREATE TABLE buy(
buy_id INTEGER,
stock_id INTEGER,
investor_id INTEGER,
broker_id INTEGER,
unit INTEGER,
price REAL,
date TEXT,
cost REAL GENERATED ALWAYS AS (unit*price),
brokage REAL GENERATED ALWAYS AS (CASE broker_id WHEN 1 THEN 9 WHEN 3 THEN 28 END),
PRIMARY KEY (buy_id, stock_id, investor_id, broker_id),
FOREIGN KEY (stock_id) REFERENCES stock(stock_id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (investor_id) REFERENCES stock(investor_id) ON DELETE CASCADE ON UPDATE NO ACTION
FOREIGN KEY (broker_id) REFERENCES stock(broker_id) ON DELETE CASCADE ON UPDATE NO ACTION
);