mysql ERROR 1215 (HY000), what did i do wrong? - mysql

i am working on a my database project for my class and i cant figure out why my ENROLL table is giving me the
"ERROR 1215 (HY000):cannot add foreign key constrain"
im out of ideas, i already checked my book. any suggestions would be appreciated.
CREATE TABLE COURSE
(COURSE_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
SUBJECT_ID INT(11) NOT NULL,
COURSE_GRADE_LEVEL CHAR(2) NOT NULL,
FAC_ID INT NOT NULL,
FOREIGN KEY(FAC_ID) REFERENCES FACULTY(FAC_ID))
CREATE TABLE STUDENT
(STU_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
STU_FNAME VARCHAR(30) NOT NULL,
STU_LNAME VARCHAR(30) NOT NULL,
STU_DOB VARCHAR(30) NOT NULL,
STU_STREET VARCHAR(150),
STU_CITY VARCHAR(50),
STU_STATE VARCHAR(3),
STU_ZIP VARCHAR(6),
STU_PHONE CHAR(12),
STU_GRADE_LEVEL CHAR(2) NOT NULL);
CREATE TABLE ENROLL
( COURSE_ID INT(11) NOT NULL ,
STU_ID INT(11) NOT NULL,
GRADE CHAR(3) NOT NULL,
FOREIGN KEY(COURSE_ID) REFERENCES COURSE(COURSE_ID),
FOREIGN KEY(STU_ID) REFERENCES STUDENT(STU_ID),
PRIMARY KEY (COURSE_ID,STU_ID));

Related

mysql #1005 errno 150

I'm creating some mysql code with phpmyadmin for a school project and am getting the error "#1005 - Can't create table world_cup.goal (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)".
I can't figure out why I'm getting the error. If I delete the GOAL entity, it still gives the error, but with the CARD entity. In fact, it doesn't like the last 4 entities I created. It has to do with the the foreign key I think.
WHAT I'VE TRIED:
making sure that all the FK and the referenced PK are the same datatype. They match perfectly.
I can't think of anything else to try.
This is due tomorrow so any help would be greatly appreciated! Thanks!
Here is the code:
DROP DATABASE IF EXISTS WORLD_CUP;
CREATE DATABASE WORLD_CUP;
USE WORLD_CUP;
CREATE TABLE TEAM(
Cid varchar(2) NOT NULL,
Continent varchar(20),
Confederation varchar(20) NOT NULL,
Population int,
CName varchar(20) NOT NULL,
PRIMARY KEY(Cid),
UNIQUE(CName)
);
CREATE TABLE PLAYER(
PCid varchar(2) NOT NULL,
Pno int NOT NULL,
Position varchar(20) NOT NULL,
Pfname varchar(20) NOT NULL,
Plname varchar(20) NOT NULL,
Weight int,
Height int,
Club varchar(20),
BDayMonth int,
BDayDay int,
BDayYear int,
PJName varchar(22) NOT NULL,
PRIMARY KEY(PCid, PNo),
FOREIGN KEY (PCid) REFERENCES TEAM(Cid)
);
CREATE TABLE GAME(
Gid varchar(2) NOT NULL,
Score1 int,
Score2 int,
Stadium varchar(20),
Team1Cid varchar(20) NOT NULL,
Team2Cid varchar(20) NOT NULL,
GMonth int,
GDay int,
GYear int,
GType char(1) NOT NULL,
KOGSubtype char(1),
PRIMARY KEY(Gid, Team1Cid, Team2Cid),
FOREIGN KEY(Team1Cid) REFERENCES TEAM(Cid),
FOREIGN KEY(Team2Cid) REFERENCES TEAM(Cid)
);
CREATE TABLE STADIUM(
Sid varchar(2) NOT NULL,
Sname varchar(20) NOT NULL,
Capacity int,
City varchar(20),
PRIMARY KEY(Sid)
);
CREATE TABLE GOAL(
GPno int NOT NULL,
GMinute varchar(6) NOT NULL,
GoalType char NOT NULL,
GGid varchar(2) NOT NULL,
PRIMARY KEY(GPno, GGid, GMinute) ,
FOREIGN KEY (GPno) REFERENCES PLAYER(Pno),
FOREIGN KEY (GGid) REFERENCES GAME(Gid)
);
CREATE TABLE CARD(
CPno int NOT NULL,
CMinute varchar(6) NOT NULL,
Color char(1) NOT NULL,
CGid varchar(2) NOT NULL,
PRIMARY KEY(CPno, CGid, CMinute) ,
FOREIGN KEY(CPno) REFERENCES PLAYER(Pno),
FOREIGN KEY(CGid) REFERENCES GAME(Gid)
);
CREATE TABLE SUBSTITUTE(
PInNo int NOT NULL,
POutNo int NOT NULL,
SMinute varchar(6) NOT NULL,
SGid varchar(2) NOT NULL,
SCid varchar(2) NOT NULL,
PRIMARY KEY(PInNo, POutNo, SMinute, SGid, SCid),
FOREIGN KEY (PInNo) References PLAYER(Pno),
FOREIGN KEY (POutNo) References PLAYER(Pno),
FOREIGN KEY (SGid) References GAME(Gid),
FOREIGN KEY (SCid) References TEAM(Cid)
);
CREATE TABLE STARTINGLINEUP(
SPno int NOT NULL,
PCid varchar(2) NOT NULL,
PGid varchar(2) NOT NULL,
PRIMARY KEY(SPno, PCid, PGid),
FOREIGN KEY (SPno) REFERENCES PLAYER(Pno),
FOREIGN KEY(PCid) REFERENCES TEAM(Cid),
FOREIGN KEY (PGid) REFERENCES GAME(Gid)
);
It's because the primary key on PLAYER is composite, so the foreign key that points to it has to be composite too.
My version of GOAL - note addition of GPCid and its inclusion in the foreign key:
CREATE TABLE GOAL(
GPCid varchar(2) not null,
GPno int NOT NULL,
GMinute varchar(6) NOT NULL,
GoalType char NOT NULL,
GGid varchar(2) NOT NULL,
PRIMARY KEY(GPno, GGid, GMinute) ,
FOREIGN KEY (GPCid,GPno) REFERENCES PLAYER(PCid,Pno),
FOREIGN KEY (GGid) REFERENCES GAME(Gid)
);
And similarly for CARD etc.

