Alter table - change the default value of a column - oracle

I have a column with REAL data type, and I'm trying to write a statement to change the default value to 4. However, when I use the select * statement to double check, the column is still empty.
ALTER TABLE recipes
MODIFY NumberOfServings DEFAULT '4';

The DEFAULT value is used at INSERT time. It gives a default value to be inserted when you do not provide a value for the column in the INSERT statement.
You may want to update the table, so that all NULL values are replaced by a given value:
UPDATE recipes SET NumberOfServings=4 WHERE NumberOfServings IS NULL;
You can also specify a value to use when the column is NULL at QUERY time.
This can be done by using the NVL function:
SELECT NVL(NumberOfServings,4) FROM recipes;
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm

The default value applies only when you insert new records into the table. It does not update any existing values in the table. Why do you enclose a numeric value in quotes?

Related

How to replace NULL values in one column to 0 (of a very large table) without creating a new column of the desired results added to the table in HIVE?

I am trying to replace all of the NULL values to 0 in a column of a big table in HIVE.
However, every time I try to implement some code I end up generating a new column to the table. The column I am trying to change/modify still exists and still has the NULL values but the new column that is automatically generated (i.e. _c1) is what I want the column I am trying to modify, to look like.
I tried to run a COALESCE but that also ended up generating a new column. I also tried to implement a CASE WHEN, but the same results ensued.
Select *,
CASE WHEN columnname IS NULL THEN 0
ELSE columnname
END
from tablename;
Also tried
SELECT coalesce(columnname, CAST(0 AS BIGINT)) FROM tablename
I would just like to update the table with the other columns being as is but the column I want to modify still has its original name but instead of NULL values it has 0's that replaced them.
I don't want to generate a new column but modify an existing one.
How should I do that?
Use insert overwrite .. option.
insert overwrite table tablename
select c1,c2,...,coalesce(columnname,0) as columnname
from tablename
Note that you have to specify all the other column names required in select.

DEFAULT column value support in CockroachDB

Does CockroachDB support default values for columns in its tables? Does it allow default values to be function values (e.g. current_date())?
You can set DEFAULT values using the DEFAULT constraint, which CockroachDB has documented here.
It also supports setting the default value as a function, e.g. to insert the date that a write occurred.
You would create a table with such a default column like:
CREATE TABLE purchase_log (
id INT PRIMARY KEY,
date_purchased DATE DEFAULT current_date()
);
Then all inserts to the table that don't specify the date_purchased column will have the column automatically populated with the return value of current_date() at the time of the insert.

Parse a varchar2 Column Value

I want to remove a piece of a string from a particular table's column. The string I wish to remove is the &expires and everything after it but leave everything before the &expires the same. Is there a way to accomplish with the update statement or is a stored procedure needed?
Table column value is:
Starting Value: DAABq3J65GvwBABbWdkFOnpCj2mEA1lMonZBZADcTYJR6QuLPUlfZBtMyoEl4x2JXQ49cOzjZAStQxWNOgrurtnMNIw04bmOcQ4SsrjuPKH4AZBBBAf8ZBjWhs8BM52aC0OpnPGzjm6V2x50qk6wboT&expires=5183999
Desired Ending Value:
DAABq3J65GvwBABbWdkFOnpCj2mEA1lMonZBZADcTYJR6QuLPUlfZBtMyoEl4x2JXQ49cOzjZAStQxWNOgrurtnMNIw04bmOcQ4SsrjuPKH4AZBBBAf8ZBjWhs8BM52aC0OpnPGzjm6V2x50qk6wboT
update table set column = regexp_replace(column, '&'||'expires=.*$')

date Not null , error ora_01758

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

Oracle Update and Return a Value

I am having a Update Statement on a large volume table.
It updates only one row at a time.
Update MyTable
Set Col1 = Value
where primary key filters
With this update statement gets executed I also want a value in return to avoid a Select Query on a same table to save resources.
What will be my syntax to achieve this?
You can use the RETURNING keyword.
Update MyTable
Set Col1 = Value
where primary key filters
returning column1,column2...
into variable1,variable2...

Resources