Generated columns in YugabyteDB - yugabytedb

[Question posted by a user on YugabyteDB Community Slack]
I'm studying YB for a while and I faced a problem. I try to create Generated Columns but this feature is available in PostgreSQL 12 so I would like to know if YB has a plan to support this feature or if there is any solution that is similar to the feature.
This is a example statement.
CREATE TABLE animal (
id int NOT NULL GENERATED ALWAYS AS IDENTITY,
name text,
normalized_name text GENERATED ALWAYS AS (upper(name::text)) STORED
);

For the moment (we will move to PostgreSQL 13 compatibility soon) the GENERATED ALWAYS AS IDENTITY is supported but the one on (upper(name::text)) is not. Workarounds are:
a trigger to set the column value
a view to show the upper name on the query
Note that you don't need to store it in the table to get it indexed with:
create index animal_normalized_name on animal(upper(name::text));

Related

How do i implement friendship functionality in database using sequelize for social media demo app

I'm building a social media demo app and to add friends functionality to that app.
In database I am thinking about making new table(model in sequelize) which contains user1 id and user2 id and a status with one character (accepted, rejected etc).
I tried implementing it but faced some issue: Now i need to have unique key but for combination of user 1 and user 2 (pair of user1 and user2 id).
How do I implement something like this using sequelize and also I don't think this is the best way to do it, so if you have a better way please let me know, thanks.
Your description is somewhat lacking and missing some components. However it is clear enough to see what you are describing
is a many-to-many (m:m) relationship. It is just that the both sides come from the same table. While not the most common relationship,
neither is it too unusual. Further the resolution is the same as any other m:m. Create another table that has the key to both sides
as foreign keys. There is one slightly unusual twist in that the combination (parent1, parent2)(P1,P2) and the combination (P2,P1)
should be considered the same. This introduces a slight twist to the norm; instead of generating a PK from the the parent columns
the PK will be just be sequence generated (technically the PK is not required unless there are anticipated child tables of it).
We then create a unique index on the ordered pair (P1,P2). So something along the line of:
create table friends( f_id integer generated always as identity
, user_id_1 integer not null
, user_id_2 integer not null
, status varchar(1) not null default 'P'
, friends_since date not null default now()::date
, constraint friends_pk primary key(f_id)
, constraint f2user_1_fk foreign key(user_id_1)
references users(user_id)
, constraint f2user_2_fk foreign key(user_id_2)
references users(user_id)
, constraint cannot_friend_self
check (user_id_1 != user_id_2)
);
--
-- unique index to recognize (P1,P2) = (P2,P1)
create unique index already_friends on friends
( least(user_id_1,user_id_2), greatest(user_id_1,user_id_2) );
See fiddle here for examples. The bi-directional nature of Friends table causes additional complications within the queries. Typically I hide complication within SQL functions. I have also added a couple useful functions. You should be able to uses these as examples. Note, since they are SQL functions you can extract the statement and convert to a parameterized query.
Sorry, I do not know sequelize so I will not guess at the translation, but there seem to be many examples of the internet for all the above.

Cassandra create and load data atomicity

I have got a web service which is looking for the last create table
[name_YYYYMMddHHmmss]
I have a persister job that creates and loads a table (insert or bulk)
Is there something that hides a table until it is fully loaded ?
First, I have created a technical table, it works but I will need one by keyspace (using cassandraAuth). I don’t like this.
I was thinking about tags, but it doesn’t seem to exist.
- create a table with tag and modify or remove it when the table is loaded.
There is also the table comment option.
Any ideas?
Table comment is a good option. We use it for some service information about the table, e.g. table versions tracking.

Liferay wrong autoincrement

I have Liferay 6.2.
I have a portlet that use MySql database.
I have a table persons:
Id | Name | Info
Id is auto-increment, so in service.xml i have:
<column name="Id" type="long" primary="true" id-type="increment" />
I have a development machine.
I have 100 rows already available in the mysql table persons.
I entered information within the table persons via a sql query and I used the id starting from 300 up to 600.
I used a Liferay backend tool to update the database cache. When I enter a new row with the application (portlet), the id is 601. It's correct.
I have a production machine, I performed the same operation except for emptying the database cache because for some reason I can not do it.
When I insert a new row the with the portlet id is 101 and not 601.
When will I come to ** id ** 199 what will happen when I insert a new line?
What can I do to solve the problem?
Given that you see the ids on "hundreds", you might just see the default increment value for Liferay entities:
From portal.properties:
# Set the number of increments between database updates to the Counter table.
# this value to a higher number for better performance.
# Defaults:
counter.increment=100
You can test for this behavior by either creating 101 objects (where object #101 wou then get 700 as an id) or by shutting down the server that you used to create the #101 object and restart, then create a new object: The granularity of 100 would mean that the next 100 ids will be allocated and #700 will be picked next.
When will I come to ** id ** 199 what will happen when I insert a new line?
It's easy to find out...
What can I do to solve the problem?
Validate that there is a problem. There might be no need to solve anything
I have a development machine. I have 100 rows already available in the mysql table persons. I entered information within the table persons via a sql query and I used the id starting from 300 up to 600
It's a quite bad idea to do so when you otherwise rely on automatic id construction. Liferay's Service Builder can't rely on id generation in the database, as every database behaves slightly different in that regard.
Liferay's id generation - when it follows the mechanism that I've described above - is done through CounterLocalService. You can make calls to this service to advance the Ids for your entity (use your entity's class name as key) in case you do something manually to your database.
You should never change any Liferay entity on the database directly. If you have your own servicebuilder-generated entities, you might have a point, but my recommendation would be to choose one method of writing/accessing the data and stick with it.

cassandra support of where clause on non primay key column in materialized view

We are designing table in cassandra which has the following schema :-
customeruuid,assetuuid,activation_status,createdtime
PK(customeruuid,assetuuid)
We need to support searching usecase where we need to list all assetuuid for particular customer with where clause on activation_status column.There could be millions of assets for customer.I did research on web and found that we could create an materialized view over this table. I am trying it out using following cql:
CREATE MATERIALIZED VIEW myiew1 AS SELECT customeruuid,agentuuid,activation_status
FROM temp WHERE customeruuid IS NOT NULL AND
agentuuid IS NOT NULL PRIMARY KEY ( customeruuid, agentuuid );
I wanted to include where clause on activaiton_status column in select query but I am not able to do it. Does latest versions of cassandra supports such kind of operation or Do we have other alternative to solve this problem maybe by changing data modelling?

Which Hybris D.B. Table contain product description

I need to get the Product description manually from database, so please suggest which table it contains.
My Finding
Productslp table contains p_dscription column but that is clob datatype and I am unable to get the data from that.
I would suggest, you raise a support ticket with SAP Product support, since it may be a matter of correct Oracle JDBC driver.
We use HANA DB and had the same issue, they provided me with an updated driver and that resolved the issue for me.
Alternatively you can check the description in backoffice/hmc if that is an acceptable solution.
You can get using the SQL tab in hac with this query.
Select ps.p_description,ps.p_summary from products as p join productslp as ps ON p.pk=ps.itempk where pk=8796158722049
See the image below for result

Resources