How to set default value to version column for existing data? - spring

I am trying to use optimistic locking.
I am adding the version column to my table how do I set the default value to the version column for existing data or this is sufficient on entity?
#Version
#Column(name = "VERSION")
private Long version = 0L;

The most easiest way it to do this in the database.
Of course you need to add the version column anyway: something like:
alter table MyEntity add column version INT(11); //no not null constraint here!
and then just add the first value to all entities:
update MyEntity set 'version' = 1;
now you can also add the not null constraint
alter table MyEntity modify version INT(11) NOT NULL;
(I expect that you stop the application while you add the version column).

In case of Oracle as a database - use with values option for nullable columns
alter table MyEntity add column version INT(11) default 0 with values
for not-null columns - DB will updates to default value for existing rows
alter table MyEntity add column version INT(11) not null default 0
From Oracle-11g onwards, default values are retrieved from metadata
for null values on modified field, Oracle does not perform update on each row to fill default values.
see - https://chandlerdba.com/2014/10/30/adding-not-null-columns-with-default-values/

Related

DEFAULT column value support in CockroachDB

Does CockroachDB support default values for columns in its tables? Does it allow default values to be function values (e.g. current_date())?
You can set DEFAULT values using the DEFAULT constraint, which CockroachDB has documented here.
It also supports setting the default value as a function, e.g. to insert the date that a write occurred.
You would create a table with such a default column like:
CREATE TABLE purchase_log (
id INT PRIMARY KEY,
date_purchased DATE DEFAULT current_date()
);
Then all inserts to the table that don't specify the date_purchased column will have the column automatically populated with the return value of current_date() at the time of the insert.

Unique constraint with null columns (Hibernate, PostgreSQL)

I have class like Clazz
#Table(
name="tablename",
uniqueConstraints=
#UniqueConstraint(
name= "uniqueColumn_deleted_uk",
columnNames={"myuniquecolumn", "deleted"}
)
)
public class Clazz {
#Column(name = "deleted")
private LocalDateTime deleted;
}
deleted is nullable, PosgreSQL creates unique index like
CREATE UNIQUE INDEX uniqueColumn_date_uk ON public.tablename (short_code_3, deleted);
and it allows insert duplicate myuniquecolumn when deleted is NULL.
How prevent this?
I want have non duplicates when deleted is null.
You should create two partial unique indexes
create unique index on public.tablename (short_code_3, deleted) where deleted is not null;
create unique index on public.tablename (short_code_3) where deleted is null;
(I don't know how to do it in your ORM).
This is not possible because null is never = null.
Read more about null values in SQL https://en.wikipedia.org/wiki/Null_(SQL)
If you want to have the deleted column in the unique index you must provide a default value for that column.
Tow partial indexes like klin provided are best practice up to Postgres 14.
Postgres 15 adds NULLS NOT DISTINCT for this purpose:
CREATE UNIQUE INDEX foo_idx ON public.tbl (short_code_3, deleted) NULLS NOT DISTINCT;
See:
Create unique constraint with null columns

adding boolean field with default value on a large postgres table

We have a very large table (1 Million records) in some cases and we need to add boolean fields to it which have default values.
If we add only column it takes 3 minutes and we add 3 columns in the same statement it takes same time.
$ALTER TABLE Job ADD COLUMN test BOOLEAN NOT NULL default false;
ALTER TABLE
Time: 186506.603 ms
$ALTER TABLE Job ADD COLUMN test BOOLEAN NOT NULL default false ,
ADD COLUMN test1 BOOLEAN NOT NULL default false,
ADD COLUMN test2 BOOLEAN NOT NULL default false;
ALTER TABLE
Time: 179055.546 ms
We are on Postgres 9.1 . Is there a postgres feature allows to add multiple boolean fields with default values in one shot? This is for a database change management /upgrade solution . Is it better than using temp table to copy and insert to add multiple boolean fields with default values to a table ? The temp table approach is described in this blog:
http://blog.codacy.com/2015/05/14/how-to-update-large-tables-in-postgresql/
You've already shown the best (simple) way - a compound ALTER TABLE statement that adds them all at once.
To do it without a long lock, you have to do it in multiple steps. Add the column as nullable without the default. Add the default but leave it nullable. UPDATE all existing rows to add the new value, preferably in batches. Then finally alter the table to add the not null constraint.

How to alter primary key column to autoincrement in derby using eclipse

I create a table and set one Integer column as a primary key.Now I want to set Auto Increment by 1 to that column.I am using this query but getting error.
ALTER TABLE APP.DocumentGroupCategory
ALTER ID INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
How can I alter column without drop table and create once again?

date Not null , error ora_01758

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

Resources