Deleting entire data from a particular column in oracle-sql - sql

Recently I have started learning Oracle-sql. I know that with the help of DELETE command we can delete a particular row(s). So, Is it possible to delete entire data from a particular column in a table using only DELETE command. (I know that using UPDATE command by setting null values to entire column we can achieve the functionality of DELETE).

DELETE
The DELETE statement removes entire rows of data from a specified
table or view
If you want to "remove" data from particular column update it:
UPDATE table_name
SET your_column_name = NULL;
or if column is NOT NULL
UPDATE table_name
SET your_column_name = <value_indicating_removed_data>;
You can also remove entire column using DDL:
ALTER TABLE table_name DROP COLUMN column_name;

In SQL, delete deletes rows not columns.
You have three options in Oracle:
Set all the values to NULL using update.
Remove the column from the table.
Set the column to unused.
The last two use alter table:
alter table t drop column col;
alter table t set unused (col);

Use Invisible Type, which is from an oracle 12cR2.
ALTER TABLE LOG1
MODIFY operation INVISIBLE
It is a better than drop of a particular column.If you need to visible you can get back by altering with an VISIBLE of a column name.

update employee set commission=nvl2(commission,'','')
this will remove all the data from the column

Related

I want to add two columns names to an existing table in Impala Query

I am writing the following query to add a column at a specified position but getting the below error:
alter table quantum_raw_dev.rpt_backup_allocation
change upt_type upt_type STRING after tray_size;
You can add one or more columns to the end of the column list using:
ALTER TABLE <table_name> ADD COLUMNS (col_name col_type, ...);
[note: there is NO comma between column name and type]
Adding or Removing Columns
You can add one or more columns to the end of the column list using ADD COLUMNS,
or (with Impala only) you can delete columns using DROP COLUMN.
The general syntax is
ALTER TABLE tablename ADD COLUMNS (col1 TYPE1,col2 TYPE2,… );
ALTER TABLE tablename DROP COLUMN colname;
For example, you can add a bonus integer column to the employees table:
ALTER TABLE employees ADD COLUMNS (bonus INT);
Or you can drop the office_id column from the employees table:
ALTER TABLE employees DROP COLUMN office_id;
Notes
DROP COLUMN is not available in Hive, only in Impala. However, see “Replacing All Columns” below.
You can only drop one column at a time.
To drop multiple columns, use multiple statements or use the method to replace columns (see below).
You cannot add a column in the middle of the list rather than at the end.
You can, however, add the column then change the order (see above) or use the method to replace columns (see below).
As with changing the column order, these do not change the data files.
If the table definition agrees with the data files before you drop any column other than the last one,
you will need to recreate the data files without the dropped column's values.
If you drop the last column, the data will still exist but it will be ignored when a query is issued.
If you add columns for which no data exists, those columns will be NULL in each row.
Replacing All Columns
You can also completely replace all the columns with a new column list.
This is helpful for dropping multiple columns,
<h1>or if you need to add columns in the middle of the list<h1>
<h2>(like your use case)<h2>
The general syntax is
ALTER TABLE tablename REPLACE COLUMNS (col1 TYPE1,col2 TYPE2,… );
This completely removes the existing list of columns and replaces it with the new list.
Only the columns you specify in the ALTER TABLE statement will exist, and they will be in the order you provide.
Note
Again, this does not change the data files, only the metadata for the table,
so you'll either want the new list to match the data files or need to recreate the data files to match the new list.
I do not think you can add columns in between columns in Impala like above.
You can backup the data, drop the and recreate with new structure, and load the table from backup. Also if you have HIVE in your system you can try to do below steps -
Add column first and then use below commands to move columns around.
ALTER TABLE tab ADD COLUMNS (id BIGINT);
This moves id column to the beginning.
ALTER TABLE tab CHANGE COLUMN id id BIGINT first;
This moves existing_col after id.
ALTER TABLE tab CHANGE COLUMN existing_col existing_col string AFTER id;
Please refresh/invalidate metadata after applying all DDLs.
You cannot add column in between. Best way is to archive the data in another table. Drop the impala old table and create a fresh table with new columns as per the desired location and then reinsert the data.

