Teradata - Column datatypes not copied for views - teradata

I am trying to create view from table as below
CREATE TABLE tb
(
a INTEGER,
b INTEGER
)
CREATE VIEW vtb AS
(SELECT *
FROM tb);
And then when I executed the following command the type of the column is null
help view vtb;
Is there a way to copy the column datatypes while creating the view ?

Related

How to add existing table column in existing projection segmented by hash clause in vertica db?

I have created one table and have one projection of that table. I have to add existing table column in existing projection segmented by hash clause in vertica db.
"I have to add SBS_ALERT_ID column in existing projection segmented by hash clause without creating new projection."
CREATE TABLE public.ALERT
(
AS_OF_DATE date,
ALERT_ID int,
LOAN_NUMBER varchar(20),
SERVICER_LOAN_NUMBER varchar(20),
SBS_LOAN_NUMBER varchar(20),
SBS_ALERT_ID int,
ALERT_TYPE_ID varchar(25),
);
CREATE PROJECTION public.ALERTTT_SEG /*+createtype(D)*/
(
AS_OF_DATE ENCODING RLE,
ALERT_ID ENCODING DELTARANGE_COMP,
LOAN_NUMBER ENCODING ZSTD_FAST_COMP,
SERVICER_LOAN_NUMBER,
SBS_LOAN_NUMBER ENCODING RLE,
SBS_ALERT_ID ENCODING DELTARANGE_COMP,
ALERT_TYPE_ID,
)
AS
SELECT ALERT.AS_OF_DATE,
ALERT.ALERT_ID,
ALERT.LOAN_NUMBER,
ALERT.SERVICER_LOAN_NUMBER,
ALERT.SBS_LOAN_NUMBER,
ALERT.SBS_ALERT_ID,
ALERT.ALERT_TYPE_ID,
FROM public.ALERT
ORDER BY ALERT.LOAN_NUMBER,
ALERT.SBS_LOAN_NUMBER
SEGMENTED BY hash(ALERT.LOAN_NUMBER, ALERT.SBS_LOAN_NUMBER) ALL NODES;
Do you mean something like this?
Initial situation:
CREATE TABLE segby (
fullname varchar(12),
dob date
);
CREATE PROJECTION segby_super /*+basename(segby),createtype(A)*/ (
fullname,
dob
)
AS
SELECT segby.fullname,
segby.dob
FROM segby
ORDER BY segby.fullname,
segby.dob
SEGMENTED BY hash(segby.dob, segby.fullname) ALL NODES OFFSET 0;
Then, you add a column and initialise it with a DEFAULT ...
ALTER TABLE segby ADD id INT NOT NULL DEFAULT HASH(dob,fullname);
And subsequently, you make sure the table is segmented by that new column, by creating such a projection:
CREATE PROJECTION segby_id
AS SELECT
id
, fullname
, dob
FROM segby
ORDER BY id
SEGMENTED BY HASH(id) ALL NODES;
And finish by running a REFRESH on your table ...
SELECT REFRESH('segby');
-- out REFRESH
-- out -
-- out Refresh completed with the following outcomes:
-- out Projection Name: [Anchor Table] [Status] [Refresh Method] [Error Count] [Duration (sec)]
-- out ----------------------------------------------------------------------------------------
-- out "dbadmin"."segby_id": [segby] [refreshed] [scratch] [0] [0]
For your specific question: It looks as if you would like to be able to fire a command like:
ALTER PROJECTION public.ALERTTT_SEG
SEGMENTED BY hash(ALERT.LOAN_NUMBER, ALERT.SBS_LOAN_NUMBER,ALERT.SBS_ALERT_ID) ALL NODES;
This simply does not work.
You will have to create a new projection, segmented the new way, refresh the table, and drop the original projection

Why view with column alias is not insertable?

CREATE TABLE `projects` (
`stars` integer,
`title` varchar(255)
);
CREATE VIEW projects_view AS SELECT *, title AS name FROM projects;
When I try to insert something:
INSERT INTO `projects_view` (`name`) VALUES ('Name');
I get: ERROR 1471 (HY000): The target table projects_view of the INSERT is not insertable-into
But this works in PostgreSQL.
You cannot insert because by using select * you have two references in the view to a single column.
From the documentation a view is not insertable where
Multiple references to any column of a base table (fails for INSERT, okay for UPDATE, DELETE)
It should work if you name each column:
CREATE VIEW projects_view AS
SELECT stars, title as name FROM projects;
Fiddle example

How to create multiple vertex in SAP HANA Graph

