how we define auto increment id in riak ts - riak-ts

How we create table in Riak TS for auto_increment id such as in mysql we create:::as example---
create table Department(id int auto_increment not null,departmentName varchar(9));
is there any way to create table with AUTO_INCREMENT
CAN ANYONE TELL ME ?

Riak TS (as of version 1.4) does not feature the ability to create an auto incrementing ID. I highly recommend reading the Riak TS documents for more information. The following page will give you a good introduction to creating tables and the supported data types: http://docs.basho.com/riak/ts/latest/using/planning/

Related

Add auto increment with labeled primary key to existing table in SQL Server and H2 / Liquibase

I am currently using liquibase to apply changes to H2 and SQL Server.
Goal:
add a column id with auto increment to an existing table
add a primary key constraint with constraint name to id
Liquibase auto increment does not support SQL Server so I need to find a solution in SQL.
What I want is something like this:
<sql>
ALTER TABLE user ADD COLUMN id INT NOT NULL AUTO_INCREMENT;
</sql>
<addPrimaryKey columnNames="id" constraintName="userPK" tableName="user"/>
This statement is only working in H2 but not in SQL Server.
For SQL Server, IDENTITY seems to be the command to apply auto increment so I tried to write another statement that uses IDENTITY instead.
The problem with IDENTITY is that it automatically creates a primary key when using H2 which is not what I want because I would have to somehow find and rename it. SQL Server does not automatically create this primary key when using IDENTITY.
It is important to control the name of the primary key and that the name is the same for both databases.
I would appreciate any help.
Edit:
Another attempt was to split the sql commands:
<sql dbms="h2">
ALTER TABLE user ADD ID INT auto_increment;
</sql>
<sql dbms="mssql">
ALTER TABLE user ADD id INT IDENTITY(1, 1);
</sql>
<addPrimaryKey columnNames="id" constraintName="userPK" tableName="user"/>
H2 however ignores the constraintName in addPrimaryKey and instead generates another name. I wanted the names to be the same but I noticed that I can still use dropPrimaryKey to drop it.
Unfortunately, H2 1.4.200 doesn't yet emulate non-standard IDENTITY clause from SQL Server well enough and SQL Server doesn't support standard identity columns (ID BIGINT GENERATED BY DEFAULT AS IDENTITY etc.) supported by H2 and various other DBMS.
You need to compile H2 from its current sources available on GitHub:
https://github.com/h2database/h2database
There are build.cmd and build.sh scripts in h2 subdirectory. You need to launch a proper script for you OS with jar argument (build jar or ./build.sh jar). Use Java 8 or 11 (compiled jar will be compatible with more recent versions too).
With the compiled snapshot version you will be able to use
CREATE TABLE TEST1(ID BIGINT IDENTITY, V INT);
CREATE TABLE TEST2(V INT);
ALTER TABLE TEST2 ADD ID BIGINT IDENTITY;
and similar commands in MSSQLServer compatibility mode (append ;MODE=MSSQLServer to JDBC URL) and these commands will not create unexpected primary key constraints.

How to tell HSQLDB to allow identity definition for SERIAL?

