Export and Import using Data Pumps in Oracle 19c - oracle

I have a table in which I want to add a new char type column
Before adding the column took the export , then dropped the table and then recreated the table and then did the import using the same dump file. When I checked the value of new added column in table , it doesn't holds any value.
How can I default the value to Spaces in this newly added column.

You don't need to drop and recreate the table to achieve this. You can simply alter the table and add the new column. You can also specify what the default value is for the existing values for the column.
alter table mytab add ( col2 CHAR2 DEFAULT ' ' );
However, are you really sure you want the column to have spaces?

Related

How do I change column type in Apex 20.1 from varchar to float?

I imported a Excel spreadsheet into Apex 20.1. Several of my columns were formatted as currency eg. $30 but they've all been imported as varchar. Many of the cells were empty so maybe that's why it didn't automatically recognise the column type.
There didn't seem to be a way to alter the table definition before it loaded the data either.
To change it back to a numeric type ( ? float ), do I need to add another column, import the data from the original column recast as float, delete the first column and then rename the new column back to the original name?
I also want to truncate some varchar(4000) down to varchar(100) but that's another question.
If column looked exactly like $30, then yes - it is a string, not a number, so Oracle created a VARCHAR2 column. If you removed formatting in Excel before doing anything with Apex, it would have been a NUMBER.
Now, yes - as you said:
create a new column
update table and set new column to numeric value of the old column
drop the old column
rename new column to the "old/original" name
Or, alternatively (from point #3)
empty the old column
modify its datatype
move data back into it
drop the new column
As of truncating varchar2(4000) column to (100): first shorten its contents to 100 characters, then modify its datatype:
update your_table set that_column = substr(that_column, 1, 100);
alter your_table modify that_column varchar2(100);
Because of such problems, I prefer to
precreate the target table (using create table command), specifying each column's datatype as I want it to be
load file contents into such a table

How to drop hive column?

I have two columns Id and Name in Hive table, and I want to delete the Name column. I have used following command:
ALTER TABLE TableName REPLACE COLUMNS(id string);
The result was that the Name column values were assigned to the Id column.
How can I drop a specific column of the table and is there any other command in Hive to achieve my goal?
In addition to the existing answers to the question : Alter hive table add or drop column
As per Hive documentation,
REPLACE COLUMNS removes all existing columns and adds the new set of columns.
REPLACE COLUMNS can also be used to drop columns. For example, ALTER TABLE test_change REPLACE COLUMNS (a int, b int); will remove column c from test_change's schema.
The query you are using is right. But this will modify only schema i.e, the metastore. This will not modify anything on data side.
So, before you are dropping the column you should make sure that you hav correct data file.
In your case the data file should not contain name values.
If you don't want to modify the file then create another table with only specific column that you need.
Create table tablename as select id from already_existing_table
let me know if this helps.

How can I add one new Column in Oracle 11g with default value, but not apply default value to old records immediatly?

I have an existing table with millions of records. Now I want to add a new column with default value. But for performances reasons I don't want to apply the new value immediately to existing records. Is there one handy way to do it?
So far, what in my mind is to work around this issue is to:
Add the new column, but do not specify a default (DDL)
Update by chunk the old rows to the default value (to avoid locking the table) (DML)
COMMIT
Alter the column to add a default (DDL)
Another option would be
Add the new column without any default value but make it accept
NULL
While inserting new record, insert NULL for this column
CREATE a AFTER INSERT trigger and update the column with it's
default value.
Something like
CREATE TRIGGER trg_update
AFTER INSERT
ON table_name
BEGIN
UPDATE table_name
SET new_column = (default_value)
WHERE Id_Column = :new.Id_Column;
END;

How to modify data type in Oracle with existing rows in table

How can I change DATA TYPE of a column from number to varchar2 without deleting the table data?
You can't.
You can, however, create a new column with the new data type, migrate the data, drop the old column, and rename the new column. Something like
ALTER TABLE table_name
ADD( new_column_name varchar2(10) );
UPDATE table_name
SET new_column_name = to_char(old_column_name, <<some format>>);
ALTER TABLE table_name
DROP COLUMN old_column_name;
ALTER TABLE table_name
RENAME COLUMN new_column_name TO old_coulumn_name;
If you have code that depends on the position of the column in the table (which you really shouldn't have), you could rename the table and create a view on the table with the original name of the table that exposes the columns in the order your code expects until you can fix that buggy code.
You have to first deal with the existing rows before you modify the column DATA TYPE.
You could do the following steps:
Add the new column with a new name.
Update the new column from old column.
Drop the old column.
Rename the new column with the old column name.
For example,
alter table t add (col_new varchar2(50));
update t set col_new = to_char(col_old);
alter table t drop column col_old cascade constraints;
alter table t rename column col_new to col_old;
Make sure you re-create any required indexes which you had.
You could also try the CTAS approach, i.e. create table as select. But, the above is safe and preferrable.
The most efficient way is probably to do a CREATE TABLE ... AS SELECT
(CTAS)
alter table table_name modify (column_name VARCHAR2(255));
Since we can't change data type of a column with values, the approach that I was followed as below,
Say the column name you want to change type is 'A' and this can be achieved with SQL developer.
First sort table data by other column (ex: datetime).
Next copy the values of column 'A' and paste to excel file.
Delete values of the column 'A' an commit.
Change the data type and commit.
Again sort table data by previously used column (ex: datetime).
Then paste copied data from excel and commit.

How to add values to a newly added column to all existing rows in Oracle 11g

I want to add a new column in to my production database (11g) that has millions of records but need default value only those existing records, meaning no new records should populate this default value.
Is it possible?
as per my understanding you want to add column to existing table and update it with default value.
alter table table_name add(col_name data_type);
Update table_name set col_name=some_default_value;

Resources