Having Trouble with MySQL Primary Key and adding Index (Error #1064) - mysql

CREATE TABLE registers(
User_ID INT UNSIGNED NOT NULL PRIMARY KEY,
IP INT UNSIGNED NOT NULL INDEX,
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL)
I get 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 'INDEX,
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL)' at line 3

To define inline indexes in a CREATE TABLE statement, you will need to place it on a separate 'create definition' line:
CREATE TABLE registers(
User_ID INT UNSIGNED NOT NULL PRIMARY KEY,
IP INT UNSIGNED NOT NULL,
INDEX(IP),
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL
);
SqlFiddle
You can also place the index definition outside of the table definition, like so:
CREATE INDEX IX_registers_ip ON registers(IP);
It is regarded as good practice to name the index, so that it can be referenced exactly.

Your synatx must be:
CREATE TABLE registers(
User_ID INT UNSIGNED NOT NULL PRIMARY KEY,
IP INT UNSIGNED NOT NULL ,
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL,
INDEX (IP )
)
For more information about the create table statement see the official mysql documentation

Try this instead - in MySQL syntax the INDEX definition cannot be included in the field definition:
CREATE TABLE registers(
User_ID INT UNSIGNED NOT NULL PRIMARY KEY,
IP INT UNSIGNED NOT NULL,
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL,
INDEX `ip_1` (`ip`)
)
Alternatively - sometimes preferred for readability you can create the index in a separate statement after you create the table:
CREATE TABLE registers(
User_ID INT UNSIGNED NOT NULL PRIMARY KEY,
IP INT UNSIGNED NOT NULL,
Success TINYINT UNSIGNED NOT NULL,
Time_Created DATETIME NOT NULL
);
CREATE INDEX ip_1 ON registers (ip);

Related

MySQL INDEX() syntax equivalent in SQL Server

I am following a PHP workbook and one of the exercises asks me to create a table with columns using the following MySQL code
CREATE TABLE messages (
message_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED NOT NULL DEFAULT 0,
forum_id TINYINT UNSIGNED NOT NULL,
user_id MEDIUMINT UNSIGNED NOT NULL,
subject VARCHAR(100) NOT NULL,
body LONGTEXT NOT NULL,
date_entered DATETIME NOT NULL,
PRIMARY KEY (message_id),
INDEX (parent_id),
INDEX (forum_id),
INDEX (user_id),
INDEX (date_entered)
);
The problem is at the place I work they use Microsoft SQL Server so the syntax is different.
What is the equivalent SQL Server syntax I can use for
INDEX (parent_id),
INDEX (forum_id),
INDEX (user_id),
INDEX (date_entered)
What is the equivalent SQL Server syntax I can use for
INDEX (parent_id),
INDEX (forum_id),
INDEX (user_id),
INDEX (date_entered)
Looking in the manual i notice the BNF form.
<column_index> ::=
INDEX index_name [ CLUSTERED | NONCLUSTERED ]
[ WITH ( <index_option> [ ,... n ] ) ]
[ ON { partition_scheme_name (column_name )
| filegroup_name
| default
}
]
[ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ]
So SQL Server 2008+ should also support INDEX keyword in the CREATE TABLE statement..
But the valid syntax for the INDEX keyword is using
CREATE TABLE test (
id INT
, INDEX index_name (id)
);
But there are other things wrong like datatypes or keywords..
The correct SQL code for SQL server is
CREATE TABLE messages (
message_id INT identity(1, 1),
parent_id INT NOT NULL DEFAULT 0,
forum_id TINYINT NOT NULL,
user_id INT NOT NULL,
subject VARCHAR(100) NOT NULL,
body TEXT NOT NULL,
date_entered DATETIME NOT NULL,
PRIMARY KEY (message_id),
INDEX parent_id (parent_id),
INDEX forum_id (forum_id),
INDEX user_id (user_id),
INDEX date_entered (date_entered)
);
You can use syntax that should work in any SQL engine, e.g.
CREATE TABLE messages (
message_id INT,
parent_id INT NOT NULL DEFAULT 0,
forum_id TINYINT NOT NULL,
user_id INT NOT NULL,
subject VARCHAR(100) NOT NULL,
body TEXT NOT NULL,
date_entered DATETIME NOT NULL,
PRIMARY KEY (message_id)
)
Then create other indexes individually:
CREATE INDEX parent_id_idx ON messages (parent_id))
And so on.
This syntax should be supported by all SQL engines.

