繁体   English   中英

oracle:更改表以使用计算列对行进行分类?

[英]oracle: ALTER table to categorise rows with computed column?

我有这张桌子Shoe

Shoe size
---------
32
32
36
38
34
40
32

我希望 32-34 码显示小码,36 码显示中码,38 码及以上码显示大码。

我应该如何使用 SQL 更改它?

我已经搞定了

ALTER TABLE Shoe 
  ADD Size varchar(50)

但我不确定如何执行if shoe size >= 32 and <= 34, size = small这种命令。

唯一的方法是手动添加它们吗?

如果您只想查看数据的这种分类,您可以使用CASE表达式创建查询或视图:

SELECT
    size,
    CASE WHEN size >= 32 AND size < 34 THEN 'small'
         WHEN size >= 34 AND size < 40 THEN 'medium'
         ELSE 'large' END AS label
FROM Shoe;

对于 Oracle:

添加计算列的查询:

 ALTER TABLE shoe ADD  sizes varchar(10) AS (CASE WHEN shoesize in(32,33,34) THEN 'small'
           WHEN shoesize in(35,36,37) THEN 'medium'
           WHEN shoesize>=38 THEN 'big' END ) ;

Select 报表:

 select * from shoe;

Output:

鞋号 尺码
32 小的
32 小的
36 中等的
34 小的
40 大的
32 小的
38 大的

db<> 在这里摆弄

如果您使用的是 sql 服务器,您可以添加一个计算列,如下所示:

架构和插入语句:

 create table shoe(shoesize int)
 
 INSERT INTO shoe(shoesize) VALUES(32),(32),(36),(38),(34),(40),(32);

添加计算列的查询:

  ALTER TABLE shoe ADD size AS (CASE WHEN shoesize in(32,33,34) THEN 'small'
          WHEN shoesize in(35,36,37) THEN 'medium'
          when shoesize>=38 THEN'big' END );

来自鞋业的 Select:

 select * from shoe

Output:

鞋号 尺寸
32 小的
32 小的
36 中等的
38 大的
34 小的
40 大的
32 小的

db<> 在这里摆弄

如果您使用的是 MySql 那么:

架构和插入语句:

 create table shoe(shoesize int);
 
 INSERT INTO shoe VALUES (32);
 INSERT INTO shoe VALUES (32);
 INSERT INTO shoe VALUES (36);
 INSERT INTO shoe VALUES (38);
 INSERT INTO shoe VALUES (34);
 INSERT INTO shoe VALUES (40);
 INSERT INTO shoe VALUES (32);

添加计算列的查询:

 ALTER TABLE shoe ADD COLUMN size VARCHAR(10) GENERATED ALWAYS AS (CASE WHEN shoesize in(32,33,34) THEN 'small'
          WHEN shoesize in(35,36,37) THEN 'medium'
          when shoesize>=38 THEN'big' END );

Select 来自鞋子:

 select * from shoe

Output:

鞋号 尺寸
32 小的
32 小的
36 中等的
38 大的
34 小的
40 大的
32 小的

db<> 在这里摆弄

正如您提到的ALTER ,这是一个修改表描述的 DDL。 在这种情况下,您将使用虚拟列。

对于您发布的样本数据:

SQL> select * from shoe;

 SHOE_SIZE
----------
        32
        36
        38
        34
        40
        32

6 rows selected.

你会

SQL> alter table shoe add
  2    description varchar2(10) generated always as
  3      (case when shoe_size <= 34 then 'small'
  4            when shoe_size  > 34 and shoe_size <= 40 then 'medium'
  5            else 'large'
  6       end)
  7    virtual;

Table altered.

结果是:

SQL> select * from shoe;

 SHOE_SIZE DESCRIPTION
---------- -----------
        32 small
        36 medium
        38 medium
        34 small
        40 medium
        32 small

6 rows selected.

SQL>

但是,这可能不是您拥有的最佳选择。 为什么? 因为您可能会改变主意并决定修改描述。 一个更简单/更好的选择是创建一个视图:

SQL> create or replace view v_shoe as
  2    select shoe_size,
  3      case when shoe_size <= 34 then 'small'
  4           when shoe_size  > 34 and shoe_size <= 40 then 'medium'
  5           else 'large'
  6      end as description
  7  from shoe;

View created.

SQL> select * from v_shoe;

 SHOE_SIZE DESCRIPTIO
---------- ----------
        32 small
        36 medium
        38 medium
        34 small
        40 medium
        32 small

6 rows selected.

SQL>

或者,还有另一种选择:详细表(不能通过引用完整性约束引用shoe表,因为shoe_size不能是主键也不能是唯一键,因为它允许重复):

SQL> select * from shoe_description;

 SHOE_SIZE DESCRIPTION
---------- -----------
        28 extra small
        30 small
        32 small
        34 small
        36 medium
        38 medium
        40 medium
        42 large
        44 large
        46 large

10 rows selected.

SQL>
SQL> select s.shoe_size, d.description
  2  from shoe s join shoe_Description d on d.shoe_size = s.shoe_size;

 SHOE_SIZE DESCRIPTION
---------- -----------
        32 small
        32 small
        34 small
        36 medium
        38 medium
        40 medium

6 rows selected.

SQL>

或者,使用 function 来完成这项工作; 但是,这样的选项可能存在一些问题- 上下文切换(从 SQL 到 PL/SQL),因此如果您正在处理很多(真的很多)鞋子,性能可能会受到影响:

SQL> create or replace function f_shoe_desc(par_shoe_size in number)
  2    return varchar2
  3  is
  4    retval varchar2(20);
  5  begin
  6    retval := case when par_shoe_size <= 34 then 'small'
  7                   when par_shoe_size  > 34 and par_shoe_size <= 40 then 'medium'
  8                   else 'large'
  9              end;
 10    return retval;
 11  end;
 12  /

Function created.

SQL> select shoe_size, f_shoe_desc(shoe_size) description
  2  from shoe;

 SHOE_SIZE DESCRIPTION
---------- -----------
        32 small
        36 medium
        38 medium
        34 small
        40 medium
        32 small

6 rows selected.

SQL>

我想我更喜欢视图选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM