473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can constraint replace a trigger?

Hi everybody.
I wrote a trigger for the follwing requirement:
In a given table T with columns A,B,C are up to 5 entries allowed for a given
combination
of (A,B).
create table t (a varchar2(10), b number, c number,
constraint t_pk primary key (a,b,c));

insert into t(a,b,c) values ('A', 1, 1);
insert into t(a,b,c) values ('A', 1, 4);
insert into t(a,b,c) values ('A', 1, 99);
insert into t(a,b,c) values ('A', 1, 1000);
insert into t(a,b,c) values ('A', 1, 3);

Next insert should raise an error, because
-- it is the 6th row with a='A' and b=1
insert into t(a,b,c) values ('A', 1, 15);

Can constraint in DB2 to replace a trigger?
Thank's in advance
Leny G.

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200809/1

Sep 12 '08 #1
7 3307
lenygold via DBMonster.com wrote:
Hi everybody.
I wrote a trigger for the follwing requirement:
In a given table T with columns A,B,C are up to 5 entries allowed for a given
combination
of (A,B).
create table t (a varchar2(10), b number, c number,
constraint t_pk primary key (a,b,c));

insert into t(a,b,c) values ('A', 1, 1);
insert into t(a,b,c) values ('A', 1, 4);
insert into t(a,b,c) values ('A', 1, 99);
insert into t(a,b,c) values ('A', 1, 1000);
insert into t(a,b,c) values ('A', 1, 3);

Next insert should raise an error, because
-- it is the 6th row with a='A' and b=1
insert into t(a,b,c) values ('A', 1, 15);

Can constraint in DB2 to replace a trigger?
No. A check constraint in DB2 (and most other DBMS I know of) operates
on a single row only.
To make this work you would need to define an MQT and then a check
constraint on that. That is not allowed today.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 12 '08 #2
Thank ypu Serge.
Trigger is working fine.
What about to create extra column with concatenated values A and B.
Can contraint be used to this new column?

Serge Rielau wrote:
>Hi everybody.
I wrote a trigger for the follwing requirement:
[quoted text clipped - 15 lines]
>>
Can constraint in DB2 to replace a trigger?
No. A check constraint in DB2 (and most other DBMS I know of) operates
on a single row only.
To make this work you would need to define an MQT and then a check
constraint on that. That is not allowed today.

Cheers
Serge
--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200809/1

Sep 12 '08 #3
lenygold via DBMonster.com wrote:
Thank ypu Serge.
Trigger is working fine.
What about to create extra column with concatenated values A and B.
Can contraint be used to this new column?

Serge Rielau wrote:
>>Hi everybody.
I wrote a trigger for the follwing requirement:
[quoted text clipped - 15 lines]
>>Can constraint in DB2 to replace a trigger?
No. A check constraint in DB2 (and most other DBMS I know of) operates
on a single row only.
To make this work you would need to define an MQT and then a check
constraint on that. That is not allowed today.
I don't see how that is meant to help. you need to be able to COUNT
rows. That is what your trigger is doing, correct?

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 13 '08 #4
Why did you post Oracle dialect to a DB2 newsgroup? Here is a trick
that will require some effort on your part to keep things right and
handle gaps in tally.

CREATE TABLE Foobar
(a VARCHAR(10) NOT NULL,
b INTEGER NOT NULL,
c INTEGER NOT NULL,
tally INTEGER DEFAULT 1 NOT NULL
CHECK (tally BETWEEN 1 AND 5),
UNIQUE (a, b, tally)
PRIMARY KEY (a, b, c));

INSERT INTO Foobar (a, b, c, tally)
VALUES ('A', 1, 1, 1),
('A', 1, 4, 2),
('A', 1, 99, 3),
('A', 1, 1000, 4)
('A', 1, 3, 5);

Another declarative trick is a VIEW and the WITH CHECK OPTION. You
can only use the VIEW for updates, tho.

CREATE VIEW LimitedFoobar (a,b,c)
AS
SELECT a,b,c
FROM Foobar
WHERE NOT EXISTS
(SELECT COUNT(*)
FROM Foobar
GROUP BY a, b
HAVING CIYBT(*) 5)
WITH CHECK OPTION;

Sep 13 '08 #5
I just tested your solutions in DB2 9.5C Express and it is working great.
I never used such table DDL.
Thank you very much.
Will it work for other DB2 versions?

--CELKO-- wrote:
>Why did you post Oracle dialect to a DB2 newsgroup? Here is a trick
that will require some effort on your part to keep things right and
handle gaps in tally.

CREATE TABLE Foobar
(a VARCHAR(10) NOT NULL,
b INTEGER NOT NULL,
c INTEGER NOT NULL,
tally INTEGER DEFAULT 1 NOT NULL
CHECK (tally BETWEEN 1 AND 5),
UNIQUE (a, b, tally)
PRIMARY KEY (a, b, c));

