简体   繁体   English

Oracle SQL 更改表添加具有当前用户名的列

[英]Oracle SQL alter table add column with current user name

It was a 2 part question and I got the timestamp correctly.这是一个两部分的问题,我正确地得到了时间戳。 I'm on 12C and trying to do something like:我在 12C 上并尝试执行以下操作:

ALTER TABLE customers
    ADD modified_by (USER FROM DUAL);

Basically just columns in a table that show who modified the table and at what time they did so.基本上只是表格中的列,显示谁修改了表格以及他们在什么时间这样做。

I also tried我也试过

ALTER TABLE customers
    ADD last_modified TIMESTAMP;
ALTER TABLE customers
    ADD modified_by USER;

and other combinations of keywords that I found on this site and other sites but none of them work.以及我在本网站和其他网站上找到的其他关键字组合,但它们都不起作用。

We only learned dual in class but I'm looking for any way to do these.我们只在课堂上学过双重,但我正在寻找任何方法来做这些。

Edit:编辑:

I now understand what was taught to me by the user with almost 1 million points.我现在明白了用户用近 100 万点教给我的东西。

Still unsure how to do the username.仍然不确定如何做用户名。 Read this: https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2114.htm#REFRN20302阅读: https : //docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2114.htm#REFRN20302

and tried:并尝试:

ALTER TABLE customers
    ADD modified_by USERNAME;

doesn't work get invalid datatype.不起作用得到无效的数据类型。

Then saw this: https://www.techonthenet.com/oracle/questions/find_users.php然后看到这个: https : //www.techonthenet.com/oracle/questions/find_users.php

and tried:并尝试:

ALTER TABLE customers
    ADD modified_by USERNAME FROM DBA_USERS;

but getting invalid alter table option.但获得无效的更改表选项。 SQL is hard. SQL 很难。

If you read through the Oracle documentation on virtual columns, you will find this:如果您通读有关虚拟列的 Oracle文档,您会发现:

Functions in expressions must be deterministic at the time of table creation, but can subsequently be recompiled and made non-deterministic表达式中的函数在创建表时必须是确定性的,但随后可以重新编译并使其成为非确定性的

A deterministic function is one that returns the same value when it is passed the same arguments.确定性函数是在传递相同参数时返回相同值的函数。 The expressions that you are using contain non-deterministic functions.您使用的表达式包含非确定性函数。 Hence, they are not allowed.因此,它们是不被允许的。

The error that I get when I try this is:我尝试这个时得到的错误是:

ORA-54002: only pure functions can be specified in a virtual column expression ORA-54002: 在虚拟列表达式中只能指定纯函数

For some unknown reason, Oracle equates "pure" with "deterministic" in the error message.由于某些未知原因,Oracle 在错误消息中将“纯”等同于“确定性”。 But this is saying that the function needs to be deterministic.但这就是说该函数需要是确定性的。

After you edited the question, it seems that you are somewhat closer to what you want.编辑问题后,您似乎更接近您想要的。 This:这个:

ALTER TABLE customers ADD modified_by VARCHAR2(30);
                                      ^^^^^^^^^^^^
                                      datatype goes here, not what you'd like
                                      to put into this column

Then, during inserts or updates of that table, you'd put USER in there, eg然后,在该表的插入或更新期间,您将USER放在那里,例如

insert into customers (id, modified_by) values (100, USER);

Or, probably even better, set it to be default, as @a_horse_with_no_name suggested:或者,可能更好,将其设置为默认值,正如@a_horse_with_no_name 建议的那样:

ALTER TABLE customers ADD modified_by VARCHAR2(30) DEFAULT USER;

so - if you don't explicitly put anything into that column, it'll be populated with the USER function's value.所以 - 如果您没有明确地将任何内容放入该列,它将使用USER函数的值填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM