Unix Technical Forum

Check constraint question

This is a discussion on Check constraint question within the Oracle Miscellaneous forums, part of the Oracle Database category; --> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi - I have a check constraint and I would like to know ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 11:12 AM
Ruben Schoenefeld
 
Posts: n/a
Default Check constraint question

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi -

I have a check constraint and I would like to know if it is going to
run before I hand the script over to the people who will actually
create the tables.

For a signal database I have signal heads (the traffic lights) that
are attached either to a mastarm (which is then attached to a signal
pole) or to a structure (like an overpass, tunnel or so forth). I want
to prevent that the signal head is attached to both.

So I have a table called mastarm, with an ID as the primary key as
well as a structure with an ID as the primary key.
Table signalhead has an ID as the primary key and mastarm_id as well
as structure_id as foreign keys.

create table structure
( id number(10) not null
, ...
, constraint pk_structure primary key (id)
);

create table mastarm
( id number(10) not null
, pole_id number(10) not null
, ...
, constraint pk_mastarm primary key (id)
, constraint fk1_mastarm foreign key (pole_id)
references pole (id)
);

create table signalhead
( id number(10) not null
, structure_id number(10)
, mastarm_id number(10)
, ...
, constraint pk_signalhead primary key (id)
, constraint fk1_signalhead foreign key (structure_id)
references structure (id)
, constraint fk2_signalhead foreign key (mastarm_id)
references mastarm (id)
, constraint ck1_signalhead check
(
(mastarm_id is not null and structure_id is null)
or
(mastarm_id is null and structure_id is not null)
)
);

Is the check constraint ck1_signalhead going to work and do what I
want it to do?

Instead of using an "or" and two "and"s, is there an "xor" that I can
use? Like:
constraint ck1_signalhead check (mastarm_id xor structure_id)

Thanks,
Ruben Schoenefeld

- --
Ruben Schoenefeld, ICQ 1-971-310, http://support.uni-oldenburg.de/~ruben/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFE7LaSKxQwayh8oDERAtARAJ9ufrX4ywXAU0Wa7VmJgc eZiHdRcwCgsol/
kKwipKubvcLYa+CC4lHAzrE=
=fo9U
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 11:13 AM
Ed Prochak
 
Posts: n/a
Default Re: Check constraint question


Ruben Schoenefeld wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi -
>
> I have a check constraint and I would like to know if it is going to
> run before I hand the script over to the people who will actually
> create the tables.
>
> For a signal database I have signal heads (the traffic lights) that
> are attached either to a mastarm (which is then attached to a signal
> pole) or to a structure (like an overpass, tunnel or so forth). I want
> to prevent that the signal head is attached to both.
>
> So I have a table called mastarm, with an ID as the primary key as
> well as a structure with an ID as the primary key.
> Table signalhead has an ID as the primary key and mastarm_id as well
> as structure_id as foreign keys.
>
> create table structure
> ( id number(10) not null
> , ...
> , constraint pk_structure primary key (id)
> );
>
> create table mastarm
> ( id number(10) not null
> , pole_id number(10) not null
> , ...
> , constraint pk_mastarm primary key (id)
> , constraint fk1_mastarm foreign key (pole_id)
> references pole (id)
> );
>
> create table signalhead
> ( id number(10) not null
> , structure_id number(10)
> , mastarm_id number(10)
> , ...
> , constraint pk_signalhead primary key (id)
> , constraint fk1_signalhead foreign key (structure_id)
> references structure (id)
> , constraint fk2_signalhead foreign key (mastarm_id)
> references mastarm (id)
> , constraint ck1_signalhead check
> (
> (mastarm_id is not null and structure_id is null)
> or
> (mastarm_id is null and structure_id is not null)
> )
> );
>
> Is the check constraint ck1_signalhead going to work and do what I
> want it to do?
>
> Instead of using an "or" and two "and"s, is there an "xor" that I can
> use? Like:
> constraint ck1_signalhead check (mastarm_id xor structure_id)
>
> Thanks,
> Ruben Schoenefeld
>
> - --
> Ruben Schoenefeld, ICQ 1-971-310, http://support.uni-oldenburg.de/~ruben/
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (GNU/Linux)
>
> iD8DBQFE7LaSKxQwayh8oDERAtARAJ9ufrX4ywXAU0Wa7VmJgc eZiHdRcwCgsol/
> kKwipKubvcLYa+CC4lHAzrE=
> =fo9U
> -----END PGP SIGNATURE-----


