error in making table -mysql - mysql

The Error is: Can't create table c.handsets
How do i find the problem?
I have made a database and I found that my other tables are created in it but only one is giving me this error.

There are conditions that must be satisfied for a foreign key:
You must use InnoDB storage engine in both tables.
The data types must be identical in both tables. E.g. VARCHAR(250) is not identical to VARCHAR(255).
There must be a PRIMARY KEY or UNIQUE constraint on the column(s) in the parent table.
Your foreign key must declare the same column(s), in the same order, as those in the PRIMARY KEY or UNIQUE constraint in the parent table. This is your most likely problem.
So for example, if this is your parent table:
CREATE TABLE PhoneModels (
make VARCHAR(255),
model VARCHAR(255),
PRIMARY KEY (make, model)
);
The following would be wrong because the primary key has two columns, but each foreign key has one column.
CREATE TABLE Handsets
(
...
make VARCHAR(255),
model VARCHAR(255),
FOREIGN KEY (make) REFERENCES PhoneModels(make), // WRONG
FOREIGN KEY (model) REFERENCES PhoneModels(model) // WRONG
);
It doesn't work this way; you should have one foreign key that references both columns.
CREATE TABLE Handsets
(
...
make VARCHAR(255),
model VARCHAR(255),
FOREIGN KEY (make, model) REFERENCES PhoneModels(make, model), // RIGHT
);

Related

Issue with MySQL, cannot add foreign key constraint

I'm trying to create a table named appointment, though when I try to create it I receive the error:
Cannot add foreign key constraint
My SQL code is as follows:
CREATE TABLE APPOINTMENT(
APNo VARCHAR(5),
PNo VARCHAR(5),
DNo VARCHAR(5),
APDATE DATETIME
);
ALTER TABLE APPOINTMENT
ADD PRIMARY KEY (APNo),
ADD FOREIGN KEY (PNo) REFERENCES PATIENT(PNo),
ADD FOREIGN KEY (DNo) REFERENCES DOCTOR(DNo)
;
You syntax looks ok. You are not showing the DDL for referrenced tables PATIENT and DOCTOR, however it is likely that the error happens because one of MySQL foreign keys requirements is not met.
Quotes from the documentation:
Corresponding columns in the foreign key and the referenced key must have similar data types.
You must ensure that both DOCTOR(DNo) and PATIENT(PNo) are VARCHAR(5).
MySQL requires indexes on foreign keys and referenced keys. [...] Such an index is created on the referencing table automatically if it does not exist.
Ideally, DOCTOR(DNo) and PATIENT(PNo) should be the primary key of their respective tables. Else, an index must exist for each of them (it could be a multi-column index where the referrenced column appears once).
See this db fiddle for a working example:
CREATE TABLE PATIENT(PNo VARCHAR(5) PRIMARY KEY);
CREATE TABLE DOCTOR(DNo VARCHAR(5) PRIMARY KEY);
CREATE TABLE APPOINTMENT(
APNo VARCHAR(5),
PNo VARCHAR(5),
DNo VARCHAR(5),
APDATE DATETIME
);
ALTER TABLE APPOINTMENT
ADD PRIMARY KEY (APNo),
ADD FOREIGN KEY (PNo) REFERENCES PATIENT(PNo),
ADD FOREIGN KEY (DNo) REFERENCES DOCTOR(DNo)
;

How to use a Foreign key in a table a primary key in another table

