Informix 7.3 - 声明日期列数据类型,默认为插入/更新时的当前日期

发布于 2024-10-08 15:34:56 字数 1577 浏览 1 评论 0原文

再次寻求您的帮助Jonathan Leffler

我正在 Informix 7.3 上创建一个表,并且需要一个时间戳字段,该字段在插入和更新时默认为今天

如何为具有当前时间默认值的表定义日期/日期时间/时间戳列?

以下是简单日期字段的字段定义:

column upd_date date
    comments ""
    desc "Last update date"
    heading "Last update date"
    text "Last update date"
    attributes
 (
 )

架构文件中还有一些其他语法,其中包含有关默认应该的注释:

column beg_date date{DEF: date academic session/subsession officially begins}

    comments ""
    desc "Beginning date."
    heading "Beg Date"
    text "Date - Begin"
    attributes
 (
 )

我不确定是否有任何其他表具有此功能,我什至不能 100% 确定它是否受支持,但如果有办法,我很想知道。

我在该主题上找到的唯一好的线索是 此处< /a>

有人有任何想法/解决方案吗?


More findings:
http://www.4js.com/techdocs/genero/fgl/devel/DocRoot/User/DatabaseSchema.html
Cast Date in Informix

我在另一个表定义中找到了一个 datetime 列类型:

column beg_time datetime year to minute
    comments    ""
    desc    "Beginning date and time of period"
    heading "Beg Time"
    text    "Date/Time - Slot Begin"
    attributes
    (
    )
{DEF: date and time this group/person may register}

Looking for your help again Jonathan Leffler!

I am creating a table on Informix 7.3 and need a timestamp field that will default to today on inserts and updates.

How can I define a date/datetime/timestamp column for a table with a default value of the current time?

Here is a field definition for a simple date field:

column upd_date date
    comments ""
    desc "Last update date"
    heading "Last update date"
    text "Last update date"
    attributes
 (
 )

There is also some other syntax in schema files that have comments about what the default should be:

column beg_date date{DEF: date academic session/subsession officially begins}

    comments ""
    desc "Beginning date."
    heading "Beg Date"
    text "Date - Begin"
    attributes
 (
 )

I'm not sure of any other tables that have this functionality, and I'm not even 100% sure that it is supported, but if there is a way, I'd love to know.

The only good lead I've found on the topic is here

Anyone have any ideas/solutions?


More findings:
http://www.4js.com/techdocs/genero/fgl/devel/DocRoot/User/DatabaseSchema.html
Cast Date in Informix

There is a datetime column type that I found in another table def:

column beg_time datetime year to minute
    comments    ""
    desc    "Beginning date and time of period"
    heading "Beg Time"
    text    "Date/Time - Slot Begin"
    attributes
    (
    )
{DEF: date and time this group/person may register}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小巷里的女流氓 2024-10-15 15:34:56

我不认识问题中使用的元语言,所以我不确定它与 DBMS 的能力相比有什么能力。

CREATE TABLE ExampleDatesAndTimes
(
    rownumber     SERIAL NOT NULL PRIMARY KEY,
    date_column   DATE DEFAULT TODAY NOT NULL,
    datetime_yd   DATETIME YEAR TO DAY
                  DEFAULT CURRENT YEAR TO DAY NOT NULL,
    datetime_ys   DATETIME YEAR TO SECOND
                  DEFAULT CURRENT YEAR TO SECOND NOT NULL,
    datetime_hs   DATETIME HOUR TO SECOND
                  DEFAULT CURRENT HOUR TO SECOND NOT NULL,
    payload       VARCHAR(255) NOT NULL
);

这将为您提供一个表,如果您未在 INSERT 操作中指定,则将为其中的 4 个时间列中的每一个分配一个默认值:

INSERT INTO ExampleDatesAndTimes(Payload) VALUES ("Hello");

另一方面,如果您指定了列,则指定的值优先。我假设 DBDATE="Y4MD-" 以便 DATE 值看起来像 DATETIME YEAR TO DAY 值:

INSERT INTO ExampleDatesAndTimes
    VALUES(0, '1066-10-14', '2001-01-01', '2012-11-10 09:08:07',
           '23:23:21', "Gezundheit");

这里,值全部指定,因此这些是存储的值。请注意,ISQL Perform 等程序(以及最典型的 I4GL 程序)将为所有列提供值,因此默认机制不会生效。