SQL update records with join table

Currently I need to move three columns from table A to table B. And I am using the update join table script to copy the existing data to the new columns. Afterwards the old column at table A will be drop.
Alter table NewB add columnA integer
Alter table NewB add columnB integer
Update NewB
Set NewB.columnA = OldA.columnA, NewB.columnB = OldA.columnB
From NewB
Join OldA on NewB.ID = OldA.ID
Alter table OldA drop column columnA
Alter table OldA drop column columnB
These script will add new columns and update the existing data from the old table to the newly created columns. Then remove the old columns.
But due to system structure, I will required to run SQL Script for more than one times to makes sure the database is up to date.
Although I did If (Columns Exist) Begin (Alter Add, Update, Alter Drop) End to ensure the existence of columns required. But when the script runs at the next time, it will hit error that says the columns was not found from the old table in the "update" query. Because the columns were dropped when the script run at the first time.
Is there other ways to solve?
you will not be able to update using join, But you can do like this :
Update NewB set NewB.columnA = (select OldA.columnA from OldA where NewB.ID = OldA.ID);
Update NewB set NewB.columnB = (select OldA.columnB from OldA where NewB.ID = OldA.ID);
I don't know which database you are using, in database there are some system tables, from where you can get whether the column does exist in table or not, like in oracle, All_TAB_COLUMNS contains the information of all the columns of tables, so you can hit that table like below :
select 1 from ALL_TAB_COLUMNS where TABLE_NAME='OldA' and COLUMN_NAME in ('columnA','columnB');
if resulting records are empty that means specified columns are not present in the table so you can skip your queries.
There must be something wrong with your is column exists check. I have similar DDL and DML operations many times. As you did not show how you are checking column existence I am not able to tell you what's wrong.
Anyway, you are adding a new column to a table. We can check if such column exists, if not - run the script, if yes- skip the script. And here is the check:
IF EXISTS(SELECT 1 FROM [sys].[columns] WHERE OBJECT_ID('[dbo].[NewB]') = [object_id] AND [name] = 'columnA')
BEGIN
BEGIN TRANSACTION;
....
COMMIT TRANSACTION;
END;

How To Alter Date column to Varchar2(20)? [duplicate]

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.

Efficiently add a column with a value

I am trying to add a column to a tsql table, i do this using SMO in c#. Altering the table is fine but i want to set the column to have a value. The table contains 650 million rows and the update query is taking over a day and a half to run.
Update [TempDatabase].[dbo].[table1] set RawSource = 'DTP'
This is the query I am running above.
Can anyone think of a more efficient way of doing this?
Thanks in advance.
Sometimes, it is more efficient to copy the table with the new value and re-create the table in a single command. Also, you might want to be sure that you have minimal logging for these operations.
Probably the best solution is to use a default value when you create the column:
alter table table1 add RawSource varchar(255) not null default 'DTP';
If you don't want the default moving forward, you can remove it after the column is added.
Another method uses computed columns, but basically does the same thing:
alter table table1 add _RawSource varchar(255);
alter table1 add RawSource as (coalesce(_RawSource, 'DTP'));
at the time of addition of column to table only we can set a default value which will applies for all rows
Note:U should keep not null compulsory because if not all rows are applicable with nulls
alter table table_name
add row_source nvarchar(5) not null default(N'DTP')

populate a column with a value based on value of other column alter table sql

I have a table 'Data' with a column 'Date'
I need to add another column called flag and populate it with 0 if the date is less than 2 years from current date and populate it with 1 if the date is more than 2 years from current date.
I did it by adding column using alter table and using update set statement as below
alter table data add flag INTEGER constraint flag_value check (flag in(0,1));
Is there a way to do this using just one alter table statement without using update set?
Is there a way to do this using just one alter table statement without using update set?
Not in sqlite, which does not support computed columns and there is no way to run a default UPDATE via an ALTER TABLE statement.
You could use the view approach from the linked question, or do as you've done and issue separate ALTER TABLE and UPDATE statements.