Table cannot be created in mysql -Error 1064 - mysql

I am trying to create a table in MySQL with the query
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rank TINYINT NOT NULL,
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rank),
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.
Error coming up is
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 TINYINT NOT NULL, groupName
VARCHAR at line 3

MySQL 8.0.2 added support for the window rank function, making it a reserverd word.
You could escape it using backticks (`):
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
`rank` TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, `rank`), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rosterRank TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rosterRank), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);

Related

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.

SQL query syntax error while creating a table

I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the 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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle

#1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I'm new to PHP and MySQL and ran into a little trouble with a learning project I'm working on.
Whenever I try to create a table
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
)
I get the following error message:
#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 ') NOT NULL, type varchar(6) NOT NULL, notes varchar(512),
receipt int(10), ' at line 6**
Here is some info on what I'm working with
Server type: MySQL
Server version: 5.5.32 - MySQL Community Server(GPL)
phpMyAdmin: 4.0.4.1, latest stable version: 4.1.7
I've spent a day knocking my head against the wall over this and now I think its time to ask for help.I was wondering if anyone can tell me what I'm doing wrong?
Remove the comma
receipt int(10),
And also AUTO INCREMENT should be a KEY
double datatype also requires the precision of decimal places so right syntax is double(10,2)
One obvious thing is that you will have to remove the comma here
receipt int(10),
but the actual problem is because of the line
amount double(10) NOT NULL,
change it to
amount double NOT NULL,
In MySQL, the word 'type' is a Reserved Word.
I see two problems:
DOUBLE(10) precision definitions need a total number of digits, as well as a total number of digits after the decimal:
DOUBLE(10,8)
would make be ten total digits, with 8 allowed after the decimal.
Also, you'll need to specify your id column as a key :
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id) );
I've faced this problem before, the reason behind that for me was the last comma in the schema definition should be deleted if there's no extra columns would be added.
CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, // this comma should be deleted
);
Rule 1: You can not add a new table without specifying the primary key constraint[not a good practice if you create it somehow].
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Rule 2: You are not allowed to use the keywords(words with predefined meaning) as a field name.
Here type is something like that is used(commonly used with Join Types).
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
transaction_type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Now you please try with this code.
First check it in your database user interface(I am running HeidiSQL, or you can try it in your xampp/wamp server also)and make sure this code works. Now delete the table from your db and execute the code in your program.
Thank You.

What am I missing? MySQL syntax error

I am having this error:
#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 ''tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'd' at line 1
From this statement:
CREATE TABLE 'tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
); ENGINE=MyISAM;
Why?
You need the backtick instead of the single quote ('). The backtick is this character:
`
Better yet - don't bother with either:
CREATE TABLE tablename (
id MEDIUMINT ...
Important: also see the comments below from tadman; they round out this answer nicely by explaining the backticks and pointing out another syntax issue.
You are using incorrect notation.
In your create table statement, you are using the single quote ( ').
You can't use that here for table names and column names.
Alternatives would be the tick mark (`). Or just completely remove all notation altogether.
Here is your code, fully functional:
CREATE TABLE tablename (
`id` MEDIUMINT NOT NULL,
`content`TEXT NOT NULL,
`date_added` DATETIME NOT NULL,
`user` VARCHAR (16) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`id`)
);
You are missing a space between the columnname and the type of the first two columns. Also, you have a ; to many at the end
CREATE TABLE 'tablename'(
'id' MEDIUMINT NOT NULL AUTO_INCREMENT,
'content' TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
) ENGINE=MyISAM;

Mysql equivalent of MS-Sql server IDENTITY field [duplicate]

This question already has answers here:
Equivalent of MSSQL IDENTITY Column in MySQL
(2 answers)
Closed 9 years ago.
I have a sql-server query and i want to run it (or create an equivalent query) on mysql. But currently i'm getting syntax error. Can anyone help me to create a mysql equivalent of the below mentioned sql-server query?
create table Emp(EmpName varchar(20) not null,keyword varchar(20) not null,
DOB datetime not null,Comments text(65535),EmpId int primary key IDENTITY(1,1));
Following 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 'IDENTITY(1,1))' at line 1
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535), PRIMARY KEY (EmpId)
);
You can use auto_increment .By default it will increase by 1.If you want any other increment you can specify your own.
It should be defined as auto_incremented primary key instead. For example:
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535),
PRIMARY KEY (EmpId)
);
You can use an auto-increment field