Cannot add foreign key constraint for mysql - 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')));

Related

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)

#1072 - Key column 'Customer_ID' doesn't exist in table

This is the parent table
CREATE TABLE Customer(
Customer_ID INT(5) not null,
CustName VARCHAR(50) not null,
CustSurname VARCHAR(50) not null,
CustEmail VARCHAR(100) unique not null,
CustMobileNo INT(12) not null,
HomeAddress VARCHAR(255) not null,
Password VARCHAR(10) not null,
constraint c_cuid_pk PRIMARY KEY (Customer_ID))
ENGINE = InnoDB;
When I try to create the property table it shows me
"#1072 - Key column 'Customer_ID' doesn't exist in table"
this is the child table
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
In table Property, you must define a column named Customer_ID (well it could be named anything!), then you define the FOREIGN KEY on it.
Your code currently tries to link IndificualCustomer(Customer_ID) to Property(Customer_ID), but Property(Customer_ID) does not exist.
Declaring the constraint just links the columns, but it does not create them.
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
In the first place, you should add a column: Customer_ID to your Property table to create your foreign key constraint:
Property.Customer_ID → IndividualCustomer.Customer_ID
You can not make a constraint on something that does not exist.
Or change the +++ +++ part of your table creation query to a column that exists.
constraint p_cuid_fk FOREIGN KEY (+++Customer_ID+++) --to be changed accordingly
references IndividualCustomer(Customer_ID)
Example: adding the Customer_ID column to your table declaration would give you the following code
CREATE TABLE Property(
Property_ID INT(5) not null,
Customer_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
PRIMARY KEY (Property_ID),
KEY p_cuid_fk (Customer_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references Customer(Customer_ID))
ENGINE = InnoDB;
You have to add Customer_ID in the table PROPERTY.

MySql foreign key constraint issue

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.

Mysql foreign key constraint is incorrectly formed?

I am receiving an error when attempting to create some tables in mysql with the foreign key
CREATE TABLE session (
code CHAR(2) NOT NULL,
date DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (date),
CONSTRAINT session_fk FOREIGN KEY (code)
REFERENCES module(code));
CREATE TABLE module (
code CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (code));
Here are the two tables I am trying to create, the syntax I've used matches w3 schools and both data types are the same so I cannot see how this is incorrect, any help would be appreciated thanks :)
You're trying to create a foreign key on table before creating the referencing table.
Interchanging the order of query will work :
CREATE TABLE module (
`code` CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (`code`));
CREATE TABLE `session` (
`code` CHAR(2) NOT NULL,
`date` DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (`date`),
CONSTRAINT session_fk FOREIGN KEY (`code`)
REFERENCES module(`code`));
Try this
CREATE TABLE module (
code CHAR(2) NOT NULL,
name VARCHAR(30) NOT NULL,
cost DECIMAL(8,2) NOT NULL,
credits TINYINT NOT NULL,
course_code CHAR(3) NOT NULL,
CONSTRAINT module_pk PRIMARY KEY (code));
CREATE TABLE session (
code CHAR(2) NOT NULL,
date DATE,
room VARCHAR(30) NULL,
CONSTRAINT session_pk PRIMARY KEY (date),
CONSTRAINT session_fk FOREIGN KEY (code)
REFERENCES module(code));

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