Rename Oracle Table or View - oracle

What is the syntax to rename a table or view in Oracle?

ALTER TABLE mytable RENAME TO othertable
In Oracle 10g also:
RENAME mytable TO othertable

To rename a table you can use:
RENAME mytable TO othertable;
or
ALTER TABLE mytable RENAME TO othertable;
or, if owned by another schema:
ALTER TABLE owner.mytable RENAME TO othertable;
Interestingly, ALTER VIEW does not support renaming a view. You can, however:
RENAME myview TO otherview;
The RENAME command works for tables, views, sequences and private synonyms, for your own schema only.
If the view is not in your schema, you can recompile the view with the new name and then drop the old view.
(tested in Oracle 10g)

In order to rename a table in a different schema, try:
ALTER TABLE owner.mytable RENAME TO othertable;
The rename command (as in "rename mytable to othertable") only supports renaming a table in the same schema.

One can rename indexes the same way:
alter index owner.index_name rename to new_name;

Past 10g the current answer no longer works for renaming views.
The only method that still works is dropping and recreating the view.
The best way I can think of to do this would be:
SELECT TEXT FROM ALL_VIEWS WHERE owner='some_schema' and VIEW_NAME='some_view';
Add this in front of the SQL returned
Create or replace view some_schema.new_view_name as ...
Drop the old view
Drop view some_schema.some_view;

Related

Restore backup table data to old one

I made some changes in my tables and I need to make backup of tables which I use something like
CREATE TABLE supplier_invoice_rows_backup
AS
SELECT * FROM supplier_invoice_rows
I made changes and I need to return data from supplier_invoice_rows_backup to supplier_invoice_rows
Is there any way to do this ?
Yes there is! Try mysql code below.
RENAME TABLE supplier_invoice_rows_backup TO supplier_invoice_rows;
Or try oracle code.
RENAME supplier_invoice_rows_backup TO supplier_invoice_rows;
An alternative to renaming the table would be to truncate the original table and copy the rows from the 'backup' table:
truncate table supplier_invoice_rows;
insert into supplier_invoice_rows (select * from supplier_invoice_rows_backup);
Or just reverse your original:
drop table supplier_invoice_rows purge;
create table supplier_invoice_rows
as
select * from supplier_invoice_rows_backup;
Or the previously suggested rename. Your response to that suggestion ("but I want to copy") suggests you haven't thought this through. You cannot have two tables with the same name in the same schema. So what do you think is the end result of 'rename table supplier_invoice_rows_backup to supplier_invoice_rows'?

How to remove a strange table named "BIN$c+eOnMB3RbKSEfg/rsxtAQ==$0" from oracle database?

I am new to Oracle and for practice I have created some tables (customer, drivers, payment, booking, location, area, job, job_history) in Oracle 11g and upon select * from cat statement I have found a strange table with other created tables named "BIN$c+eOnMB3RbKSEfg/rsxtAQ==$0".I don't know why this table is created.
I tried to remove this table through
drop table BIN$c+eOnMB3RbKSEfg/rsxtAQ==$0;
but it gives error:
drop table BIN$c+*eOnMB3RbKSEfg/rsxtAQ==$0
ERROR at line 1: ORA-00933: SQL command not properly ended
what should I do to remove it?
What you see is a deleted table in the RECYCLEBIN
You may get the original name of the table with this query
SELECT original_name FROM RECYCLEBIN where OBJECT_NAME = 'BIN$c+eOnMB3RbKSEfg/rsxtAQ==$0';
Note that (with your parameter setting) if you DROP a table it is not completely removed, but moved in the recyclebin.
You may ommit this using the PURGE option.
DROP TABLE xxx PURGE;
To remove the table from the recyclebin you must qoute the name with double quotes (as this is not a valid name) and use the PURGE statement (not a DROP - which would trigger ORA-38301: can not perform DDL/DML over objects in Recycle Bin).
PURGE TABLE "BIN$c+eOnMB3RbKSEfg/rsxtAQ==$0"
Alternatively you may use the original_name obtained with the query above:
PURGE TABLE {your_original_name};
To clean up the recyclebin completely use this statement (with the propper table user)
PURGE RECYCLEBIN;

Update new added columns in hive

I have been trying to make updates to an orc table in hive which is bucketed and also set transactional=true property. The normal update works great but as soon as I alter the table and add a new column e.g. column_added_5, and try to update column_added_5 the statement executes but the column does not get updated.
Any help/directions is appreciated.
I think that one way is:
CREATE TABLE new_table_name AS SELECT column1,column2,column3, ... "default_value" as column_added_5 FROM your_table_name;
DROP TABLE your_table_name;
ALTER TABLE new_table_name RENAME TO your_table_name;
Did you try this:
ALTER TABLE table_name ADD COLUMNS ( column_added_5 STRING COMMENT 'Column 5');

How to rename a hive table without changing location?

Based on the Hive doc below:
Rename Table
ALTER TABLE table_name RENAME TO new_table_name;
This statement lets you change the name of a table to a different name.
As of version 0.6, a rename on a managed table moves its HDFS location as well. (Older Hive versions just renamed the table in the metastore without moving the HDFS location.)
Is there any way to rename a table without changing the location?
Yeah we can do that. You just need to follow below three commands in sequence.
Lets say you have a external table test_1 in hive. And you want to rename it test_2 which should point test_2 location not test_1. Then you need to convert this table into Managed table using below command.
test_1 -> pointing to test_1 location
ALTER TABLE db_name.test_1 SET TBLPROPERTIES('EXTERNAL'='FALSE');
Rename the table name.
ALTER TABLE db_name.test_1 RENAME TO db_name.test_2;
Again convert the managed table after renaming to external table.
ALTER TABLE db_name.test_2 SET TBLPROPERTIES('EXTERNAL'='TRUE');
db_name.test_2 table will point the test_2 location. If we do it without making the managed table it will point the test_1 location.
As of Hive 2.2.0 a managed table's HDFS location is moved only if the table is created without a LOCATION clause and under its database directory.Link
ALTER TABLE does not follow the databasename.tablename syntax in Hive like it does in CREATE or SELECT.
Mention the databasename first and then run alter table statement.
syntax as below
USE databasename;
ALTER TABLE old_tablename RENAME TO new_tablename;
Here is the command executed
ALTER TABLE old_ratings RENAME TO ratings;

Oracle: Recreate database view after modifying varchar column size

I've made some changes to columns in a database table (increasing the size of varchar), however I need to have the view of the table refreshed to pick up this change. In SQL server 2005 I would use the script to -> alter to recreate the script for the view. I'm trying to figure out what the Oracle SQL command would be to rebuild the view to display the change of the column?
Unless your view is explicitly restricting the size of the column, it will pick up the change automatically. It may have been invalidated by the table change but would be automatically recompiled when first used; or can be manually recompiled with alter view <view name> compile.
If you do so:
create table sample_table(text varchar2(10));
insert into sample_table (text) values('text...');
create view sample_view as select * from sample_table;
select * from sample_view;
alter table sample_table modify text varchar2(200);
You do not do anything to promote this change in view in Oracle database.
Or you can use "ALTER VIEW sample_view COMPILE" (how wrote #AlexPole or #devio). Oracle do this automatically when you firstly use select on sample_view after alter table.
Try
ALTER VIEW MyView COMPILE;

Resources