I am a noob in MySql. I want to create the following self-referencing table:
EMPLOYEE
+-----+------+------+
|Name |E-ID |M-ID |
+-----+------+------+
|ABC |12345 |67890 |
|DEF |67890 |12345 |
+-----+------+------+
I use the following commands:
CREATE TABLE EMPLOYEE (
NAME VARCHAR(20) ,
E-ID CHAR(6) NOT NULL ,
M-ID CHAR(6) NULL ,
PRIMARY KEY (E-ID) ,
FOREIGN KEY (M-ID) REFERENCES EMPLOYEE(E-ID)
);
Now my problem is, how do I enter the two records? I mean, each time the foreign constraint will fail. I tried entering:
INSERT INTO EMPLOYEE VALUES('12345','67890');
I also tried :
INSERT INTO EMPLOYEE VALUES('12345','67890'),('67890','12345');
Both of the above commands fail. Giving error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails BLAH BLAH
Guys, actually I was trying to implement the tables given in slide number 25 of the following ppt: The Relational Data Model and Relational Database Constraints
The constraints are:
- SUPERSSN Of EMPLOYEE references SSN of EMPLOYEE.
- MGRSSN of DEPARTMENT references SSN of EMPLOYEE.
- DNO of EMPLOYEEE references DNumber of DEPARTMENT.
After I have created the tables, how do I add records? It will always fail the foreign key constraints.