sql - No unique index found for the referenced field of the primary table -
when run booking table, show message "no unique index found referenced field of primary table".
create table customer
create table customer ( custid integer not null, fname char(18) not null, lname char(18) not null, street char(6) not null, city char(18) not null, province char(8) not null, country char(8) not null, postcode char(6) not null, gender char(6) not null, primary key (custid) );
create table booking
create table booking ( bkgno integer not null, custid integer not null, fno integer not null, statusid char(3) not null, classid char(4) not null, orig char(18) not null, dest char(18), depttime date not null, arrtime date not null, bkgcity char(18) not null, paidby char(18) not null, fprice currency not null, totprice currency not null, paidamt currency not null, bal currency not null, bkgdate date not null, primary key (bkgno), index (orig,dest,depttime, arrtime), index (custid), foreign key (custid) references customer, foreign key (fno) references flight_availability(fno), foreign key(dest) references flight_availability(dest), foreign key(depttime) references flight_availability(depttime), foreign key(arrtime) references flight_availability(arrtime), foreign key(dest,depttime,arrtime) references flight_availability(dest, depttime, arrtime), foreign key (orig) references airport(airportcd) );
create table flight availability
create table flight_availability ( fno integer not null, orig char(18) not null, dest char(18), depttime date not null, arrtime date not null, flength integer not null, primary key (fno,orig,dest,depttime,arrtime) );
create tableairport
create table airport ( airportcd char(18) not null, cityid char(18) not null, airportnm char(18) not null, airporttax currency, primary key (airportcd) );
this part causes errors:
foreign key (fno) references flight_availability(fno), foreign key(dest) references flight_availability(dest), foreign key(depttime) references flight_availability(depttime), foreign key(arrtime) references flight_availability(arrtime), foreign key(dest,depttime,arrtime) references flight_availability(dest, depttime, arrtime)
a foreign key constraint (also called referential integrity constraint) designates column foreign key , establishes relationship between foreign key , specified primary or unique key, called referenced key. composite foreign key designates combination of columns foreign key.
so referenced key should either unique or primary key.
for example:
foreign key (fno) references flight_availability(fno)
referenced key = fno
not unique , not primary key. although fno
part of composite primary key (fno,orig,dest,depttime,arrtime)
, it's not unique (the combination of columns of primary key unique).
p.s. don't need many columns in primary key in flight_availability table. may create composite unique index on columns (if it's required) , create not null constraint on each column. in case you'll have same checks in current composit primary key. don't think need this.
Comments
Post a Comment