MySql foreign key constraint issue - mysql

I am trying to develop a university database and am stuck with few tables throwing a constraint error. I have tried various workarounds:
1. Checking my eninge status. It's INNODB across all tables.
2. On Update On Delete parameters(although I am not sure if I am doing that correctly).
3. Checking the NULL reference and the data types. Foreign keys referred to have the same data types as the primary key in the table which is making the reference.
This is my query:
CREATE TABLE Faculty (
FacNo CHAR(11) NOT NULL,
FacFirstName VARCHAR(30) NOT NULL,
FacLastName VARCHAR(30) NOT NULL,
FacCity VARCHAR(30) NOT NULL,
FacState CHAR(2) NOT NULL,
FacDept CHAR(6) NULL,
FacRank CHAR(4) NULL,
FacSalary DECIMAL(10,2) NULL,
FacSupervisor CHAR(11) NOT NULL,
FacHireDate DATETIME NULL,
FacZipCode CHAR(10) NOT NULL,
CONSTRAINT FacultyPK PRIMARY KEY (FacNo),
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty
ON DELETE NO ACTION
ON UPDATE NO ACTION )engine = innodb;
This is the OFFERING table making the reference:
CREATE TABLE Offering (
OfferNo INTEGER NOT NULL,
CourseNo CHAR(6) NOT NULL,
OffTerm CHAR(6) NOT NULL,
OffYear INTEGER NOT NULL,
OffLocation VARCHAR(30) NULL,
OffTime VARCHAR(10) NULL,
FacNo CHAR(11) NOT NULL,
OffDays CHAR(4) NULL,
CONSTRAINT OfferingPK PRIMARY KEY (OfferNo),
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course,
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty )ENGINE = INNODB;

Your foreign key references are lacking columns:
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty(Facno)
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course(CourseNo),
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty(FacNo)
Here is a SQL Fiddle. It doesn't have the CourseNo foreign key, because that isn't defined in the question.

Related

Table in mySQL that has several foreign keys that are primary key, getting an error ERROR 1215: Cannot add foreign key constraint