INSERT INTO Foobar (a, b, c, tally)
VALUES ('A', 1, 1, 1),
('A', 1, 4, 2),
('A', 1, 99, 3),
('A', 1, 1000, 4)
('A', 1, 3, 5);

Another declarative trick is a VIEW and the WITH CHECK OPTION. You
can only use the VIEW for updates, tho.

CREATE VIEW LimitedFoobar (a,b,c)
AS
SELECT a,b,c
FROM Foobar
WHERE NOT EXISTS
(SELECT COUNT(*)
FROM Foobar
GROUP BY a, b
HAVING CIYBT(*) 5)
WITH CHECK OPTION;
--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200809/1

Sep 13 '08 #6
lenygold via DBMonster.com wrote:
I just tested your solutions in DB2 9.5C Express and it is working great.
I never used such table DDL.
Thank you very much.
Will it work for other DB2 versions?

--CELKO-- wrote:
>Why did you post Oracle dialect to a DB2 newsgroup? Here is a trick
that will require some effort on your part to keep things right and
handle gaps in tally.

CREATE TABLE Foobar
(a VARCHAR(10) NOT NULL,
b INTEGER NOT NULL,
c INTEGER NOT NULL,
tally INTEGER DEFAULT 1 NOT NULL
CHECK (tally BETWEEN 1 AND 5),
UNIQUE (a, b, tally)
PRIMARY KEY (a, b, c));

INSERT INTO Foobar (a, b, c, tally)
VALUES ('A', 1, 1, 1),
('A', 1, 4, 2),
('A', 1, 99, 3),
('A', 1, 1000, 4)
('A', 1, 3, 5);

Another declarative trick is a VIEW and the WITH CHECK OPTION. You
can only use the VIEW for updates, tho.

CREATE VIEW LimitedFoobar (a,b,c)
AS
SELECT a,b,c
FROM Foobar
WHERE NOT EXISTS
(SELECT COUNT(*)
FROM Foobar
GROUP BY a, b
HAVING CIYBT(*) 5)
WITH CHECK OPTION;
Yes. Should work across all versions of DB2 for LUW since DB2 V2.
Reasonably certain it works against DB2 for zOS as well.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 14 '08 #7
>I never used such table DDL. Thank you very much. *<<

Welcome. Buy my books and make me rich :) In particular, THINKING IN
SETS will help. It takes time to switch from procedural thinking to a
data-driven, set oriented mindset.
>Will it work for other DB2 versions? <<
Those two solutions were straight Standard SQL 92 and higher; they
ought to work on any SQL product.
Sep 14 '08 #8

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

Similar topics

26
45394
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
3
3051
by: kjaggi | last post by:
I am trying to either write a trigger or a check constraint to prevent duplicates in my table. There are two columns I need to look at for the duplicates and only one combo value for both columns is allowed in the table. For e.g. Column Serial can have only one '123456' value with testresult value as 'PASS'. This serial can be in the table many times with any other combo so for e.g. The table could contain 100 entries for serial column...
3
2201
by: uw_badgers | last post by:
Is it possible to create a unique constraint to a column from another table? For example: tb_current: current_names -------------- aaa bbb tb_new:
5
2428
by: Anthony Robinson | last post by:
Consider the following tables: CREATE TABLE "AIMD "."CHANNELSESSION" ( "CHANNELSESSIONID" DECIMAL(13,0) NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +1 , INCREMENT BY +1 , CACHE 20 ) , "CONNECTSTARTTIME" TIMESTAMP NOT NULL , "CONNECTENDTIME" TIMESTAMP , "ACTIVESESSION" VARCHAR(1), "CHANNELID" DECIMAL(12,0) NOT NULL ) IN "USERSPACE1" ;
2
6536
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the assignment of version numbers: CREATE TRIGGER "trg_audio_file_insert" BEFORE INSERT ON "public"."audio_file" FOR EACH ROW EXECUTE PROCEDURE "public"."trg_audio_file_insert"(); My trigger function looks like this...
3
15879
by: Jeff Kish | last post by:
Hi. I'm getting errors like this when I try to run an upgrade script I'm trying to write/test: altering labels to length 60 Server: Msg 5074, Level 16, State 4, Line 5 The object 'ALTPART_ANNOT_ANNOTID_FK' is dependent on column 'label'. I used this to bracket my script:
1
1407
by: jay_wic | last post by:
I do not know this is the correct way to do this, but somehow this isnt working. All I want is not to have a null value in field A if there is a value in field B heres the code CREATE TRIGGER tiu_name ON tblName FOR INSERT, UPDATE AS DECLARE @FieldA AS REAL, @FieldB AS REAL;
5
1535
by: Quish | last post by:
Hello all The majority of my database experience comes from ORACLE and i am trying to use some functionality that i have already used in Oracle into a project i am working on in MSDE. I want to create a trigger that uses a DML constraint that will prevent a tenants from being inserted into a house if the bedroom count is less or equal to the number of tenants currently in the house. The oracle code is below
2
2518
by: Kenneth Koenraadt | last post by:
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);
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.