MYSQL FOREIGN KEY ISSUES - mysql

I am trying to create a table which references two other tables that I have planned to make, but have not made yet. I am wondering if that is the issue here or if there is a syntax error that I am missing. If anyone can help me out it would be greatly appreciated
mysql> CREATE TABLE items (
items$id INT NOT NULL AUTO_INCREMENT,
sales$id INT NOT NULL AUTO_INCREMENT,
img$id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
desc VARCHAR(255) NOT NULL,
PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFERENCES sales(sales$id),
FOREIGN KEY(img$id) REFERENCES image(img$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 'desc VARCHAR(255) NOT NULL,
PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFEREN' at line 6
I tried to remove the references, as in just do 'FOREIGN KEY(sales$id)' and 'FOREIGN KEY(img$id)' to see if that would work (I am new to mysql), but that also did not work. Any help is appreciated.

The problem is your field name: desc
desc is a keyword in mysql see here
Your sql would be better like this:
CREATE TABLE items ( items$id INT NOT NULL AUTO_INCREMENT, sales$id INT NOT NULL AUTO_INCREMENT, img$id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, descr VARCHAR(255) NOT NULL, PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFERENCES sales(sales$id),
FOREIGN KEY(img$id) REFERENCES image(img$id))
Ps: you missed closing bracket ')' at the end of your sql query.

Related

I don't understand why this code is throwing an Error?

I'm running the following code in MySQL Workbench:
CREATE TABLE beer_type ( -- create beer table
type_id INTEGER NOT NULL PRIMARY KEY, -- PK "type_id"
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));
I'm getting:
0 5 21:27:10 CREATE TABLE beer_type ( -- create beer table 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 '' at line 1 0.000 sec
I'm new to SQL so this is a bit confusing to me and my professor isn't the best of help...
Any way anyone can help would be greatly appreciated!!
Copy this and paste :
CREATE TABLE beer_type (
type_id INTEGER NOT NULL PRIMARY KEY,
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));

Simple SQL Query Error

I've been trying to debug this query that I'm using to practice on.
The error I'm getting when trying to build is:
Schema Error: Error: ER_PARSE_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 '); INSERT INTO Customers (CustomerId, CustomerName, ContactName, Address, City)' at line 10
I've tried multiple fixes, I've removed any unnecessary fluff, but I'm not sure why I still can't get it working.
Update
I've updated the link to show trying to add a Foreign Key.
You have an extra comma at the end of your Customers create table definition. Specifically, line 9's primary key has an extra comma at the end.
Same thing for your Orders table definition, the primary key definition for OrderId has an extra comma too.
CREATE TABLE Customers (
CustomerId int(6) unsigned NOT NULL,
CustomerName varchar(200) NOT NULL,
ContactName varchar(200) NOT NULL,
Address varchar(200) NOT NULL,
City varchar(50) NOT NULL,
PRIMARY KEY (CustomerId), <---------- here
);
CREATE TABLE IF NOT EXISTS Orders (
OrderId int(6) unsigned NOT NULL,
CustomerId int(6) NOT NULL,
OrderDate varchar(200) NOT NULL,
Product varchar(200) NOT NULL,
PRIMARY KEY (`OrderId`), <--- and here
);
You have error on this line
PRIMARY KEY (CustomerId),
You have extra comma. Remove that.

PhPMyAdmin Error #1064; Syntax Error

I'm trying to put together a MySQL database for a forum, And when I try to make a section table I keep encountering a problem
#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 'TYPE = INNODB' at line 7
Here's the code:
CREATE TABLE sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) TYPE=INNODB;
Use the following query
CREATE TABLE IF NOT EXISTS sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) ENGINE=InnoDB
To mention the type of engine use ENGINE keyword.

MySQL ERROR 1064 (42000) when creating a table

