Why this error in mysql making a table? - mysql

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

Related

MySQL #1075 error when importing sql file?

I have code code to create tables in MySQL:
It returns the following error:
#1075 - There can be only one auto column and it must be defined as a key
What is wrong with it? It is generated by DrawSQL application.
I tried this code:
CREATE TABLE `sp_players`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`club` INT NOT NULL,
`trophies` INT NOT NULL,
`birthdate` DATE NOT NULL,
`prints` INT NOT NULL
);
ALTER TABLE
`sp_players` ADD PRIMARY KEY `sp_players_id_primary`(`id`);
CREATE TABLE `sp_prints`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`player` INT NOT NULL,
`momemt` INT NOT NULL,
`season` INT NOT NULL,
`compeitition` INT NOT NULL
);
ALTER TABLE
`sp_prints` ADD PRIMARY KEY `sp_prints_id_primary`(`id`);
ALTER TABLE
`sp_players` ADD CONSTRAINT `sp_players_prints_foreign` FOREIGN KEY(`prints`) REFERENCES `sp_prints`(`id`);
ALTER TABLE
`sp_prints` ADD CONSTRAINT `sp_prints_player_foreign` FOREIGN KEY(`player`) REFERENCES `sp_players`(`id`);
Expected to generate all the tables correctly

phpmyadmin,mysql - Token Mismatch but i successfully created a table

I am doing a table with a foreign key, here it is.
create table PM_Team_Members
(
PM_Team_Members_ID int NOT NULL auto_increment PRIMARY KEY,
PM_Team_Members_firstName varchar(50) not null,
PM_Team_Members_middleName varchar(50) not null,
PM_Team_Members_lastName varchar(50) not null,
PM_Team_Members_address varchar(255) not null,
PM_Team_Members_contact numeric not null,
PM_Spec_id int,
constraint fk_PM_id foreign key (PM_Spec_id) references PM_Specialization(PM_Spec_id)
)
and this is the reference table
create table PM_Specialization
(
PM_Spec_ID int auto_increment PRIMARY KEY,
PM_Spec_Specialization varchar(50) not null,
PM_Spec_Description varchar(255) not null
)
When I click GO for the query. It said that MySQL returned an empty result set (i.e. zero rows). (Query took 0.2162 sec) which I assume that it go correct. But then a pop-up show that Error: Token Mismatch
How is that? Did I do something wrong or is it some kind of a bug?
This part references PM_Specialization(PM_Spec_id) )
You have PM_Spec_ID not PM_Spec_id in your PM_Specialization table.

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

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);

Why am I getting error number 150 from this MySQL statement?

I've checked that the data type of referenced table is exactly same as foreign key on this table below, and I'm still not sure? The SQL document isn't exactly clear.
P_id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(128) NOT NULL,
price DECIMAL(5,2) UNSIGNED NOT NULL,
descr TEXT,
imgName VARCHAR(50),
stock INT UNSIGNED NOT NULL DEFAULT '0',
PG_id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY (P_id),
FOREIGN KEY (PG_id) REFERENCES prodGroups(PG_id)
There could only be one AUTO INCREMENT column. So, I guess, one in the PG_id INT AUTO_INCREMENT NOT NULL is just a copy/paste went wrong :)

MySQL-error 150 solution

I have the following MySQL scripts:
CREATE TABLE user_roles (
id INT AUTO_INCREMENT,
PRIMARY KEY(id),
name TEXT NOT NULL,
access INT NOT NULL DEFAULT '0'
)
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
date_created DATETIME,
roles VARCHAR(50) NOT NULL,
active INT DEFAULT '1',
FOREIGN KEY(roles) REFERENCES user_roles(id)
)
It keeps giving me error 150. Maybe the database isn't well planned? Any help will be greatly appreciated.
The data types of your users.roles and user_roles.id columns must be the same for the FOREIGN KEY constraint to work correctly. Instead try making users.roles an INT:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
date_created DATETIME,
-- Change this...
roles INT NOT NULL,
active INT DEFAULT '1',
FOREIGN KEY(roles) REFERENCES user_roles(id)
)
UPDATE According to comments, users.roles should be text like "admin, moderator, etc." For correct data normalization, user_roles.id should be keyed against and to get the text name of the role, JOIN them in queries.
You need to separate your statements with a semicolon and use INTS instead of strings:
CREATE TABLE user_roles (
id INT AUTO_INCREMENT,
PRIMARY KEY(id),
name TEXT NOT NULL,
access INT NOT NULL DEFAULT 0
);
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
date_created DATETIME,
roles VARCHAR(50) NOT NULL,
active INT DEFAULT 1,
FOREIGN KEY(roles) REFERENCES user_roles(id)
);