This sounds like a silly question, but it has to be asked:
Did you try it?

Ed

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 11:13 AM
Ruben Schoenefeld
 
Posts: n/a
Default Re: Check constraint question

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Today Ed Prochak wrote in group comp.databases.oracle.misc:

>
> Ruben Schoenefeld wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Hi -
>>
>> I have a check constraint and I would like to know if it is going to
>> run before I hand the script over to the people who will actually
>> create the tables.
>>
>> For a signal database I have signal heads (the traffic lights) that
>> are attached either to a mastarm (which is then attached to a signal
>> pole) or to a structure (like an overpass, tunnel or so forth). I want
>> to prevent that the signal head is attached to both.
>>
>> So I have a table called mastarm, with an ID as the primary key as
>> well as a structure with an ID as the primary key.
>> Table signalhead has an ID as the primary key and mastarm_id as well
>> as structure_id as foreign keys.
>>
>> create table structure
>> ( id number(10) not null
>> , ...
>> , constraint pk_structure primary key (id)
>> );
>>
>> create table mastarm
>> ( id number(10) not null
>> , pole_id number(10) not null
>> , ...
>> , constraint pk_mastarm primary key (id)
>> , constraint fk1_mastarm foreign key (pole_id)
>> references pole (id)
>> );
>>
>> create table signalhead
>> ( id number(10) not null
>> , structure_id number(10)
>> , mastarm_id number(10)
>> , ...
>> , constraint pk_signalhead primary key (id)
>> , constraint fk1_signalhead foreign key (structure_id)
>> references structure (id)
>> , constraint fk2_signalhead foreign key (mastarm_id)
>> references mastarm (id)
>> , constraint ck1_signalhead check
>> (
>> (mastarm_id is not null and structure_id is null)
>> or
>> (mastarm_id is null and structure_id is not null)
>> )
>> );
>>
>> Is the check constraint ck1_signalhead going to work and do what I
>> want it to do?
>>
>> Instead of using an "or" and two "and"s, is there an "xor" that I can
>> use? Like:
>> constraint ck1_signalhead check (mastarm_id xor structure_id)
>>
>> Thanks,
>> Ruben Schoenefeld
>>
>> - --
>> Ruben Schoenefeld, ICQ 1-971-310, http://support.uni-oldenburg.de/~ruben/
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.5 (GNU/Linux)
>>
>> iD8DBQFE7LaSKxQwayh8oDERAtARAJ9ufrX4ywXAU0Wa7VmJgc eZiHdRcwCgsol/
>> kKwipKubvcLYa+CC4lHAzrE=
>> =fo9U
>> -----END PGP SIGNATURE-----

>
> This sounds like a silly question, but it has to be asked:
> Did you try it?
>
> Ed
>
>


Hi Ed -

not a silly question. The point of the post is that I wanted to know
if it's going to work before I even give it to the DBA in charge of
creating the database.

Ruben

- --
Ruben Schoenefeld, ICQ 1-971-310, http://support.uni-oldenburg.de/~ruben/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFE7hVBKxQwayh8oDERAnDpAKCvQoBaKdSkFIlcjMBpZf bTOJryTwCglxkc
Wvs4G2PBmcE5AQ0Whbni8Mk=
=K7kr
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 11:13 AM
Sybrand Bakker
 
Posts: n/a
Default Re: Check constraint question

On Thu, 24 Aug 2006 23:08:09 +0200, Ruben Schoenefeld
<Ruben.Schoenefeld@uni-oldenburg.de> wrote:

>not a silly question. The point of the post is that I wanted to know
>if it's going to work before I even give it to the DBA in charge of
>creating the database.


Ahh, I see, so you want US to try it.
The only thing I remember (from a while ago) is constructs like you
posted do NOT work, and you are limited to using DECODE expressions
returning *boolean* true or false.

--
Sybrand Bakker, Senior Oracle DBA
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 11:13 AM
Andy Hassall
 
Posts: n/a
Default Re: Check constraint question

On Thu, 24 Aug 2006 23:08:09 +0200, Ruben Schoenefeld
<Ruben.Schoenefeld@uni-oldenburg.de> wrote:

>not a silly question. The point of the post is that I wanted to know
>if it's going to work before I even give it to the DBA in charge of
>creating the database.


