SQL - Creating a table with the name as a result of a query - sql

Is it possible to create a table in SQL with the following requirements.
I have a table X with two cols, (primary key, value)
Every time, I add a new value to the above table, I create a new table with the name value.
So if there are 10 rows in table X, then we need to have 10 tables with names in the values field.
Let me try to explain the situation.
I have a category A and I need to define it.
The subsets of A can include {a1, a2, a3, ..... until infinity}
All the subsets have a different definition but are uniquely identified a property B.
B can be directly A, or a subset A with the definition holding true. I am not sure how to design it. I am using postgresql
So I am taking cues from a_horse_with_no_name and think that this can be a table structure.
Table A
<table border="1">
<td> primary_key </td>
<td> B1 </td>
<td> B2 </td>
<td> hstore - attribute </td>
</table>
Now what store should act like is similar to a tree.
hstore - attribute -> { a1 : {c1,c2,c3...}, a2 :{d1,d2,d3..}, .... }
C and D are structures here with multiple or single values. So if I am to use an hstore, is it possible to use something like a 2d array or something similar. It seems that it is possible from the following link.
http://www.postgresql.org/docs/9.0/static/hstore.html
I think this is certainly a better approach. Can you please check and comment.
and so on..

No, you can not do this. And should not.
edit: So, if you want subsets of a super set where all items are similar, you should create a column called 'parent_id' and reference the super set in that column.

Sounds like a bad idea, but yes, you can do that. Might need to get a bit creative to do it depending on what SQL you are using. You will most likely have to make a stored procedure, that creates a new table with it taking a name parameter. Then you can create a trigger on insert to call stored procedure, so every time a new value is inserted a table is created.

this practice is not nice to me. I think you should try another way solve your problem.

Related

Datatabel Search (or filter?) by index and redraw

i know how to search for a value programmatically and redraw the DataTable so that it shows only rows containing the given value.
I do that like so:
this.api().search('8000').draw();
But now i have the need to do the same but not use a value this time, but show only the row with the given id.
Each row in the DataTable has an id like so:
<tr id="198">
<td>8000</td><td>turnover</td>
</tr>
I know the index of the row with id 198 via:
var rowIndex = this.api().row('#198');
But how to get the DataTable to only show the row with that index?
Via Google i saw people that would place the id in a separate column and use search for that, but i think there must be a more efficient and better way as every row already has an id and i know the index of the row that i want to show.
Maybe it's simple but I googled a long time, tried lots but none of 'em worked.
Any help or pointers are greatly appreciated, many thanks.

Multiplying 2 value in database

I have a database. I created it with HeidiSQL. Its look like this.
I enter the value-1 and value-2.
Is there a way to enter a formula to Result column like " =Value-1 * Value-2 " ? I want my database to calculate the Result when I enter my values to other cells.
A trigger is one way to achieve automated column content.
A second one is a view, which you can create additionally to the table. That view could contain SQL which generates the result:
SELECT value1, value2, value1*value2 AS result
A third (more modern) alternative is adding a virtual column in your existing table. You can do that with HeidiSQL's table editor, like shown in the screenshot. Just add a new column with INT data type, and set its Virtuality to "VIRTUAL", and Expression to "value-1 * value-2". That's it.
I'm not familiar with HeidiSQL, but it appears to be a front end? What RDBMS are you using, for example SQL Server allows a computed column.
SQL
ALTER TABLE YourTable
ADD Result AS ([Value-1] * [Value-2])
Right click your database name in the folder structure, go to --> create new then -->Trigger
Then you can create a trigger that when entering data, will be activated on the entire column like this:
But you will need to know how to write the actual query and function. This requires basic knowledge that is generally generic and consistent of most all SQL languages.

The best way to store sortable list in PostreSQL and Rails

I have a model A which has many B. So a.bs is a list of B. I want this list to be ordered by user. The common way is special order field in the database and then :order_by => order_field for B. But i don't want to manually manipulate with this field. Is there any special type in PostreSQL? Or maybe there is some extension or i can do this with trigger.
Thanks.
You'll need to store this stuff with some kind of order field, and you'll need to manipulate it manually...
It doesn't necessarily need to contain an integer, btw. For small numbers of values, a float works fine too -- that's what Postgres does in the pg_catalog when you alter an enum field and add a value before an existing one.

Data structure: Link a table to one of several other

I have a database table SavedData which contains a field type_data. I need to link that table to one of the tables SpecificData1, SpecificData2 and SpecificData3, depending on what is in the type_data field.
I have thought of a few solutions, but none of them looks great... :
Have 3 fields in SavedData called specificdata1_id, specificdata2_id and specificdata3_id, and one of them is set, with the 2 others NULL. I can then use the specific data for which the id is not null. After some reading, I've seen this one is really bad, which is understandable.
Have 1 field in SavedData called specificdata_id, and depending on the type_data I know in which table I need to look for that id.
Put all the fields from the 3 specific data tables in the SavedData table and use only the data that I need depending on the value of type_data.
What would be the best solution ? Is there another one that could solve my problem ?
You need a seperate table to just save 2 columns. Type_data, specificdata_id Use this table to just save the relationship between your SavedData and Specificdat1/2/3

query a table not in normal 3rd form

Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().