您可以使用触发器来更改 UPDATE 上的值,这样您就可以插入日期和“上次更新”列(以及 whodunnit 列 - 创建者和更新者 - 如果需要)。同样,您必须担心默认值与显式提供的值。

现在,由于您使用的是 IDS 7.3x(它最终在一两年前停止服务),因此您的功能与 IDS 11.70 中提供的功能略有不同。您应该考虑升级。


我(最终)找到了这段代码,用于在更新时使用触发器。它可以追溯到 2006 年。

CREATE TABLE talx_000
(
    i       SERIAL NOT NULL PRIMARY KEY,
    s       CHAR(30) NOT NULL,
    m_user  VARCHAR(32) DEFAULT USER NOT NULL,
    m_time  DATETIME YEAR TO SECOND DEFAULT CURRENT YEAR TO SECOND NOT NULL
);
CREATE PROCEDURE current_user_time()
    RETURNING VARCHAR(32) AS m_user, DATETIME YEAR TO SECOND AS m_time;
    RETURN user(), CURRENT YEAR TO SECOND - 1 UNITS DAY;
END PROCEDURE;

CREATE TRIGGER upd_talx_000 UPDATE ON talx_000
    REFERENCING NEW AS NEW FOR EACH ROW
    (EXECUTE PROCEDURE current_user_time() INTO m_user, m_time);

INSERT INTO talx_000(s) VALUES("cached nonsense");
INSERT INTO talx_000(s, m_user) VALUES("inserted user", "sphinx");
INSERT INTO talx_000(s, m_time)
    VALUES("inserted time", DATETIME(1066-10-14 15:23:31) YEAR TO SECOND);
INSERT INTO talx_000(s, m_time, m_user)
    VALUES("inserted both", DATETIME(1805-10-21 13:15:00) YEAR TO SECOND,
           "nelson");

SELECT * FROM talx_000;

DROP TRIGGER upd_talx_000;

CREATE PROCEDURE upd_talx_000(i_val INTEGER);
    UPDATE talx_000
        SET m_user = "brandywine",
            m_time = DATETIME(3019-03-25 13:00:00) YEAR TO SECOND
        WHERE i = i_val;
END PROCEDURE;

CREATE TRIGGER upd_talx_000 UPDATE ON talx_000
    REFERENCING NEW AS NEW FOR EACH ROW
    (EXECUTE PROCEDURE upd_talx_000(NEW.i));

INSERT INTO talx_000(s) VALUES("cached nonsense");
INSERT INTO talx_000(s, m_user) VALUES("inserted user", "sphinx");
INSERT INTO talx_000(s, m_time)
    VALUES("inserted time", DATETIME(1066-10-14 15:23:31) YEAR TO SECOND);
INSERT INTO talx_000(s, m_time, m_user)
    VALUES("inserted both", DATETIME(1805-10-21 13:15:00) YEAR TO SECOND,
           "nelson");

SELECT * FROM talx_000;

玩得开心!

I don't recognize the meta-language used in the question, so I'm not sure what that is capable of compared with what the DBMS is capable of.

CREATE TABLE ExampleDatesAndTimes
(
    rownumber     SERIAL NOT NULL PRIMARY KEY,
    date_column   DATE DEFAULT TODAY NOT NULL,
    datetime_yd   DATETIME YEAR TO DAY
                  DEFAULT CURRENT YEAR TO DAY NOT NULL,
    datetime_ys   DATETIME YEAR TO SECOND
                  DEFAULT CURRENT YEAR TO SECOND NOT NULL,
    datetime_hs   DATETIME HOUR TO SECOND
                  DEFAULT CURRENT HOUR TO SECOND NOT NULL,
    payload       VARCHAR(255) NOT NULL
);

This gives you a table in which each of the 4 temporal columns will be assigned a default value if you don't specify it in the INSERT operation:

INSERT INTO ExampleDatesAndTimes(Payload) VALUES ("Hello");

On the other hand, if you specify the columns, then the specified values take precedence. I'm assuming the DBDATE="Y4MD-" so that DATE values look like DATETIME YEAR TO DAY values:

INSERT INTO ExampleDatesAndTimes
    VALUES(0, '1066-10-14', '2001-01-01', '2012-11-10 09:08:07',
           '23:23:21', "Gezundheit");

Here, the values are all specified, so those are the values stored. Note that programs such as ISQL Perform (and most typical I4GL programs) will provide values for all the columns so the default mechanism won't take effect.

You can play with triggers to alter the values on UPDATE, so you can have a date inserted and a 'last updated' column (and whodunnit columns - created_by and updated_by - if you want). Again, you have to worry about defaults versus explicitly provided values.

