[英]How to add a sequence column to an existing table with records
I had created a new table named USERLOG with two fields from a previous VIEW.我创建了一个名为 USERLOG 的新表,其中包含来自先前 VIEW 的两个字段。 The table already consist of about 9000 records.
该表已经包含大约 9000 条记录。 The two fields taken from the VIEW, ie weblog_views consist of IP (consists of IP address), and WEB_LINK (consists of URL).
从VIEW 中取出的两个字段,即weblog_views 由IP(由IP 地址组成)和WEB_LINK(由URL 组成)组成。 This is the code I used,
这是我使用的代码,
CREATE TABLE USERLOG
AS
SELECT C_IP, WEB_LINK FROM weblog_views;
I want to add another column to this table called the USER_ID, which would consists of a sequence starting with 1 to 9000 records to create a unique id for each existing rows.我想向该表中添加另一个名为 USER_ID 的列,该列将包含一个从 1 到 9000 条记录开始的序列,以便为每个现有行创建一个唯一的 ID。 I need help with this part.
我需要这部分的帮助。 I'm using Oracle SQL Developer: ODMiner version 3.0.04.
我正在使用 Oracle SQL Developer:ODMiner 3.0.04 版。 I tried using the AUTO-INCREMENT option,
我尝试使用 AUTO-INCREMENT 选项,
ALTER TABLE USERLOG
ADD USER_ID INT UNSIGNED NOT NULL AUTO_INCREMENT;
But I get an error with this,但是我得到一个错误,
Error report:
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
So, I would really appreciate any help that I can get!所以,我真的很感激我能得到的任何帮助!
You would need to add a column您需要添加一列
ALTER TABLE userlog
ADD( user_id number );
create a sequence创建序列
CREATE SEQUENCE user_id_seq
START WITH 1
INCREMENT BY 1
CACHE 20;
Update the data in the table更新表中的数据
UPDATE userlog
SET user_id = user_id_seq.nextval
Assuming that you want user_id
to be the primary key, you would then add the primary key constraint假设您希望
user_id
成为主键,那么您将添加主键约束
ALTER TABLE userlog
ADD CONSTRAINT pk_user_id PRIMARY KEY( user_id );
If you want to use the sequence to automatically add the user_id
when you do an INSERT
(the other option would be to specifically reference user_id_seq.nextval
in your INSERT
statements, you would also need a trigger如果您想在执行
INSERT
时使用序列自动添加user_id
(另一个选项是在INSERT
语句中专门引用user_id_seq.nextval
,您还需要一个触发器
CREATE OR REPLACE TRIGGER trg_userlog_user_id
BEFORE INSERT ON userlog
FOR EACH ROW
BEGIN
:new.user_id := user_id_seq.nextval;
END;
In addition to Justin's excellent answer you might want to prevent NULL values for your user_id in the future (as they could still be caused by UPDATE statements).除了 Justin 的出色回答之外,您可能希望将来防止您的 user_id 出现 NULL 值(因为它们仍然可能由 UPDATE 语句引起)。 Therefore, execute the following statement at the end:
因此,在最后执行如下语句:
ALTER TABLE userlog MODIFY(user_id number NOT NULL);
Step 1. Create sequence to be used by the column eg:步骤 1. 创建列使用的序列,例如:
CREATE SEQUENCE user_id_seq
START WITH 1
INCREMENT BY 1
CACHE 20;
Step 2. Update new column with sequence eg:步骤 2. 使用序列更新新列,例如:
UPDATE userlog
SET user_id = user_id_seq.nextval;
Step 3. - Set Sequence as the default value for the column, will work only above Oracle 12c第 3 步。- 将 Sequence 设置为列的默认值,仅适用于 Oracle 12c 以上
ALTER TABLE USERLOG
MODIFY USER_ID INT DEFAULT user_id_seq.nextval;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.