Error: Can't create table 'Goober.Employee' (errno: 150)

I get this following error for some reason, and I know it has to do with the foreignkey I just added "UsesTruck" However I don't know WHAT it is I did wrong.. I have included only the tables I think are important for this issue.
CREATE TABLE Employee (
EmployeeID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
UsesTruck CHAR(20) NULL,
FOREIGN KEY (UsesTruck) REFERENCES Truck(LicensePlate),
PRIMARY KEY (EmployeeID));
CREATE TABLE Truck (
LicensePlate CHAR(20) NULL,
color VARCHAR(45) NULL,
capacity VARCHAR(45) NULL,
PRIMARY KEY (LicensePlate));
CREATE TABLE Shifts (
ShiftTime VARCHAR(15) NOT NULL,
PRIMARY KEY (ShiftTime));
CREATE TABLE Reservation (
ReservNum INT NOT NULL,
ReserveDate VARCHAR(45) NULL,
PickupTime VARCHAR(45) NOT NULL,
NumOfPassengers INT NULL,
sheduledTime VARCHAR(45) NULL,
ActualPickupTime VARCHAR(45),
ActualTime VARCHAR(45),
PricePaid VARCHAR(45),
DriverHourlyRate DECIMAL(7,2) NULL,
PassEmployeeHourlyRate DECIMAL (7,2) NULL,
DriverSalary VARCHAR(10),
PassEmployeeSalary VARCHAR(10),
Customer_CustomerID INT,
Truck_LicensePlate char(20) NULL,
Employee_EmployeeID_Driver INT,
Location_Address_Pickup VARCHAR(100),
Employee_EmployeeID_Passenger INT,
Location_Address_Drop VARCHAR(100),
PRIMARY KEY (ReservNum),
FOREIGN KEY (Customer_CustomerID) REFERENCES Customer (CustomerID),
FOREIGN KEY (Truck_LicensePlate) REFERENCES Truck (LicensePlate),
FOREIGN KEY (Employee_EmployeeID_Driver) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Pickup) REFERENCES Location (Address),
FOREIGN KEY (Employee_EmployeeID_Passenger) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Drop) REFERENCES Location (Address));
Try to run create Truck table firstly.

