如何创建将自动创建序列ID的触发器

发布于 2025-02-07 10:12:03 字数 1163 浏览 1 评论 0原文

我一直在尝试实施逻辑。但仅是关于SQL的基本知识。

我有以下下表

create upload
(
  fileblob blob,
  mimetype varchar2(4000 char),
  filename varchar2(4000 char),
  createddate date,
  getsequence varchar2(4000 char) 
)

我在其中有以下数据

fileblob,mimetype,filename,createddate,getsequeence
(BLOB)  ,text/plain , ABC.txt , 12-JULY-19
(BLOB)  ,text/plain , XYZ.txt , 13-JULY-19
(BLOB)  ,text/plain , HTC.txt , 14-JULY-19
(BLOB)  ,text/plain , LKG.txt , 15-JULY-19

我需要在执行插入的情况下,根据create> createDate> 序列应自动插入到序列列中以获得相应的记录。

预期输出:

fileblob,mimetype,filename,createddate,getsequeence
(BLOB)  ,text/plain , ABC.txt , 12-JULY-22,20220712_002414 
(BLOB)  ,text/plain , XYZ.txt , 13-JULY-22,20220713_003513
(BLOB)  ,text/plain , HTC.txt , 14-JULY-22,20220714_004510
(BLOB)  ,text/plain , LKG.txt , 15-JULY-22,20220715_001711

序列将就像yyyymmdd_hhmmss它来自createDate,因为它设置为date。我将插入前4列,最后一列数据应通过触发本身插入CreateDate列的基础。

此4列数据是从我的Oracle Apex Web应用程序中填充的

I have been trying to implement the logic. But as only basic knowledge on SQL.

I have a below following table

create upload
(
  fileblob blob,
  mimetype varchar2(4000 char),
  filename varchar2(4000 char),
  createddate date,
  getsequence varchar2(4000 char) 
)

I have following data in it

fileblob,mimetype,filename,createddate,getsequeence
(BLOB)  ,text/plain , ABC.txt , 12-JULY-19
(BLOB)  ,text/plain , XYZ.txt , 13-JULY-19
(BLOB)  ,text/plain , HTC.txt , 14-JULY-19
(BLOB)  ,text/plain , LKG.txt , 15-JULY-19

I need to write some trigger as soon as the insert is performed depending upon createddate
a sequence should be automatically inserted to sequence column for respective record.

Expected output :

fileblob,mimetype,filename,createddate,getsequeence
(BLOB)  ,text/plain , ABC.txt , 12-JULY-22,20220712_002414 
(BLOB)  ,text/plain , XYZ.txt , 13-JULY-22,20220713_003513
(BLOB)  ,text/plain , HTC.txt , 14-JULY-22,20220714_004510
(BLOB)  ,text/plain , LKG.txt , 15-JULY-22,20220715_001711

The sequence will be like YYYYMMDD_HHMMSS it comes from createddate as it is set to date. I will insert first 4 columns and last column data should be inserted base on createddate column by trigger itself.

This 4 column data is populated from my Oracle APEX web application using form

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

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

发布评论

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

评论(1

看春风乍起 2025-02-14 10:12:03

您没有序列列,因为如果两个行具有相同的createDate,则仍然可以具有非唯一值。相反,您只有一个格式的日期。在这种情况下,只需将列转换为虚拟列:

create table upload
(
  fileblob    blob,
  mimetype    varchar2(4000 char),
  filename    varchar2(4000 char),
  createddate date,
  getsequence varchar2(4000 char)
              AS (TO_CHAR(createddate, 'YYYYMMDD_HH24MISS'))
);

如果您确实想要一个序列值(所有值都是唯一的),那么,从Oracle 12中,您可以使用身份列:

create table upload
(
  fileblob    blob,
  mimetype    varchar2(4000 char),
  filename    varchar2(4000 char),
  createddate date,
  getsequence NUMBER(10,0)
              GENERATED ALWAYS AS IDENTITY
              PRIMARY KEY
);

You don't have a sequence column as you can still have non-unique values if two rows have the same createddate. Instead, you just have a formatted date. In this case, just convert the column to a virtual column:

create table upload
(
  fileblob    blob,
  mimetype    varchar2(4000 char),
  filename    varchar2(4000 char),
  createddate date,
  getsequence varchar2(4000 char)
              AS (TO_CHAR(createddate, 'YYYYMMDD_HH24MISS'))
);

If you do want a sequence value (where all the values are unique) then, from Oracle 12, you can use an identity column:

create table upload
(
  fileblob    blob,
  mimetype    varchar2(4000 char),
  filename    varchar2(4000 char),
  createddate date,
  getsequence NUMBER(10,0)
              GENERATED ALWAYS AS IDENTITY
              PRIMARY KEY
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文