简体   繁体   English

在Oracle数据库中添加列SQL查询

[英]Add a column SQL query in Oracle database

I am using Oracle Database (version is 9i) and I want to add a column to a current table in oracle database. 我正在使用Oracle数据库(版本为9i),并且想向oracle数据库中的当前表添加一列。

I want to add an integer column to keep track of invalid tries per user, so default value should be 5. 我想添加一个整数列以跟踪每个用户的无效尝试,因此默认值应为5。

When I try to execute this query in Sql*Plus it gives an error table or view doesn't exist ( I have double checked table name is correct. 当我尝试在Sql * Plus中执行此查询时,它给出了错误表或视图不存在(我已经仔细检查了表名是否正确。

ALTER TABLE CustApps_user ADD VALID_TRIES INT DEFAULT 5 NOT NULL;

I guess the error you're getting is ORA-00942. 我猜您收到的错误是ORA-00942。 This can mean a number of things, but basically it means the object does not exist in the current scope and context of what you're doing. 这可能意味着很多事情,但是从根本上讲,这意味着该对象在您正在执行的操作的当前范围和上下文中不存在。 So for instance it is the error thrown when we attempt to build a view on a table in another schema when we have been granted privileges through a role and not directly. 因此,例如,当我们通过角色而非直接被授予特权时,尝试在另一个架构的表上构建视图时,就会抛出该错误。

In your case it probably mean that the table is in another schema. 在您的情况下,这可能意味着该表位于另一个模式中。 You normally may be accessing it through a view or synonym. 通常,您可能正在通过视图或同义词来访问它。 You can easily check this by querying the data dictionary: 您可以通过查询数据字典轻松地进行检查:

select owner, object_type
from all_objects
where object_name = 'CUSTAPPS_USER'
alter table
   table_name
add
   (
   column1_name column1_datatype column1_constraint,  
   column2_name column2_datatype column2_constraint,
   column3_name column3_datatype column3_constraint
   );

Here are some examples of Oracle "alter table" syntax to add data columns. 以下是一些用于添加数据列的Oracle“更改表”语法的示例。

alter table
   cust_table
add
   cust_sex  varchar2(1) NOT NULL;

Here is an example of Oracle "alter table" syntax to add multiple data columns. 这是一个Oracle“更改表”语法的示例,用于添加多个数据列。

ALTER TABLE 
   cust_table 
ADD 
   (
      cust_sex             char(1) NOT NULL,
      cust_credit_rating   number
   );

You have to add bracket in query: 您必须在查询中添加括号:

ALTER TABLE CustApps_user ADD (VALID_TRIES INT DEFAULT 5 NOT NULL);

INT is legal, but it will be converted to NUMBER, so you can also use: INT是合法的,但它将转换为NUMBER,因此您也可以使用:

ALTER TABLE CustApps_user ADD (VALID_TRIES NUMBER(38,0) DEFAULT 5 NOT NULL);

or change (decrease) NUMBER precision. 或更改(降低)NUMBER精度。

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

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