Extract Table(s) having name in numbers - oracle

I am using Oracle developer , my DB have 150+ tables having different namings
i Want to extract all tables having names like
tbl_1234
tbl_22
tbl_45
tbl_719
All tables whose naming convention is like " table name , underscore , number "
Pleaseee help me on this

try following query:
select table_name from user_tables where regexp_like (table_name, '_[0-9]+$');
and you can use, of course, the all_tables or dba_tables view, if you have approriate rights

Related

How to list all tables that have a matching string in its name

In Oracle DB, how to list all tables that exist in a schema with the table names having a substring like Student? Say you have a list of tables like College_student, Student_Offer or Student_Dept etc..
SELECT table_name
FROM all_tables
WHERE owner = :owner
AND upper(table_name) LIKE '%STUDENT%';
we upper the name first in the predicate because some people insist on case sensitive object names in Oracle.
I run this with STU vs STUDENT in the LIKE search and see these results -
And since you tagged SQL Developer - you can simply browse a schema using the connection tree and optionally add a filter on the name.
You may query the all_tables table:
SELECT table_name
FROM all_tables
WHERE table_name LIKE '%student%';

how to extract tables attributes like column name , datatype ,nullable for all the tables from the database from oracle pl/sql

Is there any query that can be used to retrive the Tables and its column attributes like column name , datatype, nullable etc for all the tables inside the database
For Oracle Pl/SQL
The Oracle SQL you need would be the following (run as user 'SYS'):
select owner, table_name, column_name, data_type, nullable
from dba_tab_columns;
If you do a desc dba_tab_columns you will get a list of many more columns which may be of interest to you as part of your result set.
You can use a SQL tool (i.e. SQL*Plus) to run this query or you can use PL/SQL to call this query and put the results in PL/SQL variables then print them out via DBMS_OUTPUT.PUT_LINE().
HTH

How to change lots table columns datatypes from one to another in oracle

I have a table with a lots of columns with BLOB type and I need to change it to nvarchar2.
So, to change type I can use following script:
alter table AUDIT_LOG
modify
(
column_name type_name,
column_name2 type_name2
-- etc
);
And to get all columns with given datatype I can use the following:
select column_name, 'NVARCHAR2(4000)'
from all_tab_columns
where table_name = 'TAB_NAME' and data_type = 'BLOB';
But how to join this two scripts into one?
You cannot do DML and DDL operation together in same query. You have to use dynamic SQL in a PL/SQL block
Create a variable and generate the whole alter table query in it.
Execute Immidiate
Refer this and I am sure you will be able to add rest of the logic as per your requirement.
http://www.java2s.com/Tutorial/Oracle/0440__PL-SQL-Statements/EXECUTEIMMEDIATEdynamicsqltoaltersession.htm

The different between TABLE_NAME vs "TABLE_NAME" in Oracle?

I'm a very new Oracle SQL user and I got a problem.
If I create a table with code such as
create table TBL_NAME...
after that I can get data by
select * from TBL_NAME;
However, when I create table with Navicat (just click on button new table), then I have to add "" to table name to access my table, such as:
select * from "TBL_NAME";
So, is there have other type of table? And if I use Navicat to create table, what type of it?
When you specify the name of a table in Oracle without double quotes then Oracle converts that table name to uppercase. But if you specify the name of the table within double-quotes then Oracle will respect the lower-case letters that you may have.
So, as in your example the name of your table is already all upper-case, then there is no difference in specifying or not the double-quotes.
But for example if you create a table like this:
CREATE TABLE "my_table" ....
Then you cannot access it like this:
SELECT * FROM my_table;
as Oracle will convert that select to this:
SELECT * FROM MY_TABLE;
And there is no such table in your system.
In your case with Navicat, it just needs you to specify the name of the table as-is, but don't worry, just put the double-quotes and stick to all upper-case names and you will be fine.

How do I get all records associated with a record in an aggregate root table

For testing purposes, I would like to generate an insert script for all records in all tables associated with a particular record in one of the root tables. For example, I might have a "Participant" table, which has any number of associated entries in the "Documents" table, which in turn has any number of associated entries in the "PrintRequests" table and so on and so forth. I have hundreds of these tables in the database.
Is there any way to select/script out all the records in all tables that are associated with for example ParticipantId = 1? This way, for a representative participant, I can extract all the associated records in all the tables.
One of my ideas was to restore a back up of the full database, modify all foreign key constraints to have cascade delete and then delete everything that is not participantid = 1 and let the database take care of deleting everything that is not related to the participant of interest and then script out the entire database of what remains.
For this, I might have to drop and recreate all the constraints, which I am unsure about how to do across the entire database.
Alternately, are there any other tools that would be able to do this? A migration tool for example that can take a query and only migrate the records and associated child records of that query?
While it is entirely possible to build scripts to walk through all the primary key and foreign key constraints and, via liberal use of dynamic SQL, generate these scripts, doing so would be a non-trivial undertaking. I would strongly suspect that you would be better served using a product like DataBee to generate your data subset.
You can script it creating dynamic SQL statements, but I think it is really lots of work. I think you will be faster to just find all the tables with a column "ParticipantId" with something like this
select * from all_tab_columns where column_name = 'PARTICIPANTID'
And then do some fast edit / replace / other sort of script action to generate yourself the delete statements.
Regarding the constraints. This is similar. Getting all the constraints with
SELECT owner, table_name, constraint_name
FROM dba_constraints
where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')
You switch on and off constraints using
ALTER TABLE <table name> ENABLE/DISABLE constraint <constraint name>;
Maybe this you could do with a loop. Borrowing from this page
begin
for i in
(select constraint_name, table_name from user_constraints where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')
) LOOP
execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
end loop;
end;
I am not sure about your cascading delete thing but the above gives a bit an idea how the delete would look like:
begin
for i in
(select constraint_name, table_name from user_constraints where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')
) LOOP
execute immediate 'delete from '||i.table_name||' where participantid = ''1'' ';
end loop;
end;
Hope it helps.

Resources