Errno: 1452 Constraint

When I try to source these tables into MySQL, and it created all the tables. Then I start to populate the tables, and it will not let me populate the last table Rental_Invoice. Any suggestions?
The error is
ERROR 1452 (23000): Cannot add or update a child row: a foreign key
constraint fails (SFRC_HIDDEN.Rental_Invoice, CONSTRAINT
Rental_Invoice_fk_Client_Contact FOREIGN KEY (Client_ID)
REFERENCES Client_Contact (Client_ID))
create table Client_Contact
(
Client_ID int Primary Key Auto_Increment,
Client_First_Name varchar(50) Not Null,
Client_Last_Name varchar(50) Not Null,
Client_Address varchar(50),
Client_City varchar(50) Not Null,
Client_State Char(2) Not Null,
Client_Zip_Code varchar(20) Not Null,
Client_Phone varchar(20),
Client_Email varchar(30)
);
create table Owner_Contact
( Owner_ID int Primary Key,
Owner_First_Name varchar(50) Not Null,
Owner_Last_Name varchar(50) Not Null,
Owner_Address varchar(50),
Owner_City varchar(50) Not Null,
Owner_State varchar(2) Not Null,
Owner_Zip_Code varchar(20) Not Null,
Owner_Phone varchar(20),
Owner_Email varchar(30)
);
create table Property_Info
(Property_ID varchar(20) Primary Key,
Owner_ID int Not Null,
Property_Type varchar(30) Not Null,
Pets set('Yes','No') Not Null,
Internet set('yes','No') Not Null,
constraint Property_Info_fk_Owner_Contact
foreign key (Owner_ID)
references Owner_Contact (Owner_ID));
create table Rental_Invoice
( Invoice_ID int Primary Key,
Property_ID varchar(10) Not Null,
Client_ID int Not Null,
Arrival_Date date Not Null,
Departure_Date date Not Null,
Deposit_Amount decimal(5,2) Not Null,
Pet_Deposit_Amount decimal(7,2),
Pet_Type enum('cat', 'dog', ''),
Cleaning_Fee decimal(5,2) Not Null,
Rental_Rate decimal(5,2) Not Null,
Method_Of_Payment varchar(20) Not Null,
constraint Rental_Invoice_fk_Client_Contact foreign key (Client_ID) references Client_Contact (Client_ID)
);
Initial problem
Just a random guess since I don't have MySQL installed anywhere at the moment. But Client_ID in the dependent table is a VARCHAR and it refers to an INTEGER column in the parent table.
After Edit
The foreign key constraint is caused by inserting a row into Rental_Invoice that does not have a corresponding row in Client_Contact. In other words, your data violates the Rental_Invoice_fk_Client_Contact foreign key constraint that requires that Rental_Invoice.Client_ID refers to an existing row in Client_Contact with a matching Client_ID.

Cannot add foreign key constraint with foreign key being primary

I am getting the
Error Code: 1215. Cannot add foreign key constraint 0.172 sec
I don't know what I am doing wrong. I made sure that the foreign key is a primary key.
My sql:
CREATE TABLE Student (
StudentID INT(6) NOT NULL,
StudName VARCHAR(15) NOT NULL,
StudEmail VARCHAR(15) NOT NULL,
StudeHomeNum INT(10) NOT NULL,
StudCellPhoneNum INT(10) NOT NULL,
CourseCode INT(6) NOT NULL,
ProgramCode INT(6) NOT NULL,
PRIMARY KEY (StudentID),
FOREIGN KEY (CourseCode)
REFERENCES Course (CourseCode)
);
CREATE TABLE Course (
CourseCode INT(6) PRIMARY KEY NOT NULL,
CourseName VARCHAR(15) NOT NULL,
Prequisites VARCHAR(15) NOT NULL
);
Since CourseCode code column in Student table is referencing Course table, you have to create Course table first. Change your sql code as below
CREATE TABLE Course (
CourseCode INT(6) PRIMARY KEY NOT NULL,
CourseName VARCHAR(15) NOT NULL,
Prequisites VARCHAR(15) NOT NULL
);
CREATE TABLE Student (
StudentID INT(6) NOT NULL,
StudName VARCHAR(15) NOT NULL,
StudEmail VARCHAR(15) NOT NULL,
StudeHomeNum INT(10) NOT NULL,
StudCellPhoneNum INT(10) NOT NULL,
CourseCode INT(6) NOT NULL,
ProgramCode INT(6) NOT NULL,
PRIMARY KEY (StudentID),
FOREIGN KEY (CourseCode)
REFERENCES Course (CourseCode)
);

