Creating a column in MySql with id such as CHV18000002 - mysql

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

Related

Where is the error in this sql statement?

I keep getting an sql error when attempting to use this statement:
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL
);
The error I get 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 '
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR' at line 1
Using
MySql V5.7.26
Just Add KEY in PRIMARY like PRIMARY KEY, you have missed the SQL syntax.
CREATE TABLE users
(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL);
You should change the query.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL
);
In your query KEY is missing after PRIMARY.
Run this query then, u will get no syntax.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`)
);
You place the PRIMARY KEY identifier on a separate line

mysql workbench duplicate key error

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;

Duplicate data is inserted with unique index

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

Account Number Auto generation

I new to programming. How do I apply auto generation to the accountNo in accounts table. I tried everything. But I don't know how to get this done. Can anyone explain me.
CREATE TABLE accounts(
accountNo int(100) NOT NULL, // I need this to be auto generated.
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
Try this one.
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate VARCHAR(20) NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
ALTER TABLE accounts AUTO_INCREMENT = 1001;
When you are adding new data to your accounts table, accountNo will be auto generated.
Make it as an AUTO_INCREMENT:
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
For further informations have a look at this: https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
declear it AUTO_INCREMENT and you can define PRIMARY KEY like that also. Read manual auto_increment
accountNo INT NOT NULL AUTO_INCREMENT PRIMARY KEY

Error in MySQL query

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