How do I find the definition of a named constraint in Oracle? - oracle

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)

Related

I cannot use values claus with subquery?

I need this information for my task. I can not use alternative. I know that I can use subquery like this:
insert into (select id from test) (select employee_id FROM employees);
But this is not what I want. I tasked to finde the answer to this question. Please help me. Thanks everybody.
You should use like the below without using values clause in INSERT
Insert into test
select employee_id FROM employees;

How to order by column name when getting a list of columns their data types using DESCRIBE in SQL Developer?

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;

Invalid Constraint name when selecting CONSTRAINT_NAME from USER_CONSTRAINTS

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.

How do I check if a sequence exists or not in Oracle 11g?

I am using Oracle 11g. I want to be able to determine whether a particular sequence exists or not. I have tried the code below but it is not working. (It is returning 0 as count value when there should be more):
SELECT COUNT(*)
FROM user_sequences
WHERE sequence_name = 'SCHEMA.SEQUENCE_NAME';
If anyone knows why this is, please help me.
If you are running the query as user MP then try it like this:
SELECT COUNT(*)
FROM user_sequences
WHERE sequence_name = 'SEQ_SSO_KEY_AUTHENTICATION';
else, try it like this:
SELECT COUNT(*)
FROM all_sequences
WHERE sequence_name = 'SEQ_SSO_KEY_AUTHENTICATION'
AND sequence_owner = 'MP' ;
Also, keep in mind that you may not be granted to see all sequences in DB.
In this case scripts provided above may not work, and you should run something like
SELECT COUNT(*) FROM DBA_SEQUENCES;
But this also may not work if you have no access to DBA_SEQUENCES view.
Check Oracle docs.
For newer/11g versions, the following query works. One suggestion, I couldn't initially make it work because I was using lowercase letters for sequence names. Making it all caps returned expected value
SELECT * FROM all_objects ao WHERE ao.owner = 'SCHEMA_NAME' AND ao.OBJECT_TYPE ='SEQUENCE'
AND ao.OBJECT_NAME ='CAPITAL_SEQ_NAME';

Oracle get column data size

I'm new to Oracle db, and i wonder if i can find out the datatype and datasize...
I mean : let's say i have the column "location" of type varchar2(20) in table "products".
Could i write a query that would return me in the result "varchar2" and "20" ?
Thank you!
Check out user_tab_columns.
select data_type, data_length
from user_tab_columns
where table_name = 'PRODUCTS'
and column_name = 'LOCATION';
Do you need to find out the type and size of a column for the purpose of understanding the data, or are you writing a program that needs to know it at runtime?
If you only need to know it for your knowledge, typing desc tablename in SQLPlus will tell you about the columns in the table.
If you need to find out the type and size programmatically, what language are you using ... PL/SQL? Java?
Note that selecting from USER_TAB_COLUMNS will only list the columns in tables in your own schema. For other tables you'll need to look at ALL_TAB_COLUMNS (or DBA_TAB_COLUMNS, if the DBA has allowed you to access it). Note that your employer or client may disallow stored procedures or packages from being compiled against either.
SELECT CHAR_LENGTH FROM USER_TAB_COLUMNS WHERE TABLE_NAME = <TABLENAME> AND COLUMN_NAME = <COLUMNNAME>;
The answer to this question was useful to me. But, there is one important difference needs to be considered while taking data_length when the column type is NVARCAHR2. The data_length returned for NVARCHA2 column is doubled.
This is due to the character encoding being used.
Oracle documentation says:
The number of bytes can be up to two times size for AL16UTF16
encoding and three times size for UTF8 encoding.
So, ideal query would be the following:
select table_name, column_name,
data_type, NVL(char_col_decl_length, data_length)
from user_tab_columns where table_name='T51_NOCOMP';
I do the following from sqlplus for oracle:
SELECT COLUMN_NAME,DATA_TYPE,DATA_LENGTH
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'YOUR_TABLE_NAME'
AND COLUMN_NAME = 'YOUR';`
Hope it works for you too!

Resources