MySQL error while creating tables

I have an error in my database
Error: Invalid structure on line 18. Refer to our Manual (PHPMYADMIN)
I use (WAMPSERVER 2 32bits)
-PHPMYADMIN
- MYSQL 5.5.6
- PHP 5
Although i need to use InnoDB (ENGINE=InnoDB)
thanks to help me. Take a look at the structure... the variable name meaning is not important.
Here's my code:
DROP TABLE IF EXISTS Adresse;
DROP TABLE IF EXISTS Telephone;
DROP TABLE IF EXISTS Personne;
DROP TABLE IF EXISTS TelPers;
DROP TABLE IF EXISTS Specialiste;
DROP TABLE IF EXISTS Patient;
DROP TABLE IF EXISTS ListePatient;
DROP TABLE IF EXISTS Produit;
DROP TABLE IF EXISTS Medicament;
DROP TABLE IF EXISTS Materiel;
DROP TABLE IF EXISTS Panier;
CREATE TABLE Adresse(
idAdresse INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
num INT(5) NOT NULL,
rue VARCHAR(30) NOT NULL,
ville VARCHAR(15) NOT NULL,
postal VARCHAR(6) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Telephone(
idTel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
typeTel VARCHAR(15) NOT NULL,
ind INT(3) NOT NULL,
quartier INT(3) NOT NULL,
num INT(4) NOT NULL,
)ENGINE=InnoDB;
CREATE TABLE Personne(
idPersonne INT(100) PRIMARY KEY NOT NULL AUTO_INCREMENT,
nom VARCHAR(15) NOT NULL,
prenom VARCHAR(15) NOT NULL,
idTel INT(100) NOT NULL,
idAdresse INT(100) NOT NULL,
FOREIGN KEY(idAdresse) REFERENCES Adresse(idAdresse),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE TelPers(
idPersonne INT(100) PRIMARY KEY NOT NULL,
idTel INT(100) PRIMARY KEY NOT NULL,
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE Specialiste(
login VARCHAR(10) PRIMARY KEY NOT NULL PRIMARY KEY,
password VARCHAR(10) NOT NULL,
profession VARCHAR(20) NOT NULL,
idListeP INT(5),
idPanier INT(5),
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE Patient(
idPatient INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
sexe CHAR NOT NULL,
anniv DATE,
assurance INT(3) NOT NULL,
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE ListePatient(
idListeP INT(5) NOT NULL PRIMARY KEY,
idPatient INT(10)NOT NULL PRIMARY KEY,
FOREIGN KEY(idListeP) REFERENCES Specialiste(idListeP),
FOREIGN KEY(idPatient) REFERENCES Patient(idPatient)
)ENGINE=InnoDB;
CREATE TABLE Produit(
idProduit INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(50) NOT NULL,
descr VARCHAR(255) NOT NULL,
prix DECIMAL(5,2) NOT NULL,
qte INT(100) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Medicament(
idMedic INT(100)NOT NULL PRIMARY KEY AUTO_INCREMENT,
marque VARCHAR(10) NOT NULL,
typeMed VARCHAR(10) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Materiel(
idMateriel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
rabais INT(99) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Panier(
idPanier INT(5) NOT NULL PRIMARY KEY,
idProduit INT(100) NOT NULL PRIMARY KEY,
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
THANKS FOR YOUR HELP! :D
In Telephone declaration there is a comma (,) before closing )
In TelPers there are two primary keys
In Specialiste there is PRIMARY KEY twice in login column
In ListePatien there are two primary keys
In Panier there are two primary keys
To define a PRIMARY key on two columns use
CREATE TABLE Panier(
idPanier INT(5) NOT NULL,
idProduit INT(100) NOT NULL,
PRIMARY KEY (`idPanier`,`idProduit`),
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
(same on other tables)