[英]How to update oracle list column with sequence number
Hi I have the oracle data table like that嗨,我有这样的 oracle 数据表
seq_no ![]() |
name![]() |
place![]() |
---|---|---|
1 ![]() |
Rian![]() |
Us![]() |
1 ![]() |
Moli![]() |
Us![]() |
1 ![]() |
Molina![]() |
Us![]() |
and i want to update automaticly the seq_no to be like that我想自动更新 seq_no 是这样的
seq_no ![]() |
name![]() |
place![]() |
---|---|---|
1 ![]() |
Rian![]() |
Us![]() |
2 ![]() |
Moli![]() |
Us![]() |
3 ![]() |
Molina![]() |
Us![]() |
If you have a table:如果你有一张桌子:
CREATE TABLE table_name (seq_no, name, place) AS
SELECT 1, 'Rian', 'Us' FROM DUAL UNION ALL
SELECT 1, 'Moli', 'Us' FROM DUAL UNION ALL
SELECT 1, 'Molina', 'Us' FROM DUAL;
and a sequence:和一个序列:
CREATE SEQUENCE your_sequence;
Then you can update the existing rows to the sequence values using:然后,您可以使用以下方法将现有行更新为序列值:
UPDATE table_name
SET seq_no = your_sequence.NEXTVAL;
Then the table would contain:然后该表将包含:
SEQ_NO 序列号
NAME 姓名
PLACE 地方
1 1
Rian 日安
Us 我们
2 2
Moli 莫里
Us 我们
3 3
Molina 莫利纳
Us 我们
Then when you want to INSERT
more rows, you can use:然后当你想
INSERT
更多行时,你可以使用:
INSERT INTO table_name (seq_no, name, place)
VALUES (your_sequence.NEXTVAL, 'New Name', 'New Place');
and the row:和行:
SEQ_NO 序列号
NAME 姓名
PLACE 地方
4 4
New Name 新名字
New Place 新地方
Would be added with the next sequence number.将添加下一个序列号。
Alternatively, you could write a trigger to get the next sequence number or, from Oracle 12, use an IDENTITY
column.或者,您可以编写触发器来获取下一个序列号,或者从 Oracle 12 中使用
IDENTITY
列。
What does "automatically" mean? “自动”是什么意思?
You could have created table so that SEQ_NO
is automatically populated by a database trigger (can't use an identity column as you're on 11g).您可以创建表,以便
SEQ_NO
由数据库触发器自动填充(在 11g 上不能使用标识列)。
Or, if you want to update the table, a simple option is或者,如果您想更新表,一个简单的选项是
update your_table set seq_no = rownum;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.