MySQL Syntax errors - mysql

CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
),
I have this as part of a database I'm setting up, yet whenever I try to run the .sql file that this is included in, I get the following error:
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 '
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(' at line 8
Any ideas what's wrong? Thanks.

Remove the , from the end CREATE TABLE statement
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
Or use semicolon instead comma:
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
);

You have comma at the end if you remove that it is working fin for me.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)

We don't use "," at the end to separate two sql queries, you have to use ";" and if you are running a single query then you don't need.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)

Related

MySQL Syntax Error - ERROR 1064 because of a close bracket

I am trying to create a mysql table and am getting the following 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 ')' at line 19
here is the create table script:
CREATE TABLE User(
userID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (userID),
username char(30) NOT NULL UNIQUE,
lastName char(30) NOT NULL,
firstName char(30) NOT NULL,
birthDay int NOT NULL CHECK(birthDay BETWEEN 1 AND 31),
birthMonth int NOT NULL CHECK(birthMonth BETWEEN 1 AND 13),
birthYear int NOT NULL CHECK(birthYear > 1900),
password char(30) NOT NULL,
geschlecht char(30) NOT NULL,
profilPicURL char(255) NOT NULL,
CONSTRAINT fk_lives_in
FOREIGN KEY (lives_in)
REFERENCES Location(LocationID),
CONSTRAINT fk_comes_from
FOREIGN KEY (comes_from)
REFERENCES Location(LocationID),
);
i appreciate any help,
thanks!
CREATE TABLE User(
userID int NOT NULL AUTO_INCREMENT Primary Key,
username char(30) NOT NULL UNIQUE,
lastName char(30) NOT NULL,
firstName char(30) NOT NULL,
birthDay int NOT NULL CHECK(birthDay BETWEEN 1 AND 31),
birthMonth int NOT NULL CHECK(birthMonth BETWEEN 1 AND 13),
birthYear int NOT NULL CHECK(birthYear > 1900),
password char(30) NOT NULL,
geschlecht char(30) NOT NULL,
profilPicURL char(255) NOT NULL,
CONSTRAINT fk_lives_in
FOREIGN KEY (lives_in)
REFERENCES Location(LocationID),
CONSTRAINT fk_comes_from
FOREIGN KEY (comes_from)
REFERENCES Location(LocationID)
);
Try it now

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

Error creating table in MySQL 5.7

Can some one help me in correcting the syntax according to MySQL 5.7?
Table :
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
PRIMARY KEY (ID)
);
Error :
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 '(ID)
You forgot comma at the end of this line:
ADDRESS VARCHAR(20) NOT NULL
You dont have a column named ID, you may want EMPID?
Replace
ADDRESS VARCHAR(20) NOT NULL
By
ADDRESS VARCHAR(20) NOT NULL,
There are two problems here:
A primary key clause is it's own clause, and needs to be separated from the previous column definition by a comma.
You don't have an id column - your primary key should (probably) be empid:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY (EMPID)
);
I guess this is what you are looking for:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
);

I'm trying to create a database but i keep getting the same error, and according to me and my fellow students the code is fine

This is just an example, from the first 2 tables but i get the same error:
MySQL said: Documentation
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 ')' at line 10
CREATE TABLE Tallas
(
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
CREATE TABLE Modelo
(
IDModelo int NOT NULL AUTO_INCREMENT,
IDTalla int NOT NULL,
modeloNombre varchar(45) NOT NULL,
precio varchar(45) NOT NULL,
informacion varchar(45) NOT NULL,
PRIMARY KEY (IDModelo),
FOREIGN KEY (IDTalla) REFERENCES Modelo(IDTalla),
)
Remove the coma at PRIMARY KEY (IDTalla), to make it PRIMARY KEY (IDTalla)
and the same with REFERENCES Modelo(IDTalla),
Remove the comma -> (IDTalla),) (IDTalla))
Looks like you put an extra comma in your create table statement just after primary key.
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
This should be:
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla)
)
Same with the second statement.

MySQL error on CREATE TABLE with BOOL column

I have created the following script to set up my MySQL database:
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
};
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
};
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
However, when I run this script I get the following error messages:
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 '}' at line 8
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 '}' at line 17
ERROR 1215 (HY000): Cannot add foreign key constraint
I suspect it has something to do with using the bool datatype since that is what is happening at lines 8 and 17; however I have not been able to find any solutions to this issue online. Is someone able to see what might be causing the problem? I'm running MySQL 5.6.11 on my local Windows machine.
Thanks!
The characters need to be the same. Use parentheses for both
CREATE TABLE homes (
};
In this case: It's showing you the incorrect character in the error message:
check the manual that corresponds to your MySQL server version for the right
syntax to use near '}' at line 8
Get rid of the curly braces and replace with (
Works great : http://sqlfiddle.com/#!8/4ad37
Try this one, I tested it and it created the database successfully. The issues were caused due to the opening and closing brackets not matching each other.
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
);
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
);
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
you have 2 syntax errors as below...
And since below tables are not created, you are facing foreign key error..
run below sqls your problem will be solved :)
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
); /* previously }; */
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
); /* previously }; */