How to Make String Auto Increment

how to create custom auto increment for string field in laravel

You could also use model event's. Below you have the boot function in which you would put into your model. After that you would have access to various of event's like creating, created, updating, updated .. so on and so for. We are interested in created, as it is triggered after your model is saved.

The static method receives a function that has as parameter an instance of the model that was just saved so you can access the id and concatenate with the string you want.

/**
* Enables us to hook into model event's
*
* @return void
*/
public static function boot()
{
parent::boot();

static::created(function($product) {
$product->sku .= 'sku-' . $product->id;
$product->save();
});
}

Create autoincrement ID with Strings Like USER101,USER102 in NetBeans IDE with MySql database

If id is your auto increment column, make a second generated column with

 CONCAT(''USER'',id);

As generating formula.

The next code would change a column to generate automatocally the USER101 depending on the id from the autoincrement.

ALTER TABLE `testdb`.`testtable` 
CHANGE COLUMN `columname` CHAR(50) NULL GENERATED ALWAYS AS (CONCAT('USER',id));

But i don't know how you make this in NEtbeans and google also didn't help.

Adding String + Auto Increment - pandas, python

You may try something like this:

nullCond = df.Email.isnull()    
# or nullCond = (df.Email == "") it those are empty strings

df.loc[nullCond, 'Subscriberkey'] = "string" + nullCond[nullCond].cumsum().astype(str) + "string"

Sample Image

how to add an ID with both string and auto incremented number in sql

This exactly my case was, and I have solved, see my answer there in similar question.

Let me try to answer it systematically as well.

create table my_seq(
min_value integer,
Max_value integer,
last_value integer,
increment_by tinyint,
type varchar)ENGINE = InnoDB;

Then create data values like,

insert into my_seq(min_value,max_value,last_value,increment_by,type) 
values(1,99999999,1,1,'foo#','DS'),(1,999999999,1,1,'foo#','MS'),(1,999999999,1,1,'foo#','DOC');

Make sure you have auto-commit=false.
Then, do it like this in your app, or database code,

//very import to begin the transaction
begin;
select CONCAT(type_val,'-',last_value) from my_seq where type_val=? FOR UPDATE;

Read the result in App or database procedure/trigger

update my_seq set last_number=last_number+1 where type_val=?;
commit;

Make sure there is index on type_val.

I believe this should work.



Related Topics



Leave a reply



Submit