Unknown CREATE TABLE syntax error near 'unsigned int ..'

I've been staring at this simple CREATE TABLE query for 20 minutes, and I can't figure out why it's throwing an error:
create table `schema_change` (
`schema_change_id` unsigned int not null auto_increment,
`major_release_number` unsigned int not null,
`minor_release_number` unsigned int not null,
`point_release_number` unsigned int not null,
`script_name` varchar(100) not null,
`date_applied` datetime not null,
constraint `pk_schema_change` primary key (
`schema_change_id`
)
);
The error returned is a basic syntax error, but I can't spot any syntax that's incorrect:
#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 'unsigned int not null, minor_release_number unsigned int not
null, point_rel' at line 3
What am I missing?
(Using MySQL version 5.1.73)
UNSIGNED is a type attribute, and must come after the type name: INT UNSIGNED, not UNSIGNED INT.
You must use unsigned after the type, as it modifies the type of int
create table `schema_change` (
`schema_change_id` int unsigned auto_increment,
`major_release_number` int unsigned not null,
`minor_release_number` int unsigned not null,
`point_release_number` int unsigned not null,
`script_name` varchar(100) not null,
`date_applied` datetime not null,
constraint `pk_schema_change` primary key (
`schema_change_id`
)
);
See in action here: http://sqlfiddle.com/#!9/685bf/1

Mysql creating table error integer

With this code:
create table jogadores(
id INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
nome varchar NOT NULL,
idade int NOT NULL UNSIGNED,
nacionalidade varchar NOT NULL
)
I keep getting this error:
)
Error report -
SQL Error: 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,
idade int NOT NULL UNSIGNED,
nacionalidade varchar NOT NULL
)' at line 3.
Also, I get the red underline under the "T" and the "(" in "INT(10)".
varchar() should have a length:
create table jogadores (
id INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
nome varchar(255) NOT NULL,
idade int UNSIGNED NOT NULL,
nacionalidade varchar(255) NOT NULL
);
And UNSIGNED needs to go immediately after the numeric declaration, not after NOT NULL. See here.
You need to specify the length of the varchar i.e. varchar(100).

Why this error in mysql making a table?

I just want a make table like below.
create table android_data (
index int unsigned primary key AUTO_INCREMENT,
phone int not null,
sensorID int not null,
press int not null,
temp int not null,
accel int not null,
gps_lat double not null,
gps_lng double not null,
time timestamp default current_timestamp on update current_timestamp
)engine=innodb;
But I got an error in
index int unsigned primary key AUTO_INCREMENT,
phone int not null,
I cant understand Why this is wrong with manual....
What should I do to make right table??
Index is a reserved keyword in MySQL. If you must name your primary key index you should put it in backticks:
create table android_data (
`index` int unsigned primary key AUTO_INCREMENT,
...
)
But ideally you should avoid naming tables and columns using MySQL keywords for the very reason you have already seen.
MySQL keywords

Trouble creating table in MySQL

I am trying to create a table using this command in MySQL:
CREATE TABLE tutorial_info(
tutorial_id INT NOT_NULL AUTO_INCREMENT PRIMARY KEY,
tutorial_title VARCHAR(100) NOT_NULL,
tutorial_author VARCHAR(100) NOT_NULL,
submission_date TIMESTAMP
);
I just cut and paste the code into the terminal but I am getting 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 'NOT_NULL AUTO_INCREMENT PRIMARY KEY, tutorial_title VARCHAR(100) NOT_NULL, ' at line 1
Not sure what is going on here. Could someone give me a pointer to what I might be doing wrong here? Thanks
Change NOT_NULL to NOT NULL as:
CREATE TABLE tutorial_info(
tutorial_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
tutorial_title VARCHAR(100) NOT NULL,
tutorial_author VARCHAR(100) NOT NULL,
submission_date TIMESTAMP
);
Use not null without _
CREATE TABLE tutorial_info(
tutorial_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
tutorial_title VARCHAR(100) NOT NULL,
tutorial_author VARCHAR(100) NOT NULL,
submission_date TIMESTAMP
);