New to SQL, CREATE TABLE error - mysql

CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_DELIVERY1`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID` , `DELIVERY_DRIVER_dr_ID`)
(`VEHICLE_veh_ID` , `DRIVER_dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
I'm new to SQL and trying to learn by practicing. There's an error in my syntax that I don't understand. Any help/suggestions?
(VEHICLE_veh_ID , DRIVER_dr_ID) at line 9
CREATE TABLE `DRIVER` (
`dr_ID` INT NOT NULL,
`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,
PRIMARY KEY (`dr_ID`));
CREATE TABLE `VEHICLE` (
`veh_ID` INT NOT NULL,
`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,
PRIMARY KEY (`veh_ID`));
These are the other tables ROUTE is related to. I had no issues inserting DRIVER and VEHICLE tables into the DB, what's wrong with ROUTE?

It looks like there are two foreign keys in the ROUTE table.
One of them references the DRIVER table.
The other references the VEHICLE table.
Your foreign key constraint definitions should look more like this:
, CONSTRAINT `fk_ROUTE_DELIVERY_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER` (`dr_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT
, CONSTRAINT `fk_ROUTE_DELIVERY_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE` (`veh_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT

CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE`(`veh_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ROUTE_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER`(`dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

Related

Can't create constraint key

I have these 3 MySQL tables:
Apps:
CREATE TABLE IF NOT EXISTS `apps` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`id` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NULL,
`description` VARCHAR(255) NULL,
`cloud` VARCHAR(255) NULL,
`onpremise` VARCHAR(255) NULL,
`type` VARCHAR(255) NULL,
`editor` VARCHAR(255) NULL,
`provider` VARCHAR(255) NULL,
`administrators` INT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT admin_ref_team
FOREIGN KEY(`administrators`) REFERENCES `teams`(`identifier`)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Quality:
CREATE TABLE IF NOT EXISTS `quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`quality` VARCHAR(255) NOT NULL,
`label` VARCHAR(255) NOT NULL,
PRIMARY KEY(`identifier`)
);
Association of apps and quality:
CREATE TABLE IF NOT EXISTS `assoc_apps_quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`apps_id` VARCHAR(255) NOT NULL,
`quality_id` INT NOT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT apps_ref_quality
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT quality_ref_apps
FOREIGN KEY(`quality_id`) REFERENCES `quality`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Apps and Quality are created, but the third table is giving me this error and I can't create it:
Error in query (1215): Cannot add foreign key constraint
I don't see where is the problem. The constraint names are unique among all the database and there is no typos. Any ideas ?
Your apps_id defined in "quality_ref_apps" is wrong please try below sql.
CREATE TABLE IF NOT EXISTS `assoc_apps_quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`apps_id` INT NOT NULL,
`quality_id` INT NOT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT apps_ref_quality
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT quality_ref_apps
FOREIGN KEY(`quality_id`) REFERENCES `quality`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Change from apps_id VARCHAR(255) NOT NULL, TO apps_id INT NOT NULL,
Check data type.
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
apps_id is VARCHAR,
identifier is INT

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.

MySQL Workbench won't allow me to create foreign keys

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

Syntax Error while creating a table in SQL

I am trying this query and despite several attempts, I am still getting a syntax error when I create Table Call. The other tables get created just fine.. I don't understand why
Code for Student
CREATE TABLE Student (
`student_id` int NOT NULL AUTO_INCREMENT,
`phone_number` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`school` varchar(50) NOT NULL,
`class` varchar(50) NOT NULL,
PRIMARY KEY (`student_id`)
)
Table Stories
CREATE TABLE Stories (
`story_id` int NOT NULL AUTO_INCREMENT,
`story_name` varchar(50) NOT NULL,
`number_questions` int NOT NULL,
`file_name` varchar(100) NOT NULL,
PRIMARY KEY (`story_id`)
)
Table Questions
CREATE TABLE Questions (
`question_id` int NOT NULL,
`story_id` int NOT NULL,
`file_name` varchar(100) NOT NULL,
`concept_tested` varchar(100) NOT NULL,
`difficuly` varchar(50) NOT NULL,
`number_options` int NOT NULL,
`correct_answer` int NOT NULL,
`call_number` int NOT NULL,
PRIMARY KEY (`question_id`,`story_id`),
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`story_id`)
ON DELETE CASCADE
)
Table Call
CREATE TABLE Call (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Stories`(`question_id`)
ON DELETE CASCADE
)
Here is my 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 'Call ( `call_id` int NOT NULL AUTO_INCREMENT, `student_id` int NOT NULL, ' at line 1
I have tried editing my code and checking it several times but the problem remains unsolved
I solved the problem,
It is:
CREATE TABLE `Call` (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Questions`(`question_id`)
ON DELETE CASCADE
)
I found that these conditions must be satisfied to not get error 150:
The two tables must be ENGINE=InnoDB. (can be others: ENGINE=MyISAM works too)
The two tables must have the same charset.
The PK column(s) in the parent table and the FK column(s) must be the same data type.
The PK column(s) in the parent table and the FK column(s), if they have a define collation type, must have the same collation type;
If there is data already in the foreign key table, the FK column value(s) must match values in the parent table PK columns.
And the child table cannot be a temporary table.
Hope this helps.
Try like this
CREATE TABLE Questions (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Stories`(`question_id`)
ON DELETE CASCADE
)ENGINE=MYISAM CHARACTER SET UTF8;
FIDDLE DEMO
Take a look at here

Mysql: using two foreign keys to the same table

I'm using MySQL workbench to design a database. Server is mysql 5.5.6
I've defined a few foreign keys linking the "candidates" table to the "countries" table. I get this error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)
The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. Is that allowed? Is this the right way to do it?
Here is the generated code that seems to trigger the issue.
CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom` VARCHAR(40) NULL,
`prenom` VARCHAR(40) NULL,
`qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
`sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
`date_de_naissance` DATE NULL,
`Nationalite` INT NOT NULL,
`selor_bilinguisme` TINYINT(1) NULL,
`rue` VARCHAR(60) NULL,
`numero` VARCHAR(10) NULL,
`pays` INT NOT NULL,
`region` INT NOT NULL,
`localité` VARCHAR(40) NULL,
`code_postal` VARCHAR(10) NULL,
`email` VARCHAR(241) NULL,
`tel_domicile` VARCHAR(30) NULL,
`tel_bureau` VARCHAR(30) NULL,
`tel_mobile` VARCHAR(30) NULL,
`tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
PRIMARY KEY (`id`),
INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
INDEX `fk_candidats_régions1_idx` (`region` ASC),
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`Nationalite` , `pays`)
REFERENCES `customer`.`pays` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_régions1`
FOREIGN KEY (`region`)
REFERENCES `customer`.`régions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The "pays" table ("countries" in French)
CREATE TABLE IF NOT EXISTS `customer`.`pays` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom_fr` VARCHAR(45) NULL,
`nom_nl` VARCHAR(45) NULL,
`nationalite_fr` VARCHAR(45) NULL,
`nationalite_nl` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
Your schema generator doesn't work correctly.
It should generate:
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`pays`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
FOREIGN KEY (`Nationalite`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship.