database - i used the program SQL Fiddle and it keeps telling me that the table doesn't exist,what can i do to fix the two tables referencing each other? -


the staff table references branch table

create table staff( staffno varchar(5) not null, firstname varchar(15) not null unique, lastname varchar(15) not null, position varchar(10) not null, salary integer default 3000, check (salary between 3000 , 25000), email varchar(25), branchno char(6) not null, primary key (staffno), foreign key (branchno) references branch (branchno)); 

and @ same time branch table references staff table

create table branch( branchno char(6) not null primary key, street varchar(30) not null, city varchar(20), postcode char(5) not null, managerno varchar(5) not null, foreign key (managerno) references staff(staffno));  

since tables reference each other in foreign keys error on either table creation if other table has not been created yet. suggest remove creation of foreign keys separate alter table statements:

create table staff(   staffno varchar(5) not null,   firstname varchar(15) not null unique,   lastname varchar(15) not null,   position varchar(10) not null,   salary integer   default 3000,   check (salary between 3000 , 25000),   email varchar(25),   branchno char(6) not null,   primary key (staffno) );   create table branch(   branchno char(6) not null primary key,   street varchar(30) not null,   city varchar(20),   postcode char(5) not null,   managerno varchar(5) not null );   alter table staff   add constraint fk1_branchno foreign key (branchno) references branch (branchno);  alter table branch   add constraint fk1_managerno foreign key (managerno) references staff (staffno); 

see sql fiddle demo


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -