Error 1064 (42000 in mysql code - mysql

When i put my code in
CREATE TABLE EVENT (
Event_ID SMALLINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
Event_Name VARCHAR(30) NOT NULL,
Event_Date DATE NOT NULL,
Event_Route VARCHAR(40) NOT NULL,
Event_Location VARCHAR(60) NULL,
Event_Cost DECIMAL(8,2) NOT NULL,
Staff_ID SMALLINT FOREIGN KEY NOT NULL,
Package_ID SMALLINT FOREIGN KEY NOT NULL,
CONSTRAINT Staff_ID FOREIGN KEY (Staff_ID) REFERENCES STAFF (Staff_ID),
CONSTRAINT Package_ID FOREIGN KEY (Package_ID) REFERENCES PACKAGE (Package_ID)
);
I get this error message below
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 NOT NULL, Package_ID SMALLINT FOREIGN
KEY NOT NULL, CONSTRAINT Staff' at line 8

I think the error is pretty clear. You have key words in the middle of the definition of Package_Id and Staff_Id (FOREIGN KEY). These are not necessary, they are handled by the constraint definition:
CREATE TABLE EVENT (
Event_ID SMALLINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
Event_Name VARCHAR(30) NOT NULL,
Event_Date DATE NOT NULL,
Event_Route VARCHAR(40) NOT NULL,
Event_Location VARCHAR(60) NULL,
Event_Cost DECIMAL(8,2) NOT NULL,
Staff_ID SMALLINT NOT NULL,
Package_ID SMALLINT NOT NULL,
CONSTRAINT Staff_ID FOREIGN KEY (Staff_ID) REFERENCES STAFF (Staff_ID),
CONSTRAINT Package_ID FOREIGN KEY (Package_ID) REFERENCES PACKAGE (Package_ID)
);
I would dissuade you from naming the table Event or even Events. The name is not reserved, but it is a keyword in MySQL.

Related

Mysql statement is having syntax issue

I am running below the MYSQL query and getting this error. Can someone help me here? I am using xampp to run MySQL and executing the statement
#1064 - 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 'NOT NULL,
DeliveryMode varchar NOT NULL,
Qty int NOT NULL,
Spicy ' at line 6
Below is my create table query
CREATE TABLE Bag (
BagId int NOT NULL AUTO_INCREMENT,
DishName varchar NOT NULL,
DeliveryMode varchar NOT NULL,
Qty int NOT NULL DEFAULT 1,
Spicy varchar(20) NULL,
SpecialInstructionsFood varchar(250),
BagStatus varchar(100) NOT NULL,
DateCreated datetime DEFAULT current_timestamp(),
DateUpdated datetime DEFAULT current_timestamp(),
FOREIGN KEY(DishName) REFERENCES Dish(DishName),
CONSTRAINT FK_DishName_Bag FOREIGN KEY(DishName)
REFERENCES Dish(DishName),
FOREIGN KEY(DeliveryMode) REFERENCES DeliveryModeType(Name),
CONSTRAINT FK_DeliveryModeType_Bag FOREIGN KEY(DeliveryMode)
REFERENCES DeliveryModeType(Name),
FOREIGN KEY(Spicy) REFERENCES SpicyType(Name),
CONSTRAINT FK_SpicyType_Bag FOREIGN KEY(Spicy)
REFERENCES SpicyType(Name),
FOREIGN KEY(BagStatus) REFERENCES BagStatusType(Name),
CONSTRAINT FK_BagStatusType_Bag FOREIGN KEY(BagStatus)
REFERENCES BagStatusType(Name),
PRIMARY KEY (BagId)
);
When you define a varchar in mysql you must provide the max length that it supports.
So modify your columns(DishName & DeliveryMode) which uses varchar as datatype to something like varchar(50) whichever suits them.
DishName VARCHAR(50) NOT NULL,
DeliveryMode VARCHAR(20) NOT NULL
CREATE TABLE Bag (
BagId int(11) NOT NULL AUTO_INCREMENT,
DishName varchar(100) NOT NULL,
DeliveryMode varchar(40) NOT NULL,
Qty int(11) NOT NULL DEFAULT 1,
Spicy varchar(20),
SpecialInstructionsFood varchar(250),
BagStatus varchar(100) NOT NULL,
DateCreated datetime DEFAULT current_timestamp(),
DateUpdated datetime DEFAULT current_timestamp(),
FOREIGN KEY(DishName) REFERENCES Dish(DishName),
CONSTRAINT FK_DishName_Bag FOREIGN KEY(DishName)
REFERENCES Dish(DishName),
FOREIGN KEY(DeliveryMode) REFERENCES DeliveryModeType(Name),
CONSTRAINT FK_DeliveryModeType_Bag FOREIGN KEY(DeliveryMode)
REFERENCES DeliveryModeType(Name),
FOREIGN KEY(Spicy) REFERENCES SpicyType(Name),
CONSTRAINT FK_SpicyType_Bag FOREIGN KEY(Spicy)
REFERENCES SpicyType(Name),
FOREIGN KEY(BagStatus) REFERENCES BagStatusType(Name),
CONSTRAINT FK_BagStatusType_Bag FOREIGN KEY(BagStatus)
REFERENCES BagStatusType(Name),
PRIMARY KEY (BagId)
);

What is the correct way to make text column not null

I'm try to create a table in mysql
CREATE TABLE messages (
message_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
time_stamp DATETIME NOT NULL,
from INT UNSIGNED NOT NULL,
to INT UNSIGNED NOT NULL,
message_content TEXT NOT NULL,
media_id INT,
seen BOOLEAN,
subject TINYTEXT NOT NULL,
PRIMARY KEY (message_id),
FOREIGN KEY (from) REFERENCES user (user_id),
FOREIGN KEY (to) REFERENCES user (user_id),
FOREIGN KEY (media_id) REFERENCES digital_media (media_id),
INDEX (time_stamp, from, to, seen));
But I'm getting the error message
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 'from INT UNSIGNED NOT NULL,
to INT UNSIGNED NOT NULL,
message_content TEXT NOT N' at line 4
I have no idea what that error message means, I don't understand why the syntax is wrong.
to and from are lousy names for columns, because they are SQL key words.
I think this will work:
CREATE TABLE messages (
message_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
time_stamp DATETIME NOT NULL,
from_user_id INT UNSIGNED NOT NULL,
to_user_id INT UNSIGNED NOT NULL,
message_content TEXT NOT NULL,
media_id INT,
seen BOOLEAN,
subject TINYTEXT NOT NULL,
PRIMARY KEY (message_id),
FOREIGN KEY (from_user) REFERENCES user (user_id),
FOREIGN KEY (to_user) REFERENCES user (user_id),
FOREIGN KEY (media_id) REFERENCES digital_media (media_id),
INDEX (time_stamp, from_user, to_user, seen)
);
It works without the foreign key constraints.

FOREIGN KEY constraint help Mysql

I'm reviewing mysql and I am working on composing foreign keys when creating tables. However when I create the foreign key I receive a syntax error.
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id)
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Can anyone see what I've done wrong here?
EDIT:
The error I receive reports as:
19:35:55 CONSTRAINT staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_id)) Error Code: 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 staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_' at line 1 0.00025 sec
UPDATE: Error Code: 1215. Cannot add foreign key constraint 0.110 sec
How does one resolve error code 1215??
You are missing comma:
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id), -- here
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Try this code
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
Foregn_Key int,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id),
CONSTRAINT staff_id_fk FOREIGN KEY (Foregn_Key)
REFERENCES computer_staff (Foregn_Key)
);

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
.

What is wrong with my foreign key in mySQL?

Whenever I try to run this script in mySQL I get an error that reads:
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 '
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIG' at line 7
The code for the table it is referencing is on top and the code for the table with the failing foreign key constraint is below:
CREATE TABLE CATEGORY(
Category_Name varchar(35) NOT NULL,
ShippingPerPound DECIMAL(4,2),
OffersAllowed ENUM('y', 'n'),
CONSTRAINT CATEGORY_pk PRIMARY KEY(Category_Name)
);
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);
Your Category_Name declaration in yourITEM` table is incomplete:
Try:
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name varchar(35) NOT NULL,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);