What is the default primary key generation strategy for MySQL - mysql

I have been using AUTO_INCREMENT attribute to generate a primary key when a new row is inserted into a table in MySQL.
Could somebody help me understand what is the default primary key generation strategy for MySQL and how does it work?
EDIT
Hibernate has a identifier generation strategy native selects identity, sequence or hilo depending upon the capabilities of the underlying database. I used MySQL with hibernate.hbm2ddl.auto=update which generated id BIGINT(20) NOT NULL AUTO_INCREMENT for id property of Long Java data type.
I am trying to understand how did Hibernate choose AUTO_INCREMENT when it used SchemaExport tool. Is AUTO_INCREMENT the default primary key generation strategy for MySQL?

From here
The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows.
If no value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers. If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the auto_increment column of that row will not be re-assigned.
The auto_increment value of the last inserted row can be accessed using the mySQL function LAST_INSERT_ID() but it must be called right after the insert query, in the same database connection

Always not necessary to use auto increment to put it as primary key.
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
PRIMARY KEY (P_Id)
)

You can use 'SERIAL' data type.
CREATE TABLE test(
"id" Serial NOT NULL,
);
ALTER TABLE "test" ADD CONSTRAINT "Key1" PRIMARY KEY ("id");

Related

How can I use mysql table as a unique key generator in Spring Boot JPA?

I want to generate unique keys at run time across regions and the key generation will be owned by master region. I will have one table with one column(key) with default value as 0 and one row only. Now whenever I need to generate the new unique key,i will increment that column key and return its value. Now I want both this to happen in a transactional way: Increment count of column and return value.My method will look as:
int generateKey(){
UPDATE key_generation SET key = key+1;
Select * from key_generation limit 1;
}
How can I ensure that both of this are executed together and no read/write should happen on this table when this one request is running?
You should use auto_increment for that purpose.
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
Source: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html

How to create sequence for a number that starts at 100 and increment by 5 in MySQL?

I need that the values for a ColumnID, which is a Primary Key, to start at 100 and increment by 5. This condition is asked to be included as a constraint before populating the tables. I already created the tables, I just need to add that constraint. I know I can't use AUTO_INCREMENT because the increase is only by 1. Is there a way to do it in MySQL?
MySQL does not provide any built-in function to create a sequence for a table's rows or columns. But we can generate it via SQL query.
Example:
Let us understand it with the help of the following example. First, we need to create a new table and make sure that there is one column with the AUTO_INCREMENT attribute and that too, as PRIMARY KEY.
Execute the below query to create a table:
CREATE TABLE Insects (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
Origin VARCHAR(30) NOT NULL
);
Then you can alter your column to start from another value:
ALTER TABLE Insects AUTO_INCREMENT=100;
You can check it here.

external GUID to satisfy unique constraint in mysql

I have a MYSQL 8.x table, each row is unique since the PK is auto incremented. A column transactionID is used to store an external transactionID that needs to be coupled with the row/record.
However a new requirement came and we want to create a new row with the same transactionID. I was thinking to add a new column that holds the GUID of the transaction.
Is this good idea? Is this going to be slow? Are there any second thougths regarding uuid as strings? MySQL 8.0 added UUID_TO_BIN and BIN_TO_UUID function that could store UUID as number.
CREATE TABLE `testme`.`new_table` (
`ID` INT NOT NULL AUTO_INCREMENT,
`transactionID` INT NOT NULL,
`maybe_uuid` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ID`));
ALTER TABLE `testme`.`new_table`
ADD UNIQUE INDEX `index2` (`transactionID` ASC, `maybe_uuid` ASC) VISIBLE;
;
GUIDs are not great for use as a primary key for many reasons that i will not detail here and it would faster to use you auto_increment key that is already in place. But if you also need to store the GUID it is perfectly reasonable to just add another column for that. And you can use the two functions you mentioned.

How can we migrate sequence in PostgreSQL to Mysql

All,
I am trying to migrate from PostgreSQL DB to MySql DB. I have used sequence in some table to get auto increment column other than the primary key. How to create auto increment column which is the primary key in Mysql DB.
I am listing an example table below in PostgreSQL.
CREATE TABLE bills
(
id serial NOT NULL,
billname character varying(255) NOT NULL,
invoiceid character varying(255) NOT NULL DEFAULT nextval('bill_invoiceid_seq'::regclass),
CONSTRAINT combine_campaigns_pkey PRIMARY KEY (id)
)
In the example, "id" is the primary key and invoice id is not a key but getting auto increment
MySQL int auto_increment primary key should be very similar to postgreSQL serial (which does not need to be a primary key)
If you want to reserve some used sequence, you can insert a value (eg 3000) into MySQL auto_increment field, after that any new record will increase from this value i.e. 3001 onwards:
The equivalent for a SERIAL column in mysql is AUTO_INCREMENT. Define your primary key as follows:
CREATE TABLE my_table(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
...
)
or
CREATE TABLE my_table(
id int NOT NULL AUTO_INCREMENT ,
...
PRIMARY KEY(id)
)
Be aware that in mysql you can only have one AUTO_INCREMENT per table. Also worth mentioning is that you are moving from an RDBS that is rich in features and closer to standard compliance to one that has less features and less compliant.

What is the difference between SERIAL and AUTO_INCREMENT in mysql

I have come across two ways to increment the ids in mysql automatically.
One is SERIAL and other is AUTOINCREMENT.
So Suppose i want to create a table myfriends.
I can create it in two ways like:
1)
mysql> create table myfriends(id int primary key auto_increment,frnd_name varchar(50) not null);
2)
mysql> create table myfriends(id serial primary key,frnd_name varchar(50) not null);
What is difference between the two ?
OR
Do anyone way has advantages over other ?
Please Help.
As per the docs
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
So, be careful when creating a reference to a SERIAL PK, since that reference column has to be of this exact type.
AUTO_INCREMENT is an attribute of a specific column of any numeric type (int or float), both signed and unsigned. When rows are inserted it automatically assigns sequential numbers, so you don't have to (e.g. by using LAST_INSERT_ID()). See http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
SERIAL is an alias that combines column type casting (BIGINT specifically), AUTO_INCREMENT, UNSIGNED and other attributes for a specific column (see quote from docs below). See https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
SERIAL DEFAULT VALUE in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE.
From mysql doc
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
SERIAL DEFAULT VALUE in the definition of an integer column is an
alias for NOT NULL AUTO_INCREMENT UNIQUE.
If no value is specified for the AUTO_INCREMENT column, MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers. MySQL doesn't automatically decrease the autoincrement value when you delete a row. Reasons are:
Danger of broken data integrity (imagine multiple users perform
deletes or inserts...doubled entries may occur or worse)
Errors may occur when you use master slave replication or
transactions