Here is my table game
:
create table game (
h_team number,
a_team number,
p_date date
);
Condition to be followed: Every team plays a single game on a particular date. Basically normal rules that usually should happen for a tournament.
I have added following constraints:
I want to add another constraints which restricts to add what the following queries performs:
select h_team, p_date
from game
where (h_team,p_date) not in (select a_team,p_date from game);
select a_team, p_date
from game
where (a_team,p_date) not in (select h_team,p_date from game);
For example,Suppose a record in that table is (1,2,23-JAN-2000). So records like (3,1,23-JAN-2000), (2,4,23-JAN-2000) etc. cannot be inserted. Thanks!
I preferred in SQl but it seems it is not possible in SQL. So How will it be using PL-SQL.
The feature you're looking for is called SQL assertions, and it's not yet implemented in Oracle 12c . Meanwhile, use a trigger, as you've suggested yourself.
Of course, your trigger doesn't work because its syntax is quite wrong.
CREATE TRIGGER xx_game_trigger
BEFORE INSERT -- This clause
ON xx_game -- before this one
REFERENCING NEW AS new -- You'll probably need this
FOR EACH ROW
BEGIN
-- There's no such thing as IF EXISTS in PL/SQL. Here's a workaround. This loop will run
-- zero or one times.
FOR rec IN (
SELECT 1 FROM dual
WHERE EXISTS (
-- I'm assuming that you're interested in matches between existing records
-- And the record you're about to insert (:new.xxx). Adapt accordingly
SELECT 1 FROM xx_game WHERE (home_team,play_date) IN (:new.away_team,:new.play_date)
)
OR EXISTS (
SELECT 1 FROM xx_game WHERE (away_team,play_date) IN (:new.home_team,:new.play_date)
)
)
LOOP
-- There's no TRANSACTION keyword here. But anyway, I'd rather raise an exception
-- than roll back the transaction. That seems much cleaner to me.
ROLLBACK;
END LOOP;
END xx_game_trigger;
Please consider the Oracle documentation for the complete CREATE TRIGGER
syntax
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.