Modify default option of a column - sql

I am trying to change the default value of 2 columns from 'N' to 'Y' using the below query, but it throws a error. Any idea whats wrong in here. Error: Invalid Alter table option.
Alter Table USER
Modify
CONTACT_FLAG Default 'Y',
APPROVAL_FLAG Default 'Y' ;

You have to enclose the columns like this
alter table
table_name
modify
(
column1_name column1_datatype,
column2_name column2_datatype,
column3_name column3_datatype,
column4_name column4_datatype
);

Related

Updating current time for a column in SQL

So i am trying to store the time at which any changes were made to any row.
I was using the following query:
ALTER TABLE SPRD_MGMT_INP_INDEX_CHANGE_RATE
CHANGE "UPLOAD TIME"
"UPLOAD TIME" TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
I am getting the following error:
SQL compilation error: syntax error line 2 at position 0 unexpected 'CHANGE'. Please help
The syntax uses alter column and set, but the bigger issue is that you can't change the default of a column in Snowflake with the exception noted here:
https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html
In that page, it states the following:
Change the default for a column, unless the default is a sequence.
In that table for the action quoted above it has a checkmark by Unsupported.
You will need to create a new table with this default. You can then copy your rows from the original table using an insert from select.
You can specify the default in your new table like this:
create or replace table T1
(COL1 int, COL2 string, UPLOAD_TIME timestamp_tz default current_timestamp());

Handling null for char(1) and varcar(2) in hive

I am reading a flat file in hive and i have null values coming in file like below
a|b|null|null|d
and when I create table on top of this with below datatypes
a char(1),b char(1),c char(1),varchar2(2),char(1)
and the value in table coming like this
a,b,n,nu,d
The oneway I can do this is to make the datatype as varchar2(4) and add check at null.
But is there any other way i can do this.
SerDe treats 'null' strings as normal values, no difference between value 'a' and 'null'.
Try to add 'serialization.null.format'='null' property to your table definition:
ALTER TABLE mytable SET tblproperties('serialization.null.format'='null');
Another approach is to use STRING data type and case statements is select:
select case when col = 'null' then null end as col
...

How to reset DEFAULT value on a column On ORACLE TABLE

I have a table in which one of the column is set default to 0000 and the data type of of the column is number.
I want to remove the default value and when a record is saved null should save on this column.
You could override default to NULL:
ALTER TABLE tab_name MODIFY col_name DEFAULT NULL;
db<>fiddle demo
If columns was set as NOT NULL then:
ALTER TABLE tab_name MODIFY col_name type_name DEFAULT NULL NULL;
db<>fiddle demo2
First, update current records:
update tab set col=null where col=0;
Then remove default value:
alter tab modify col default null;

Dropping column with quotation marks in column name

I added a column using following commands (I used quotation marks so that N is in upper case in my column name)
ALTER TABLE new_table
ADD “Name” VARCHAR(50);
However, I see "nam" column in my table now after running that command.
How can I drop that column?
ALTER TABLE new_table
DROP COLUMN "Name";
I get following error:
ERROR: column "name" of relation "new_table" does not exist
The following statement causes this error:
ALTER TABLE new_table
DROP COLUMN "Name";
seems you are not using double quote " in first query bus some others quotes try suing the same chars “”
ALTER TABLE new_table DROP COLUMN “Name”;
Check how Postgres is storing the column name when you are using a double quote in column name:
select table_name, column_name from information_schema.columns where
table_name='tab1';
Also, you may view the same when you execute a select statement:
select * from table;
Copy the same column name text in your alter statement. Here is the sample column name with both kind of double quotes in PostgreSQL:
create table tab1(data varchar(30));
alter table tab1 ADD “Name1” varchar(50);
alter table tab1 ADD "Name2" varchar(50);
select *from tab1;
select table_name, column_name from information_schema.columns where table_name='tab1';
alter table tab1 drop column “Name1”;
alter table tab1 drop column "Name2";
table_name column_name
tab1 data
tab1 “name1”
tab1 Name2
Here is the link to the fiddle
Note: Avoid using a double quote in the table name, column, etc. In case you use you have to ensure that the same names are specified in all queries.
Edit:
It's simple. If you use "Name" (not same as “Name”, notice quote angle) in the column name then you have to refer the column as "Name". In case “Name” is used then you have to refer by “Name”. The quotes need to match.
Another observation is when "Name" used as column name it makes the column name as Name (N uppercase) as opposed to all lowercase column names by default in the database but needs to be referred as "Name".

Delete Data from Non-Nullable Column in SQL

Question: I have table with three column, with column names APP_NAME, APP_TYPE and VALUE_TIME.
I would like to edit VALUE_TIME for particular APP_NAME and APP_TYPE. So my query should look like below mentioned if VALUE_TIME column is Nullable. So what would be the best way to delete the data for particular condition ?
UPDATE TABLE_NAME
SET VALUE_TIME = null
WHERE APP_NAME = 'XYZ'
AND APP_TYPE = 'TEST';
Thanks
If you want to delete the row:
DELETE TABLE_NAME WHERE APP_NAME = 'XYZ' AND APP_TYPE = 'TEST';
The column VALUE_TYPE is defined as NOT NULL, so you can't set it to null. You could alter the table to make it nullable:
ALTER TABLE TABLE_NAME MODIFY VALUE_TYPE VARCHAR2(500) NULL;
And then run the UPDATE statement in your question.
Hopefully this answers your question - it wasn't clear what you want to do exactly.
Simple Answer, You can't update non-nullable column data to NULL or ' ' in oracle. I could only think of is Alter the column to have null.
ALTER
TABLE TABLE_NAME
MODIFY VALUE_TYPE VARCHAR2(500) NULL