CREATE TABLE doctor(
Did varchar(30) not null,
spid int,
Hid int,
Dname varchar(200)not null,
Dnumber int,
fee decimal(10,2)not null,
constraint primary key(Did),
constraint unique(Did,Dname),
constraint foreign key(spid)references speciality(spid)
on delete cascade on update cascade,
constraint foreign key(Hid)references hospital(Hid)
on delete cascade on update cascade
)engine=innodb;
It's hard to give an answer without the error message or your version of mysql, but I'd recommend to be sure that spid column of speciality is also defined is int, not int(11) or unsigned int(and also for other foreign key references).
And also your primary key is already always unique, I don't see any point including that in another unique constraint.
Related
I am facing an error when creating a table between the following two statement
CREATE TABLE SECTION (
cid varchar(10) not null,
sno varchar(3) not null,
primary key (cid, sno),
foreign key (cid) references COURSE (cid)
);
CREATE TABLE ROUND_RELEASE (
cid varchar(10) not null,
sno varchar(3) not null,
rid int not null,
foreign key (cid) references SECTION (cid),
foreign key (sno) references SECTION (sno),
foreign key (rid) references ROUND (rid)
);
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'round_release_ibfk_2' in the referenced table 'section'
However, when I try to swap the primary key order in SECTION table, I am able to create both table without error
CREATE TABLE SECTION (
cid varchar(10) not null,
sno varchar(3) not null,
primary key (**sno, cid**),
foreign key (cid) references COURSE (cid)
);
CREATE TABLE ROUND_RELEASE (
cid varchar(10) not null,
sno varchar(3) not null,
rid int not null,
foreign key (cid) references SECTION (cid),
foreign key (sno) references SECTION (sno),
foreign key (rid) references ROUND (rid)
);
The follow code above works, and I only swap the attribute, from my knowledge, the order does not matter, thus I am quite puzzled by why is this occurring.
Any guidance on this? Thanks!
Based on the error message, I assume that you are using MySQL.
What you are seeing is a documented behavior:
In the referenced table, there must be an index where the referenced columns are the first columns in the same order.
Let me pinpoint, however, that your code probably does not do what you really want. You probably should be creating a compound foreign key, that references the tuples of columns, rather than one foreign key per column:
create table round_release (
cid varchar(10) not null,
sno varchar(3) not null,
rid int not null,
foreign key (cid, sno) references section(cid, sno),
foreign key (rid) references round(rid)
);
One last thing to note is that your round_release table has no primary key defined; this is not a good practice, and might hurt you in several ways in the future. So, do create a primary key for the table, either as a separate column (possibly auto-incremented), or as a combination of existing columns.
I'm trying to create a table with a recursive column, but It doesn't work. Later I tried it and it did work, but the I dropped the table because of a problem and now I don't know what I did.
The table's name is "security" and this is the script:
create table security (
id_emplo int(6) ZEROFILL NOT NULL,
id_boss varchar(10) DEFAULT NULL,
TIP int(5) NOT NULL,
security_division varchar(40),
PRIMARY KEY (id_emplo),
FOREIGN KEY (id_emplo) REFERENCES employees (id_emple) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (id_boss) REFERENCES security (id_emple) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=INNODB;
I have another table called "employees". If I try to create the table with only "id_emplo" REFERENCES employess... It doesn't have any problem.
You have to be very careful about types and declarations. The following should work:
create table security (
id_emplo int(6) ZEROFILL NOT NULL,
id_boss int(6) ZEROFILL DEFAULT NULL,
TIP int(5) NOT NULL,
security_division varchar(40),
PRIMARY KEY (id_emplo),
FOREIGN KEY (id_emplo) REFERENCES employees (id_emple) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (id_boss) REFERENCES security (id_emplo) ON DELETE RESTRICT ON UPDATE CASCADE
);
Here is the SQL Fiddle.
You had two main problems:
You had the name of the column incorrect. It should be id_emplo rather than id_emple (although I give foreign key columns the same name as the column they refer to when I can).
The types were incompatible.
Why does this throw an error "XAMPP:CANT ADD FOREIGN KEY".
The create table is:
CREATE TABLE FIR_T (
FIR_id INT(5) NOT NULL,
Incident_date DATE NOT NULL,
Incident_place VARCHAR(30),
Time_lodged TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Incident_time TIMESTAMP,
Date_lodged DATE NOT NULL,
Petitioner_id VARCHAR(15),
Victim_Name VARCHAR(50),
Victim_Address VARCHAR(100),
Case_id INT(10),
Petitioner_Name VARCHAR(50),
CONSTRAINT FIR_PK PRIMARY KEY(FIR_id),
CONSTRAINT FIR_FK1 FOREIGN KEY(victim_name,victim_address)
REFERENCES Victim_T(name,address),
CONSTRAINT FIR_FK2 FOREIGN KEY(Petitioner_id,Petitioner_Name)
REFERENCES Petitioner_T(NIC,Name),
CONSTRAINT FIR_FK3 FOREIGN KEY(Case_ID)
REFERENCES Case_T(Case_ID));
Do the types of the child and parent columns for your foreign key match exactly ? If yes, is there any existing data in the table that would violate the foreign key constraints you are trying to add ?
im new on mysql workbench, and i tried so many things to put my script working but i simply cant... Ive got these tables:
CREATE TABLE Utilizador (email varchar(40) not null, nome varchar(50)
not null, dataNascimento date, profissao varchar(50) not null,
reputacao double(3,2) unsigned not null, constraint pk_Utilizador
primary key(email))
This is the first table created!
CREATE TABLE POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, coordenadaX int, coordenadaY int,
descricaoPOI varchar(200), constraint pk_POI primary key(email,
designacaoPOI), constraint fk_POI foreign key(email) references
Utilizador(email) on delete cascade)
This is the second table created!
CREATE TABLE Utilizador_POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, constraint pk_Utilizador_POI primary key(email,
designacaoPOI), constraint fk1_Utilizador_POI foreign key(email)
references Utilizador(email) on delete cascade, constraint
fk2_Utilizador_POI foreign key(designacaoPOI) references
POI(designacaoPOI) on delete cascade)
This table gives me the error: Error Code: 1215. Cannot add foreign key constraint
I did some tests and im almost sure that the problem is in the foreign key "designacaoPOI". The other FK ("email") dont give me any error, so maybe the problem is in the Table POI?
Thanks in advanced!
The problem here is twofold:
1/ Use IDs for PRIMARY KEYs
You should be using IDs for primary keys rather than VARCHARs or anything that has any real-world "business meaning". If you want the email to be unique within the Utilizador table, the combination of email and designacaoPOI to be unique in the POI table, and the same combination (email and designacaoPOI) to be unique in Utilizador_POI, you should be using UNIQUE KEY constraints rather than PRIMARY KEY constraints.
2/ You cannot DELETE CASCADE on a FOREIGN KEY that doesn't reference the PRIMARY KEY
In your third table, Utilizador_POI, you have two FOREIGN KEYs references POI. Unfortunately, the PRIMARY KEY on POI is a composite key, so MySQL has no idea how to handle a DELETE CASCADE, as there is not a one-to-one relationship between the FOREIGN KEY in Utilizador_POI and the PRIMARY KEY of POI.
If you change your tables to all have a PRIMARY KEY of ID, as follows:
CREATE TABLE blah (
id INT(9) AUTO_INCREMENT NOT NULL
....
PRIMARY KEY (id)
);
Then you can reference each table by ID, and both your FOREIGN KEYs and DELETE CASCADEs will work.
I think the problem is that Utilizador_POI.email references POI.email, which itself references Utilizador.email. MySQL is probably upset at the double-linking.
Also, since there seems to be a many-to-many relationship between Utilizador and POI, I think the structure of Utilizador_POI isn't what you really want. Instead, Utilizador_POI should reference a primary key from Utilizador, and a matching primary key from POI.
The problem is in your second table. Your primary key is (email,designacaoPOI), when you try to reference that in your table it gives you error because of this:
InnoDB permits a foreign key to reference any index column or group of
columns. However, in the referenced table, there must be an index
where the referenced columns are listed as the first columns in the
same order.
For it to work, either change the order of your second tale PRIMARY KEY :
CREATE TABLE POI (
email VARCHAR(40) NOT NULL,
designacaoPOI VARCHAR(10) NOT NULL,
coordenadaX INT,
coordenadaY INT,
descricaoPOI VARCHAR(200),
CONSTRAINT pk_POI PRIMARY KEY (designacaoPOI,email), -- changed the order
CONSTRAINT fk_POI FOREIGN KEY (email)
REFERENCES Utilizador(email) ON DELETE CASCADE
);
sqlfiddle demo
or add an index for designacaoPOI:
CREATE TABLE POI (
email VARCHAR(40) NOT NULL,
designacaoPOI VARCHAR(10) NOT NULL,
coordenadaX INT,
coordenadaY INT,
descricaoPOI VARCHAR(200),
CONSTRAINT pk_POI PRIMARY KEY (designacaoPOI,email),
KEY key_designacaoPOI(designacaoPOI), -- added index for that column
CONSTRAINT fk_POI FOREIGN KEY (email)
REFERENCES Utilizador(email) ON DELETE CASCADE
);
sqlfiddle demo
Either of these solutions will let you create your third table without errors.
Its between so long since i did SQL when i try to paste this code into phpmyadmin database it wont create the table i get error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL PRIMARY KEY, WORKER CHAR(30), CONSTRAINT WORKER_FK FOREIGN KEY(WORKE' at line 2
this is code for the sql table:
CREATE TABLE REPORT(
REPORT_ID NOT NULL PRIMARY KEY,
WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
COMMENT CHAR(30)
)engine innoDB;
can anyone help me out please
The actual error message comes from the fact that report_id doesn't have a data type.
REPORT_ID NOT NULL PRIMARY KEY,
should be
REPORT_ID INTEGER NOT NULL PRIMARY KEY,
But you have a lot more problems. The "inline" foreign keys are silently ignored by MySQL - even with InnoDB. You have to move them to the end.
Additionally your foreign key to the jobs table can not be correct. First off, there is no job column in your table, so FOREIGN KEY(JOB) REFERENCES JOB(END_DATE) can't be correct.
Secondly you are referencing two different PK definitions to the same table. My guess is you actually want:
CONSTRAINT START_END_DATE_FK
FOREIGN KEY (START_DATE, END_DATE)
REFERENCES JOB(START_DATE, END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
This assumes that (START_DATE, END_DATE) is the primary key of the JOB table - which does sound a bit strange.
Not sure if comment is a reserved word in MySQL. If it is, you need to quote the column name.
So put this alltogether, you probably need something like this:
CREATE TABLE REPORT
(
REPORT_ID INTEGER NOT NULL PRIMARY KEY,
WORKER CHAR(30) ,
CLIENT CHAR (30),
START_DATE DATE ,
END_DATE DATE,
COMMENT CHAR(30),
CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT CLIENT_FK FOREIGN KEY (client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT START_END_DATE_FK FOREIGN KEY (START_DATE, END_DATE) REFERENCES JOB(START_DATE, END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL
);
You are not defining a column type for REPORT_ID. ie REPORT_ID int(11) NOT NULL PRIMARY KEY,
CREATE TABLE REPORT(
REPORT_ID int(11) NOT NULL PRIMARY KEY,
WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
COMMENT CHAR(30)
)engine innoDB;