我正在尝试制作一个触发器,说明客户不能从下表中租出2辆以上的车辆。有些帮忙吗?
CREATE TABLE Customer
(
Cust_ID numeric primary key ,
Cust_fname varchar(20),
Cust_lname varchar(20),
sex varchar(15),
Cust_street varchar(30),
Cust_parish varchar(30),
Cust_cc# numeric(30),
Cust_trn# numeric (20),
);
CREATE TABLE Vehicle
(
cust_ID numeric ,
V_ID numeric primary key not null ,
Body_type varchar(20),
License# varchar(20),
V_year varchar(15),
chassis# numeric(30),
unladen_weight varchar(30),
make varchar(30),
Color varchar (20),
Eng# varchar(20),
fuel_type varchar(30),
V_weight numeric (30),
Foreign key (Cust_ID)references Customer,
);
create table Car
(
V_ID numeric not null,
Foreign key (V_ID)references Vehicle,
);
create table truck
(
V_ID numeric not null,
truck_length varchar(30) ,
max_clearance numeric,
laden_weight numeric,
haul_cap numeric,
Foreign key (V_ID)references Vehicle,
);
Create table rents
(
Cust_ID numeric ,
V_ID numeric not null,
desposit numeric,
date_of_rent date ,
validID varchar (10),
proof_of_add varchar (10),
Foreign key (Cust_ID)references Customer,
foreign key (V_ID) references vehicle,
);
答案 0 :(得分:0)
情侣cust_ID
和V_ID
必须是唯一的吗?
在这些字段的表租金中创建唯一索引。
答案 1 :(得分:0)
嗯,首先是不同的DB有不同的触发程序等。
然而,这是Postgres的一个例子。
CREATE OR REPLACE FUNCTION test_insert_trigger()
RETURNS "trigger" AS
$BODY$
declare
cnt integer;
begin
select count(*) into cnt from test where id = NEW.id;
if (cnt >= 2) then
raise exception 'Can not insert more than 2 of same thing.';
end if;
end;
$BODY$
LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION test_insert_trigger()
OWNER TO postgres;
CREATE TRIGGER test_insert_trigger
BEFORE INSERT OR UPDATE
ON test
FOR EACH ROW
EXECUTE PROCEDURE test_insert_trigger();
它只是检查数据库中是否存在您要插入或更新的行的2个以上。更改查询条件以适合您的应用。
但实质是存在的。