I am new to MySQL and hope someone can help me with this.
I want to create a simple table with two special settings:
a two-column primary key including the columns "de" and "location"
an auto incrementing column "tID" that generates a unique ID,
starting with 1
I tried the following (and several other approaches) but this always returns the below error:
The SQL:
CREATE TABLE TranslationsMain (
de VARCHAR(100) NOT NULL,
tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
location VARCHAR(50) NOT NULL,
classes VARCHAR(100) NOT NULL,
info VARCHAR(100) NOT NULL,
sortOrder INT NOT NULL,
en VARCHAR(100) NOT NULL,
PRIMARY KEY(de, location)
)
The error message:
"Incorrect table definition; there can be only one auto column and it must be defined as a key."
It works if I leave the auto incrementing column ("tID") out so there seems to be something wrong here.
Can someone help me with this ?
Many thanks in advance,
Mike
Try below query, I think this will solve your problem
CREATE TABLE TranslationsMain (
de VARCHAR(100) NOT NULL,
tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
location VARCHAR(50) NOT NULL,
classes VARCHAR(100) NOT NULL,
info VARCHAR(100) NOT NULL,
sortOrder INT NOT NULL,
en VARCHAR(100) NOT NULL,
PRIMARY KEY(tID),
UNIQUE(de, location))
OR
CREATE TABLE TranslationsMain12 (
de VARCHAR(100) NOT NULL,
tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
location VARCHAR(50) NOT NULL,
classes VARCHAR(100) NOT NULL,
info VARCHAR(100) NOT NULL,
sortOrder INT NOT NULL,
en VARCHAR(100) NOT NULL,
unique(tID),
primary key(de,location)
)
In your SQL what the problem is auto increment must be primary key. Otherwise it will not work.
You can use like these. i think your problem will solve.
CREATE TABLE TranslationsMain (
de VARCHAR(100) NOT NULL,
tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
location VARCHAR(50) NOT NULL,
classes VARCHAR(100) NOT NULL,
info VARCHAR(100) NOT NULL,
sortOrder INT NOT NULL,
en VARCHAR(100) NOT NULL,
PRIMARY KEY(tID,de, location))
Related
i could do this in Microsoft sql to create a column incrementing such as CHV180000001, CHV180000002 but trying to do that in MySql. I have tried but getting error: incorrect syntax. Any guide to achieve this: This is my code:
CREATE TABLE Candidates (ID INT AUTO_INCREMENT NOT NULL Primary Key,
[ApplicationID] AS ('CHV18'+right('000000'+CONVERT([varchar](6),[ID]),(6))),
[FirstName] [varchar](100) NOT NULL,
[MiddleName] [varchar](100) NOT NULL,
[LastName] [varchar](100) NOT NULL,
[DateOfBirth] [date] NOT NULL,
[Gender] [nchar](1) NOT NULL
Try this to create the table
CREATE TABLE Candidates (ID INT(11) AUTO_INCREMENT NOT NULL Primary Key,
ApplicationID varchar(6),
FirstName varchar(100) NOT NULL,
MiddleName varchar(100) NOT NULL,
LastName varchar(100) NOT NULL,
DateOfBirth date NOT NULL,
Gender varchar(1) NOT NULL);
I have a data set with a list of country names, and the country names are repeated once for "Males" and then again for "Females".
For example:
c_name gender
China M
Greece M
Algeria M
China F
Greece F
Algeria F
When I create table and import data from a csv file, I get a 'duplicate key' error. I am wondering if this has anything to do with the engine settings? Any ideas how this can be resolved? (I know it works because my friend got it to work on her Mac, and she did not have the option to choose 'Collate' or 'Engine' when creating her tables, but I'm on Windows)
EDIT: Here's how I'm creating the table:
CREATE TABLE dbs.enrollment (
e_id INT NOT NULL,
c_name VARCHAR(45) NOT NULL,
gender VARCHAR(45) NULL,
2001 INT NULL,
2002 INT NULL,
2003 INT NULL,
2004 INT NULL,
2005 INT NULL,
2006 INT NULL,
2007 INT NULL,
2008 INT NULL,
2009 INT NULL,
2010 INT NULL,
PRIMARY KEY (e_id, c_name));
Your friend might set a primary key with c_name in this table ,if you want to have a change ,you can cancel the primary key。
It would be better to answer if you shared the query which is used for the above. I agree with Walker Li.
If your query is something like this..
CREATE TABLE enrollment (
c_name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
PRIMARY KEY (c_name)
);
You can change it by removing the primary id line as
CREATE TABLE enrollment (
c_name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL
);
Hope this solves your problem.
If your csv data contains any unique column as ID you could use it as primary key as,
CREATE TABLE enrollment (
e_id INT NOT NULL,
gender VARCHAR(255) NOT NULL,
PRIMARY KEY (e_id)
);
A table can have only one primary key. Remove c_name in the last line and only have e_id as shown below
PRIMARY KEY (e_id);
The combination of e_id and c_name must be unique. If not you can create an additional artifical primary key as work around.
CREATE TABLE `enrollment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`e_id` int(11) NOT NULL,
`c_name` varchar(45) NOT NULL,
`gender` varchar(45) DEFAULT NULL,
`2001` int(11) DEFAULT NULL,
`2002` int(11) DEFAULT NULL,
`2003` int(11) DEFAULT NULL,
`2004` int(11) DEFAULT NULL,
`2005` int(11) DEFAULT NULL,
`2006` int(11) DEFAULT NULL,
`2007` int(11) DEFAULT NULL,
`2008` int(11) DEFAULT NULL,
`2009` int(11) DEFAULT NULL,
`2010` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I have a table called users with 4 unique columns and when I insert data in email it doesn't give me any error and inserts the data even when when the same value already exists in that column.
Here is my database structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL,
google_id BIGINT UNSIGNED NULL,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL,
username VARCHAR(20) NULL,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id),
UNIQUE(email,username,fb_id,google_id)
)";
But, when I create my table with following structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL UNIQUE,
google_id BIGINT UNSIGNED NULL UNIQUE,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL UNIQUE,
username VARCHAR(20) NULL UNIQUE,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id)
)";
It gives me an error when there is a duplicate entry.
Creating table with any of those methods doesn't give any error. After creating the tables I have verified with phpmyadmin that all those columns have unique index in both methods.
Akash, in the 1st create table, the composite (combination) is unique. If you want them individually to be unique, separate them into ... separate UNIQUE key statements, like in the 2nd.
Let's say the bottom of your first table read this
PRIMARY KEY(id),
UNIQUE KEY(email,username,fb_id,google_id)
Then there is nothing wrong with these two rows existing in the composite index:
'akash#gmail.com','Akash',101,102
and
'akash#gmail.com','Akash2',101,102
The following image shows the error I'm getting. What am I doing wrong?
http://tinypic.com/r/2vabf3c/8
When I try to add the following to a new table through the command line to a mysql database I get the following error shown in the image.
CREATE TABLE songs (
my_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
analysis_sample_rate FLOAT NOT NULL,
artist_7digitalid INT NOT NULL,
artist_familiarity FLOAT NOT NULL,
artist_hotttnesss FLOAT NOT NULL,
artist_id VARCHAR(30) NOT NULL,
artist_latitude FLOAT NOT NULL,
artist_location VARCHAR(30) NOT NULL,
artist_longitude FLOAT NOT NULL,
artist_mbid VARCHAR(30) NOT NULL,
artist_name VARCHAR(30) NOT NULL,
artist_playmeid INT NOT NULL,
audio_md5 VARCHAR(30) NOT NULL,
danceability FLOAT NOT NULL,
duration FLOAT NOT NULL,
end_of_fade_in FLOAT NOT NULL,
energy FLOAT NOT NULL,
key_of_song INT NOT NULL,
key_confidence FLOAT NOT NULL,
loudness FLOAT NOT NULL,
mode INT NOT NULL,
mode_confidence FLOAT NOT NULL,
album_name VARCHAR(30) NOT NULL,
release_7digitalid INT NOT NULL,
song_hotttnesss FLOAT NOT NULL,
song_id VARCHAR(30) NOT NULL,
tempo FLOAT NOT NULL,
time_signature INT NOT NULL,
time_signature_confidence FLOAT NOT NULL,
title VARCHAR(30) NOT NULL,
track_id VARCHAR(30) NOT NULL,
track_7digitalid INT NOT NULL,
year INT NOT NULL);
I have corrected the usage of key and of request however I still am getting an error.
http://tinypic.com/r/2akl6q8/8
You cannot use the word "key" for a column name since it is a reserved word.
key is a reserved word in MySQL.
You can however, still use it, but you must escape it with backticks, like so:
...
`key` INT NOT NULL,
...
Try to change mode's parameter name. Probably, mode is a reserved keyword.
CREATE TABLE questions
(
qid INT AUTO_INCREMENT,
submitterId VARCHAR(8) NOT NULL,
approverId VARCHAR(8) NOT NULL,
questionText TEXT NOT NULL UNIQUE,
answerA VARCHAR(100) NOT NULL,
answerB VARCHAR(100) NOT NULL,
answerC VARCHAR(100) NOT NULL,
answerD VARCHAR(100) NOT NULL,
difficulty INT NOT NULL,
category INT NOT NULL,
correctAnswer INT NOT NULL,
selectionProb INT NOT NULL,
status INT NOT NULL
);
What is wrong with the above SQL command in MySQL?
im getting
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 'TABLE
questions
(
qid INT AUTO_INCREMENT,
submitterId VARCHAR(8) NOT NULL,
appr' at line 1
There can be only one auto column and it must be defined as a key.
BLOB/TEXT columns in key specification can't be used without a key length.
Thus, the correct SQL DDL shall be:
CREATE TABLE questions (
qid INT AUTO_INCREMENT PRIMARY KEY,
submitterId VARCHAR(8) NOT NULL,
approverId VARCHAR(8) NOT NULL,
questionText TEXT NOT NULL,
answerA VARCHAR(100) NOT NULL,
answerB VARCHAR(100) NOT NULL,
answerC VARCHAR(100) NOT NULL,
answerD VARCHAR(100) NOT NULL,
difficulty INT NOT NULL,
category INT NOT NULL,
correctAnswer INT NOT NULL,
selectionProb INT NOT NULL,
status INT NOT NULL
);
An idtentity-column must be declared as PRIMARY or UNIQUE
Add PRIMARY KEY (qid) at the end of your query.
Make your AUTO INCREMENT NOT NULL and add PRIMARY KEY at the end.
CREATE TABLE questions
(
qid INT AUTO_INCREMENT NOT NULL,
submitterId VARCHAR(8) NOT NULL,
approverId VARCHAR(8) NOT NULL,
questionText TEXT NOT NULL,
answerA VARCHAR(100) NOT NULL,
answerB VARCHAR(100) NOT NULL,
answerC VARCHAR(100) NOT NULL,
answerD VARCHAR(100) NOT NULL,
difficulty INT NOT NULL,
category INT NOT NULL,
correctAnswer INT NOT NULL,
selectionProb INT NOT NULL,
status INT NOT NULL,
PRIMARY KEY (qid)
);