Oracle 将 table 转换为 interval day(3) to second

Oracle convert table to interval day(3) to second

我有以下 table 定义,其中的数据创建良好,但我想将其转换为更通用的格式,但我遇到了问题。有人能给我指出正确的方向吗


CREATE  TABLE partition_retention
(
  seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
   TABLE_NAME VARCHAR2(30),
DAYS NUMBER(6),
CONSTRAINT
partition_retention_pk primary key (table_name)); 
/

INSERT into partition_retention(TABLE_NAME, DAYS) 
        WITH data as (
          select 'T1', 0
from dual union all
         select 'T3', 15
from dual union all
        select 'T4', 10
from dual union all
       select 'T5', 5
from dual)
        SELECT * from data;
/

-- having problem creating 

CREATE TABLE PARTITION_RETENTION AS (
    TABLE_NAME AS VARCHAR2(30)
    RETENTION DAY AS INTERVAL DAY(3) TO SECOND(0) 
);

三个AS关键字无效(create table .. as select ..有效,但你没有这样做);你少了一个逗号;并且您有一个不带引号的列名称 space.

纠正那些事情,这有效:

CREATE TABLE PARTITION_RETENTION (
    TABLE_NAME VARCHAR2(30),
    RETENTION_DAY INTERVAL DAY(3) TO SECOND(0) 
);

您的插入将必须插入 interval 值,显然不是简单的数字。

db<>fiddle

how can I add a CONSTRAINT on the table to ensure the day>0 and the time is always 0

您可以添加单独的约束来检查这两项:

CREATE TABLE PARTITION_RETENTION (
    TABLE_NAME VARCHAR2(30),
    RETENTION_DAY INTERVAL DAY(3) TO SECOND(0),
    CONSTRAINT CHK_NON_ZERO_DAYS CHECK (
        RETENTION_DAY > INTERVAL '0' DAY
    ),
    CONSTRAINT CHK_WHOLE_DAYS CHECK (
        EXTRACT(HOUR FROM RETENTION_DAY) = 0
        AND EXTRACT(MINUTE FROM RETENTION_DAY) = 0
        AND EXTRACT(SECOND FROM RETENTION_DAY) = 0
    )
);

给出略有不同的错误(通过约束名称)- db<>fiddle - 或将它们合并为一个。

我不确定这是否真的比将数字列限制为 1 到 999 之间的整数更清晰或更容易。