An expression was expected. MySQL - mysql

DROP TABLE IF EXISTS group ;
CREATE TABLE group (id_group INT(10) AUTO_INCREMENT NOT NULL,
title_group VARCHAR(200),
discription_group VARCHAR(200),
image_group VARCHAR(200),
date_group DATE,
id_user INT(10),
id_category INT(10),
PRIMARY KEY (id_group) ) ENGINE=InnoDB;
Error
Static analysis:
2 errors were found during analysis.
An expression was expected. (near "group" at position 21)
Unrecognized keyword. (near "group" at position 21)
SQL query:
DROP TABLE IF EXISTS group
MySQL said
#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 'group' at line 1

group is reserve keyword.
You can use below statement.
DROP TABLE IF EXISTS `group` ;
CREATE TABLE `group` (
id_group INT(10) AUTO_INCREMENT NOT NULL,
title_group VARCHAR(200),
discription_group VARCHAR(200),
image_group VARCHAR(200),
date_group DATE,
id_user INT(10),
id_category INT(10),
PRIMARY KEY (id_group)
) ENGINE=InnoDB;

Related

Mysql Error 1064 when trying to create a table

I'm trying to create a table so that I can pass in a csv file to load it into the table but I'm getting this error and I'm not sure why. Here's what I have:
CREATE TABLE imdb(
-> rank int auto_increment PRIMARY key,
-> title varchar(255),
-> genre varchar(255),
-> director varchar(255),
-> actors varchar(255),
-> year int,
-> rating decimal(1,1),
-> votes int,
-> metascore int
-> );
Here is the 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 'rank int
auto_increment PRIMARY key,
title varchar(255),
genre varchar(255),
dir' at line 2
Those arrows shouldn't be there (maybe something that got added when copy/pasting?). Also, rank and year are reserved words (thanks #
Aloiso Gomes) so you have to add backticks to allow the name anytime you reference it (stored procs, selects, inserts, etc.)
This works:
CREATE TABLE imdb(
`rank` int auto_increment PRIMARY key,
`title` varchar(255),
`genre` varchar(255),
`director` varchar(255),
`actors` varchar(255),
`year` int,
`rating` decimal(1,1),
`votes` int,
`metascore` int
);
INSERT INTO imdb (title,genre) VALUES
('Fast Furious 1', 'racing'),
('Fast Furious 2', 'racing'),
('Fast Furious 3', 'racing?'),
('Fast Furious 4', '"racing"');
SELECT `rank`, title, genre FROM imdb;

Where Not Exists -- ERROR 1064 (42000): You have an error in your SQL syntax; check

