I'm Confused as to why my code isnt working.
CREATE TABLE DELIVERY (
VEHICLE_VEH_ID INT NOT NULL,
DRIVER_DR_ID INT NOT NULL,
DEL_DATE DATETIME NOT NULL,
DEL_TIME DATETIME NOT NULL,
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID)
INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID)
CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER FOREIGN KEY (DRIVER_DR_ID) DRIVER (DR_ID));
CREATE TABLE DRIVER (
DR_ID INT NOT NULL PRIMARY KEY,
DR_TITLE VARCHAR(15) NOT NULL,
DR_FNAME VARCHAR(45) NOT NULL,
DR_LNAME VARCHAR(45) NOT NULL,
DR_DOB DATETIME NOT NULL,
DR_LICENCENO VARCHAR(45) NOT NULL,
DR_PHONE VARCHAR(15) NOT NULL,
DR_EMAIL VARCHAR(45) NOT NULL);
CREATE TABLE VEHICLE (
VEH_ID INT NOT NULL PRIMARY KEY,
VEH_REG VARCHAR(15) NOT NULL,
VEH_MAKE VARCHAR(45) NOT NULL,
VEH_MODEL VARCHAR(45) NOT NULL,
VEH_MILEAGE INT NOT NULL,
VEH_MOTDATE DATETIME NOT NULL,
VEH_SERVICEDATE DATETIME NOT NULL);
mysql> CREATE TABLE DELIVERY (
-> VEHICLE_VEH_ID INT NOT NULL,
-> DRIVER_DR_ID INT NOT NULL,
-> DEL_DATE DATETIME NOT NULL,
-> DEL_TIME DATETIME NOT NULL,
-> PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID)
-> INDEX (DRIVER_DR_ID),
-> INDEX (VEHICLE_VEH_ID),
-> CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE , CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID) FOREIGN KEY (DRIVER_DR_ID) DRIVER (DR_ID));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_V' at line 7
mysql>
Im trying to create a table but it says i have an error when i dont think i do. I'll be very greatful if you can see what i've done wrong. Thanks everyone that helps!
You forgot a comma.
...
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID), -- Need a comma here
INDEX (DRIVER_DR_ID),
...
You first need to create these two tables:
CREATE TABLE DRIVER (
DR_ID INT NOT NULL PRIMARY KEY,
DR_TITLE VARCHAR(15) NOT NULL,
DR_FNAME VARCHAR(45) NOT NULL,
DR_LNAME VARCHAR(45) NOT NULL,
DR_DOB DATETIME NOT NULL,
DR_LICENCENO VARCHAR(45) NOT NULL,
DR_PHONE VARCHAR(15) NOT NULL,
DR_EMAIL VARCHAR(45) NOT NULL);
CREATE TABLE VEHICLE (
VEH_ID INT NOT NULL PRIMARY KEY,
VEH_REG VARCHAR(15) NOT NULL,
VEH_MAKE VARCHAR(45) NOT NULL,
VEH_MODEL VARCHAR(45) NOT NULL,
VEH_MILEAGE INT NOT NULL,
VEH_MOTDATE DATETIME NOT NULL,
VEH_SERVICEDATE DATETIME NOT NULL);
Then use this sql code to create the last one, that references the two tables above:
CREATE TABLE DELIVERY (
VEHICLE_VEH_ID INT NOT NULL,
DRIVER_DR_ID INT NOT NULL,
DEL_DATE DATETIME NOT NULL,
DEL_TIME DATETIME NOT NULL,
PRIMARY KEY (VEHICLE_VEH_ID , DRIVER_DR_ID),
INDEX (DRIVER_DR_ID),
INDEX (VEHICLE_VEH_ID),
CONSTRAINT FK_VEHICLE_HAS_DRIVER_VEHICLE FOREIGN KEY (VEHICLE_VEH_ID) REFERENCES VEHICLE (VEH_ID) ,
CONSTRAINT FK_VEHICLE_HAS_DRIVER_DRIVER FOREIGN KEY (DRIVER_DR_ID) REFERENCES DRIVER (DR_ID));
Your code is also missing two commas plus the REFERENCES keyword in the FK_VEHICLE_HAS_DRIVER_DRIVER foreign key constraint.
Demo here
Related
I keep getting this error and I am not sure what I'm doing wrong. I
Error SQL query:
CREATE TABLE members
(
member_ID INT NOT NULL,
email VARCHAR(255) NOT NULL,
phone CHAR(10) NOT NULL,
rating VARCHAR(5) NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
member_level INT NOT NULL,
PRIMARY KEY (member_ID),
FOREIGN KEY (member_level) REFERENCES member_level(member_level)
)
MySQL said:
#1215 - Cannot add foreign key constraint
The SQL I'm trying to run is below, I can't figure out what the problem with it is
CREATE TABLE category
(
category_ID INT NOT NULL,
category_name VARCHAR(255) NOT NULL,
PRIMARY KEY (category_ID)
);
CREATE TABLE member_level
(
member_level CHAR(1) NOT NULL,
member_level_description VARCHAR(255) NOT NULL,
PRIMARY KEY (member_level)
);
CREATE TABLE members
(
member_ID INT NOT NULL,
email VARCHAR(255) NOT NULL,
phone CHAR(10) NOT NULL,
rating VARCHAR(5) NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
member_level INT NOT NULL,
PRIMARY KEY (member_ID),
FOREIGN KEY (member_level) REFERENCES member_level(member_level)
);
CREATE TABLE tools
(
tool_ID INT NOT NULL,
serial_number VARCHAR(255) NOT NULL,
tool_name VARCHAR(255) NOT NULL,
tool_description VARCHAR(255) NOT NULL,
tool_picture VARCHAR(255) NOT NULL,
Member_ID INT NOT NULL,
PRIMARY KEY (tool_ID),
FOREIGN KEY (member_ID) REFERENCES members(member_ID)
);
CREATE TABLE tool_category
(
tool_ID INT NOT NULL,
category_ID INT NOT NULL,
FOREIGN KEY (tool_ID) REFERENCES tools(tool_ID),
FOREIGN KEY (category_ID) REFERENCES category(category_ID)
);
-- Populate tables
INSERT INTO member_level VALUES ('a', 'admin');
INSERT INTO member_level VALUES ('m', 'member');
member_level in table members is of type int, while in table member_level is of type CHAR(1).
They must be the same type.
I am trying to create a table with multiple foreign keys which reference other parent tables. 1 of my foreign keys is being accepted 'fk_film_id' however the second is not due to a syntax error. how can I get this to pass?
the body of my tables is below:
create table `Actor_Role` (
`Actor_Role_id` INT AUTO_INCREMENT,
`Actor_Role` VARCHAR(20) NOT NULL,
`Character_Name` VARCHAR(50) NOT NULL,
`Alias_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`Actor_Role_id`)
);
create table `Actor` (
`Actor_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Date_of_Birth` DATE NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
`Gender` VARCHAR(10) NOT NULL,
`Residence` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Actor_id`)
);
create table `Film` (
`Film_id` INT AUTO_INCREMENT,
`Title` VARCHAR(50) NOT NULL,
`Release_Date` DATE NOT NULL,
`Running_Time` INT NOT NULL,
`Budget` BIGINT NOT NULL,
`Box_Office` BIGINT NOT NULL,
`Rating` INT NOT NULL,
`Language` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Film_id`)
);
create table `Film_Staff` (
`Staff_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_id`)
);
create table `Staff_Role` (
`Staff_Role_id` INT AUTO_INCREMENT,
`Staff_Role` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_Role_id`)
);
create table `Infinity_Stones` (
`Stone_id` INT AUTO_INCREMENT,
`Colour` VARCHAR(10) NOT NULL,
`Power` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Stone_id`)
);
The table I am trying to make looks like this:
CREATE TABLE Film_Staff_Role(
-> `Film_id` INT NOT NULL,
-> `Role_id` INT NOT NULL,
-> `Staff_id` INT NOT NULL,
-> FOREIGN KEY fk_film_id(Film_id)
-> REFERENCES film(Film_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Role_id(Role_id)
-> REFERENCES Staff_Role(Staff_Role_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Staff_id(Staff_id)
-> REFERENCES Film_Staff(Staff_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> )ENGINE=InnoDB;
However I receive the following issue when I attempt to execute:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'FOREIGN KEY fk_Staff_Role_id(Staff_Role_id)
REFERENCES Staff_Role(Staff_Role_id' at line 9
How can this be overcome?
Fetching back view definitions in final form.
Nothing to fetch
Executing SQL script in server results in:
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `r' at line 16
SQL Code:
CREATE TABLE IF NOT EXISTS `rrm17b`.`employee` (
`emp_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`dpt_id` INT UNSIGNED NULL,
`emp_first` VARCHAR(20) NOT NULL,
`emp_last` VARCHAR(20) NOT NULL,
`emp_type` ENUM('f', 'p') NOT NULL,
`emp_street` VARCHAR(30) NOT NULL,
`emp_city` VARCHAR(20) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` INT UNSIGNED NOT NULL,
`emp_phone` BIGINT UNSIGNED NOT NULL,
`emp_email` VARCHAR(45) NOT NULL,
`emp_notes` VARCHAR(100) NULL,
PRIMARY KEY (`emp_id`),
INDEX `fk_employee_department_idx` (`dpt_id` ASC)
CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `rrm17b`.`department` (`dpt_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Please try this:
CREATE TABLE IF NOT EXISTS rrm17b.employee(
emp_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
dpt_id INT UNSIGNED NULL,
emp_first VARCHAR(20) NOT NULL,
emp_last VARCHAR(20) NOT NULL,
emp_type ENUM('f', 'p') NOT NULL,
emp_street VARCHAR(30) NOT NULL,
emp_city VARCHAR(20) NOT NULL,
emp_state CHAR(2) NOT NULL,
emp_zip INT UNSIGNED NOT NULL,
emp_phone BIGINT UNSIGNED NOT NULL,
emp_email VARCHAR(45) NOT NULL,
emp_notes VARCHAR(100) NULL,
PRIMARY KEY (emp_id),
INDEX fk_employee_department_idx(dpt_id ASC),
CONSTRAINT fk_employee_department
FOREIGN KEY (dpt_id)
REFERENCES rrm17b.department(dpt_id) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB
Fixed multiple things:
--Added comma after INDEX
--Removed "`" after dpt_id
I am having trouble adding 3 tables to my database (MINOR_DEGREES, STUDENT, STUDENT_RECORD).
Can anyone assist?
Relational Map has been included if there is any confusion.
CREATE TABLE PROFESSOR(
PSSN int NOT NULL,
PName varchar(255) NOT NULL,
PStreet varchar(255) NOT NULL,
PCity varchar(255) NOT NULL,
PState varchar(255) NOT NULL,
PZip int NOT NULL,
PArea int NOT NULL,
PNum int NOT NULL,
PSex ENUM ('M','F') NOT NULL,
PTitle varchar(255) NOT NULL,
PSalary int NOT NULL,
PRIMARY KEY (PSSN)
);
CREATE TABLE DEGREES(
ProfessorSSN int NOT NULL,
PDegrees varchar(255) NOT NULL,
PRIMARY KEY (ProfessorSSN, PDegrees),
FOREIGN KEY (ProfessorSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE DEPARTMENT(
DNum int NOT NULL,
DName varchar(255) NOT NULL,
DPhone varchar(255) NOT NULL,
DOffice_Location varchar(255) NOT NULL,
PChairSSN int NOT NULL,
PRIMARY KEY (DNum),
FOREIGN KEY (PChairSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE PRE_REQ(
PreReqCNum int NOT NULL,
PreReqFor varchar(255) NOT NULL,
PreReqTo varchar(255) NOT NULL,
FOREIGN KEY (PreReqCNum) REFERENCES COURSE (CNum) ON DELETE CASCADE
);
CREATE TABLE COURSE(
CNum int NOT NULL,
CTitle varchar(255) NOT NULL,
CUnits int NOT NULL,
CTextBook varchar(255) NOT NULL,
Department_Num int NOT NULL,
PRIMARY KEY (CNum),
FOREIGN KEY (Department_Num) REFERENCES DEPARTMENT (DNum)
);
CREATE TABLE MINOR_DEGREES(
MinorCWID int NOT NULL,
MinorDNum int NOT NULL,
Minor varchar(255) NOT NULL,
PRIMARY KEY (MinorCWID, MinorDNum),
FOREIGN KEY (MinorCWID) REFERENCES STUDENT (SCWID) ON DELETE CASCADE,
FOREIGN KEY (MinorDNum) REFERENCES DEPARTMENT (DNum) ON DELETE CASCADE
);
CREATE TABLE STUDENT(
SCWID int NOT NULL,
SFname varchar(255) NOT NULL,
SLname varchar(255) NOT NULL,
SAdrress varchar(255) NOT NULL,
SPhone int NOT NULL,
Major varchar(255) NOT NULL,
MajorDeptNum int NOT NULL,
PRIMARY KEY (SCWID),
FOREIGN KEY (Major, MajorDeptNum).
);
CREATE TABLE SECTION(
CourseNum int NOT NULL,
SNum int NOT NULL,
Classroom varchar(255) NOT NULL,
Meet_Dates varchar(255) NOT NULL,
Time_Start int NOT NULL,
Time_End int NOT NULL,
No_Of_Seats int NOT NULL,
ProSSN int NOT NULL,
PRIMARY KEY (CourseNum, SNum),
FOREIGN KEY (CourseNum) REFERENCES COURSE (CNum),
FOREIGN KEY (ProSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE STUDENT_RECORD(
Student_SWID int NOT NULL,
Course_Number int NOT NULL,
Section_Number int NOT NULL,
Grade varchar(255)
PRIMARY KEY (Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) REFERENCES STUDENT (SCWID)
);
And the errors
Error for MINOR_DEGREES:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'REFERENCES STUDENT (SCWID) ON DELETE CASCADE,
UNIQUE KEY (MinorDNum) REFERENCE' at line 6
Error for STUDENT:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual. that corresponds to your MariaDB server version for the right syntax to use near ')' at line 11
Error for STUDENT_RECORD:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual. that corresponds to your MariaDB server version for the right syntax to use. near '(Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) RE' at line 6.
I'm trying to create a few tables and one of them has should have foreign keys referencing the other tables, but MySQL Workbench keeps giving me "Error Code: 1215. Cannot add foreign key constraint". This happens if I try to create them during the table creation and if I just create the table and then try to add FK through ALTER. I just can't figure out the problem. I've tried both with and without ENGINE = InnoDB that I saw some people suggest on the web. And yes, tables kommune and person has been created.
CREATE TABLE kommune (
Kommunenr varchar(4) NOT NULL,
Kommunenavn varchar(45) NOT NULL,
PRIMARY KEY (Kommunenr));
CREATE TABLE person (
PersonID varchar(4) NOT NULL,
Fornavn varchar(45) NOT NULL,
Etternavn varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
PRIMARY KEY (PersonID));
CREATE TABLE oppdrag (
Oppdragsnr varchar(5) NOT NULL,
Eiendomnr varchar(4) NOT NULL,
Gateadresse varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
Kommunenr varchar(4) NOT NULL,
Prisantydning varchar(10) NOT NULL,
Solgt boolean NOT NULL,
PRIMARY KEY (Oppdragsnr),
FOREIGN KEY (Postnr) REFERENCES person(Postnr),
FOREIGN KEY (Poststed) REFERENCES person(Poststed),
FOREIGN KEY (Kommunenr) REFERENCES kommune(Kommunenr));
Check the following lines:
FOREIGN KEY (Postnr) REFERENCES person(Postnr),
FOREIGN KEY (Poststed) REFERENCES person(Poststed),
but in your table structure:
CREATE TABLE person (
PersonID varchar(4) NOT NULL,
Fornavn varchar(45) NOT NULL,
Etternavn varchar(45) NOT NULL,
Postnr varchar(4) NOT NULL,
Poststed varchar(45) NOT NULL,
PRIMARY KEY (PersonID));
Postnr, Poststed are neither unique or not primary key. To make foreign key, the referring column in the base table must be an indexed column