I want to get all the canstraint_name so i could generate there ddl in java:
for each canstraint_name i'm going to execute :
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'CONSTRAINTS',true);
DBMS_METADATA.GET_DDL('CONSTRAINT','Constraint_name','hr');
I tried to execute this statement :
selectSQL = "SELECT CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE OWNER='" +Schemas+ "'";
I get a long list of constraints including invalid ones like: pdpzZlEqTgUxb7qMBM8w==$0
It generates an error when executing :
DBMS_METADATA.GET_DDL('CONSTRAINT','pdpzZlEqTgUxb7qMBM8w==$0','hr')
can't find pdpzZlEqTgUxb7qMBM8w==$0 in Schemas='hr' (the error it's in French)
The error message from GET_DDL suggest that those constraints belong to dropped tables (although those names start with BIN$). You cannot retrieve the DDL for those. If you don't want them you can do a
purge recyclebin;
before retrieving the constraints from user_constraints.
Or exclude them when retrieving the list of constraints:
SELECT constraint_name
FROM user_constraints
WHERE constraint_name NOT LIKE 'BIN$%';
This query:
SELECT CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE OWNER='" +Schemas+ "'";
doesn't really make sense. USER_CONSTRAINTS will only list constraints owned by the current user. You will never get constraints from a different schema when using USER_CONSTRAINTS.
If you want to get constraints from a different schema, you need to use ALL_CONSTRAINTS.
If you do want the current user's constraints, you can safely remove the WHERE clause.
Related
I need a way to figure out how to get the list of columns in alphabetical order when using the DESCRIBE command in SQL Developer. Something like desc table order by Name; this won't work of course but something along these lines.
a_horse_with_no_name and Littlefoot are BOTH right, but where does that leave you?
If you'd like, you can create a new command that will get you what you want.
In SQLcl -
SQL> alias alphadesc=select column_name, data_type, column_id
2 from user_tab_columns
3 where table_name = upper(:tablename)
4* order by column_name;
We can take advantage of the ALIAS command, used to basically create shortcuts for predefined bits of SQL or PL/SQL.
you're in sql developer - you could do this there as well, either using the ALIAS feature (would need to go into your login default script for connections, or you could create a code template)
PS SQLcl is our modern take on SQLPlus, it's available as a small standalone, but it's also in your SQL Developer / bin directory
You can't change the way the result of a DESCRIBE command are displayed, but you could query the system catalog directly:
select column_name, data_type, column_id
from user_tab_columns
where table_name = 'YOUR_TABLE'
order by column_name
If the current user does not own the table you are looking at, use all_tab_columns but you have to provide the owner name as well:
select column_name, data_type, column_id
from all_tab_columns
where table_name = 'YOUR_TABLE'
and owner = 'SOME_USER'
order by column_name
I presume you want to query USER_TAB_COLUMNS.
In its simplest way, that would be
select *
from user_tab_columns
where table_name = 'SOME_TABLE_NAME'
order by column_name;
I created a table in oracle like
CREATE TABLE suppliers AS (SELECT * FROM companies WHERE id > 1000);
I would like to know the complete select statement which was used to create this table.
I have already tried get_ddl but it is not giving the select statement. Can you please let me know how to get the select statement?
If you're lucky one of these statements will show the DDL used to generate the table:
select *
from gv$sql
where lower(sql_fulltext) like '%create table suppliers%';
select *
from dba_hist_sqltext
where lower(sql_text) like '%create table%';
I used the word lucky because GV$SQL will usually only have results for a few hours or days, until the data is purged from the shared pool. DBA_HIST_SQLTEXT will only help if you have AWR enabled, the statement was run in the last X days that AWR is configured to hold data (the default is 8), the statement was run after the last snapshot collection (by default it happens every hour), and the statement ran long enough for AWR to think it's worth saving.
And for each table Oracle does not always store the full SQL. For security reasons, DDL statements are often truncated in the data dictionary. Don't be surprised if the text suddenly cuts off after the first N characters.
And depending on how the SQL is called the case and space may be different. Use lower and lots of wildcards to increase the chance of finding the statement.
TRY THIS:
select distinct table_name
from
all_tab_columns where column_name in
(
select column_name from
all_tab_columns
where table_name ='SUPPLIERS'
)
you can find table which created from table
I had to use temporally the enabling cascade to fix some problems in my DB.
I used this alter:
ALTER TABLE table
DISABLE PRIMARY KEY CASCADE;
And now I'd want to undo it and enable all the constraints again. Have I got to enable them one by one?
Yes, you have to re-enable the the dependent constraints one by one. There is no magic cascade option for the re-enabling of the constraint.
Here is a quote from Darl Kuhn's "Pro Oracle Database 12c Administration" book. The information applies to Oracle 11g as well:
Keep in mind that there is no ENABLE ... CASCADE statement. To reenable the constraints, you have to query the data dictionary to determine which constraints have been disabled and then reenable them individually.
In line with that quote, przemo_pl has provided you a good answer to minimize the pain of handling your use case.
I don't know any way of automatic enabling constraints in a cascade way, even tried to google that but no results of any value.
So this is what I would do:
Go and ask dba_constraints for all constraints referencing this primary key:
select *
from dba_constraints
connect by prior constraint_name = r_constraint_name
start with constraint_name = '<your_primary_key_constraint>';
Just double check that those are the ones you should enable and create a script to enable them all:
select 'alter table ' || table_name || ' enable ' || constraint_name || ';'
from dba_constraints
connect by prior constraint_name = r_constraint_name
start with constraint_name = '<your_primary_key_constraint>'
where c.status = 'DISABLED';
and just run it...
How does Oracle use COLUMN_ID as found in USER_TAB_COLUMNS view? I just need to confirm that it does not use this internal column ordering while creating implicit indexes - such as when a primary key is enforced or a unique key constraint is created (that is key/constraints columns provided are used in the same order - left to right and not these internal column ordering). (if possible please point me in the direction of Oracle documentation.). Thanks in advance.
It'll be hard to find something stating that it doesn't do something, but there isn't anything stating that it will use column_id to override the index creation.
You can see all the reference to column_id in the documentation here; the only one that seems matter is the all_tab_columns view.
You can verify the order of the columns as used in the index by querying the all_ind_columns view, where you will be able to see that there is no enforced relationship between its column_position - which comes from the order the columns are listed in the index creation command - and column_id.
If you are specifically interested in checking indexes that back up constraints, you can do something like:
select ac.owner, ac.table_name, ac.constraint_name, ac.index_owner,
ac.index_name, aic.column_position, aic.column_name
from all_constraints ac
join all_ind_columns aic on aic.index_owner = coalesce(ac.index_owner, ac.owner)
and aic.index_name = ac.index_name
order by 1, 2, 3, 6;
... adding filters for owner or table as needed.
All I know about the constraint is it's name (SYS_C003415), but I want to see it's definition.
Looks like I should be querying ALL_CONSTRAINTS.
select OWNER, CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME, SEARCH_CONDITION from ALL_CONSTRAINTS where CONSTRAINT_NAME = 'SYS_C003415';
Another option would be to reverse engineer the DDL...
DBMS_METADATA.GET_DDL('CONSTRAINT', 'SYS_C003415')
Some examples here....
http://www.psoug.org/reference/dbms_metadata.html
Use following query to get a definition of constraint in oracle:
Select DBMS_METADATA.GET_DDL('CONSTRAINT', 'CONSTRAINT_NAME') from dual
Or to see all constaints use SYS.DBA_CONSTRAINTS (If you have the privileges)