How to Migration step for Long column in room database? - android-room

I have to add new column like Long, Integer and string in room database.
But migration not working?
ALTER TABLE 'followup_list' ADD COLUMN 'rmdTime' LONG NOT NULL DEFAULT 0

Related

How can I set index for json column in mysql 8 with laravel migration

I'm creating a project with laravel 6. One of my table column type is json.
The data format in the table column is like this:{age:30, gender:male, nation:china,...}. I am wondering if there is a way for me to set index for this column with laravel migration. my database version is mysql 8.0.21.
thank you!
I found this article very helpful for figuring this out. So for your example structure above, you might have a migration that looks like the following:
public function up(){
Schema::create('my_table', function(Blueprint $table){
$table->bigIncrements('id');
$table->json('my_json_col')->nullable();
$table->timestamps();
// add stored columns with an index
// index in this is optional, but recommended if you will be filtering/sorting on these columns
$table->unsignedInteger('age')->storedAs('JSON_UNQUOTE(my_json_col->>"$.age")')->index();
$table->string('gender')->storedAs('JSON_UNQUOTE(my_json_col->>"$.gender")')->index();
$table->string('nation')->storedAs('JSON_UNQUOTE(my_json_col->>"$.nation")')->index();
});
}
And this is equivalent to the following mysql statement:
create table my_table
(
id bigint unsigned auto_increment primary key,
my_json_col json null,
created_at timestamp null,
updated_at timestamp null,
age int unsigned as (json_unquote(json_unquote(json_extract(`my_json_col`, _utf8mb4'$.age')))) stored,
gender varchar(255) as (json_unquote(json_unquote(json_extract(`my_json_col`, _utf8mb4'$.gender')))) stored,
nation varchar(255) as (json_unquote(json_unquote(json_extract(`my_json_col`, _utf8mb4'$.nation')))) stored
)
collate = utf8mb4_unicode_ci;
create index my_table_age_index
on my_table (age);
create index my_table_gender_index
on my_table (gender);
create index my_table_nation_index
on my_table (nation);
And a simple view of the table after creation:
This example created actual stored columns, which for this scenario i think is what you would want. But you can also make virtual columns, which are created at query time instead of persistent columns, and you would just use the virtualAs function instead of the storedAs function in the migration.
These functions are documented in the Column Modifiers section of the Laravel migration docs, but it doesn't go into detail on JSON columns, this requires a bit more mysql knowledge.
I also found this article helpful for the mysql side of things for the JSON columns (SemiSQL).

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

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/

In hive, is there a way to specify between which columns to add a new column to?

I can do
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT)
to add a new column to the end of my non-partition columns and before my partition columns.
Is there any way to add a new column to anywhere among my non-partition columns?
For example, I would like to put this new column user_id as the first column of my table
Yes it is possible to change the location of columns but only after adding it in the table using CHANGE COLUMN
In your case, first add the column user_id to the table with below command:
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT);
Now to make user_id column as the first column in your table use change column with FIRST clause:
ALTER TABLE table_name CHANGE COLUMN user_id user_id BIGINT first;
This will move the user_id column to the first position.
Similarly you can use After instead of first if you want to move the specified column after any other column. Like say, I want to move dob column after user_id column. Then my command would be:
ALTER TABLE table_name CHANGE COLUMN dob dob date AFTER user_id;
Please note that this commands changes metadata only. If you are moving columns, the data must already match the new schema or you must change it to match by some other means.
Ah, here's the explanation for why you listed user_id twice (it's not a type):
// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is: b int, a2 string, c int.
No, it is not possible.
One solution is to create new table using "CREATE TABLE AS SELECT" approach and drop older one.

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?

ORA-01440: column to be modified must be empty to decrease precision or scale

I have an Oracle table called Products. It has a ID column with the type of NUMBER.
I'd like to change its type to Number(20, 0) but it's giving me this error:
ORA-01440: column to be modified must be empty to decrease precision or scale
So I've used this script:
alter table Products add ID_TEMP NUMBER(20,0);
update Products set ID_TEMP = ID;
update Products set ID = NULL;
alter table Products modify ID NUMBER(20,0);
update Products set ID = ID_TEMP;
alter table Products drop column ID_TEMP;
But it complains that
cannot update ID to NULL
which is reasonable as it's a not nullable primary key.
How to change its datatype from Number to Number(20, 0)?
check whether ID IS A PRIMARY KEY.
If yes , you cannot modify it to nullable or insert NULL value to it..
so its better to do the following after doing 'UPDATE ID_TEMP WITH VALUES OF ID'
DROP THE ID COLUMN, AND ITS PRIMARY KEY CONSTRAINT.
RENAME THE COLUMN ID_TEMP TO ID
Then set the ID column to primary key. Example below :
ALTER TABLE Products ADD COLUMN ID_TEMP NUMBER(20,0);
UPDATE PRODUCTS SET ID_TEMP = ID;
ALTER TABLE PRODUCTS DROP COLUMN ID;
ALTER TABLE PRODUCTS DROP CONSTRAINT ID_PK;
RENAME COLUMN PRODUCTS.ID_TEMP TO ID;
ALTER TABLE PRODUCTS ADD CONSTRAINT ID_PK PRIMARY KEY (ID);

Resources