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
Related
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?
Im workin in mySQL workbench 8.0CE, i've created two tables, one for person and another one for persons direction, i'm trying to export data, but it throws and error
ERROR 1064 (42000) at line 67: 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 '
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES' at line 8
Operation failed with exitcode 1
This is the sql code
CREATE TABLE IF NOT EXISTS `dinSchema`.`Personas` (
`nombre` VARCHAR(20) NOT NULL,
`apellidoP` VARCHAR(20) NOT NULL,
`apellidoM` VARCHAR(20) NOT NULL,
`foto` MEDIUMBLOB NULL,
`fechaCaptura` TIMESTAMP(6) NOT NULL,
`escolaridad` VARCHAR(25) NOT NULL,
`carrera` VARCHAR(25) NULL,
`telefono` VARCHAR(10) NULL,
`correo` VARCHAR(50) NOT NULL,
`sexo` VARCHAR(10) NOT NULL,
`rfc` VARCHAR(13) NOT NULL,
`curp` VARCHAR(18) NOT NULL,
`observaciones` MEDIUMTEXT NULL,
`idPersonas` INT NOT NULL,
PRIMARY KEY (`idPersonas`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `dinSchema`.`direccion`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `dinSchema`.`direccion` ;
CREATE TABLE IF NOT EXISTS `dinSchema`.`direccion` (
`pais` VARCHAR(6) NOT NULL DEFAULT 'México',
`estado` VARCHAR(20) NOT NULL,
`ciudad` VARCHAR(25) NOT NULL,
`direccion` VARCHAR(150) NOT NULL,
`cp` INT(8) NOT NULL,
`idPersona` INT NOT NULL,
INDEX `fk_PersonaDireccion_idx` (`idPersona` ASC) VISIBLE,
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES `dinSchema`.`Personas` (`idPersonas`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
next i append EER Diagram img
Note: "Personas" id field is at last, beacuse i deleted a foreign key connection.
Somehow i fixed it, unchecked user default global settings in model options, and
INDEX `fk_PersonaDireccion_idx` (`idPersona` ASC) VISIBLE
disapeared, now "direccion" table got like this
CREATE TABLE IF NOT EXISTS `dinSchema`.`direccion` (
`pais` VARCHAR(6) NOT NULL DEFAULT 'México',
`estado` VARCHAR(20) NOT NULL,
`ciudad` VARCHAR(25) NOT NULL,
`direccion` VARCHAR(150) NOT NULL,
`cp` INT(8) NOT NULL,
`idPersona` INT NOT NULL,
CONSTRAINT `fk_PersonaDireccion`
FOREIGN KEY (`idPersona`)
REFERENCES `dinSchema`.`Personas` (`idPersonas`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm learning mySQL and running into syntax errors that I can't seem to fix. I get an error that states that "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 ')' at line 8" line 8 being the declaration of state.
I am checking my code with sqlfiddle and mySQL 5.6 with the following:
CREATE TABLE `customer` (
`customer_id` int(10) NOT NULL,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`address` varchar(150) NOT NULL,
`apartment` int(10) DEFAULT NULL,
`city` varchar(30) NOT NULL,
`state` varchar(30) NOT NULL,
`zip` varchar(10) NOT NULL,
`home_phone` varchar(10) DEFAULT NULL,
`cell_phone` varchar(10) DEFAULT NULL,
`other_phone` varchar(10) DEFAULT NULL,
PRIMARY KEY (`customer_id`)
);
CREATE TABLE `donut` (
`donut_id` int(10) NOT NULL,
`name` varchar(60) NOT NULL,
`description` varchar(150) DEFAULT NULL,
`unit_price` int(4) NOT NULL,
PRIMARY KEY (`donut_id`)
);
CREATE TABLE `order` (
`order_id` int(10) NOT NULL,
`customer_id` int(10) NOT NULL,
`order_date` date DEFAULT NULL,
`handling_notes` varchar(300),
PRIMARY KEY (`order_id`),
FOREIGN KEY (`customer_id`)
);
CREATE TABLE `order_line` (
`order_id` int(10) NOT NULL,
`donut_id` int(10) NOT NULL,
`donut_quantity` int(6) NOT NULL,
PRIMARY KEY (`order_id`, `donut_id`)
);
The FOREIGN KEY clause in the CREATE TABLE statement for order misses a REFERENCES clause.
Presumably you want to replace
FOREIGN KEY (`customer_id`)
with:
FOREIGN KEY (`customer_id`)
REFERENCES `customer`
(`customer_id`)
I created a Database with Mysql Qorkbench and i tried to Forward Engineer to Database but it completed with errors and this is the Message Log:
> Executing SQL script in server
ERROR: Error 1005: Can't create table `mydb`.`cds` (errno: 121 "Duplicate key on write or update")
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`CDs`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`),
UNIQUE INDEX `CDid_UNIQUE` (`CDid` ASC),
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
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.
Could not get definition for mydb.view1 from server
1 views were read back.
This is the EER Diagram of my Database
A primary key is essentially an indexed unique not null constraint. You don't need to add an additional unique constraint, and indeed can't, as the error specifies. Remove the extra unique index clause and you should be OK:
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`), -- no need for an extra index
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
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