如何在 sql 中创建派生属性列,它是其他两个现有列的总和
How to create a derived attribute column in sql which is sum of other two existing columns
CREATE TABLE Software
(
name varchar(8) not null,
title varchar(20) not null,
dev_in varchar(8) not null,
scost decimal(7,2),
dcost integer(5),
sold integer(3)
);
我想要一列 selling_price,它是 scost 和 dcost 的总和。
您可以使用 MySQL 为其生成的列。 Reference Doc
-- when creating table
create table software (
name varchar(8) not null,
title varchar(20) not null,
dev_in varchar(8) not null,
scost decimal(7,2),
dcost integer(5),
sold integer(3),
selling_price decimal(7,2) as (dcost + scost)
);
-- or for an existing table
alter table software add column selling_price decimal(7,2) as (dcost + scost);
mysql> insert into software (name, title, dev_in, scost, dcost, sold)
-> values("s", "s", "ss", 100.1, 2, 3);
mysql> select * from software;
+------+-------+--------+--------+-------+------+---------------+
| name | title | dev_in | scost | dcost | sold | selling_price |
+------+-------+--------+--------+-------+------+---------------+
| s | s | ss | 100.10 | 2 | 3 | 102.10 |
+------+-------+--------+--------+-------+------+---------------+
1 row in set (0.00 sec)
CREATE TABLE Software
(
name varchar(8) not null,
title varchar(20) not null,
dev_in varchar(8) not null,
scost decimal(7,2),
dcost integer(5),
sold integer(3)
);
我想要一列 selling_price,它是 scost 和 dcost 的总和。
您可以使用 MySQL 为其生成的列。 Reference Doc
-- when creating table
create table software (
name varchar(8) not null,
title varchar(20) not null,
dev_in varchar(8) not null,
scost decimal(7,2),
dcost integer(5),
sold integer(3),
selling_price decimal(7,2) as (dcost + scost)
);
-- or for an existing table
alter table software add column selling_price decimal(7,2) as (dcost + scost);
mysql> insert into software (name, title, dev_in, scost, dcost, sold)
-> values("s", "s", "ss", 100.1, 2, 3);
mysql> select * from software;
+------+-------+--------+--------+-------+------+---------------+
| name | title | dev_in | scost | dcost | sold | selling_price |
+------+-------+--------+--------+-------+------+---------------+
| s | s | ss | 100.10 | 2 | 3 | 102.10 |
+------+-------+--------+--------+-------+------+---------------+
1 row in set (0.00 sec)