Mysql Error 1064 when trying to create a table - mysql

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;

Related

SQL Error Code: 1049. Unknown database 'dbo'

I am trying to create a database use mysql workbench and I keep getting this error and my code looks fine. Not sure how I can resolve this. This is the error I am getting: Error Code: 1049. Unknown database 'dbo'.
I have tried so many things but only the first two lines of creating and using the database works. [screenshot attached]
use employeedb;
create table dbo.Department(
DeprartmentId int AUTO_INCREMENT,
DepartmentName nvarchar(500),
PRIMARY KEY(DepartmentId)
);
insert into dbo.Department(DepartmentName) values ('IT');
insert into dbo.Department(DepartmentName) values ('Support');
create table dbo.Employee(
EmployeeId int AUTO_INCREMENT,
EmployeeName nvarchar(500),
Department nvarchar(500),
DateOfJoining datetime,
PhotoFileName nvarchar(500),
PRIMARY KEY(EmployeeId)
);
insert into dbo.Employee(EmployeeName,Department,DateOfJoining,PhotoFileName)
values ('John','IT','2022-11-27','anonymous.png');
select * from dbo.Employee;
You seem to be using SQL Server syntax, which won't work on MySQL. Here is your script updated for MySQL:
USE employeedb;
CREATE TABLE Department (
DeprartmentId int AUTO_INCREMENT,
DepartmentName varchar(500),
PRIMARY KEY(DepartmentId)
);
INSERT INTO Department(DepartmentName) VALUES ('IT');
INSERT INTO Department(DepartmentName) VALUES ('Support');
CREATE TABLE Employee (
EmployeeId int AUTO_INCREMENT,
EmployeeName varchar(500),
Department varchar(500),
DateOfJoining datetime,
PhotoFileName varchar(500),
PRIMARY KEY(EmployeeId)
);
INSERT INTO Employee (EmployeeName, Department, DateOfJoining, PhotoFileName)
VALUES ('John', 'IT', '2022-11-27', 'anonymous.png');
SELECT * FROM Employee;
Seems you are changing from MSSQL to MySQL, for MySQl do not need "dbo", just:
use employeedb;
create table Department(
DeprartmentId int AUTO_INCREMENT,
DepartmentName nvarchar(500),
PRIMARY KEY(DepartmentId)
);
....

Can't Create MYSQL DataBase Tables [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to create MYSQL tables for my DataBase but I keep getting an error
Here is my code
CREATE DATABASE Hospitals;
CREATE TABLE hospital (
hospitalId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
postcode VARCHAR(10),
phoneNumber VARCHAR(20),
emailAddress VARCHAR(255),
PRIMARY KEY (hospitalId),
) ENGINE = INNODB;
CREATE TABLE wards (
wardID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
specialisation VARCHAR(100),
PRIMARY KEY (wardID),
) ENGINE = INNODB;
CREATE TABLE staff (
staffId INT NOT NULL AUTO_INCREMENT,
foreName VARCHAR(50),
surName VARCHAR(50),
position VARCHAR(50),
salary DOUBLE,
PRIMARY KEY (staffId)) ENGINE = INNODB;
CREATE TABLE patients (
niNumber VARCHAR(50),
name VARCHAR(100),
dob DATE,
sex VARCHAR(10),
nokName VARCHAR(100),
nokPhone VARCHAR(50),
PRIMARY KEY niNumber) ENGINE = INNODB;
CREATE TABLE medications (
medicationId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
contraIndications VARCHAR(255),
PRIMARY KEY (medicationId)) ENGINE = INNODB;
INSERT INTO hospital (hospitalId, name, postcode, phoneNumber, emailAddress)
VALUES(1, 'Bangor', 'LL72GW', '01276541789', 'Bangor.Hospital#gmail.com'),
VALUES(2, 'london', 'SW1A 0AA', '01673531789', 'London.Hospital#gmail.com');
INSERT INTO wards (wardId, name, specialisation)
VALUES(1, 'Ward1', 'Brain Surgery'),
VALUES(2, 'Ward2', 'Heart Surgery');
INSERT INTO staff (staffId, foreName, surName, position, salary)
VALUES(1, 'Malcolm', 'Gross', 'Manager', '50000.0'),
VALUES(1, 'Lewis', 'Castle', 'Surgeon', '30000.0');
INSERT INTO patients (niNumber, name, dob, sex, nokName, nokPhone)
VALUES('001AFCY6', 'Sam', '20-02-2002', 'male', 'David', '09876543268'),
VALUES('0SSHD2J3', 'Jim', '19-09-1977', 'male', 'Terry', '06789347382');
INSERT INTO medications (medicationId, name, contraIndications)
VALUES(1, 'Heart Medicine', 'Dont Use if heart is ok'),
VALUES(2, 'Antibiotics', 'Dont use if patient is feeling well');
But the error I keep getting is Query 1 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 'VALUES(2, 'Antibiotics', 'Dont use if patient is feeling well')' at line 3
I have no idea why I am getting such an error
What am I doing wrong and how can I fix my error please
Many Thanks
You dont need to use 2 VALUES keywords in your INSERT statements. So you updated INSERT statement should look alike -
INSERT INTO hospital (hospitalId, name, postcode, phoneNumber, emailAddress)
VALUES(1, 'Bangor', 'LL72GW', '01276541789', 'Bangor.Hospital#gmail.com'),
(2, 'london', 'SW1A 0AA', '01673531789', 'London.Hospital#gmail.com');
And you have to get rid of last commas in first 2 tables also -
CREATE TABLE hospital (
hospitalId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
postcode VARCHAR(10),
phoneNumber VARCHAR(20),
emailAddress VARCHAR(255),
PRIMARY KEY (hospitalId) --, This should be removed.
) ENGINE = INNODB;
CREATE TABLE wards (
wardID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
specialisation VARCHAR(100),
PRIMARY KEY (wardID) --, This should be removed.
) ENGINE = INNODB;

Use trigger as assertion in MySQL

I have a database and a few tables in MySql. I am trying to implement an Oracle assertion in MySQL using CREATE TRIGGER. I am not sure if I used the proper syntax.
This is what I have so far, but I'm not sure why it's wrong.
CREATE TRIGGER tg_review_before_insert
BEFORE INSERT ON review
FOR EACH ROW
SET NEW.paper = IF((SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
Here are my tables:
CREATE TABLE paper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
abstract VARCHAR(250),
pdf VARCHAR(100),
PRIMARY KEY(paperid)
);
CREATE TABLE author(
email VARCHAR(100) NOT NULL,
name VARCHAR(50),
affiliation VARCHAR(100),
PRIMARY KEY(email)
);
CREATE TABLE writePaper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(100),
paper_order INT,
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES author(email)
);
CREATE TABLE pcmember(
email VARCHAR(100) NOT NULL,
name VARCHAR(20),
PRIMARY KEY(email)
);
CREATE TABLE review(
reportid INT UNSIGNED,
sdate DATE,
comment VARCHAR(250),
recommendation CHAR(1),
paperid INT UNSIGNED,
email VARCHAR(100),
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES pcmember(email)
);
Here are the assertions I am trying to implement from Oracle SQL code to MySQL SQL code:
CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
( SELECT *
FROM review R
WHERE R.email = P.email
)
);

An expression was expected. 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;

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)