i understand,below column will be signed int by default.
id INT(6);
Can an auto increment column specified below be signed by default? Mysql starts the value from 1 for an auto increment column.
id INT(6) AUTO_INCREMENT PRIMARY KEY
Yes, you can create an auto increment primary key with a signed int. Try this:
CREATE TABLE mytable( id int(6) AUTO_INCREMENT PRIMARY KEY);
Then the following queries are both valid
INSERT INTO mytable values();
INSERT INTO mytable values(-10);
This will result in the table having a row with -10 and another with 1 as values. But you will run into problems if you try this:
ALTER TABLE mytable AUTO_INCREMENT=-10;
yes, you cannot have auto increment values that are negative numbers.
After a lot of searches... I looked for a solution with a TRIGGER called BEFORE INSERT ! I found this : https://stackoverflow.com/a/43441586/2282880
Here is my variant :
CREATE TRIGGER `invertID`
BEFORE INSERT ON `<table>`
FOR EACH ROW SET NEW.id=CONCAT("-", (
SELECT `auto_increment`
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = '<table>')
)
It worked for me fine.
It was the best way I found to sync in both directions two databases with same schema without same ID's in my tables.
Related
How do I set the initial value for an "id" column in a MySQL table that start from 1001?
I want to do an insert "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";
Without specifying the initial value for the id column.
Use this:
ALTER TABLE users AUTO_INCREMENT=1001;
or if you haven't already added an id column, also add it
ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (id);
MySQL - Setup an auto-incrementing primary key that starts at 1001:
Step 1, create your table:
create table penguins(
my_id int(16) auto_increment,
skipper varchar(4000),
PRIMARY KEY (my_id)
)
Step 2, set the start number for auto increment primary key:
ALTER TABLE penguins AUTO_INCREMENT=1001;
Step 3, insert some rows:
insert into penguins (skipper) values("We need more power!");
insert into penguins (skipper) values("Time to fire up");
insert into penguins (skipper) values("kowalski's nuclear reactor.");
Step 4, interpret the output:
select * from penguins
prints:
'1001', 'We need more power!'
'1002', 'Time to fire up'
'1003', 'kowalski\'s nuclear reactor'
MySQL Workbench
If you want to avoid writing sql, you can also do it in MySQL Workbench by right clicking on the table, choose "Alter Table ..." in the menu.
When the table structure view opens, go to tab "Options" (on the lower bottom of the view), and set "Auto Increment" field to the value of the next autoincrement number.
Don't forget to hit "Apply" when you are done with all changes.
PhpMyAdmin:
If you are using phpMyAdmin, you can click on the table in the lefthand navigation, go to the tab "Operations" and under Table Options change the AUTO_INCREMENT value and click OK.
With CREATE TABLE statement
CREATE TABLE my_table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) AUTO_INCREMENT = 100;
or with ALTER TABLE statement
ALTER TABLE my_table AUTO_INCREMENT = 200;
First you need to add column for auto increment
alter table users add column id int(5) NOT NULL AUTO_INCREMENT FIRST
This query for add column at first.
Now you have to reset auto increment initial value. So use this query
alter table users AUTO_INCREMENT=1001
Now your table started with 1001
You could also set it in the create table statement.
`CREATE TABLE(...) AUTO_INCREMENT=1000`
Alternatively, If you are too lazy to write the SQL query. Then this solution is for you.
Open phpMyAdmin
Select desired Table
Click on Operations tab
Set your desired initial Value for AUTO_INCREMENT
Done..!
For this you have to set AUTO_INCREMENT value
ALTER TABLE tablename AUTO_INCREMENT = <INITIAL_VALUE>
Example
ALTER TABLE tablename AUTO_INCREMENT = 101
Also , in PHPMyAdmin , you can select table from left side(list of tables) then do this by going there.
Operations Tab->Table Options->AUTO_INCREMENT.
Now, Set your values and then press Go under the Table Options Box.
SET GLOBAL auto_increment_offset=1;
SET GLOBAL auto_increment_increment=5;
auto_increment_increment: interval between successive column values
auto_increment_offset: determines the starting point for the AUTO_INCREMENT column value.
The default value is 1.
read more here
I want to know the next value of auto increment field
I wanted to test this :
select max(contactid) from contact
and I add 1
but I realized that it can give me an error
for exemple
if I insert one record and I delete it
so if I insert after the field will increase by two
how can I achieve that ?
thank you
There are multiple solutions to this problem:
1. (Preferable) Stop trying to predict auto-increment values
This is the more typical case, and basically is using auto-increment as designed. This assumes that you don't actually need the auto-increment value before you insert. What you can do is:
DROP TABLE IF EXISTS t;
CREATE TABLE t (id INT UNSIGNED NOT NULL auto_increment, x INT NOT NULL, PRIMARY KEY(id));
INSERT INTO t (x) VALUES (100);
SELECT LAST_INSERT_ID();
The call to SELECT LAST_INSERT_ID() will return the ID that was just generated for your INSERT.
2. Set up an ID generation table specifically to generate IDs
You can create a table with just an auto-increment column, like so:
DROP TABLE IF EXISTS id_generator;
CREATE TABLE id_generator (id INT UNSIGNED NOT NULL auto_increment, PRIMARY KEY(id));
You can then generate a new, unique ID with:
INSERT INTO id_generator (id) VALUES (NULL);
SELECT LAST_INSERT_ID();
And use that ID to insert into the table you're actually working with. As long as all generated IDs come from this ID generation table, there will be no conflicts. However there is a cost to generating these IDs, and auto-increment is not very efficient at it.
3. Use an external ID generation scheme
This is more or less similar to solution 2, but doesn't use MySQL at all for the ID generation. You can use something like a UUID/GUID scheme which generates a string, or you could use something like Snowflake to generate integer IDs.
You should use LAST_INSERT_ID like this:
SELECT LAST_INSERT_ID()
It will return the last value of AUTO_INCREMENT ID field.
More details here: http://goo.gl/RkmR5
This will give you the next id value that will be inserted:
SELECT LAST_INSERT_ID() + 1;
I am trying to insert a unique value into my table, however I need to know the ID before I create it. I know an AUTO_INCREMENT would have solved this problem, but this field is not AUTO_INCREMENTed.
This is my code...
INSERT INTO networks
(NETWORK_ID, ADMIN_USER_ID, NETWORK_NAME, ADDRESS)
VALUES
((SELECT MAX(NETWORK_ID)+1 FROM networks) , 3, 'Arcafe', 'habarzel 2 TA')
When I run it, I get a warning that I can't use the table in the FROM, I guess because it is pointing to itself. How can I achieve what I need? Can I change a field into an AUTO_INCREMENT field?
Usually you set the field to be an auto increment field when it is defined. To do so afterwards, you can use:
ALTER TABLE networks MODIFY NETWORK_ID int(10) unsigned NOT NULL auto_increment;
To then insert an ew record and for it to automatically get an assigned ID, merely omit the field from the insert, eg.
INSERT INTO networks
(ADMIN_USER_ID, NETWORK_NAME, ADDRESS)
VALUES
(3, 'Arcafe', 'habarzel 2 TA')
You can use separate table for generation unique ids and use this ids
ALTER TABLE networks CHANGE NETWORK_ID NETWORK_ID int auto_increment
Alter Table Manual
How can I set a minimum length for my primary key ID which is auto incremented. Now the auto increment starts at 1 and goes up from there. However I would like the id to be at least 5 characters long. So it would start at 10001, 10002, 10003 etc
If you have the table, but not the column run the following code with the appropriate modifications:
ALTER TABLE MyTableName
ADD MyTableNameId INT NOT NULL AUTO_INCREMENT,
ADD INDEX (MyTableNameId);
If you have already created the column, you can do this:
ALTER TABLE MyTableName AUTO_INCREMENT = 10001;
Beginning with MySQL 5.0.3, InnoDB supports the AUTO_INCREMENT = N table option in CREATE TABLE and ALTER TABLE statements.
You can't define a length, but you can specify what value it starts with.
At declaration time:
CREATE TABLE test
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
whatever VARCHAR(10),
...
) AUTO_INCREMENT = 100000;
Or, after declaring / at run time:
ALTER TABLE test AUTO_INCREMENT=200000;
How do I set the initial value for an "id" column in a MySQL table that start from 1001?
I want to do an insert "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";
Without specifying the initial value for the id column.
Use this:
ALTER TABLE users AUTO_INCREMENT=1001;
or if you haven't already added an id column, also add it
ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (id);
MySQL - Setup an auto-incrementing primary key that starts at 1001:
Step 1, create your table:
create table penguins(
my_id int(16) auto_increment,
skipper varchar(4000),
PRIMARY KEY (my_id)
)
Step 2, set the start number for auto increment primary key:
ALTER TABLE penguins AUTO_INCREMENT=1001;
Step 3, insert some rows:
insert into penguins (skipper) values("We need more power!");
insert into penguins (skipper) values("Time to fire up");
insert into penguins (skipper) values("kowalski's nuclear reactor.");
Step 4, interpret the output:
select * from penguins
prints:
'1001', 'We need more power!'
'1002', 'Time to fire up'
'1003', 'kowalski\'s nuclear reactor'
MySQL Workbench
If you want to avoid writing sql, you can also do it in MySQL Workbench by right clicking on the table, choose "Alter Table ..." in the menu.
When the table structure view opens, go to tab "Options" (on the lower bottom of the view), and set "Auto Increment" field to the value of the next autoincrement number.
Don't forget to hit "Apply" when you are done with all changes.
PhpMyAdmin:
If you are using phpMyAdmin, you can click on the table in the lefthand navigation, go to the tab "Operations" and under Table Options change the AUTO_INCREMENT value and click OK.
With CREATE TABLE statement
CREATE TABLE my_table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) AUTO_INCREMENT = 100;
or with ALTER TABLE statement
ALTER TABLE my_table AUTO_INCREMENT = 200;
First you need to add column for auto increment
alter table users add column id int(5) NOT NULL AUTO_INCREMENT FIRST
This query for add column at first.
Now you have to reset auto increment initial value. So use this query
alter table users AUTO_INCREMENT=1001
Now your table started with 1001
You could also set it in the create table statement.
`CREATE TABLE(...) AUTO_INCREMENT=1000`
Alternatively, If you are too lazy to write the SQL query. Then this solution is for you.
Open phpMyAdmin
Select desired Table
Click on Operations tab
Set your desired initial Value for AUTO_INCREMENT
Done..!
For this you have to set AUTO_INCREMENT value
ALTER TABLE tablename AUTO_INCREMENT = <INITIAL_VALUE>
Example
ALTER TABLE tablename AUTO_INCREMENT = 101
Also , in PHPMyAdmin , you can select table from left side(list of tables) then do this by going there.
Operations Tab->Table Options->AUTO_INCREMENT.
Now, Set your values and then press Go under the Table Options Box.
SET GLOBAL auto_increment_offset=1;
SET GLOBAL auto_increment_increment=5;
auto_increment_increment: interval between successive column values
auto_increment_offset: determines the starting point for the AUTO_INCREMENT column value.
The default value is 1.
read more here