I want to insert two values(composite keys) only when those don't already exist otherwise Mysql will give error about duplicate keys getting entered.
My this query is giving error:
INSERT INTO group_msg_response (license_id,grp_id) VALUES (1,1) WHERE NOT EXISTS (SELECT 1 FROM group_msg_response WHERE license_id=1 AND grp_id=1 )
If I run them individually they both run fine.
It is giving 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 'WHERE NOT EXISTS (SELECT 1 FROM group_msg_response WHERE license_id=1 AND ' at line 1
What could be the problem? I'm doing the way as mentioned in https://dev.mysql.com/doc/refman/5.5/en/exists-and-not-exists-subqueries.html
Here is the table definition:
create table IF NOT EXISTS msg(
id INT UNSIGNED AUTO_INCREMENT ,
en varchar(5000),
hi varchar(5000),
PRIMARY KEY(id)
) ENGINE=InnoDB;
create table IF NOT EXISTS group_msg(
id INT UNSIGNED AUTO_INCREMENT ,
msg_id INT UNSIGNED,
lsource INT UNSIGNED NOT NULL,
browser CHAR(1) NOT NULL DEFAULT 'a' ,
expiry_date DATETIME NOT NULL ,
dated DATETIME NOT NULL,
deleteit TINYINT DEFAULT 0 ,
FOREIGN KEY (msg_id)
REFERENCES msg(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY(id)
) ENGINE=InnoDB;
create table IF NOT EXISTS group_msg_response(
license_id MEDIUMINT,
grp_id INT UNSIGNED,
FOREIGN KEY (grp_id)
REFERENCES group_msg(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY(license_id,grp_id)
) ENGINE=InnoDB;
Use select, not values:
INSERT INTO group_msg_response (license_id, grp_id)
SELECT license_id, grp_id
FROM (SELECT 1 as license_id, 1 as grp_id) x
WHERE NOT EXISTS (SELECT 1
FROM group_msg_response gmr
WHERE gmr.license_id = x.license_id AND
gmr.grp_id = x.grp_id
);
Or, better yet, let the database do the work by creating a unique index/constraint:
create unique index unq_gmr_license_grp on group_msg_response(license_id, grp_id) ;
Then you can do the insert as:
INSERT INTO group_msg_response (license_id, grp_id)
VALUES (1, 1)
ON DUPLICATE KEY UPDATE license_id = VALUE(license_id);
The ON DUPLICATE KEY clause does nothing -- except prevent an error from occurring.

MySQL: "CREATE DATABASE" results in MySQL Error Code: 1064

I recently bought this book called "SQL Queries for Mere Mortals (3rd Edition" to study SQL. It came with MySQL scripts that they said I could run and have example databases to work with and follow along with the book. However, some of the scripts are resulting in an error message. Here is one example script that will not work:
CREATE DATABASE EntertainmentAgencyModify;
USE EntertainmentAgencyModify;
CREATE TABLE Agents (
AgentID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
AgtFirstName nvarchar (25) NULL ,
AgtLastName nvarchar (25) NULL ,
AgtStreetAddress nvarchar (50) NULL ,
AgtCity nvarchar (30) NULL ,
AgtState nvarchar (2) NULL ,
AgtZipCode nvarchar (10) NULL ,
AgtPhoneNumber nvarchar (15) NULL ,
DateHired date NULL ,
Salary decimal(15, 2) NULL DEFAULT 0 ,
CommissionRate float(24) NULL
);
CREATE TABLE Customers (
CustomerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustFirstName nvarchar (25) NULL ,
CustLastName nvarchar (25) NULL ,
CustStreetAddress nvarchar (50) NULL ,
CustCity nvarchar (30) NULL ,
CustState nvarchar (2) NULL ,
CustZipCode nvarchar (10) NULL ,
CustPhoneNumber nvarchar (15) NULL
);
CREATE TABLE Engagements (
EngagementNumber int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL DEFAULT 0 ,
CustomerID int NULL DEFAULT 0 ,
AgentID int NULL DEFAULT 0 ,
EntertainerID int NULL DEFAULT 0
);
CREATE TABLE Engagements_Archive (
EngagementNumber int NOT NULL ,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL ,
CustomerID int NULL ,
AgentID int NULL ,
EntertainerID int NULL
);
CREATE TABLE Entertainer_Members (
EntertainerID int NOT NULL ,
MemberID int NOT NULL DEFAULT 0 ,
Status smallint NULL DEFAULT 0
);
CREATE TABLE Entertainer_Styles (
EntertainerID int NOT NULL ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Entertainers (
EntertainerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
EntStageName nvarchar (50) NULL ,
EntSSN nvarchar (12) NULL ,
EntStreetAddress nvarchar (50) NULL ,
EntCity nvarchar (30) NULL ,
EntState nvarchar (2) NULL ,
EntZipCode nvarchar (10) NULL ,
EntPhoneNumber nvarchar (15) NULL ,
EntWebPage nvarchar (50) NULL ,
EntEMailAddress nvarchar (50) NULL ,
DateEntered date NULL ,
EntPricePerDay decimal(15,2) NULL
);
CREATE TABLE Members (
MemberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
MbrFirstName nvarchar (25) NULL ,
MbrLastName nvarchar (25) NULL ,
MbrPhoneNumber nvarchar (15) NULL ,
Gender nvarchar (2) NULL
);
CREATE TABLE Musical_Preferences (
CustomerID int NOT NULL DEFAULT 0 ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Musical_Styles (
StyleID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StyleName nvarchar (75) NULL
);
CREATE INDEX AgtZipCode ON Agents(AgtZipCode);
CREATE INDEX CustZipCode ON Customers(CustZipCode);
CREATE INDEX AgentsEngagements ON Engagements(AgentID);
CREATE INDEX CustomerID ON Engagements(CustomerID);
CREATE INDEX EmployeeID ON Engagements(AgentID);
CREATE INDEX EntertainerID ON Engagements(EntertainerID);
ALTER TABLE Engagements_Archive
ADD CONSTRAINT Engagements_Archive_PK PRIMARY KEY
(
EngagementNumber
);
CREATE INDEX CustomerID ON Engagements_Archive(CustomerID);
CREATE INDEX EmployeeID ON Engagements_Archive(AgentID);
CREATE INDEX EntertainerID ON Engagements_Archive(EntertainerID);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY
(
EntertainerID,
MemberID
);
CREATE INDEX EntertainersEntertainer_Members ON Entertainer_Members(EntertainerID);
CREATE INDEX MembersEntertainer_Members ON Entertainer_Members(MemberID);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY
(
EntertainerID,
StyleID
);
CREATE INDEX EntertainersEntertainer_Styles ON Entertainer_Styles(EntertainerID);
CREATE INDEX Musical_StylesEntertainer_Styles ON Entertainer_Styles(StyleID);
CREATE INDEX EntZipCode ON Entertainers(EntZipCode);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY
(
CustomerID,
StyleID
);
CREATE INDEX CustomerID ON Musical_Preferences(CustomerID);
CREATE INDEX StyleID ON Musical_Preferences(StyleID);
ALTER TABLE Engagements
ADD CONSTRAINT Engagements_FK00 FOREIGN KEY
(
AgentID
) REFERENCES Agents (
AgentID
),
ADD CONSTRAINT Engagements_FK01 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
),
ADD CONSTRAINT Engagements_FK02 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
),
ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY
(
MemberID
) REFERENCES Members (
MemberID
);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
) ON DELETE CASCADE,
ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles
(
StyleID
);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
) ON DELETE CASCADE,
ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles (
StyleID
);
Running this script results in the following error message:
16:33:35 CREATE DATABASE EntertainmentAgencyModify 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 'CREATE DATABASE EntertainmentAgencyModify' at line 1 0.00041
sec
Screenshot
I have tried running other scripts that came with the book that also starts with "CREATE DATABASE" and some of them ran smoothly without any errors, so I'm confused as to why I'm getting this error message. Any help would be greatly appreciated! Thanks all!
Note that since this script has a "CREATE DATABASE" at the top it will fail if that database already exists. That also means that if it works partially and you run it again, it;ll fail completely the next time. "CREATE DATABASE" is a pretty big hammer, so you probably don't want to get into the habit of doing it a lot. But, having said that, here's an even bigger hammer; add
DROP DATABASE IF EXISTS EntertainmentAgencyModify;
in front of the CREATE and similarly for the others. When you have real data, you, of course, never want to use either of these.
Can you try running the following just by typing it in your mysql console:
CREATE DATABASE EntertainmentAgencyModify;
It should simply work, or at least tell you something more then a syntax error.
The code executes fine. May be its a problem with your MySql.
Try to install a new MySql. :)
https://www.mysql.com/downloads/
Thanks everyone! I have no idea why this works, but I found a solution. I had to simply delete "CREATE " from the first line of the script and re-type it. Then it worked. If anyone has any idea why this seemingly irrelevant solution worked, I'd love to know. Thanks everyone for your help!