There are freely downloadable versions of Oracle - it's generally cheaper to
get a computer to validate syntax than it is to ask a newsgroup, or to try and
code "blind" and post the results to someone else.

The full versions of Oracle are available for development, or there's Oracle
XE which is free even for production usage.

The short answers, though:

>>For a signal database I have signal heads (the traffic lights) that
>>are attached either to a mastarm (which is then attached to a signal
>>pole) or to a structure (like an overpass, tunnel or so forth). I want
>>to prevent that the signal head is attached to both.

....
>>, constraint ck1_signalhead check
>> (
>> (mastarm_id is not null and structure_id is null)
>> or
>> (mastarm_id is null and structure_id is not null)
>> )
>>);
>>
>>Is the check constraint ck1_signalhead going to work and do what I
>>want it to do?


Yes, looks like it. I haven't tried it in an Oracle database either, but it
looks both straightforward and correct to me.

>>Instead of using an "or" and two "and"s, is there an "xor" that I can
>>use? Like:
>>constraint ck1_signalhead check (mastarm_id xor structure_id)


Curiously, no, Oracle haven't implemented an xor operator; an omission on
their part which you have worked around correctly.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-08-2008, 11:13 AM
Andy Hassall
 
Posts: n/a
Default Re: Check constraint question

On Fri, 25 Aug 2006 01:24:52 +0200, Sybrand Bakker <postbus@sybrandb.demon.nl>
wrote:

>>not a silly question. The point of the post is that I wanted to know
>>if it's going to work before I even give it to the DBA in charge of
>>creating the database.

>
>Ahh, I see, so you want US to try it.


This remonstration is valid; the OP should try it first on his own database,
not ask other people to run what his own computer can run.

>The only thing I remember (from a while ago) is constructs like you
>posted do NOT work, and you are limited to using DECODE expressions
>returning *boolean* true or false.


The OP's expression does evauluate to Boolean true or false; it's a Boolean
expression. It WILL work.

A DECODE statement can NEVER return true or false, as there is no Boolean type
on the SQL level, only on the PL/SQL level. A DECODE will NOT work as the
subject of a check constraint - you'll just get "ORA-00920: invalid relational
operator"

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-08-2008, 11:13 AM
Ed Prochak
 
Posts: n/a
Default Re: Check constraint question


Andy Hassall wrote:
> On Thu, 24 Aug 2006 23:08:09 +0200, Ruben Schoenefeld
> <Ruben.Schoenefeld@uni-oldenburg.de> wrote:
>

[]

> >>Instead of using an "or" and two "and"s, is there an "xor" that I can
> >>use? Like:
> >>constraint ck1_signalhead check (mastarm_id xor structure_id)

>
> Curiously, no, Oracle haven't implemented an xor operator; an omission on
> their part which you have worked around correctly.


Not an ommision by Oracle. There is no such operator in the SQL
standard.

It might be a nice enhancement though.

Ed

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-08-2008, 11:13 AM
DA Morgan
 
Posts: n/a
Default Re: Check constraint question

Ed Prochak wrote:
> Andy Hassall wrote:
>> On Thu, 24 Aug 2006 23:08:09 +0200, Ruben Schoenefeld
>> <Ruben.Schoenefeld@uni-oldenburg.de> wrote:
>>

> []
>
>>>> Instead of using an "or" and two "and"s, is there an "xor" that I can
>>>> use? Like:
>>>> constraint ck1_signalhead check (mastarm_id xor structure_id)

>> Curiously, no, Oracle haven't implemented an xor operator; an omission on
>> their part which you have worked around correctly.

>
> Not an ommision by Oracle. There is no such operator in the SQL
> standard.
>
> It might be a nice enhancement though.
>
> Ed


I'm not sure that is true though I'll readily acknowledge that I
have yet to test it.

$ cd $ORACLE_HOME/rdbms/admin
$ more stdspec.sql

There you will find, in the definition of the package STANDARD, the
following spec.

function XOR (LEFT BOOLEAN, RIGHT BOOLEAN) return BOOLEAN;
pragma BUILTIN('XOR',8, 3, 9); -- PEMS_INTEGER, INT_XOR
pragma FIPSFLAG('XOR', 1450);

Right between >= and NOT ... so I expect we have it but just
don't have it documented.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
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 12:48 AM.


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