Error when use auto_increment in MySQL - mysql

I meet some errors when using auto_increment in MySQL.
code is here:
user_id bigint(20) unsigned not null auto_increment=1000
but when I try
user_id bigint(20) unsigned not null auto_increment
it works. why?

This should work:
create table test1 (
id int unsigned not null auto_increment,
primary key (id)
)auto_increment=100;

Are you trying to seed the auto increment number?
If so, the syntax is part of the CREATE|ALTER TABLE command.
See http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html

This happens because auto increment is set for the table and not a particular column.
So to start from 1000,
Create a table column with just auto_increment
And then alter the table using
ALTER TABLE your_table_name AUTO_INCREMENT=1000;

auto increment of mysql is only +1 value.
look at this http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Related

Auto Increment missing in my users table after importing on production. How should I modify it?

When I run this query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=56;
It gives me error like :
#1833 - Cannot change column 'id': used in a foreign key constraint
'designation_user_user_id_foreign' of table
'databasename.designation_user'
Found a solution that worked!
I made id column in users table unique using more option in action column of users table and then executed below query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

SQL AUTO_INCREMENT starting at number

am on a MySQL database and am using MySQL workbench.
here's my table DDL
CREATE TABLE `contract_validator` (
`id` bigint(20) NOT NULL,
`created` datetime(6) NOT NULL,
`user_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to make an id column in a table auto_increment, i did so using
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE validator MODIFY id bigint AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS = 1;
I have two issues, i want to set the auto_increment to max(id) + 1
i have tried:
SELECT #validator_id := max(id) + 1 FROM validator;
ALTER TABLE validator AUTO_INCREMENT = #validator_id ;
but i get syntax error,
the second issue is that when i try to insert a line i get an error
Error Code: 1364. Field 'id' doesn't have a default value
I don't know what I did wrong,
thank you for your help.
Normally, you don't have to take care of AUTO_inCREMENT value.
When you update id column to make it auto increment, MySQL will automatically set AUTO_INCREMENT value to the MAX + 1 value, event if there are gaps in in column numerotation.
SQL for setting auto increment on id field:
ALTER TABLE `test` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT, add PRIMARY KEY (`id`);

Add custom auto increment value [duplicate]

This question already has answers here:
How to set initial value and auto increment in MySQL?
(10 answers)
Closed 1 year ago.
Am trying to create a MySQL that tells
auto_increment
Where and how to start incrementing. Each time I run the code, it always tells me I have error near
auto.
Each time I removed the "=", it always work.
This is the code
CREATE TABLE staff(
id into(11) not null primary key auto_increment=001,
Names varchar(109) not null
);
What am I doing wrong
First create table like below:
CREATE TABLE staff( id into(11) not null primary key
auto_increment, Names varchar(109) not null );
then alter to customized auto increment.
ALTER TABLE staff AUTO_INCREMENT=001;
Schema (MySQL v5.7)
CREATE TABLE staff(
id int not null primary key auto_increment,
Names varchar(255) not null
)auto_increment=001;
View on DB Fiddle
The default auto-increment value is an option on the table not on the column (perhaps counterintuitively, but a table is only allowed to have one such column).
The syntax looks like:
CREATE TABLE staff (
id int not null primary key auto_increment,
Names varchar(109) not null
) auto_increment = 10;
Here is a db<>fiddle.

Null values for all records

I am writing a code for a table where 1st table is id --> auto incremented and primary key
2nd table is status --> varchar
Which i want to keep totally null
And
Later on be changed as updated
By insert
I tried
Example:
Create table justcheck ( id int auto increment primary key , status varchar(1) IS NULL);
IT GAVE ME AN ERROR.
1064 : SYNTAX ERROR.
You can try to use AUTO_INCREMENT instead of AUTO INCREMENT and IS NULL is for check column is null
Create table justcheck (
id int AUTO_INCREMENT primary key ,
`status` varchar(1) NULL
);
INSERT INTO justcheck(status) VALUES (NULL)

Defining Composite Key with Auto Increment in MySQL

Scenario:
I have a table which references two foreign keys, and for each unique combination of these foreign keys, has its own auto_increment column. I need to implement a Composite Key that will help identify the row as unique using combination of these three (one foreign keys and one auto_increment column, and one other column with non-unique values)
Table:
CREATE TABLE `issue_log` (
`sr_no` INT NOT NULL AUTO_INCREMENT ,
`app_id` INT NOT NULL ,
`test_id` INT NOT NULL ,
`issue_name` VARCHAR(255) NOT NULL ,
primary key (app_id, test_id,sr_no)
);
Of course, there has to be something wrong with my query, because of which the error thrown is:
ERROR 1075: Incorrect table definition; there can be only one auto
column and it must be defined as a key
What I am trying to achieve:
I have an Application Table (with app_id as its primary key), each Application has a set of Issues to be resolved, and each Application has multiple number of tests (so the test_id col)
The sr_no col should increment for unique app_id and test_id.
i.e. The data in table should look like:
The database engine is InnoDB.
I want to achieve this with as much simplicity as possible (i.e. avoid triggers/procedures if possible - which was suggested for similar cases on other Questions).
You can't have MySQL do this for you automatically for InnoDB tables - you would need to use a trigger or procedure, or user another DB engine such as MyISAM. Auto incrementing can only be done for a single primary key.
Something like the following should work
DELIMITER $$
CREATE TRIGGER xxx BEFORE INSERT ON issue_log
FOR EACH ROW BEGIN
SET NEW.sr_no = (
SELECT IFNULL(MAX(sr_no), 0) + 1
FROM issue_log
WHERE app_id = NEW.app_id
AND test_id = NEW.test_id
);
END $$
DELIMITER ;
You can do this with myISAM and BDB engines. InnoDB does not support this. Quote from MySQL 5.0 Reference Manual.
For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix.
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
I don't fully understand your increment requirement on the test_id column, but if you want an ~autoincrement sequence that restarts on every unique combination of (app_id, test_id), you can do an INSERT ... SELECT FROM the same table, like so:
mysql> INSERT INTO `issue_log` (`sr_no`, `app_id`, `test_id`, `issue_name`) SELECT
IFNULL(MAX(`sr_no`), 0) + 1 /* next sequence number */,
3 /* desired app_id */,
1 /* desired test_id */,
'Name of new row'
FROM `issue_log` /* specify the table name as well */
WHERE `app_id` = 3 AND `test_id` = 1 /* same values as in inserted columns */
This assumes a table definition with no declared AUTO_INCREMENT column. You're essentially emulating autoincrement behavior with the IFNULL(MAX()) + 1 clause, but the manual emulation works on arbitrary columns, unlike the built-in autoincrement.
Note that the INSERT ... SELECT being a single query ensures atomicity of the operation. InnoDB will gap-lock the appropriate index, and many concurrent processes can execute this kind of query while still producing non-conflicting sequences.
You can use a unique composite key for sr_no,app_id & test_id. You cannot use incremental in sr_no as this is not unique.
CREATE TABLE IF NOT EXISTS `issue_log` (
`sr_no` int(11) NOT NULL,
`app_id` int(11) NOT NULL,
`test_id` int(11) NOT NULL,
`issue_name` varchar(255) NOT NULL,
UNIQUE KEY `app_id` (`app_id`,`test_id`,`sr_no`)
) ENGINE=InnoDB ;
I have commented out unique constraint violation in sql fiddle to demonstrate (remove # in line 22 of schema and rebuild schema )
This is what I wanted
id tenant
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
My current table definition is
CREATE TABLE `test_trigger` (
`id` BIGINT NOT NULL,
`tenant` varchar(255) NOT NULL,
PRIMARY KEY (`id`,`tenant`)
);
I created one table for storing the current id for each tenant.
CREATE TABLE `get_val` (
`tenant` varchar(255) NOT NULL,
`next_val` int NOT NULL,
PRIMARY KEY (`tenant`,`next_val`)
) ENGINE=InnoDB ;
Then I created this trigger which solve my problem
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE INSERT
ON test_trigger
FOR EACH ROW
BEGIN
UPDATE get_val SET next_val = next_val + 1 WHERE tenant = new.tenant;
set new.id = (select next_val from get_val where tenant=new.tenant);
END$$
DELIMITER ;
This approach will be thread safe also because any insertion for the same tenant will happen sequentially because of the update query in the trigger and for different tenants insertions will happen parallelly.
Just add key(sr_no) on auto-increment column:
CREATE TABLE `issue_log` (
`sr_no` INT NOT NULL AUTO_INCREMENT ,
`app_id` INT NOT NULL ,
`test_id` INT NOT NULL ,
`issue_name` VARCHAR(255) NOT NULL ,
primary key (app_id, test_id,sr_no),
key (`sr_no`)
);
Why don't you try to change the position of declare fields as primary key, since when you use "auto_increment" it has to be referenced as the first. Like in the following example
CREATE TABLE `issue_log` (
`sr_no` INT NOT NULL AUTO_INCREMENT ,
`app_id` INT NOT NULL ,
`test_id` INT NOT NULL ,
`issue_name` VARCHAR(255) NOT NULL ,
primary key (sr_no,app_id, test_id)
);