MySQL syntax Error on create table query - mysql

im creating tables in mySql through phpmyadmin as soon as i run this query
CREATE TABLE `teacher` (
'id' int(11) NOT NULL,
'name' varchar(45) NOT NULL,
'gender' ENUM('F','M') NOT NULL,
'department' varchar(32) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY ('department') REFERENCES
departments('name')
);
#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
''id' int(11) NOT NULL, 'name' varchar(45) NOT NULL, 'gender' ENUM('F','M') N'
at line 2
this error shows up

Try this:
CREATE TABLE teacher (
id int(11) NOT NULL,
name varchar(45) NOT NULL,
gender ENUM('F', 'M') NOT NULL,
department varchar(32) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (department) REFERENCES departments(name)
);

Try this
CREATE TABLE teacher (
id int(11) NOT NULL,
name varchar(45) NOT NULL,
gender ENUM('F','M') NOT NULL,
department varchar(32) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (department) REFERENCES
departments(name)
);
for more Click

Related

MySQL workbench data importing, foreign key error

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;

Error when uploading table to server

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.

Error creating tables with foreign keys

I modelled a really simple DB for the first time and trying to insert the queries:
CREATE TABLE `product` (
`pid` varchar(255) NOT NULL,
`mfr` varchar(255) NULL,
`pnum` varchar(255) NOT NULL,
`ssku` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
CREATE TABLE `detail` (
`pid` varchar(255) NOT NULL,
`sdesc` varchar(255) NULL,
`supplier` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
CREATE TABLE `images` (
`pid` varchar(255) NOT NULL,
`url` varchar(255) NULL,
`alt` varchar(255) NULL,
`position` varchar(255) NULL,
PRIMARY KEY (`pid`)
);
ALTER TABLE `detail` ADD CONSTRAINT `fk_detail` FOREIGN KEY (`pid`) REFERENCES `product` ();
ALTER TABLE `images` ADD CONSTRAINT `pid` FOREIGN KEY () REFERENCES `product` ();
... but I get this error:
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 ') REFERENCES `product` ()' at line 1
what am I doing wrong?
You must pass the column name to the REFERENCES clause:
REFERENCES product(pid);
Also, you're creating a blank foreign key on the images constraint:
FOREIGN KEY () REFERENCES `product` ();

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

MYSQL help not letting me create table

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