I have 3 tables. Courses,CourseDestails, CourseBooking.
Courses
CREATE TABLE Courses (
courseId int NOT NULL,
courseName varchar(255),
level varchar(255),
description varchar(255),
PRIMARY KEY (courseId)
);
CourseDetails
CREATE TABLE CourseDetails (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
MaxNoPlaces int,
Length int,
Instructor varchar(255),
TotalPlacesBooked int,
NoPlacesCancelled int,
AdultPrice int,
ChildPrice int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,courseId),
FOREIGN KEY (courseId) REFERENCES Courses(courseId)
);
courseId from courses table is used as foreign key in the CourseDetails table.
Now I want to use CourseStartDate Location & courseId from CourseDetails table in another table called CourseBooking. I know how to add CourseStartDate Location as foreign keys. But I'm confused how to add courseId from courseDetails table as foreign key in the new CourseBookings Table.
I have tried the following
CREATE TABLE CourseBookings (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
GuestNo int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,GuestNo,courseId),
FOREIGN KEY (courseId) REFERENCES CourseDetails.courseId(courseId),
FOREIGN KEY (CourseStartDate) REFERENCES CourseDetails(CourseStartDate),
FOREIGN KEY (Location) REFERENCES CourseDetails(Location),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
But there is an error saying
Can't create table 'b8040777_db1.CourseBookings'
Supports transactions, row-level locking, and foreign keys
There are several issues with the declaration of table CourseBookings.
ISSUE 1
This :
FOREIGN KEY (courseId) REFERENCES CourseDetails.courseId(courseId),
Should be written as :
FOREIGN KEY (courseId) REFERENCES CourseDetails(courseId),
ISSUE 2
One of the columns of CourseDetails, that is referenced by a foreign key of table CourseBookings, has no index.
From the documentation :
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.
In table CourseDetails :
courseId had an index automatically created because it references Courses(courseId)
CourseStartDate has no index but it is the first column in the index automatically generated by the primary key constraint declaration
Location has no index and it is only the second column in the primary key index --> it is not possible to create a foreign key referencing it, an error is raised when you try
SOLUTION 1
It is always possible to add the missing index to the table, by adding this to the CREATE TABLE CourseDetails statement :
INDEX idx_Location (Location)
SOLUTION 2
In your use case, I suspect that what you actually need is a multicolumn foreign key (supported in InnoDB only), that references the primary key of CourseDetails (an index already exists on these columns). This will allow you to associate each record of CourseBookings with a unique parent record in CourseDetails.
Suggestion of DDL :
CREATE TABLE CourseBookings (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
GuestNo int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,courseId,GuestNo),
FOREIGN KEY (CourseStartDate,Location,courseId)
REFERENCES CourseDetails(CourseStartDate,Location,courseId),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
SOLUTION 3
If solution 2 fits for your use case, then it means that your schema can be optimized by creating an autoincremented integer primary key on table CourseDetails. The existing primary key can be turned into a UNIQUE constraint. Then, in table CourseBookings, you can just store a foreign key to that column.
This would give you a simple and efficient way to relate one table to the other, while avoiding duplicating information across tables (this actually leaves you with just 3 columns in CourseBookins).
In relational database design it is usually a good practice to create such a primary key on most tables. You could consider adding one to other tables too.
Example :
CREATE TABLE CourseDetails (
id int PRIMARY KEY AUTO_INCREMENT,
CourseStartDate int NOT NULL,
...
CONSTRAINT PK_CourseDetails UNIQUE (CourseStartDate,Location,courseId),
FOREIGN KEY (courseId) REFERENCES Courses(courseId)
);
CREATE TABLE CourseBookings (
id int PRIMARY KEY AUTO_INCREMENT,
courseDetailId INT NOT NULL,
GuestNo int,
CONSTRAINT PK_CourseBookings UNIQUE (courseDetailId,GuestNo),
FOREIGN KEY (courseDetailId) REFERENCES CourseDetails(id),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
PS, just in case :
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
Does table Guest exists in your schema (you did not show the CREATE TABLE for it) ?

error 1215: CANNOT ADD FOREIGN KEY CONSTRAINT ; Composite Foreign key

I am getting ERROR CODE 1215: cannot add foreign key constraint for the child table.
Parent table has composite primary key. I want to use that composite primary key as foreign key in the child table.
Please guide me.
PARENT TABLE
CREATE TABLE health.procedures(
Specialty varchar(40),
Procedure_Name varchar(60),
PRIMARY KEY (Procedure_Name, Specialty)
);
CHILD TABLE
CREATE TABLE health.procedureProvided(
specialization varchar(40),
procedure_name varchar(60),
Insurance_ID int REFERENCES health.insurance (idInsurance),
Hospital_ID int REFERENCES health.hospital (idHospital) ,
Doctor_ID int REFERENCES health.doctor( idDoctor) ,
CONSTRAINT procedures_fk foreign key (specialization,procedure_name)references health.procedures(Specialty,Procedure_Name) ,
PRIMARY KEY (specialization, procedure_name, Insurance_ID, Hospital_ID, Doctor_ID)
);
You are specifying a foreign key that is invalid. Your primary key is procedure_name in health.procedure, but you are trying to create a composite foreign key in health.procedureProvided. You can't create that as a foreign key as the column Specialty in the master table is not part of the primary. A foreign key must contain all the columns in the contributing table's primary key but cannot contain values that are not in that primary key. You have three real options. 1. Specify Specialty as being a component of the primary key in procedure. Unfortunately that means the procedure will not necessarily by unique, it will be unique by specialty. 2. add a surrogate key - a system generated sequence value, uuid or something else (timestamps are not recommended). 3. Create validation tables valid_procedures and valid_specialties and use the table health.procedure as an intersection between those to provide valid procedures and correlated specialties and then you could migrate the entire primary key.

Database Foreign Key Error

I am having problems with 2 tables.
I just created a table called "ejemplar" with the following SQL code:
CREATE TABLE ejemplar
(
id_revista INT NOT NULL,
id_ejemplar INT NOT NULL,
id_art INT NOT NULL,
fecha_ejem VARCHAR (30),
precio_ejem decimal,
num_pag_ejem INT,
PRIMARY KEY (id_revista,id_ejemplar,id_art),
FOREIGN KEY (id_revista) REFERENCES revista (id_rev)
)
And I want to create a table called "ejem_art" with the following SQL code:
CREATE TABLE ejem_art(
id_rev INT NOT NULL,
id_ejem INT NOT NULL,
id_art INT NOT NULL,
num_pag INT,
PRIMARY KEY (id_rev,id_ejem,id_art),
FOREIGN KEY (id_rev) REFERENCES revista (id_rev),
FOREIGN KEY (id_ejem) REFERENCES ejemplar (id_ejemplar),
FOREIGN KEY (id_art) REFERENCES articulo (id_art)
);
The DBMS is MySQL and as you can see I have the same type of data in the PK and FK fields in both tables.
When I try to run the second SQL code after creating the table "ejemplar" MySQL shows the famous errno: 150 which relates to different type of data or even different engine rather than InnoDB.
I have made sure both things are correct (same data type and same engine) but I still cannot run the second code.
Hope you could help me with this, because I would not want to make an "Alter Table" just because of this error.
Thanks in advance!
The column you reference in the foreign key must be the leftmost column in a key of the parent table.
CREATE TABLE parent ( A INT, B INT, PRIMARY KEY (A,B) );
CREATE TABLE child (B INT, FOREIGN KEY (B) REFERENCES parent(B) ); /* WRONG */
CREATE TABLE child (A INT, FOREIGN KEY (A) REFERENCES parent(A) ); /* RIGHT */
Re your comments:
It doesn't matter what order the columns are defined in the tables. It matters what order the columns are listed in the PRIMARY KEY constraint. The foreign key can reference only the leftmost column of the parent's key.
In your parent table, you have this primary key:
PRIMARY KEY (id_revista,id_ejemplar),
The column id_revista is leftmost. The column id_ejemplar is not leftmost.
Therefore the child table's foreign key cannot do this:
FOREIGN KEY (id_ejem) REFERENCES ejemplar (id_ejemplar), /* WRONG */
The best solution is to reference both id_revista and id_ejemplar. Then you will be referencing a set of columns of the parent's primary key, starting with the leftmost.
FOREIGN KEY (id_rev, id_ejem) REFERENCES ejemplar (id_revista, id_ejemplar),

MySQL Error Code 1215: Cannot add foreign key Constraint

I'm very new to SQL, I'm trying to define a 2 tables Hospital and Hospital_Address but when I'm trying to add foreign key in Hospital_Address it throws an error: "1215: Cannot add foreign key"
create table Hospital (
HId Int not null,
HName varchar(40) not null,
HDept int, Hbed Int,
HAreaCd int not null,
Primary Key (HId)
);
create table Hospital_Address (
HAreaCd Int not null,
HArea varchar(40) not null,
HCity varchar(40),
HAdd1 varchar(40),
HAdd2 varchar(40),
Primary Key (HArea),
foreign key (HAreaCd) references Hospital (HAreaCd));
Please help me in this regard. Thanks in advance.
MySQL requires that there be an index on the HAreaCd column in the parent Hospital table, in order for you to reference that column in a FOREIGN KEY constraint.
The normative pattern is for the FOREIGN KEY to reference the PRIMARY KEY of the parent table, although MySQL extends that to allow a FOREIGN KEY to reference a column that is a UNIQUE KEY, and InnoDB extends that (beyond the SQL standard) and allows a FOREIGN KEY to reference any set of columns, as long as there is an index with those columns as the leading columns (in the same order specified in the foreign key constraint.) (That is, in InnoDB, the referenced columns do not need to be unique, though the behavior with this type of relationship may not be what you intend.)
If you create an index on that column in Hospital table, e.g.:
CREATE INDEX Hospital_IX1 ON Hospital (HAreaCd);
Then you can create a foreign key constraint that references that column.
However, because this is a non-standard extension of MySQL and InnoDB, the "best practice" (as other answers here indicate) is for a FOREIGN KEY to reference the PRIMARY KEY of the foreign table. And ideally, this will be a single column.
Given the existing definition of the Hospital table, a better option for a foreign key referencing it would be to add the Hid column to the Hospital_Address table
... ADD HId Int COMMENT 'FK ref Hospital.HId'
... ADD CONSTRAINT FK_Hospital_Address_Hospital
FOREIGN KEY (HId) REFERENCES Hospital (HId)
To establish the relationship between the rows, the values of the new HId column will need to be populated.
You cannot add a foreign key to a non-primary key element of another table usually.
If you really need to do so, refer to this question for help : Foreign Key to non-primary key
HAreaCd in the Hospital table should be a primary key. Only then can you reference it in the Hospital_Address table