Home » SQL & PL/SQL » SQL & PL/SQL » create dynamic view based on metadata (Oracle 11gR2/12c)
create dynamic view based on metadata [message #634850] Mon, 16 March 2015 18:23 Go to next message
ravikanth_b
Messages: 42
Registered: November 2007
Location: Bay Area, CA
Member
Hello Experts,

Need your valuable advise with this problem.

Description.

We have a base table with fixed columns.
We have a metadata table which holds metadata for additional columns for this base table.
We have a table which holds key-value pair data for the additional columns.

Need to create a view listing all the fixed columns plus 'additional columns' that are defined in metadata table.

Following is an example with data structures and expected results.

Actual problem is much more complicated than this but i tried to present it as simple as possible.

DROP TABLE EMPLOYEE;

--base table with fixed columns
CREATE TABLE EMPLOYEE
(EMPLOYEE_ID NUMBER,
 NAME        VARCHAR2(100),
 DEPTNO      NUMBER,
 SAL         NUMBER
 );
 
 insert into EMPLOYEE VALUES(10,'BLAKE',500,1000);
 insert into EMPLOYEE VALUES(20,'KING',500,2000);
 insert into EMPLOYEE VALUES(30,'ROGER',300,3000);
 
 commit;
 
 select * from EMPLOYEE;

DROP TABLE TABS_METADATA;

--metadata table which as definition to additional columns
CREATE TABLE TABS_METADATA
 (
  TABS_METADATA_ID            NUMBER(18,0) CONSTRAINT PK_TABS_METADATA PRIMARY KEY,
  OWNER                       NVARCHAR2(30),
  TABLE_NAME                  NVARCHAR2(30),
  COLUMN_NAME                 NVARCHAR2(30),
  COLUMN_LABEL                NVARCHAR2(30),
  DATA_TYPE                   NVARCHAR2(106),
  DATA_LENGTH                 NUMBER(18,0),
  DATA_PRECISION              NUMBER(18,0),
  NULLABLE                    NVARCHAR2(1),
  DATA_DEFAULT                NVARCHAR2(100),
  COLUMN_ORDER                NUMBER(18,0)
 );
 
insert into TABS_METADATA VALUES(123,'SCOTT','EMPLOYEE','ADDRESS1','Address1','VARCHAR2',1000,NULL,'Y',NULL,1);
insert into TABS_METADATA VALUES(456,'SCOTT','EMPLOYEE','ADDRESS2','Address2','VARCHAR2',1000,NULL,'Y',NULL,2);

commit;

SELECT * FROM TABS_METADATA;

DROP TABLE TABS_ATTR_DATA;

-- this table has key valaue pair
-- TABS_ATTR_DATA_ID => primarky key
-- UUID => in this example EMPLOYEE_ID from EMPLOYEE table
-- ATTRIBUTE_VALUE => actual value for the attribute
CREATE TABLE TABS_ATTR_DATA
(TABS_ATTR_DATA_ID  NUMBER CONSTRAINT PK_TABS_ATTR_DATA PRIMARY KEY,
 TABS_METADATA_ID   NUMBER,
 UUID               NUMBER,
 DATA_TYPE          VARCHAR2(10),
 ATTRIBUTE_VALUE    VARCHAR(255),
 CONSTRAINT FK_TABS_ATTR_DATA FOREIGN KEY (TABS_METADATA_ID)REFERENCES TABS_METADATA(TABS_METADATA_ID)
 );
 
insert into TABS_ATTR_DATA  values (1,123,20,'VARCHAR2','999 Abc Ave');
insert into TABS_ATTR_DATA  values (2,456,20,'VARCHAR2','CA, 12345');

commit;

select * from TABS_ATTR_DATA;

-- expected output from view
EMPLOYEE_ID NAME  DEPTNO  SAL ADDRESS1  ADDRESS2

10  BLAKE 500 1000  NULL  NULL
20  KING  500 2000  999 Abc Ave  CA, 12345
30  ROGER 300 3000  NULL  NULL
Re: create dynamic view based on metadata [message #634851 is a reply to message #634850] Mon, 16 March 2015 18:26 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Garbage In, Garbage Out.

Normalize the data & stop this nonsense before it gets worse.
Re: create dynamic view based on metadata [message #634852 is a reply to message #634851] Mon, 16 March 2015 18:31 Go to previous messageGo to next message
ravikanth_b
Messages: 42
Registered: November 2007
Location: Bay Area, CA
Member
Sorry. Did not understand your concern. What is garbage here?
Re: create dynamic view based on metadata [message #634853 is a reply to message #634852] Mon, 16 March 2015 18:33 Go to previous messageGo to next message
ravikanth_b
Messages: 42
Registered: November 2007
Location: Bay Area, CA
Member
This is a good candidate for schema less solution like mongoDB but I do not have an option at this point and had to work with Relational model.
Re: create dynamic view based on metadata [message #634854 is a reply to message #634852] Mon, 16 March 2015 18:36 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>What is garbage here?
this whole "design"

Please post URL to any database design book that advocates using what is posted above?
I suspect some self proclaimed "data architect" was tripping on acid when he first came up with this Rube Goldberg contraption.
Key/Value pair implementations scale as well as my goat can fly.
Re: create dynamic view based on metadata [message #634855 is a reply to message #634854] Mon, 16 March 2015 18:39 Go to previous messageGo to next message
ravikanth_b
Messages: 42
Registered: November 2007
Location: Bay Area, CA
Member
Sounding "intellectual" is not one of my traits. I strongly believe it is due to lack of substance and in-built insecurities. Try somewhere else.
Re: create dynamic view based on metadata [message #634856 is a reply to message #634855] Mon, 16 March 2015 19:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/m/626254/#msg_626254

so what did you implement
Re: create dynamic view based on metadata [message #634857 is a reply to message #634856] Mon, 16 March 2015 19:11 Go to previous message
ravikanth_b
Messages: 42
Registered: November 2007
Location: Bay Area, CA
Member
All in single schema. Facing hell lot of problems as I warned. concurrency issues, locking issues, data inconsistencies, security issues and much more. All these are more amplified in SQL Server.

Someone sent me this reply (copy-and-paste) when asked for suggestion Smile


When we build a house we divide it into different areas. To mention a few...

1. Living room
2. Bed room
3. Bath room
4. Kitchen

What if we don't have any of this and have one big room. You end up spending less money and time on
the construction.

Sit back, put your entire family in a house which does not have any of these partitions and then
write down the consequences.

The consequences could be alarming.
Previous Topic: rank
Next Topic: OCI8 ERROR
Goto Forum:
  


Current Time: Fri Apr 19 05:01:24 CDT 2024