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)
Related
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.
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')));
USE ccva1;
CREATE TABLE art_piece(
art_inventory_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
artist_id INT(11) NOT NULL,
gallery_id INT(11) NOT NULL,
art_collection VARCHAR(4) NOT NULL,
art_genre VARCHAR(50)NOT NULL,
art_title VARCHAR(50)NOT NULL,
art_medium VARCHAR(50)NOT NULL,
art_acquired VARCHAR(200)NOT NULL,
art_date_acquired INT(10)NOT NULL,
art_appraisal_value VARCHAR(20),
art_size_inches VARCHAR(65),
art_size_centimeters VARCHAR(50),
art_image VARCHAR(500)NOT NULL,
art_modified_by VARCHAR(30) NOT NULL,
art_modified_date VARCHAR(30),
INDEX art_piece_ind(artist_id),
FOREIGN KEY (artist_id)
REFERENCES artist(artist_id)
ON DELETE CASCADE,
INDEX art_piece1_ind(gallery_id),
FOREIGN KEY (gallery_id)
REFERENCES gallery(gallery_id)
ON DELETE CASCADE,
INDEX art_piece2_ind(artist_id),
FOREIGN KEY (art_modified_by)
REFERENCES admin(admin_user_name)
ON DELETE CASCADE )
ENGINE = INNODB;
CREATE TABLE art_exhibition(exhibition_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
gallery_id INT(11) NOT NULL,
exhibition_name VARCHAR(200) NOT NULL,
exhibition_date VARCHAR(60) NOT NULL,
exhibition_time VARCHAR(30) NOT NULL,
exhibition_description VARCHAR(3000) NOT NULL,
exhibition_image VARCHAR(65) NOT NULL,
exhibition_modified_by VARCHAR(30)NOT NULL,
exhibition_modified_date VARCHAR(30),
INDEX gal_ind(gallery_id),
FOREIGN KEY (gallery_id)
REFERENCES gallery(gallery_id),
INDEX artist_ind(artist_id),
FOREIGN KEY (artist_id)
REFERENCES artist(artist_id),
CONSTRAINT admin_unique UNIQUE (admin_user_name)
) ENGINE = INNODB;
You don't have a column called artist_id in art_exhibition.
Assuming you have a table called artist, define a column with the appropriate type, which is presumably int, before defining the foreign key constraint.
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.
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