I've looked at previous posts but I am struggling to figure out where i am going wrong.
I am trying to create a table using this code:
mysql> CREATE TABLE Engine
-> (
-> ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
-> Name VARCHAR(255) NOT NULL,
-> Displacement VARCHAR(10) NOT NULL,
-> Code VARCHAR(10) NOT NULL,
-> PowerOutput VARCHAR(10),
-> MadeAt VARCHAR(255) NOT NULL,
-> PRIMARY KEY (ID),
-> FOREIGN KEY (MadeAt)
-> );
However I am repeatedly getting this 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 'UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
Name VARCHAR(255) NOT NULL,
Displace' at line 3
Any help would be much appreciated.
Try to change
ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE
into:
ID INT NOT NULL AUTO_INCREMENT
UNASSIGNED should be UNSIGNED, but it is not necessary to define this, because AUTO_INCREMENT will start at 1 by default when not defined otherwise. UNIQUE is redundant as well, because the ID will increment and the numbers will be unique anyway.
In most cases VARCHAR is not recommended to use as PRIMARY KEY.

MySql error, trying to use INNER JOIN to select data from two tables

I'm getting this error, when i try to select data from two tables in my database.
#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 'INNER JOIN hbr_galleri
ON hbr_billede.GalleriID = hbr_galleri.GalleriID
LIMIT ' at line 4
This is the query:
(Sorry for the danish table/column names)
SELECT
`hbr_billede.BilledeID`,
`hbr_billede.GalleriID`,
`hbr_billede.FilePath`,
`hbr_billede.FilePathThumb`,
`hbr_billede.UploadetDato`,
`hbr_billede.UploadetAf`,
`hbr_billede.Fotograf`,
`hbr_billede.Caption`,
`hbr_billede.FeaturedFrontGallery`,
`hbr_billede.FeaturedWorldtour`,
`hbr_galleri.GalleriNavn`
FROM `hbr_billede`
INNER JOIN `hbr_galleri`
ON `hbr_billede.GalleriID` = `hbr_galleri.GalleriID`
WHERE `hbr_billede.BilledeID` = 17
I have tried multiple ways to make it work in phpMyAdmin, but with no luck. Can someone tell me what i'm doing wrong, or point me in the direction?
My sql create scripts for these two tables, looks like this:
CREATE TABLE hbr_galleri
(
GalleriID int AUTO_INCREMENT NOT NULL,
GalleriNavn varchar(255) NOT NULL,
OprettetDato timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
DatoEvent date NOT NULL,
AntalBilleder int,
FeaturedBillede int,
OprettetAf int NOT NULL, /* BrugerID*/
Offentligt boolean NOT NULL DEFAULT true,
unikt_galleri varchar(255) DEFAULT NULL,
PRIMARY KEY (GalleriID),
UNIQUE KEY unikt_galleri (GalleriNavn,DatoEvent),
FOREIGN KEY (OprettetAf) REFERENCES hbr_bruger(BrugerID) /*ON DELETE CASCADE*/
);
CREATE TABLE hbr_billede
(
BilledeID int AUTO_INCREMENT NOT NULL,
GalleriID int NOT NULL,
FilePath varchar(255) NOT NULL,
FilePathThumb varchar(255) NOT NULL,
UploadetDato timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UploadetAf int NOT NULL,
Fotograf varchar(255) NOT NULL,
Caption varchar(255),
FeaturedFrontGallery boolean NOT NULL DEFAULT false,
FeaturedWorldtour boolean NOT NULL DEFAULT false,
PRIMARY KEY (BilledeID),
FOREIGN KEY (GalleriID) REFERENCES hbr_galleri(GalleriID) ON DELETE CASCADE,
UNIQUE (FilePath),
UNIQUE (FilePathThumb)
);
You need to put the INNER JOIN clause before WHERE. You also need to fix the backticks, for example this
`hbr_billede.BilledeID`
should be changed to this
`hbr_billede`.`BilledeID`
Try to change your query to this
SELECT
`hbr_billede`.`BilledeID`,
`hbr_billede`.`GalleriID`,
`hbr_billede`.`FilePath`,
`hbr_billede`.`FilePathThumb`,
`hbr_billede`.`UploadetDato`,
`hbr_billede`.`UploadetAf`,
`hbr_billede`.`Fotograf`,
`hbr_billede`.`Caption`,
`hbr_billede`.`FeaturedFrontGallery`,
`hbr_billede`.`FeaturedWorldtour`,
`hbr_galleri`.`GalleriNavn`
FROM `hbr_billede`
INNER JOIN `hbr_galleri`
ON `hbr_billede`.`GalleriID` = `hbr_galleri`.`GalleriID`
WHERE `BilledeID` = 17