0

I have to create a number of tables for my project and each one of them has a foreign key besides their primary key. I know how to create a table on putty without the foreign key but if they are all depends on each other, then how to create them individually ?

For example, if I want to create table A, B and C

CREATE TABLE PROFESSOR
(
SSN numeric(9) primary key,
PNAME varchar(20),
CITY varchar(20),
STREETADDRESS varchar(50),
STATE char(2),
ZIP numeric(5),
AREACODE numeric(3),
PHONENUMBER numeric(7),
SEX char(1),
TITLE char(4),
SALARY float(9),
foreign key (DNUM) references DEPARTMENT(DNUM)
);

CREATE TABLE DEPARTMENT
(
DNUM numeric(1) primary key,
DNAME varchar(20),
DPHONE numeric(10),
OFFICELOCATION varchar(20),

foreign key (CWID) references STUDENT(CWID)
);

CREATE TABLE STUDENT
(
CWID numeric(9) primary key,
FNAME varchar(20),
LNAME varchar(20),
SADDRESS varchar(50),
SPHONE numeric(10),

foreign key (DNUM) references DEPARTMENT(DNUM)
);

I'm using Putty to create table and I have to run them separately. The thing is if I run the scrip separately for each table, it's going to give me an error, because the table that has the foreign key does not create yet. How am I going to fix it ? and is there a work around way on solving this problem ? Thank you. If I run the script for creating table separately, it will give me an error

Hoang Minh
  • 1,066
  • 2
  • 21
  • 40
  • 3
    As your case shows, circular references are a bad idea. – Isaac Kleinman May 13 '14 at 19:30
  • 1
    possible duplicate of [How to temporarily disable a foreign key constraint in MySQL?](http://stackoverflow.com/questions/15501673/how-to-temporarily-disable-a-foreign-key-constraint-in-mysql) – Marcus Adams May 13 '14 at 19:34
  • 1
    @Marcus Adams: But even then he will have many many problems with insert or delete operations. Why should a `DEPARTMENT` reference a `STUDENT` anyway? – VMai May 13 '14 at 19:40

0 Answers0