Add a column SQL query in Oracle database - sql

I am using Oracle Database (version is 9i) and I want to add a column to a current table in oracle database.
I want to add an integer column to keep track of invalid tries per user, so default value should be 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.
ALTER TABLE CustApps_user ADD VALID_TRIES INT DEFAULT 5 NOT NULL;

I guess the error you're getting is 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.
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.
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:
ALTER TABLE CustApps_user ADD (VALID_TRIES NUMBER(38,0) DEFAULT 5 NOT NULL);
or change (decrease) NUMBER precision.

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

SQL Server : removes null constraint when changing the column's datatype, Oracle does not

I was reviewing something for a project and noticed in SQL Server, modifying the datatype of a column removed an existing not null check. I wanted to compare the same to Oracle and noticed that the null check is not removed when a data type change occurs.
My question: is there a reason why SQL Server does not preserve the null check without explicitly specifying the alter statement to make the column not null? After googling some, couldn't really find an answer. Maybe this is specific to a setting in SQL Server that is off?
If there isn't config, seems maybe there is a really good reason that I can't see for why this occurs.
Here is the SQL I was using to compare:
-- SQL Server
CREATE TABLE TestTable (Name varchar(50) NOT NULL);
-- Does not allow null
SELECT COLUMNPROPERTY(OBJECT_ID('dbo.TestTable', 'U'), 'Name', 'AllowsNull');
ALTER TABLE TestTable ALTER COLUMN Name varchar(250);
-- Allows null now
SELECT COLUMNPROPERTY(OBJECT_ID('dbo.TestTable', 'U'), 'Name', 'AllowsNull');
DROP TABLE TestTable;
-- Oracle
CREATE TABLE MYSCHEMA.TestTable (Name VARCHAR2(50) NOT NULL);
select nullable from all_tab_columns where owner = 'MYSCHEMA' and table_name = 'TESTTABLE' and column_name = 'NAME';
ALTER TABLE MYSCHEMA.TestTable MODIFY Name VARCHAR2(250);
select nullable from all_tab_columns where owner = 'MYSCHEMA' and table_name = 'TESTTABLE' and column_name = 'NAME';
Drop Table MYSCHEMA.TestTable;
Environment:
SQL Server 2017
Oracle 12c
Both running in docker on linux.
NULL may be the default.
From https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-ver15
When you create or alter a table with the CREATE TABLE or ALTER TABLE
statements, the database and session settings influence and possibly
override the nullability of the data type that's used in a column
definition. Be sure that you always explicitly define a column as NULL
or NOT NULL for noncomputed columns.
Did you try this?
ALTER TABLE TestTable ALTER COLUMN Name varchar(250) NOT NULL;

Snowflake: Trying to make a column to use as default the value from a sequence

So, I have something like this:
CREATE OR REPLACE TABLE TABLE_NAME (
ID NUMBER(38, 0) NOT NULL,
/* OTher elements */
)
With some values already (manually) inserted. I need to update this table so, for future inserts, the value of ID is taken from a sequence I just created:
CREATE OR REPLACE SEQUENCE S_TABLE_NAME_ID
START WITH 451;
For what I've seen in the documentation and in several forums, the syntax should be like this:
ALTER TABLE TABLE_NAME ALTER ID SET DEFAULT S_TABLE_NAME_ID.NEXTVAL;
But when I try to execute it, I get the following error message:
SQL Error [2] [0A000]: Unsupported feature 'Alter Column Set Default'.
Am I missing here something?
from Snowflake Doc (https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html):
"To change the default sequence for a column, the column must already
have a default sequence. You cannot use the command ALTER TABLE ...
SET DEFAULT <seq_name> to add a sequence to a column that does not
already have a sequence."
So I guess you have to set the sequence as column default when creating the table.

Create a new column based on other columns

I've managed to create the following column in a new table:
CREATE TABLE t_issue_dates as
SELECT issue_d,
cast(substr(issue_d,5,4) as numeric) as issue_year
FROM myDB
(The code recodes a year-month variable to a year-only variable)
However, i can't add this variable to my existing table "myDB". I've tried using the:
ALTER TABLE myDB ADD v_year - command, but i can't manage to get it right.
Does anyone have an idea how i add the above variable to the "original" table myDB?
Thank you!
First, many databases support computed or generated columns. That means that you can add a virtual column to the database by doing:
alter table t_issue_dates add issue_year as (cast(substr(issue_d, 5, 4) as numeric));
I recommend this approach because issue_year is always up-to-date.
Second, you can do this as an actual column, but the value can get out-of-date and needs to be re-calculated for each inserted/updated row:
alter table t_issue_dates add issue_year numeric;
update t_issue_dates
set issue_year = cast(substr(issue_d, 5, 4) as numeric);
For the record, I would use int rather than numeric.
Assuming you are using MSSQL. It would be worth of reading documentation or a simple google on how to insert
Adding column :
Alter table t_issue_dates
Add V_year Int
Next step: This will only insert data for this particular column.
Insert into t_issue_dates (v_year)
SELECT
cast(substr(issue_d,5,4) as numeric)
FROM myDB

ERROR: syntax error at or near "modify" - in postgres

I executed this SQL statement in Postgres
alter table user modify column 'distinguishedName1' text;
and
alter table user modify column distinguishedName1 text;
user is the table name
distinguishedName1 is the column name with integer data type.
I wanted to modify the data type to boolean or text or varchar(256) etc based on user's input. But when I run the query I get the error
ERROR: syntax error at or near "modify"
Not sure what is the problem. Help required on right query.
POSTGRES syntax for altering column type :
ALTER TABLE user ALTER COLUMN distinguishedName1 TYPE text;
Try this:
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text USING code::text;
or
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text
Also do note that the USING is optional. See the manual here:
The optional USING clause specifies how to compute the new column
value from the old; if omitted, the default conversion is the same as
an assignment cast from old data type to new. A USING clause must be
provided if there is no implicit or assignment cast from old to new
type.
On a side note try to avoid naming your tables as reserved keywords.
alter table user Alter column distinguishedName1 text;
Syntax mistake , for sql server you have to use alter to modify the column of table