Unix Technical Forum

How to find automatically generated "not null" checks in user_constraints?

This is a discussion on How to find automatically generated "not null" checks in user_constraints? within the Oracle Database forums, part of the Database Server Software category; --> Hello, When I create a table with a not null column, Oracle implicitly creates a check constraint "<field> is ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-24-2008, 10:36 AM
Piotr B.
 
Posts: n/a
Default How to find automatically generated "not null" checks in user_constraints?

Hello,

When I create a table with a not null column, Oracle implicitly
creates a check constraint "<field> is not null":

> create table AAA (B number(10) not null);
> select * from user_constraints where table_name = 'AAA';

constraint_name: SYS_C00114059
constraint_type: 'C'
search_condition: '"B" IS NOT NULL'

Since the information is already stored in user_tab_columns.nullable,
I want to omit these rows from user_constraints.
The problem is I cannot issue such query:

> select * from user_constraints where constraint_type = 'C'

and search_condition not like '% IS NOT NULL';

....because search_condition is of type LONG:

ORA-00932: inconsistent datatypes: expected NUMBER got LONG

I've tried to cast search_condition to string using TO_CHAR() and
CAST(... AS VARCHAR2(2000)), but I was still getting the same error.

How to solve it? I'd prefer not to create any PL/SQL functions/procedures,
since I've got read-only access to the database...

Thank you!

PS. I use Oracle9i 9.2.0.1.0.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-24-2008, 10:36 AM
nomorekiss@yahoo.com
 
Posts: n/a
Default Re: How to find automatically generated "not null" checks in user_constraints?

http://www.dbasupport.com/forums/sho...threadid=39607

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-24-2008, 10:36 AM
nomorekiss@yahoo.com
 
Posts: n/a
Default Re: How to find automatically generated "not null" checks in user_constraints?

from http://asktom.oracle.com :

GOTO a page to Bookmark Review | Bottom | Top
How to deal with long for this query June 24, 2003
Reviewer: Yong from CA, USA

Tom,

Search_condition is long type in Oracle data dictionary. How can I
construct a
query to get the following results without set a condition on a long
type

SQL> select constraint_name from user_constraints
2 where search_condition not like '%NOT NULL';
where search_condition not like '%NOT NULL'
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected NUMBER got LONG




Followup:

ops$tkyte@ORA920LAP> create or replace function get_search_condition(
p_cons_name in varchar2 ) return varchar2
2 authid current_user
3 is
4 l_search_condition user_constraints.search_condition%type;
5 begin
6 select search_condition into l_search_condition
7 from user_constraints
8 where constraint_name = p_cons_name;
9
10 return l_search_condition;
11 end;
12 /

Function created.

ops$tkyte@ORA920LAP>
ops$tkyte@ORA920LAP> select constraint_name
2 from user_constraints
3 where get_search_condition(constraint_name) like '%NOT NULL%';

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-24-2008, 10:36 AM
Piotr B.
 
Posts: n/a
Default Thanks, but...

nomorekiss@yahoo.com wrote:
> ops$tkyte@ORA920LAP> create or replace function get_search_condition(
> p_cons_name in varchar2 ) return varchar2
> 2 authid current_user
> 3 is
> 4 l_search_condition user_constraints.search_condition%type;
> 5 begin
> 6 select search_condition into l_search_condition
> 7 from user_constraints
> 8 where constraint_name = p_cons_name;
> 9
> 10 return l_search_condition;
> 11 end;
> 12 /


Thanks, but:

Piotr B. wrote:
> I'd prefer not to create any PL/SQL functions/procedures,
> since I've got read-only access to the database...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-24-2008, 10:37 AM
Mark D Powell
 
Posts: n/a
Default Re: Thanks, but...

This is not a direct answer to the posted question which has been
answered but it may also be of interest that all_, dba_, and
user_tab_columns contain a column named NULLABLE that can be used to
determine the NULL allowed status of a column.

HTH -- Mark D Powell --

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-24-2008, 10:37 AM
Piotr B.
 
Posts: n/a
Default Re: Thanks, but... [2]

Mark D Powell wrote:
> it may also be of interest that all_, dba_, and user_tab_columns
> contain a column named NULLABLE that can be used to
> determine the NULL allowed status of a column.


Yes, I mentioned this fact in my first post:
> the information is already stored in user_tab_columns.nullable


Unfortunately, my problem remains unsolved. I want to write some
application to export database schema from Oracle, and I don't
see an easy way to distinguish "real", user-added check constraints
(like "GENDER IN ('M', 'F')") from automatically generated and
redundant "column IS NOT NULL" constraints for every non-null column.

Creating PL/SQL procedures/function to cast
user_constraints.search_condition to varchar2 in order to apply
"NOT LIKE '% IS NOT NULL'" is not possible when you have read-only
access to the database...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-24-2008, 10:37 AM
Sybrand Bakker
 
Posts: n/a
Default Re: Thanks, but... [2]

On Fri, 28 Jan 2005 18:33:52 +0100, "Piotr B."
<piotrb@nie.spamuj.mnie> wrote:

>Unfortunately, my problem remains unsolved. I want to write some
>application to export database schema from Oracle, and I don't
>see an easy way to distinguish "real", user-added check constraints
>(like "GENDER IN ('M', 'F')") from automatically generated and
>redundant "column IS NOT NULL" constraints for every non-null column.


The constraints are not redundant.
Also the constraint name will always start with SYS_C
You also shouldn't 'write some application' as Oracle comes with
a export in any version
b dbms_metadata in 9i and higher

Looks like you would better concentrate on getting familiar with
Oracle, than reinventing the wheel.


--
Sybrand Bakker, Senior Oracle DBA
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-24-2008, 10:38 AM
DA Morgan
 
Posts: n/a
Default Re: Thanks, but... [2]

Piotr B. wrote:

> Mark D Powell wrote:
>
>> it may also be of interest that all_, dba_, and user_tab_columns

>
> > contain a column named NULLABLE that can be used to

>
>> determine the NULL allowed status of a column.

>
>
> Yes, I mentioned this fact in my first post:
> > the information is already stored in user_tab_columns.nullable

>
> Unfortunately, my problem remains unsolved. I want to write some
> application to export database schema from Oracle, and I don't
> see an easy way to distinguish "real", user-added check constraints
> (like "GENDER IN ('M', 'F')") from automatically generated and
> redundant "column IS NOT NULL" constraints for every non-null column.
>
> Creating PL/SQL procedures/function to cast
> user_constraints.search_condition to varchar2 in order to apply
> "NOT LIKE '% IS NOT NULL'" is not possible when you have read-only
> access to the database...


What is the point of this exercise? There is not a bit of difference
between a NOT NULL and a NOT NULL.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace 'x' with 'u' to respond)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-24-2008, 10:40 AM
Mark D Powell
 
Posts: n/a
Default Re: Thanks, but... [2]

As Sybrand points out dbms_metadata can be used to generate the souce
as can a full export with no rows plus some edit scripts to extract or
reformat the code.

As Daniel points our a "NOT NULL" is a not null so why not just use the
"nullable" column to always generate a "not null" entry in the create
table? Then you could ignore all type "C" not null constraints.
HTH -- Mark D Powell --

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:13 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com