What's wrong with this MySQL syntax?

Alright so I'm trying to tie a Spigot plugin that I'm making into MySQL, and I did it successfully up until the point where I edited the code that was creating the table. I find that MySQL stack traces are much too ambiguous to be useful, so I have no idea what I'm doing wrong here.
Code:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys(id INT NOT NULL KEY AUTO_INCREMENT, key VARCHAR(36), trimmedUUID VARCHAR(36), playerUUID VARCHAR(36), date TIMESTAMP, status TEXT);
key is a reserved word in MySQL. If you absolutely must use it as a column name, you can escape it using backticks:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys (
id INT NOT NULL KEY AUTO_INCREMENT,
`key` VARCHAR(36), -- Here!
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
date TIMESTAMP,
status TEXT
)
Or, better yet, use a name that isn't a reserved word, such as link_key:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys (
id INT NOT NULL KEY AUTO_INCREMENT,
link_key VARCHAR(36), -- Here!
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
date TIMESTAMP,
status TEXT
)
CREATE TABLE IF NOT EXISTS WebsiteLink_keys(
id INT NOT NULL AUTO_INCREMENT,
`key` VARCHAR(36),
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
`date` TIMESTAMP,
status TEXT,
PRIMARY KEY (id)
);
PRIMARY KEY (id) is at the end

SQL oracle Developer error

CREATE TABLE person_details
( id NOT NULL AUTO_INCREMENT,
name varchar(100),
age int,
gender varchar(8),
adult varchar(10),
contact_no int,
address varchar(255),
pnr_number int
)
Error at Command Line:2 Column:15
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
You forgot to define the type of id field here:
( id NOT NULL AUTO_INCREMENT,
Should be something like:
( id INT NOT NULL AUTO_INCREMENT,
^^^^
And make it as a primary key like
pnr_number int,
PRIMARY KEY (id)