I had created those tables:
CREATE TABLE `course` (
`idcourse` varchar(2) NOT NULL,
`courseName` varchar(45) NOT NULL,
`subjectID` varchar(2) NOT NULL,
PRIMARY KEY (`idcourse`),
KEY `subjectID_idx` (`subjectID`),
CONSTRAINT `subjectID` FOREIGN KEY (`subjectID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `exam` (
`subjectID` varchar(2) NOT NULL,
`courseID` varchar(2) NOT NULL,
`examNumber` varchar(2) NOT NULL,
`duration` int(11) DEFAULT NULL,
PRIMARY KEY (`subjectID`,`courseID`,`examNumber`),
KEY `idCourse_idx` (`courseID`),
CONSTRAINT `idCo` FOREIGN KEY (`courseID`) REFERENCES `course` (`idcourse`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `idSu` FOREIGN KEY (`subjectID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `question` (
`questionText` varchar(100) DEFAULT NULL,
`answer1` varchar(100) DEFAULT NULL,
`answer2` varchar(100) DEFAULT NULL,
`answer3` varchar(100) DEFAULT NULL,
`answer4` varchar(100) DEFAULT NULL,
`subjetID` varchar(2) NOT NULL,
`questionNumber` varchar(3) NOT NULL,
`rightAnswer` int(4) DEFAULT NULL,
PRIMARY KEY (`subjetID`,`questionNumber`),
CONSTRAINT `idsubject` FOREIGN KEY (`subjetID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `subject` (
`idsubject` varchar(2) NOT NULL,
`subjectName` varchar(45) NOT NULL,
PRIMARY KEY (`idsubject`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
To subject table I add a record:
idsubject = 02, subjectName = Mathematica
To course table I add a record:
idcourse = 03, courseName = Algebra 1, subjectID = 02
To exam table I add a record:
subjectID = 02, courseID = 03, examNumber = 01, duration = 180
Now, I want to create a table: questionsinexam
CREATE TABLE `test`.`questionsinexam` (
`idExamSubject` VARCHAR(2) NOT NULL,
`idExamCourse` VARCHAR(2) NOT NULL,
`idExamNumber` VARCHAR(2) NOT NULL,
`idQuestionNumber` VARCHAR(3) NOT NULL,
`pointsPerQuestion` INT NULL,
PRIMARY KEY (`idExamSubject`, `idExamCourse`, `idExamNumber`, `idQuestionNumber`),
INDEX `idExamCourse_idx` (`idExamCourse` ASC),
INDEX `idExamNumber_idx` (`idExamNumber` ASC),
INDEX `idQuestionNumber_idx` (`idQuestionNumber` ASC),
CONSTRAINT `idExamSubject`
FOREIGN KEY (`idExamSubject`)
REFERENCES `test`.`exam` (`subjectID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `idExamCourse`
FOREIGN KEY (`idExamCourse`)
REFERENCES `test`.`exam` (`courseID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `idExamNumber`
FOREIGN KEY (`idExamNumber`)
REFERENCES `test`.`exam` (`examNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `idQuestionNumber`
FOREIGN KEY (`idQuestionNumber`)
REFERENCES `test`.`question` (`questionNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Why I'm getting this error?
Thanks.
This constraint is definitely wrong:
CONSTRAINT `idExamSubject`
FOREIGN KEY (`idExamSubject`)
REFERENCES `test`.`exam` (`subjectID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
Perhaps you intend:
CONSTRAINT `idExamSubject`
FOREIGN KEY (`idExamSubject`)
REFERENCES `test`.`subject` (`subjectID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
Be sure that you create the target tables before you create the foreign key constraint. The referred to table needs to exist first (so the engine can validate types on the columns used for the foreign key).

SQL foreign key,not able to set foreign key constraint

CREATE TABLE students(
student_id int(11) NOT NULL AUTO_INCREMENT,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
advisor_id int(11) DEFAULT NULL,
PRIMARY KEY (student_id),
KEY advisor_id (advisor_id),
CONSTRAINT FOREIGN KEY (advisor_id) REFERENCES advisors (advisor_id)
);
CONSTRAINT (advisor_id) FOREIGN KEY REFERENCES advisors (advisor_id)

Cannot add foreign key constraint for mysql

CREATE TABLE EMPLOYEE
(enum VARCHAR(5) NOT NULL,
first_name VARCHAR(30) NOT NULL,
Last_name VARCHAR(30) NOT NULL,
Salary decimal(8,2) NOT NULL,
CONSTRAINT EMPLOYEE_pkey PRIMARY KEY(enum));
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));
CREATE TABLE MANAGER
(enum VARCHAR(5) NOT NULL,
role VARCHAR(10) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT MANAGER_fkey FOREIGN KEY(enum)
REFERENCES EMPLOYEE(enum) ON DELETE CASCADE);
CREATE TABLE SUBJECT
(subjectCode CHAR(10) NOT NULL,
SubjectName CHAR(30) NOT NULL,
credit decimal(3,3) NOT NULL,
taughtBy char(10) NOT NULL,
CONSTRAINT SUBJECT_PK PRIMARY KEY(subjectCode),
CONSTRAINT SUBJECT_fkey FOREIGN KEY(taughtBy)
REFERENCES LECTURER(lecturerNum) ON DELETE CASCADE,
CONSTRAINT CREDIT_CHECK CHECK(credit >0));
I did not find any error on my create table, why when I run it at the subject table it says:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why..... can anyone help me with it?
LECTURER.lecturerNum does not have an index, which is required to be able to use it in a FK constraint:
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
KEY lecturerNum (lecturerNum),
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));

Error Code 1215. Cannot add foreign key constraint for my tables

I'm trying to write a script for SQL for adding 2 tables and adding another table that references both tables through a foreign key. I keep getting an error from the 'Enrolls' table. It says foreign key cannot be create.
Here are the tables.
CREATE TABLE IF NOT EXISTS `homework7`.`Section` (
`CourseNo` INT NOT NULL,
`SectionNo` INT NOT NULL,
`Instructor` VARCHAR(45) NULL,
PRIMARY KEY (`CourseNo`, `SectionNo`),
FOREIGN KEY (`CourseNo`) REFERENCES Course(`CourseNo`));
CREATE TABLE IF NOT EXISTS `homework7`.`Student` (
`SSN` INT NOT NULL,
`FirstName` VARCHAR(45) NULL,
`LastName` VARCHAR(45) NULL,
`Street` VARCHAR(45) NULL,
`City` VARCHAR(45) NULL,
`State` VARCHAR(2) NULL,
`Zip` INT NULL,
PRIMARY KEY (`SSN`));
Here's the one I'm having trouble with.
CREATE TABLE IF NOT EXISTS `homework7`.`Enrolls` (
`SSN` INT NOT NULL,
`CourseNo` Int NOT NULL,
`SectionNo` INT NOT NULL,
PRIMARY KEY (`SSN`, `SectionNo`, `CourseNo`),
FOREIGN KEY (`SSN`) REFERENCES Student(`SSN`),
FOREIGN KEY (`CourseNo`) REFERENCES Section(`CourseNo`),
FOREIGN KEY (`SectionNo`) REFERENCES Section(`SectionNo`));
Also the schema is here.
http://imgur.com/a/fTg5O
So should
Enrolls (CourseNo) reference Course (CourseNo) or Section (CourseNo)?
foreign key must be primary key of other table so for the trouble portion u can use Course(CourseNo) instead of Section(CourseNo) and liff also mention that
CREATE TABLE IF NOT EXISTS `homework7`.`Enrolls` (
`SSN` INT NOT NULL,
`CourseNo` Int NOT NULL,
`SectionNo` INT NOT NULL,
PRIMARY KEY (`SSN`, `SectionNo`, `CourseNo`),
FOREIGN KEY (`SSN`) REFERENCES Student(`SSN`),
FOREIGN KEY (`CourseNo`) REFERENCES Course(`CourseNo`),
FOREIGN KEY (`SectionNo`) REFERENCES Section(`SectionNo`));
Try:
FOREIGN KEY (`CourseNo`, `SectionNo`) REFERENCES Section(`CourseNo`, `SectionNo`)
Foreign keys must reference fields indexed in the table referenced.

can't add foreign key constraint

CREATE TABLE tblTransaction (
strTransCode VARCHAR(50) NOT NULL,
dtmTransDate datetime,
strOwnCode VARCHAR(50) NOT NULL,
strOwnName VARCHAR(50) NOT NULL,
strTransDesc VARCHAR(50) NOT NULL,
dblTransAmt double,
strAcctCode VARCHAR(50) NOT NULL,
strEntryCode VARCHAR(50) NOT NULL,
FOREIGN KEY (strEntryCode) REFERENCES tblEntry (strEntryCode) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (strAcctCode) REFERENCES tblAccount (strAcctCode) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (strTransCode)
)ENGINE=InnoDB;
CREATE TABLE tblVoucher (
intVoucRefCode INT,
strVoucRefDesc VARCHAR(50) NOT NULL,
dtmVoucDate datetime,
strOwnCode VARCHAR(50) NOT NULL,
strOwnVoucCode VARCHAR(50) NOT NULL,
FOREIGN KEY (strOwnVoucCode) REFERENCES tblTransaction(strOwnCode) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (intVoucRefCode)
)ENGINE=InnoDB;
can't add foreign key constraint help
can't add foreign key constraint help
can't add foreign key constraint help
can't add foreign key constraint help
To create a foreign key for strOwnCode from table tblTransaction you need to define strOwnCode as primary key
CREATE TABLE tblTransaction (
strTransCode VARCHAR(50) NOT NULL,
dtmTransDate datetime,
strOwnCode VARCHAR(50) NOT NULL,
strOwnName VARCHAR(50) NOT NULL,
strTransDesc VARCHAR(50) NOT NULL,
dblTransAmt double,
strAcctCode VARCHAR(50) NOT NULL,
strEntryCode VARCHAR(50) NOT NULL,
PRIMARY KEY (strOwnCode)
)ENGINE=InnoDB;
DEMO