473,883 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Joining tables on unknown table name

Can anyone please shed some light on the following...

I have a framework that uses dynamically created tables, named using an
incremental "attribute set ID", as follows:

attrdata_1
attrdata_2
attrdata_3
etc, etc...

I also have another table that stores data for each object within the
framework, called "object". The fields in this table are fixed so are always
known, one of which is "attrset_id " which relates to one of the
"attrdata_* **" tables.

The fields in the "attrdata_* **" tables are all unknown except for a fixed
"object_id" field that links objects with an entry in those tables.

The idea is that the "attrdata_* **" tables can be used to extend the data
for each object so, for example:

We need to extend Object 100 to store new properties "name", "address" and
"tel", so a new "attrdata_4 " table is created containing fields "object_id" ,
"name", "address" and "tel" and "object_id" is set to "100" (the object's
ID) and the "attrset_id " field in the "object" table is set to "4" (the
newly created attribute set's ID).

Now, when querying the DB I want to link the "object" table with the
relevant "attrdata_* **" table to get the complete extended data set, so
ideally this would be....

SELECT O.*, A.* FROM object AS O
LEFT JOIN CONCAT("attrdat a_", O.attrset_id) AS A ON O.id=A.object_i d
WHERE O.id=100

... but that doesn't work. What I need to know is if anything along those
lines exists? I've looked, but not found so I'm now making sure :) Although
it could be done using a couple of statements, I'd prefer, if possible, a
single statement solution.

Thanks in advance!

Sep 27 '05 #1
2 3516
>I have a framework that uses dynamically created tables, named using an
incremental "attribute set ID", as follows:

attrdata_1
attrdata_2
attrdata_3
etc, etc...
If you have to play games with table names like that, it would
usually be best to add a column to the table, containing the table
name you would have used (or part of it, like the number portion
of "attrdata_3 "), and use one table with a fixed name.
I also have another table that stores data for each object within the
framework, called "object". The fields in this table are fixed so are always
known, one of which is "attrset_id " which relates to one of the
"attrdata_** *" tables.

The fields in the "attrdata_* **" tables are all unknown except for a fixed
"object_id" field that links objects with an entry in those tables.

The idea is that the "attrdata_* **" tables can be used to extend the data
for each object so, for example:

We need to extend Object 100 to store new properties "name", "address" and
"tel", so a new "attrdata_4 " table is created containing fields "object_id" ,
"name", "address" and "tel" and "object_id" is set to "100" (the object's
ID) and the "attrset_id " field in the "object" table is set to "4" (the
newly created attribute set's ID).
Although it probably violates some principle of normalization, I
have on occasion set up a keyword=value set approach with a table
containing ID (of a person, usually ), attribute code (e.g. name,
home address, work address, home fax, work fax, date of birth, flag
indicating he's on my Christmas card list, etc.), and value (value
of the attribute indicated by the attribute code. This field is
unfortunately of rather ambiguous type, as it may contain dates,
strings, numbers, etc.). A given person might have up to a couple
dozen of these records. A whole "little black book" can be done in
one table, although I typically have another table with all of the
valid attribute codes, an explanation of what it is, and field
label to display, and what order to display them in.
Now, when querying the DB I want to link the "object" table with the
relevant "attrdata_* **" table to get the complete extended data set, so
ideally this would be....

SELECT O.*, A.* FROM object AS O
LEFT JOIN CONCAT("attrdat a_", O.attrset_id) AS A ON O.id=A.object_i d
WHERE O.id=100

.. but that doesn't work. What I need to know is if anything along those
lines exists?
I doubt it very much. Oracle even strongly discourages having a
variable table name supplied as a parameter that remains fixed for
the query, much less having it constructed from the fields in a
record you're joining with.
I've looked, but not found so I'm now making sure :) Although
it could be done using a couple of statements, I'd prefer, if possible, a
single statement solution.


Gordon L. Burditt
Sep 27 '05 #2
James wrote:
SELECT O.*, A.* FROM object AS O
LEFT JOIN CONCAT("attrdat a_", O.attrset_id) AS A ON O.id=A.object_i d
WHERE O.id=100


In SQL, object names are not strings. You can't use expressions or
parameters in place of table names or field names to make them dynamic
in the way you're doing. A join is between one specific table and one
other specific table, on every row of the join.

The only time object names are strings is when you're building up a SQL
statement as a string in your application code. You can do anything you
want here, concatenating partial strings, or substituting application
variables for table names, etc. But it has to be done before the SQL is
submitted as a statement.

You really should reconsider your schema design. Use a schema that can
represent your attributes without the need for creating new tables
dynamically.

For instance, there is a schema model called Entity-Attribute-Value
(EAV). Some relational purists don't like this model. Claims have been
made that it blurs the distinction between data and metadata, doesn't
scale well, lacks referential integrity, etc. Still, it is certain to
be a better solution than to create separate tables and then try to join
them together per row, as you're trying to do now.

See also:
http://classweb.gmu.edu/kersch/inft8...son/JAMIA5.pdf
http://www.georeferenceonline.com/Le...sociations.htm
http://ycmi.med.yale.edu/nadkarni/In...%20systems.htm

Regards,
Bill K.
Sep 27 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2060
by: mr_burns | last post by:
hi, i have two tables that i am joining using the following sql: SELECT * FROM clients, programmes WHERE clients.client_id = programmes.client_id AND programmes.client_id = 12 if both clients table and programmes table contain a column called date_added, how do i access them both seperately? i have tried the
2
8679
by: Amy | last post by:
This is what I want to do: 1. Delete all tables in database with table names that ends with a number. 2. Leave all other tables in tact. 3. Table names are unknown. 4. Numbers attached to table names are unknown. 5. Unknown number of tables in database. For example:
4
4299
by: Job Lot | last post by:
Is there anyway of Joining two or more DataTable with similar structure? I have three DataTables with following structures Data, AmountB/F, Repayments, InterestCharged and AmountC/F i want to join these tables on the basis of Date. I don't want three rows for each date, i want the values to be added for similar dates. thanx
2
1253
by: airkart | last post by:
Hello, I've scoured groups and the web, and haven't found a question like mine answered. I'm using Visual Studio 2003 with the Crystal Reports it comes bundled with and SQL Server 2000. Throughout the project I'm working on, I often find I need a temporary table displayed in a report, something that I cannot seem to do. Since temporary tables are so useful, I cannot imagine Crystal Reports wouldn't have a way to get such
3
4479
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: select * from table 1, table 2, table 3 where table 1.com_field = table 2.com_field and table 2.com_field = table 3.com_field
1
6446
by: Steve C | last post by:
Hi, I'm having problems constructing a nested join. It's quite complex, so here's a simplfied example of the problem. Any thoughts on what I'm doig wrong - or if I've got the whole approach wrong are welcome. I've two tables :- one is a contact table contacting name, addresses etc. Three of the fields represent users - 'created by', 'last modified by' and 'owner'.
3
9304
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both fields in design view and the Select statement in SQL view looks good. The query runs perfectly and shows the result I want but when I save the query and close it, re-opening in design view shows the error message "Microsoft Office Access can't
1
2048
by: buzzw | last post by:
Hi I need to join tables to get employee data records. document.xml is ------------------------------------------ <document name="company"> <table name="A"> <r>
0
1721
bilibytes
by: bilibytes | last post by:
Hi there, I am wondering if there is a possibility to declare a Model_DbTable_Table1JoinedToTable2ToTable3... class It would be convenient to create compound table from several tables joined from a foreign key. Lets say we have: tables: -------------------------------------------
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10745
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10848
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10417
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.