How to create a unique autoincrement field with a string prefix? - mysql

I have a table 'user' with 3 items.
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`customernumber` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`type` enum('customer','admin') COLLATE utf8_unicode_ci NOT NULL,
How can I increment 'customernumber' with the first two letters of the 'type'?
For example, user with ID=10 and type='admin' will have a customernumber 'AD000010'. And user with ID = 12 and type = 'customer' will have a customernumber 'CU000012'?
Is this possible to do this in MySQL without using a trigger? If not, how can I do this with a trigger?

What you could do is store the key as two columns. A char prefix and an auto-incrementing int, both of which are grouped for the primary key.
CREATE TABLE myItems (
id INT NOT NULL AUTO_INCREMENT,
prefix CHAR(30) NOT NULL,
PRIMARY KEY (id, prefix),
...
Please refer to this link
How to make MySQL table primary key auto increment with some prefix
hope this help you .

Related

How to make mysql table column as KEY, when already another column is set as PRIMARY KEY

In our mysql database, one table say "mytable" is having coumn mobile_no as primary key. But we are in need to make another column also as key. So that I can use that column in where condition.
Show create of table is below-:
CREATE TABLE `report_data` (
`api_request_id` int(11) NOT NULL AUTO_INCREMENT,
`emailid` varchar(100) NOT NULL,
`api_recipient_data` longtext,
`request_params` varchar(255) DEFAULT NULL,
`sent_date` date DEFAULT NULL,
`sent_time` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1-processed, 2-success, 3-in-bounce, 4-invalid-domain,5-in-unsubscribe, 6-in-scrubbing',
`api_userid` int(11) DEFAULT NULL,
`domain_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`api_request_id`,`emailid`),
KEY `sent_date` (`sent_date`)
);
I want to add one new column to this existing table and make that column as KEY.
we want to add one column d_name and want it to make it as KEY
ALTER TABLE report_data
ADD COLUMN d_name {column definition} ,
ADD INDEX (d_name);
sample fiddle

How to create table in MariaDB?

When I try to create simple table via HeidiSQL I'm getting an error like this
CREATE TABLE `prg_config` (
`id` INT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT '',
`value` VARCHAR NULL DEFAULT ''
) COLLATE='utf8_bin';
Please Check Following query :
CREATE TABLE prg_config (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT '',
`value` VARCHAR(50) NULL DEFAULT '',
PRIMARY KEY (id)
)COLLATE='utf8_bin';
CREATE TABLE `prg_config` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NULL DEFAULT '',
`value` VARCHAR(100) NULL DEFAULT ''
) COLLATE='utf8_bin';
Add PRIMARY KEY/UNIQUE/KEY to AUTO_INCREMENT column
Specify length for VARCHAR.
AUTO_INCREMENT :
Each table can have only one AUTO_INCREMENT column. It must defined as
a key (not necessarily the PRIMARY KEY or UNIQUE key). If the key
consists of multiple columns, the AUTO_INCREMENT column must be the
first one, unless the storage engine is Aria or MyISAM.
SqlFiddleDemo
If you created that table with HeidiSQL's table designer, I guess it looked like this:
HeidiSQL does not complain when you make the length/set empty for a VARCHAR column. MySQL + MariaDB both require a length for VARCHAR columns, so I can probably fix that by not letting the user to make the VARCHAR length empty.

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

Update existing unique keys columns in MYSQL

I have a table in that i have unique key .Now because of some reasons i want to update the unique key. AS of now unique key is having 4 columns now i want to add 2 more columns in that
CREATE TABLE abc (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
one varchar(64) NOT NULL,
search_id varchar(64) NOT NULL,
session_id varchar(64) NOT NULL,
pnr varchar(64) NOT NULL,
origin varchar(5) NOT NULL,
destination varchar(5) NOT NULL,
type varchar(15) NOT NULL,
name1 varchar(55) NOT NULL,
name2 varchar(55) NOT NULL,
number varchar(55) DEFAULT '',
text text,
cr_dt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY aedi_id (one,search_id,session_id,origin,destination,pnr,number) //I have this
UNIQUE KEY aedi_id (one,search_id,session_id,origin,destination,pnr,name1,name2,number) I want this
)
The syntax is:
alter table drop key aedi_id;
alter table add key aedi_id (one, search_id, session_id, origin, destination, pnr, name1, name2, number) ;
Note that a unique key is implemented as an index, which will be used for queries. This means that the order of the columns matters in the key definition -- some orderings will work better for your queries. So, depending on your queries, you may want to re-arrange the columns.

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

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;