简体   繁体   English

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

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

I have this table Shoe :我有这张桌子Shoe

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

And I would like it to display Small for sizes 32-34, medium for 36 and big for 38 and above.我希望 32-34 码显示小码,36 码显示中码,38 码及以上码显示大码。

How should I change it using SQL?我应该如何使用 SQL 更改它?

I've done我已经搞定了

ALTER TABLE Shoe 
  ADD Size varchar(50)

but I'm not sure how to do a if shoe size >= 32 and <= 34, size = small kind of command.但我不确定如何执行if shoe size >= 32 and <= 34, size = small这种命令。

Is the only way to do it is to add them manually?唯一的方法是手动添加它们吗?

If you just want to view this classification of your data, you could just create a query or view using a CASE expression:如果您只想查看数据的这种分类,您可以使用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;

For Oracle:对于 Oracle:

Query to add computed column:添加计算列的查询:

 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 statements: Select 报表:

 select * from shoe;

Output: Output:

SHOESIZE鞋号 SIZES尺码
32 32 small小的
32 32 small小的
36 36 medium中等的
34 34 small小的
40 40 big大的
32 32 small小的
38 38 big大的

db<>fiddle here db<> 在这里摆弄

If you are using sql server you can add an computed column as below:如果您使用的是 sql 服务器,您可以添加一个计算列,如下所示:

Schema and insert statements:架构和插入语句:

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

Query to add computed column:添加计算列的查询:

  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 from Shoe:来自鞋业的 Select:

 select * from shoe

Output: Output:

shoesize鞋号 size尺寸
32 32 small小的
32 32 small小的
36 36 medium中等的
38 38 big大的
34 34 small小的
40 40 big大的
32 32 small小的

db<>fiddle here db<> 在这里摆弄

If you are using MySql then:如果您使用的是 MySql 那么:

Schema and insert statements:架构和插入语句:

 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);

Query to add computed column:添加计算列的查询:

 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 from shoe: Select 来自鞋子:

 select * from shoe

Output: Output:

shoesize鞋号 size尺寸
32 32 small小的
32 32 small小的
36 36 medium中等的
38 38 big大的
34 34 small小的
40 40 big大的
32 32 small小的

db<>fiddle here db<> 在这里摆弄

As you mentioned ALTER , that's a DDL which modifies table's description .正如您提到的ALTER ,这是一个修改表描述的 DDL。 In that case, you'd use a virtual column.在这种情况下,您将使用虚拟列。

For sample data you posted:对于您发布的样本数据:

SQL> select * from shoe;

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

6 rows selected.

you'd then你会

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.

The result is:结果是:

SQL> select * from shoe;

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

6 rows selected.

SQL>

However, that's probably not the best option you have.但是,这可能不是您拥有的最佳选择。 Why?为什么? Because you might change your mind and decide to modify descriptions.因为您可能会改变主意并决定修改描述。 A simpler/better option would be to create a view:一个更简单/更好的选择是创建一个视图:

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>

Or, yet another option: a detail table (can't reference the shoe table via referential integrity constraint as shoe_size can't be primary nor unique key because it allows duplicates):或者,还有另一种选择:详细表(不能通过引用完整性约束引用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>

Or, using a function which does the job;或者,使用 function 来完成这项工作; though, there's something potentially wrong with such an option - context switching (from SQL to PL/SQL) so performance might suffer if there are a LOT (really a lot) of shoes you're dealing with:但是,这样的选项可能存在一些问题- 上下文切换(从 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>

I think I prefer the view option.我想我更喜欢视图选项。

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

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