I'm trying to create 2 (multiple) vertex in SAP HANA like -
Create two table for vertex ITEM and DATASET
CREATE COLUMN TABLE "GREEK_MYTHOLOGY"."ITEM" (
"ITEM_ID" VARCHAR(100) PRIMARY KEY,
"ITEM_NAME" VARCHAR(100)
);
CREATE COLUMN TABLE "GREEK_MYTHOLOGY"."DATASET" (
"DATASET_ID" VARCHAR(100) PRIMARY KEY,
"DATASET_NAME" VARCHAR(100)
);
And creating edge as REFERENCES
CREATE COLUMN TABLE "GREEK_MYTHOLOGY"."REFERENCES" (
"REF_ID" INT UNIQUE NOT NULL,
"SOURCE" VARCHAR(100) NOT NULL
REFERENCES "GREEK_MYTHOLOGY"."ITEM" ("ITEM_ID")
ON UPDATE CASCADE ON DELETE CASCADE,
"TARGET" VARCHAR(100) NOT NULL
REFERENCES "GREEK_MYTHOLOGY"."DATASET" ("DATASET_ID")
ON UPDATE CASCADE ON DELETE CASCADE,
"TYPE" VARCHAR(100)
);
Now I would like to connect both vertex (ITEM and DATASET) with edge REFERENCES like below
CREATE GRAPH WORKSPACE "GREEK_MYTHOLOGY"."GRAPH"
EDGE TABLE "GREEK_MYTHOLOGY"."DATASET"
SOURCE COLUMN "SOURCE"
TARGET COLUMN "TARGET"
VERTEX TABLE "GREEK_MYTHOLOGY"."ITEM" KEY COLUMN "ITEM_ID"
VERTEX TABLE "GREEK_MYTHOLOGY"."DATASET"KEY COLUMN "DATASET_ID"
KEY COLUMN "REF_ID";
But it throws this exception at line VERTEX TABLE "GREEK_MYTHOLOGY"."DATASET"KEY COLUMN "DATASET_ID":
sql syntax error: incorrect syntax near "VERTEX": line 6 col 1 (at pos 200)
Is it possible to create multiple vertex in SAP HANA graph ? If yes then what is the right way to do this.
There's a misunderstanding here. The REFERENCES clause in the CREATE TABLE statement has nothing to do with the graph structure you want to represent.
Instead, it defines a foreign key constraint between the two tables.
The CREATE GRAPH WORKSPACE command only accepts one EDGE TABLE and one VERTEX TABLE as parameters.
However, you can also pass in synonyms or views here.
That way, you could create a view "ALL_ITEMS" like this:
CREATE VIEW "GREEK_MYTHOLOGY"."ALL_ITEMS" as
SELECT "ITEM_ID" as "ID", "ITEM_NAME" as "NAME" FROM "GREEK_MYTHOLOGY"."ITEM"
UNION
SELECT "DATASET_ID" as "ID", "DATASET_NAME" as "NAME" FROM "GREEK_MYTHOLOGY"."DATASET";
and then reference this view:
CREATE GRAPH WORKSPACE "GREEK_MYTHOLOGY"."GRAPH"
EDGE TABLE "GREEK_MYTHOLOGY"."DATASET"
SOURCE COLUMN "SOURCE"
TARGET COLUMN "TARGET"
VERTEX TABLE "GREEK_MYTHOLOGY"."ALL_ITEMS"
KEY COLUMN "NAME";
Using this approach is possible, but you now have to make sure that the "NAME" values are unique and not NULL across both tables.

SQLite syntax to create index on a temp table?

The syntax to create a table is
CREATE [temp] TABLE TableName(...)
so table can be created in the temp database using
CREATE temp TABLE MyTable(...)
or
CREATE TABLE temp.MyTable(...)
and the fully resolved way to refer to that table would be
temp.Mytable
which would be a different table to main.MyTable.
However, the syntax to create an index is just
CREATE INDEX IndexName ON TableName (FieldName )
which does not allow the table name to have the database name prepended
so this is OK
CREATE INDEX MyIndex ON MyTable (MyField)
but this is not
CREATE INDEX MyIndex ON Temp.MyTable (MyField)
How then can I create an index on a temp table (especially if there might be table of the same name in the main database?
The database name must be put on the index name:
CREATE INDEX temp.MyIndex ON MyTable(MyField)

using sqlite create virtual table using fts3 with unique column dosnot stay unique

im creating (in Qt )
QString q = "CREATE VIRTUAL TABLE playlist USING fts3 ("
"from_user , "
"from_id , "
"created_time , "
"created_time_formated , "
"user_id unique )";
then execute
"INSERT OR IGNORE INTO play_list (from_user,from_id,created_time,created_time_formated,user_id) SELECT '....','....','.....','....','123'"
im using full Full-Text Search so this is way im using VIRTUAL TABLE
if i have another row with user_id == 123 it will still insert the row , why ?
The FTS documentation states that:
The same applies to any constraints specified along with an FTS column name - they are parsed but not used or recorded by the system in any way.
So the UNIQUE constraint is ignored.
You can emulate the constraint by using a trigger on a view (not on the table because triggers are not available on virtual tables), and then by inserting the row on that view:
CREATE VIEW playlist_view AS SELECT * FROM playlist;
CREATE TRIGGER insert_playlist INSTEAD OF INSERT ON playlist_view
BEGIN
SELECT RAISE(ABORT, 'column user_id is not unique') FROM playlist
WHERE user_id=new.user_id;
INSERT INTO playlist (from_user, from_id, created_time,
created_time_formated, user_id)
VALUES (NEW.from_user, NEW.from_id, NEW.created_time,
NEW.created_time_formated, NEW.user_id);
END;
-- And you do the insertion on the view
INSERT INTO playlist_view (from_user,from_id,created_time,created_time_formated,user_id) SELECT ...;
Because OR IGNORE won't ignore an explicit RAISE, if you want to ignore the error, you have to replace RAISE(ABORT,...) by RAISE(IGNORE).

Resources