I'm currently writing tests for a spring boot application which is using a postgreSQL database. During test I want to replace the database by some in-memory variant like H2 or HSQLDB. Sadly both do not behave the same as the postgreSQL database.
I have migrations that look like
CREATE TABLE foo(id BIGSERIAL PRIMARY KEY, ...)
This results in hsqldb telling me
SQL State : 42525
Error Code : -5525
Message : identity definition not allowed: FOO_ID
So apparently creating the matching sequence for the primary key is forbidden. Is there a way to tell hsqldb to accept this?
You need to set PostgreSQL compatibility mode in HSQLDB.
SET DATABASE SQL SYNTAX PGS TRUE
Your table definition is then accepted and converted internally to the SQL Standard equivalent.
CREATE TABLE FOO(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY, ..

Jetbrains Datagrip 2017.1.3, force columns exported when dumping data to sql inserts file

I have an SQL server database with a lot of tables and data. I need to reproduce it locally in a docker container.
I have successfully exported the schema and reproduced it. When I dump data to an SQL file, it does not export automatically generated fields (Like ids or uuids for example)
Here is the schema for the user table:
create table user (
id_user bigint identity constraint PK_user primary key,
uuid uniqueidentifier default newsequentialid() not null,
id_salarie bigint constraint FK_user_salarie references salarie,
date_creation datetime,
login nvarchar(100)
)
When it exports and element from this table, I get this kind of insert:
INSERT INTO user(id_salarie, date_creation, login) VALUES (1, null, "example")
As a consequence, most of my inserts give me foreign key errors, because the ids generated by my new database are not the same as the ones in the old database. I can't change everything manually as there is way too much data.
Instead, I would like to have this kind of insert:
INSERT INTO user(id_user, uuid, id_salarie, date_creation, login) VALUES (1, 1, "manuallyentereduuid" null, "example")
Is there any way to do this with Datagrid directly? Or maybe a specific SQL server way of generating insert statements this way?
Don't hesitate to ask for more details in comments.
You need the option 'Skip generated columns' while configuring INSERT extractor.
It seems like Datagrip does not give you that possibility so I used something else : DBeaver. It is free and based on the Eclipse Environment.
The method is simple :
Select all the tables you want to export
Right click -> Export table data
From there you just have to follow the instructions. It outputs one file per table, which is a good thing if you have a consequent volumetry. I had trouble executing the whole script and had to split it when using Datagrip.
Hope this helps anyone encountering the same problem. If you find the solution directly in datagrip, I would like to know too.
EDIT : See the answer above

How to create an Index in Amazon Redshift

I'm trying to create indexes in Amazon Redshift but I received an error
create index on session_log(UserId);
UserId is an integer field.
If you try and create an index (with a name) on a Redshift table:
create index IX1 on "SomeTable"("UserId");
You'll receive the error
An error occurred when executing the SQL command:
create index IX1 on "SomeTable"("UserId")
ERROR: SQL command "create index IX1 on "SomeTable"("UserId")" not supported on Redshift tables.
This is because, like other data warehouses, Redshift uses columnar storage, and as a result, many of the indexing techniques (like adding non-clustered indexes) used in other RDBMS aren't applicable.
You do however have the option of providing a single sort key per table, and you can also influence performance with a distribution key for sharding your data, and selecting appropriate compression encodings for each column to minimize storage and I/O overheads.
For example, in your case, you may elect to use UserId as a sort key:
create table if not exists "SomeTable"
(
"UserId" int,
"Name" text
)
sortkey("UserId");
You might want to read a few primers like these
You can Define Constraints but will be informational only, as Amazon says: they are not enforced by Amazon Redshift. Nonetheless, primary keys and foreign keys are used as planning hints and they should be declared if your ETL process or some other process in your application enforces their integrity.
Some services like pipelines with insert mode (REPLACE_EXISTING) will need a primary key defined in your table.
For other performance purposes the Stuart's response is correct.
Redshift allow to create primary key
create table user (
id int ,
phone_number int,
primary key(id))
but since Redshift does not enforce this constraints, primary key accepts duplicate values.
attached article on that issue
http://www.sqlhaven.com/amazon-redshift-what-you-need-to-think-before-defining-primary-key/

Standardized way of AUTO_INCREMENT

Is there a standardized way I can create a table in SQL with a column (lets call it ID) that is auto incremental so that I can basically use it in all databases?
(e.g. standardized in SQL-92)
If so - how? If not, why? I think auto_increment is a very often used property so I thought it would be very important to standardize it…
Nope, sorry. There is AUTO_INCREMENT in MySQL, but e.g. in MS SQL this is called IDENTITY and SERIAL in PGSQL. Many things are not really standardized in SQL - and most are in the schema creating area.
It's a mess, but you can use stuff like e.g. Hibernate/NHibernate to try to use a single code base.
Update: Few year later there is a more standard way that some DBMS support (e.g. PG SQL from version 10.0, so from October 2017):
GENERATED BY DEFAULT AS IDENTITY -- the value has a default auto incrementation, but you can insert your own.
GENERATED ALWAYS AS IDENTITY -- forbids inserting own values (in a standard query, might be overriden)
This is something that should work in PG SQL 10+, DB2, Oracle:
DROP TABLE IF EXISTS simple_test;
CREATE TABLE simple_test(
s_id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
);
Note however that this will not work in Microsoft SQL Server (not even in MS SQL Server 2022). MSSQL does not support the generated keyword. MySQL/MariaDb has generated columns, but MariaDb does not support the identity syntax.
So yeah, 10 years later the answer is kind of the same really -- it is still a mess and you should probably use a framework for that.
You can use so-called identity columns:
CREATE TABLE foo(id int GENERATED ALWAYS AS IDENTITY);
This is in the SQL standard and should be supported by PostgreSQL 10 and Oracle:
https://www.2ndquadrant.com/en/blog/postgresql-10-identity-columns/#comment-248607
In Oracle you need to create a SEQUENCE
SQLite uses it for rowid and a synonym of it e.g. RowIdSyn INTEGER PRIMARY KEY AUTOINCREMENT