Syntax Error while creating a table in SQL - mysql

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

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 while running script in MySQL workbench version 6.3

First of all i apologize that the names of the tables and so on are in another language.
The issue is i can't seem to add a foreign key named ID_KOPIE from the table KOPIA_KNIHY into the table SKLAD for some reason. When it gets to adding the foreign key to to the table SKLAD it throws out an error 1215. Here is the code:
CREATE TABLE BOOK (
BOOK_NAME VARCHAR(30) NOT NULL,
YEAR CHAR(4) NOT NULL,
NAME_OF_EDITOR VARCHAR(30) NOT NULL,
WRITER_ID INTEGER NOT NULL,
ISBN VARCHAR(17) NOT NULL,
BOOK_ID INTEGER NOT NULL,
PRIMARY KEY (BOOK_ID),
);
CREATE TABLE BOOK_COPY(
BOOK_ID INTEGER NOT NULL,
LANGUAGE_CODE CHAR(3) NOT NULL,
COPY_ID INTEGER NOT NULL,
BOOK_PICTURES CHAR(1) NOT NULL
CHECK (BOOK_PICTURES IN ("Y", "N")),
PRIMARY KEY (BOOK_ID, LANGUAGE_CODE, COPY_ID)
FOREIGN KEY(BOOK_ID)
REFERENCES BOOK(BOOK_ID),
);
CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
I researched the error code 1215 on the internet, i couldn't find anything wrong with my database. I checked if there's a typo or if i didn't forget to add the reference.
This is the error:
0 769 18:19:37 CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
Error Code: 1215. Cannot add foreign key constraint 0.016 sec
My question is how can this be fixed that it would work.
Help would be greatly appreciated.
Try this way. Please do alter the ON UPDATE ... ON DELETE syntax in this example with the one you need.
CREATE TABLE `KNIHA` (
`NAZOV_KNIHY` VARCHAR(30) NOT NULL,
`ROK_PRVEHO_VYDANIA` CHAR(4) NOT NULL,
`NAZOV_VYDAVATELA` VARCHAR(30) NOT NULL,
`ID_AUTORA` INTEGER NOT NULL,
`ISBN` VARCHAR(17) NOT NULL,
`ID_KNIHY` INTEGER NOT NULL,
PRIMARY KEY (`ID_KNIHY`)
);
CREATE TABLE `KOPIA_KNIHY` (
`ID_KNIHY` INTEGER NOT NULL,
`KOD_JAZYKA` CHAR(3) NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ORAZKY_V_KNIHE` CHAR(1) NOT NULL
CHECK (`OBRAZKY_V_KNIHE` IN ("A", "N")),
INDEX(`ID_KOPIE`),
PRIMARY KEY (`ID_KNIHY`, `KOD_JAZYKA`, `ID_KOPIE`),
CONSTRAINT `idx_1` FOREIGN KEY `idx_1` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `SKLAD` (
`ID_KNIHY` INTEGER NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ID_BUDOVY` INTEGER NOT NULL,
`CISLO_MIESTNOSTI` NUMERIC(4,0) NOT NULL,
`CISLO_REGALU` NUMERIC(4,0) NOT NULL,
PRIMARY KEY(`ID_KNIHY`, `ID_BUDOVY`, `ID_KOPIE`),
CONSTRAINT `idx_2` FOREIGN KEY `idx_2` (`ID_KOPIE`) REFERENCES `KOPIA_KNIHY`(`ID_KOPIE`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `idx_3` FOREIGN KEY `idx_3` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
Try it on SQL Fiddle
.

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.

New to SQL, CREATE TABLE error

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);

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.