473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: Conditional Constraint?

Hi Wing,

The utility Oracle offers for that is *check constraints*.

For instance :

ALTER TABLE Demerit ADD CONSTRAINT a1 CHECK (points between 1 and 6);

ALTER TABLE Demerit ADD CONSTRAINT a2 CHECK (dem_code <= 20 or
dem_code 20 and points <=3);

Will implement the rules you mention.

Also,

ALTER TABLE Demerit ADD CONSTRAINT a3 CHECK ( c2 between 1 and 6 and
(c1 <= 20 or c220 and c2 <=3) );

will implement a1 and a2 above in one constraint, but is less
readable.
Read the doc to get the full syntax and full *understanding* of check
constraints. They are not good for all purposes, in fact misusing them
is equal to painting yourself into the corner.

Happy Christmas
- Kenneth Koenraadt


wi******@witty.com (wing) wrote in message news:<87**************************@posting.google. com>...
Hi,

I am new in Oracle and have a query on how to add conditional
constraint.

Say, I have a simple table Demerit with three fields.

Demerit(DEM_CODE, DEM_DES, POINTS)
where
DEM_CODE (N, 2)
DEM_DES (C, 30)
POINTS (N, 1)

How to add the following constraint?

All DEM_CODE should carry an integer POINT between 1 to 6 inclusive,
and DEM_CODE greater than 20 should carry no more than 3 POINT.

Thanks in advance of any ideas and inputs.

Wing
Jun 27 '08 #1
2 2491
Hi Wing,

I'm with Kenneth and the "constraint camp" on this one. This is most
appropriately resolved through the use of constraints rather than a
trigger. For a number of reasons. It's easier to code and handle error
conditions, it's potentially more efficient and the CBO may have more to go
on (although admittedly, it's not too smart when it comes to "complex" check
constraints).

The only thing I would add to what Kenneth and Jonathon have suggested
is that I would personally recommend going for option 1 (2 constraints)
rather than option 2 (1 complicated constraint). The reason being that
if the constraint were to be violated, if they were handled separately,
it would be obvious what the issue was. If you had them grouped
together, it might require further investigation to determine the exact
cause of the violation.

Merry Xmas everyone !!

Cheers

Richard

"Kenneth Koenraadt" <pl******@mail-online.dkwrote in message
news:25**************************@posting.google.c om...
Hi Wing,

The utility Oracle offers for that is *check constraints*.

For instance :

ALTER TABLE Demerit ADD CONSTRAINT a1 CHECK (points between 1 and 6);

ALTER TABLE Demerit ADD CONSTRAINT a2 CHECK (dem_code <= 20 or
dem_code 20 and points <=3);

Will implement the rules you mention.

Also,

ALTER TABLE Demerit ADD CONSTRAINT a3 CHECK ( c2 between 1 and 6 and
(c1 <= 20 or c220 and c2 <=3) );

will implement a1 and a2 above in one constraint, but is less
readable.
Read the doc to get the full syntax and full *understanding* of check
constraints. They are not good for all purposes, in fact misusing them
is equal to painting yourself into the corner.

Happy Christmas
- Kenneth Koenraadt


wi******@witty.com (wing) wrote in message
news:<87**************************@posting.google. com>...
Hi,

I am new in Oracle and have a query on how to add conditional
constraint.

Say, I have a simple table Demerit with three fields.

Demerit(DEM_CODE, DEM_DES, POINTS)
where
DEM_CODE (N, 2)
DEM_DES (C, 30)
POINTS (N, 1)

How to add the following constraint?

All DEM_CODE should carry an integer POINT between 1 to 6 inclusive,
and DEM_CODE greater than 20 should carry no more than 3 POINT.

Thanks in advance of any ideas and inputs.

Wing

Jun 27 '08 #2
Richard Foote wrote:
Hi Wing,

I'm with Kenneth and the "constraint camp" on this one. This is most
appropriately resolved through the use of constraints rather than a
trigger. For a number of reasons. It's easier to code and handle error
conditions, it's potentially more efficient and the CBO may have more to go
on (although admittedly, it's not too smart when it comes to "complex" check
constraints).

The only thing I would add to what Kenneth and Jonathon have suggested
is that I would personally recommend going for option 1 (2 constraints)
rather than option 2 (1 complicated constraint). The reason being that
if the constraint were to be violated, if they were handled separately,
it would be obvious what the issue was. If you had them grouped
together, it might require further investigation to determine the exact
cause of the violation.

Merry Xmas everyone !!

Cheers

Richard

"Kenneth Koenraadt" <pl******@mail-online.dkwrote in message
news:25**************************@posting.google.c om...
Hi Wing,

The utility Oracle offers for that is *check constraints*.

For instance :

ALTER TABLE Demerit ADD CONSTRAINT a1 CHECK (points between 1 and 6);

ALTER TABLE Demerit ADD CONSTRAINT a2 CHECK (dem_code <= 20 or
dem_code 20 and points <=3);

Will implement the rules you mention.

Also,

ALTER TABLE Demerit ADD CONSTRAINT a3 CHECK ( c2 between 1 and 6 and
(c1 <= 20 or c220 and c2 <=3) );

will implement a1 and a2 above in one constraint, but is less
readable.
Read the doc to get the full syntax and full *understanding* of check
constraints. They are not good for all purposes, in fact misusing them
is equal to painting yourself into the corner.

Happy Christmas
- Kenneth Koenraadt


wi******@witty.com (wing) wrote in message
news:<87**************************@posting.google. com>...
Hi,
>
I am new in Oracle and have a query on how to add conditional
constraint.
>
Say, I have a simple table Demerit with three fields.
>
Demerit(DEM_CODE, DEM_DES, POINTS)
where
DEM_CODE (N, 2)
DEM_DES (C, 30)
POINTS (N, 1)
>
How to add the following constraint?
>
All DEM_CODE should carry an integer POINT between 1 to 6 inclusive,
and DEM_CODE greater than 20 should carry no more than 3 POINT.
>
Thanks in advance of any ideas and inputs.
>
Wing
Now that I can see the examples ... I too would agree. Go with the constraint
not the trigger.

Dan Morgan

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Ney André de Mello Zunino | last post by:
Hello. I have noticed, in a lot of C and C++ code, that many programmers seem to prefer putting the test values first in conditional expressions. I.e., they would rather write this: if (-1 ==...
10
by: Ilik | last post by:
Hi all I'm trying to create a summery table of rain data that holds a record for every 0.1mm of rain in the following format: Station Name, Time, Value A, 2002-12-03 14:44:41.000, 0.1 A,...
3
by: pdlevine | last post by:
Hi, I need to enforce that a table does not have "duplicates" for a specific status type in the table. If the column "STATUS" = 2, then there can not be more than one row with a specific "ID"...
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
0
by: negmat | last post by:
Guys, Does SQL Server allow for creating conditional constraints on the table columns? My table is: CREATE TABLE ( IDENTITY (1, 1) NOT NULL ,
3
by: askohen | last post by:
I have this CREATE TABLE statement: CREATE TABLE howto ( id INT IDENTITY(1,1) PRIMARY KEY, title VARCHAR(100) UNIQUE NOT NULL, url VARCHAR(20) UNIQUE NOT NULL, order_on_homepage SMALLINT...
2
by: DA Morgan | last post by:
wing wrote: What you are trying to do requires a trigger. I would suggest a BEFORE INSERT AND UPDATE trigger that fires for each row. I would also suggest that in the future you: 1. List...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.