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 :)
Related
I create a table and Identity_no is unique in that but I am getting error when I try to add a integer value with 8 number.Probably I am getting this error because I identified unique to Identity_no.How can I fix that
Here is the code for creating table:
CREATE TABLE Members(
Member_id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
Identity_no INTEGER NOT NULL UNIQUE,
Member_Name varchar(80) NOT NULL,
Member_Surname varchar(80) NOT NULL,
Member_Phone CHAR(11),
MemberCityID INTEGER NOT NULL,
FOREIGN KEY(MemberCityID) REFERENCES Cities(City_id));
Here is the code for adding data:
INSERT INTO Members
(Member_id,Identity_no,Member_Name,Member_Surname,Member_Phone,MemberCityID)
VALUES
(1,89768434566,"John","W","05379621522",78);
The INTEGER type is a 32 bit signed int, which means numbers bigger than 2^31 cannot be stored in a column with this type.
Use BIGINT instead:
CREATE TABLE Members(
Member_id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
Identity_no BIGINT NOT NULL UNIQUE,
Member_Name varchar(80) NOT NULL,
Member_Surname varchar(80) NOT NULL,
Member_Phone CHAR(11),
MemberCityID INTEGER NOT NULL,
FOREIGN KEY(MemberCityID) REFERENCES Cities(City_id));
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
The first two tables successfully pass, but as soon as the mysql checker reach the third table, it informs me that there is a problem with the reference key.
Here is my code:
CREATE TABLE USER(
Username VARCHAR(20) NOT NULL,
Email VARCHAR(50) NOT NULL,
Picture VARCHAR(30),
Points INT(5) DEFAULT 1 NOT NULL,
Password VARCHAR(50) NOT NULL,
Firstname VARCHAR(25) NOT NULL,
Lastname VARCHAR(25) NOT NULL,
PRIMARY KEY(Username)
);
CREATE TABLE THREAD (
ThreadID INT(9) NOT NULL AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
PostID int(9) NOT NULL,
PRIMARY KEY (ThreadID)
);
CREATE TABLE POST(
ThreadID INT(9) NOT NULL,
PostID INT(9) NOT NULL AUTO_INCREMENT,
Content TEXT NOT NULL,
NumberOfLines INT(5) NOT NULL,
PRIMARY KEY(ThreadID, PostID),
FOREIGN KEY ThreadID(ThreadID) REFERENCES THREAD(ThreadID)
);
The error I am getting is the following:
#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 '
PRIMARY KEY(ThreadID, PostID),
FOREIGN KEY fk_ThreadID(ThreadID) REFE' at line 6
I tried creating the PRIMARY KEY(ThreadID, PostID) as separate entities, but this doesn't seem to work.
Thank you.
Adding to #Schwern, its giving error due to wrong usage of primary key. Below create table will work.
create table a (
id bigint(20) NOT NULL AUTO_INCREMENT,
col2 varchar(10), primary key (id, col2)
) engine=innodb;
So auto-increment column needs to be first one in the primary key.
We have extensively used this kind of primary key to partition the table on col2 - to range partition on a column, the column need to be part of primary key.
Also to mention, you can achieve additional unique key at the cost of an extra index
MySQL isn't being terribly informative there. SQLFiddle gives it straight.
Incorrect table definition; there can be only one auto column and it must be defined as a key
You can't have a multi-column key where one of them is AUTO_INCREMENT The AUTO_INCREMENT column must be first in a multi-column key in an InnoDB table. Either remove AUTO_INCREMENT from PostID, or make PostID alone the PRIMARY KEY and protect a post from appearing in the same thread twice with UNIQUE(ThreadID, PostID).
CREATE TABLE POST(
PostID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
ThreadID INT NOT NULL REFERENCES Thread(ThreadID),
Content TEXT NOT NULL,
NumberOfLines INT NOT NULL,
UNIQUE(ThreadID, PostID)
);
UPDATE: The best answer is #georgecj11's to put PostID first in the key as it only uses a single index.
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.
I have a serial no. column which is auto increment, but I want enrollment id. to be the primary key and MySQL is just not allowing me to do that. Is there any way around to do that?
You can only define a column as AUTO_INCREMENT if it is a PRIMARY KEY and an INT (not sure of this but BIGINT will work too). Since you want the SerialNo to be set as AUTO_INCREMENT, why not make it as PRIMARY KEY and the EnrollmentID as UNIQUE?
CREATE TABLE TableName
(
SerialNo INT AUTO_INCREMENT PRIMARY KEY,
EnrollmentID INT UNIQUE,
-- other columns...
)
Make sure you define your serial number column as UNIQUE.
CREATE TABLE tbl_login (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
first_name varchar(100) NOT NULL,
last_name varchar(100) NOT NULL,
gender varchar(30) NOT NULL,
email varchar(200) NOT NULL,
password varchar(200) NOT NULL,
address text NOT NULL,
mobile_no varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;