How to migrate tables with consistent foreign keys in Pentaho Kettle? - etl

I have csv file with data
name, state
.
I want to put those data to tables:
Table1{id[PK], name}
State{table1_id[FK], state}.
Main problem here is to how store newly created Table1 id and use it during inserting to State table
How to do this with pentaho?

For Table1 (in your example it is dimension) use 'Combination lookup/update' from 'Data Warehouse' tab:
Dimension field=name
Field in stream=name
Technical key field=id
Connect 'Combination lookup/update' to 'Insert / Update'. Use 'Insert / Update' for State table (in your example it is fact table)
the key(s) to look up the value(s): state=state, table1_id=id;
Update fields: state=state, Table1_id=id, all with Y flag for update
'Combination lookup/update' check if given Field exists in Dimension table if yes then you have id from database if not then new value will be inserted and new key returned.
Regards
Mateusz

Related

Updating unique id column for newly added records in table in hive

I have a table in which I want unique identifier to be added automatically as a new record is inserted into it. Considering I have column for unique identifier already created.
hive can't update the table but you can create a temporary table or overwrite your first table.
you can also use concat function to join the two diferent column or string.
here is the examples
function :concat(string A, string B…)
return: string
hive> select concat(‘abc’,'def’,'gh’) from dual;
abcdefgh
HQL &result
insert overwrite table stock select tradedate,concat('aa',tradetime),stockid ,buyprice,buysize ,sellprice,sellsize from stock;
20130726 aa094251 204001 6.6 152000 6.605 100
20130726 aa094106 204001 6.45 13400 6.46 100

Add a column, with a default value, to an existing table in oracle

I created a table named- books and have a column in that by the title 'color' . Initially I have null values in the column 'color'. Now, when I run the following query :
alter table books modify color default 'blue';
schema is formed but on doing select *from books , all the values in column color are still null. What would be the correct query to fire?
here is the link:
http://sqlfiddle.com/#!4/f4210/1
Of course. Alter table just changes the table structure but not the content. New entries will get the default.
To update the existing values run a sql-update query like:
update books set color='blue' where colore is null;
If you now inserting into table then only will come with default values. This statement don't know about previous contents of this table. In non technical language, you are telling oracle to do so now on-wards. This statement will not perform check to old values.
alter is ok for the next values to be inserted: try to insert lines without specifying a value for column color, value should be blue.
But this does not work for existing values, for which you just need an update:
update books set color = 'blue';
Hi this query will be used to add column with default value in existing table in oracle.
alter table <table_name> add <column_name> <contraint> default <default_value> not null;
example:
alter table books add record_status number(1,0) default 1 not null;
alter table books add color varchar(20) default 'blue' not null;

SSAS - Creating named calculation from different tables

I am having troubles when i want to create a named calculation from two different tables.
I have the table "CallesDim" with an id(PK) and a description and the table "UbicacionesDim" with an id (PK), another id (FK to "CallesDim") and a description:
--
CallesDim
id PK
Descripcion VARCHAR
--
UbicacionesDim
id PK
CalleId FK to id from CallesDIM
Altura INT
--
I want to concatenate "Descripcion" from "CallesDim" with Altura from "UbicacionesDim".
I try doing this:
CallesDim.Descripcion + ' ' + CONVERT(VARCHAR,UbicacionesDim.Altura)
but i am having the following error:
the multi-part identifier "CallesDim.Descripcion" could not be bound
Any ideas?
Thanks!
In a named calculation you can only access columns from the table that it is defined on.
Which record of the other table should it take in case it would accept columns from other tables? How should it join? All this cannot be configured.
If you need to join two (or more) tables, you can define a named query that can contain joins and access as many tables as you like. A named query can contain everything that you can state in a single select statement.

Copy entire values of 2 columns from one table to another ensuring the relationship

I have a table STUDENT with columns st_id,name,age,dept_name. Now I want to create a new table STUDENT_DESC with columns st_id,dept_name,st_desc. So I need to copy all the values of st_id and dept_name to the newly created table STUDENT_DESC. I need to ensure relationship while copying st_id and dept_name , the dept_name should be corresponding to st_id.So how can I do it in PL/SQL?
insert into STUDENT_DESC (select st_id, dept_name, null from student);
this will simply copy all the records. The third column st_desc is left empty (null)
To ensure referential integrity you would add a primary key and a referential integrity constraint to the STUDENT_DESC table
However, note that in many cases it could be "wrong" to introduce a second table containing student data like that. It could be "better" to add st_desc to the STUDENT table.
I'm not sure I understand your data model, but at face value you can create your table simply:
CREATE TABLE student_desc AS SELECT st_id, dept_name FROM student;
ALTER TABLE student_desc ADD (st_desc VARCHAR2(..));
Fill in the .. with the desired max size for st_desc.

Create after insert trigger which updates record by getting value from other Oracle 11g table

I have two tables
Parent(id, name, occupation)
Child(id, name, gender,parent_id, parent_name, parent_occupation)
Now to insert value in child i'll run chi query
insert into Child(id,name,gender,parent_id) values(10,'XYZ','Male',15);
So now my requirement is when this insert query is executed a trigger will run and get name and occupation from parent table for id 15 (parent_id of the Child record) and add it to the newly inserted row in fields parent_name and parent_occupation respectively.
I am using Oracle 11g as my database.
You want something akin to this (though you'll need to add code to handle the exception):
CREATE OR REPLACE
TRIGGER ai_child_tg
AFTER INSERT ON child
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
SELECT name,
occupation
INTO :NEW.parent_name,
:NEW.parent_occupation
FROM parent
WHERE id = :NEW.parent_id;
EXCEPTION
WHEN no_data_found
THEN
<handle_your_exception_>
END ai_child_tg;
However, if your CHILD table is really a relational child to your PARENT table and there is a FK relationship in place (via the CHILD.PARENT_ID column) then storing the PARENT_NAME and PARENT_OCCUPATION columns in the CHILD table is logically redundant.
I'd query why you have those two columns in the CHILD table at all.
Hope it helps...

Resources