I have a big problem When i take a dump of database the empty tables are truncated
How i can export the all tables in oracle 10g
exp SYSTEM/password FILE=expdat.dmp
This might be because of these tables may not be extent allocated. Before taking backup you need to identify all the tables that do not have data. Then alter these tables to allocate extent.
ALTER TABLE <table_name> ALLOCATE EXTENT;
Use the below script to alter all tables they do not have extent allocated.
SELECT 'ALTER TABLE '||table_name||' ALLOCATE EXTENT;' FROM user_tables WHERE segment_created = 'NO';
Copy the output and execute it.
You might consider expdp (data pump) instead. There's a parameter CONTENT=METADATA_ONLY that might get you what you want.
I found another solution here.
Oracle has an option called DEFERRED_SEGMENT_CREATION, default true.
from the docs:
If set to true, then segments for tables and their dependent objects (LOBs, indexes) will not be created until the first row is inserted into the table.
I'll sum up the solution from the above link:
SQL> alter system set DEFERRED_SEGMENT_CREATION=FALSE scope=both;
Run the output of the following statement:
SQL> select 'alter table ' || table_name || ' move;' from user_tables where num_rows=0;
This made my exp export empty tables as well.
Related
I need import utility to remove old data then reinsert new data. How can I do that without using data pump?
The reason I can't use data pump is because I need to run this script on application server, I don't have the privilege to run this on database server. below is the script I'm currently trying to fix
imp < username >/< password >#//< host > fromuser=< schema > touser=< schema > file=exp_$TODAY.dmp log=exp_import_$TODAY.log ignore=y
IMP can't truncate tables prior to importing, so - you're out of luck here. If you could use the Data Pump Import, you could use the TABLE_EXISTS_ACTION parameter set to TRUNCATE (which would remove existing data) or even REPLACE (which drops table, creates it and loads data). Ask the DBA to provide access to a directory you could use for importing purposes.
Alternatively, as it seems that you're importing everything, consider dropping the target user (with the cascade option) and then import its objects.
Or, if it isn't too complicated, create a script which would truncate tables, for example
SQL> select 'truncate table ' || tname ||';' from tab;
'TRUNCATETABLE'||TNAME||';'
----------------------------------------------
truncate table BONUS;
truncate table DEPT;
truncate table EMP;
truncate table SALGRADE;
truncate table WC;
SQL>
so you'd run those TRUNCATE TABLE statements prior to importing.
Note that - if there are foreign key constraints - they won't let you do it unless you disable them; as above, you could use SQL to create SQL for you:
SQL> select 'alter table ' || table_name || ' disable constraint ' || constraint_name ||';'
2 from user_constraints
3 where constraint_type = 'R';
'ALTERTABLE'||TABLE_NAME||'DISABLECONSTRAINT'||CONSTRAINT_NAME||';'
---------------------------------------------------------------------------------------------
alter table EMP disable constraint FK_DEPTNO;
SQL>
In one of my Windows form application in asp .net, I am creating tables on daily basis sharp at 00:00 am with name as "data_YYYY_MM_DD" in Oracle database. A large amount of data exist in each table as after every 5 seconds I am writing some useful data into these.
Now consider all tables of month Oct 2016. All will have name like 'data_2016_10%'. How can I take backup of only these tables (not backup of entire database) and then drop these tables from the database.
You can take logical backup of these tables using data pump. It creates dump file(binary file) which can be imported as per your needs.
Export:Table Mode
A table mode export is specified using the TABLES parameter. In table mode, only a specified set of tables, partitions, and their dependent objects are unloaded.
Example:
expdp hr TABLES=hr.employees VERSION=LATEST DIRECTORY=dpump_dir1 DUMPFILE=emp.dmp NOLOGFILE=YES
Reference:
Data Pump Export
Try like below,
you can schedule it in dbms_scheduler jobs,So that every day it will run and create backup table and drop existing table.Also please add exceptions like table does not exist...if you want
create or replace procedure backup_monthly_table
as
prev_date varchar2(20) := to_char(sysdate-1, 'yyyy-mm-dd');
begin
for i in (select table_name from dba_tables where upper(table_name) like '%'||'"'||prev_date||'_%')
loop
dbms_output.put_line('working');
dbms_output.put_line(i.table_name);
execute immediate 'create table sysman.'||'"'||i.table_name||'_bkp'||'"'||' as select * from sysman.'||'"'||i.table_name||'"';
execute immediate 'drop table sysman.'||'"'||i.table_name||'"';
end loop;
end;
verification output:
select owner,table_name from dba_tables where upper (table_name) like '%2017-%'
SYSMAN 2017-02-01_test1_bkp
SYSMAN 2017-02-01_test2_bkp
SYSMAN 2017-02-01_test3_bkp
SYSMAN 2017-02-01_test4_bkp
I have a big table with lot of data partitioned into multiple partitions. I want to keep a few partitions as they are but delete the rest of the data from the table. I tried searching for a similar question and couldn't find it in stackoverflow. What is the best way to write a query in Oracle to achieve the same?
It is easy to delete data from a specific partition: this statement clears down all the data for February 2012:
delete from t23 partition (feb2012);
A quicker method is to truncate the partition:
alter table t23 truncate partition feb2012;
There are two potential snags here:
Oracle won't let us truncate partitions if we have foreign keys referencing the table.
The operation invalidates any partitioned Indexes so we need to rebuild them afterwards.
Also, it's DDL, so no rollback.
If we never again want to store data for that month we can drop the partition:
alter table t23 drop partition feb2012;
The problem arises when we want to zap multiple partitions and we don't fancy all that typing. We cannot parameterise the partition name, because it's an object name not a variable (no quotes). So leave only dynamic SQL.
As you want to remove most of the data but retain the partition structure truncating the partitions is the best option. Remember to invalidate any integrity constraints (and to reinstate them afterwards).
declare
stmt varchar2(32767);
begin
for lrec in ( select partition_name
from user_tab_partitions
where table_name = 'T23'
and partition_name like '%2012'
)
loop
stmt := 'alter table t23 truncate partition '
|| lrec.partition_name
;
dbms_output.put_line(stmt);
execute immediate stmt;
end loop;
end;
/
You should definitely run the loop first with execute immediate call commented out, so you can see which partitions your WHERE clause is selecting. Obviously you have a back-up and can recover data you didn't mean to remove. But the quickest way to undertake a restore is not to need one.
Afterwards run this query to see which partitions you should rebuild:
select ip.index_name, ip.partition_name, ip.status
from user_indexes i
join user_ind_partitions ip
on ip.index_name = i.index_name
where i.table_name = 'T23'
and ip.status = 'UNUSABLE';
You can automate the rebuild statements in a similar fashion.
" I am thinking of copying the data of partitions I need into a temp
table and truncate the original table and copy back the data from temp
table to original table. "
That's another way of doing things. With exchange partition it might be quite quick. It might also be slower. It also depends on things like foreign keys and indexes, and the ratio of zapped partitions to retained ones. If performance is important and/or you need to undertake this operation regularly then you should to benchmark the various options and see what works best for you.
You must very be careful in drop partition from a partition table. Partition table usually used for big data tables and if (and only if) you have a global index on the table, drop partition make your global index invalid and you should rebuild your global index in a big table, this is disaster.
For minimum side effect for queries on the table in this scenario, I first delete records in the partition and make it empty partition, then with
ALTER TABLE table_name DROP PARTITION partition_name UPDATE GLOBAL INDEXES;
drop empty partition without make my global index invalid.
I run oracle 11g and need to move table (tbl1) from one tablespace (tblspc1) to another (tblspc2). What is the easiest way to do that?
Try this:-
ALTER TABLE <TABLE NAME to be moved> MOVE TABLESPACE <destination TABLESPACE NAME>
Very nice suggestion from IVAN in comments so thought to add in my answer
Note: this will invalidate all table's indexes. So this command is usually followed by
alter index <owner>."<index_name>" rebuild;
Use sql from sql:
spool output of this to a file:
select 'alter index '||owner||'.'||index_name||' rebuild tablespace TO_TABLESPACE_NAME;' from all_indexes where owner='OWNERNAME';
spoolfile will have something like this:
alter index OWNER.PK_INDEX rebuild tablespace CORRECT_TS_NAME;
Moving tables:
First run:
SELECT 'ALTER TABLE <schema_name>.' || OBJECT_NAME ||' MOVE TABLESPACE '||' <tablespace_name>; '
FROM ALL_OBJECTS
WHERE OWNER = '<schema_name>'
AND OBJECT_TYPE = 'TABLE' <> '<TABLESPACE_NAME>';
-- Or suggested in the comments (did not test it myself)
SELECT 'ALTER TABLE <SCHEMA>.' || TABLE_NAME ||' MOVE TABLESPACE '||' TABLESPACE_NAME>; '
FROM dba_tables
WHERE OWNER = '<SCHEMA>'
AND TABLESPACE_NAME <> '<TABLESPACE_NAME>
Where <schema_name> is the name of the user.
And <tablespace_name> is the destination tablespace.
As a result you get lines like:
ALTER TABLE SCOT.PARTS MOVE TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Moving indexes:
First run:
SELECT 'ALTER INDEX <schema_name>.'||INDEX_NAME||' REBUILD TABLESPACE <tablespace_name>;'
FROM ALL_INDEXES
WHERE OWNER = '<schema_name>'
AND TABLESPACE_NAME NOT LIKE '<tablespace_name>';
The last line in this code could save you a lot of time because it filters out the indexes which are already in the correct tablespace.
As a result you should get something like:
ALTER INDEX SCOT.PARTS_NO_PK REBUILD TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Last but not least, moving LOBs:
First run:
SELECT 'ALTER TABLE <schema_name>.'||LOWER(TABLE_NAME)||' MOVE LOB('||LOWER(COLUMN_NAME)||') STORE AS (TABLESPACE <table_space>);'
FROM DBA_TAB_COLS
WHERE OWNER = '<schema_name>' AND DATA_TYPE like '%LOB%';
This moves the LOB objects to the other tablespace.
As a result you should get something like:
ALTER TABLE SCOT.bin$6t926o3phqjgqkjabaetqg==$0 MOVE LOB(calendar) STORE AS (TABLESPACE USERS);
Paste the results in a script or in a oracle sql developer like application and run it.
O and there is one more thing:
For some reason I wasn't able to move 'DOMAIN' type indexes. As a work around I dropped the index. changed the default tablespace of the user into de desired tablespace. and then recreate the index again.
There is propably a better way but it worked for me.
Try this to move your table (tbl1) to tablespace (tblspc2).
alter table tb11 move tablespace tblspc2;
I tried many scripts but they didn't work for all objects. You can't move clustered objects from one tablespace to another. For that you will have to use expdp, so I will suggest expdp is the best option to move all objects to a different tablespace.
Below is the command:
nohup expdp \"/ as sysdba\" DIRECTORY=test_dir DUMPFILE=users.dmp LOGFILE=users.log TABLESPACES=USERS &
You can check this link for details.
I am trying to export data in oracle 11g
exp user/password file=dump.dmp tables = (table1)
via sqlplus.
And i get the following error:
About to export specified tables via Conventional Path ...
EXP-00011: USER.TABLE1 does not exist Export terminated
successfully with warnings.
But when i check who is the owner of this table:
SELECT owner, table_name from dba_tables where table_name = 'TABLE1';
I get that the owner of TABLE1 is USER
What should i do to export this table?
UPDATE
Actually, i found a solution. I hope it will help someone else.
Since version 11g Oracle has introduced new feature that is called deferred segment creation. Thus oracle doesn't create table segment if there are now rows in it. So i recreated my table with option 'segment creation immediate'
Actually, i found a solution. I hope it will help someone else. Since version 11g Oracle has introduced new feature that is called deferred segment creation. Thus oracle doesn't create table segment if there are no rows in it. And my table didn't contain any data. So i recreated my table with option 'segment creation immediate'
The solution was found here. There are more options how to fix the problem and an explanation why this thing happens to be in oracle 11g. :)
In addition to the posted answer from Olivia i would like to add some code:
SELECT 'alter table ' || table_name || ' allocate extent;'
from dba_tables where SEGMENT_CREATED = 'NO';
Execute the output and exp again. Your schema will be exported including empty tables.
EDIT:
similar question here, maybe you'll find your solution there.
Change this:
exp user/password file=dump.dmp tables = (table1);
to this:
exp user/password tables = (table1) file=dump.dmp;
You Can Use below query to take table level export from specific user.
exp user/password file=dump.dmp tables = user.table1