Now, since you are using IDS 7.3x, which finally went out of service a year or two ago, you have slightly different functionality from what is available in IDS 11.70. You should be looking at upgrading.


I found this code (eventually) for playing with triggers on update. It dates from 2006.

CREATE TABLE talx_000
(
    i       SERIAL NOT NULL PRIMARY KEY,
    s       CHAR(30) NOT NULL,
    m_user  VARCHAR(32) DEFAULT USER NOT NULL,
    m_time  DATETIME YEAR TO SECOND DEFAULT CURRENT YEAR TO SECOND NOT NULL
);
CREATE PROCEDURE current_user_time()
    RETURNING VARCHAR(32) AS m_user, DATETIME YEAR TO SECOND AS m_time;
    RETURN user(), CURRENT YEAR TO SECOND - 1 UNITS DAY;
END PROCEDURE;

CREATE TRIGGER upd_talx_000 UPDATE ON talx_000
    REFERENCING NEW AS NEW FOR EACH ROW
    (EXECUTE PROCEDURE current_user_time() INTO m_user, m_time);

INSERT INTO talx_000(s) VALUES("cached nonsense");
INSERT INTO talx_000(s, m_user) VALUES("inserted user", "sphinx");
INSERT INTO talx_000(s, m_time)
    VALUES("inserted time", DATETIME(1066-10-14 15:23:31) YEAR TO SECOND);
INSERT INTO talx_000(s, m_time, m_user)
    VALUES("inserted both", DATETIME(1805-10-21 13:15:00) YEAR TO SECOND,
           "nelson");

SELECT * FROM talx_000;

DROP TRIGGER upd_talx_000;

CREATE PROCEDURE upd_talx_000(i_val INTEGER);
    UPDATE talx_000
        SET m_user = "brandywine",
            m_time = DATETIME(3019-03-25 13:00:00) YEAR TO SECOND
        WHERE i = i_val;
END PROCEDURE;

CREATE TRIGGER upd_talx_000 UPDATE ON talx_000
    REFERENCING NEW AS NEW FOR EACH ROW
    (EXECUTE PROCEDURE upd_talx_000(NEW.i));

INSERT INTO talx_000(s) VALUES("cached nonsense");
INSERT INTO talx_000(s, m_user) VALUES("inserted user", "sphinx");
INSERT INTO talx_000(s, m_time)
    VALUES("inserted time", DATETIME(1066-10-14 15:23:31) YEAR TO SECOND);
INSERT INTO talx_000(s, m_time, m_user)
    VALUES("inserted both", DATETIME(1805-10-21 13:15:00) YEAR TO SECOND,
           "nelson");

SELECT * FROM talx_000;

Have fun!

征﹌骨岁月お 2024-10-15 15:34:56
TABLE sample
(
timestamp DATETIME(YEAR TO SECONDS)
)


In Informix-SQL Perform screen:

INSTRUCTIONS

AFTER EDITADD OF sample.timestamp
LET screen_tag = CURRENT

AFTER EDITUPDATE OF sample.timestamp
LET screen_tag = CURRENT

注意:我从不在 DATE 或 DATETIME 列上使用WITHOUT NULL,因为在临时列上指定WITHOUT NULL 时,最好有一个不存在的值,而不是1899 年12 月31 日。

TABLE sample
(
timestamp DATETIME(YEAR TO SECONDS)
)


In Informix-SQL Perform screen:

INSTRUCTIONS

AFTER EDITADD OF sample.timestamp
LET screen_tag = CURRENT

AFTER EDITUPDATE OF sample.timestamp
LET screen_tag = CURRENT

NOTE: I never use WITHOUT NULL on a DATE or DATETIME column since it's better to have an absent value as opposed to 12/31/1899 when WITHOUT NULL is specified on a temporal column.

罪歌 2024-10-15 15:34:56

这是经过测试的&适合我 - 仅适用于插入。我假设如果您需要字段在每次更新时更新到当前日期,您可以使用触发器处理更新场景

column use_date date default today not null
        comments ""
        desc "Date this use case was executed"
        heading "Usage date"
        text "Usage date"
        attributes
     (
     )

This is tested & works for me - ONLY FOR INSERTS. I assume that you can handle the update scenario with a trigger if you need the field to update to the current date on every update

column use_date date default today not null
        comments ""
        desc "Date this use case was executed"
        heading "Usage date"
        text "Usage date"
        attributes
     (
     )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文