I have a two databases that contain the exact same tables and are on the same server. I want to be able to create a report that will allow me to "merge" these databases so that when a user queries they will query BOTH databases at the same time. Is that even possible?
The simplest way to achieve this would be to create database views that UNION ALL the values from the same tables in both databases - something like:
CREATE VIEW CombinedSalesTable AS
SELECT * FROM database1.SalesTable
UNION ALL
SELECT * FROM database2.SalesTable
- and design the reports to query the views.
You may want to add an additional column to the views to show which database each record comes from, as a key value that is unique in one table may have a "duplicate" in the equivalent table in the other database.
Related
I need to get complete list of external tables in db2 database. I have defined schema called DB2INST1. How to get complete list of external tables information using system tables?
The information lives in syscat.tables (documentation here) for Db2-LUW databases that support external tables, which have their PROPERTY column with value Y in position 27 of that column.
Example query returns the fully qualified name of external tables:
select trim(tabschema)||'.'||rtrim(tabname)
from syscat.tables
where substr(property,27,1)='Y'
with ur;
In general, the best and most reliable way to retrieve all information and to recreate the DDL statements for tables is to use the db2look tool. If you want to extract the metadata on your own, there are some catalog views to start with:
SYSCAT.TABLES holds the table information. Look for the PROPERTY column and check there if it is an external table.
SYSCAT.COLUMNS has the basic column information. But there are more related tables depending on types and attributes.
SYSCAT.EXTERNALTABLEOPTIONS shows the actual options for an external table, the things in addition to what makes a regular table.
There are many more tables to hold table properties, depending on the complexity of the table and column definitions.
Since Postgres also supports partitioned tables, what is the use of child table.
Suppose there is a table of users which has a column created_date. We can store data in 2 ways:
We create many child tables of this user table and distribute the data of users on the basis of created_date (say, one table for every date, like user_jan01_21).
We can create a partitioned table with the partitioning key created_date
Then what is the difference between these solution?
Basically, I want to know what problem table inheritance can solve that partitioning cannot.
Another doubt I have: if I follow solution 1, and I query the user table without the ONLY keyword, will it scan all the child tables?
For example:
SELECT * FROM WHERE where created_date = current_date - 10;
If the objective is partitioning, as in your example, then there is no advantage in using table inheritance. Declarative partitioning is far superior in ease of use, performance and available features.
Table inheritance has uses that are unrelated to partitioning. Features that partitioning doesn't offer are:
the child table can have additional columns
a table can inherit from more than one table
With table inheritance, if you select from the parent table, you will also get all results from the child tables, just as if you had used UNION ALL to combine the results.
I'm trying to run the same query over multiple tables in my Postgres database, that all have the same schema.
This question: Select from multiple tables without a join?
shows that this is possible, however they are hard-coding the set of tables.
I have another query that returns the five specific tables I would like my main query to run on. How can I go about using the result of this with the UNION approach?
In short, I want my query to see the five specific tables (determined by the outcome of another query) as one large table when it runs the query.
I understand that in many cases similar to my scenario you'd simply just want to merge the tables. I can not do this.
One way of doing this that may satisfy your constraints is using table inheritance. In short, you will need to create a parent table with the same schema, and for each child you want to query you must ALTER that_table INHERIT parent_table. Any queries against the parent table will query all of the child tables. If you need to query different tables in different circumstances, I think the best way would be to add a column named type or some such, and query only certain values of that table.
What I am trying to do is verify a URL. I just need to be able to select that single value from all databases that we have currently in SQL Server 2008. All the databases are the same, just multiple instances of the same database for different users.
I am looking to pull one item from one table in each database.
Each database contains a table SETTINGS and within that table a value for MapIconURL. I need that value from each table from within each database. I am looking at around 30 or so databases that would have this value.
So I found the "undocumented" Stored Proc sp_MsForEachDb and have working....to a point.
The code I am using is this:
EXEC sp_MsForEachDb 'use ?; SELECT "?" as databasename,SETTINGSKEYID, SECTION, NAME, INIVALUE, DESCRIPTION
FROM ?.dbo.SETTINGS
WHERE [NAME] = "MapIconURL"'
I have noticed that it is not selecting all the databases, but that it is also selecting the master table as well as other system tables, and am thinking that may be why it is not selecting all tables. Is there a way to exclude the system related tables?
If the number (and name) of the databases is fixed, then you could simply do:
SELECT MapIconUrl FROM db1.dbo.SETTINGS
UNION ALL
SELECT MapIconUrl FROM db2.dbo.SETTINGS
...
However, if either the number or names of the databases is not fixed, then you have to build the query dynamically.
First, run a query such as SELECT name FROM master..sysdatabases to get the names of the databases.
Then, loop over the result set (in T-SQL, use a CURSOR) and build your query and execute it (in T-SQL, use sp_executesql).
In a single database example abc.sqlite has the 4 tables (cases,questions,category and options).
Each table has different columns.
How to get the data from table1 column1, table2 column 2.
How to write queries for that.
You want to learn more on SQL92 the query language for the databases, that let you select and insert data to your tables. Actually the sqlite documentation is one of the best because contains the useful syntax diagrams that you find on http://sqlite.com/lang.html