I have a few instances of DB2 10.5 server running on one physical Linux machine, let name them INST1 and INST2.
All of them contain multiple schemas, however schema-naming is unique accross whole machine, for example
INST1_SCHEMA_A,
INST2_SCHEMA_A etc.
What I would like to do is to somehow create a user that can access all of those schemas as they were on one instance, so it would be possible to make a queries like:
SELECT ID
FROM INST1_SCHEMA_A
UNION
SELECT ID
FROM INST2_SCHEMA_A
How can I achieve that? Should I just link databases and alias schemas?
Federation
is the keyword for your request. DB2 LUW to DB2 LUW is included in the license and this could be done across multiple databases - not matter if they reside within the same instance, another instance on the same server or even a different server.
Set FEDERATED = YES in the DBM CFG, define a server and set up Nicknames for remote tables. For details refer to this article or this one or the IBM Knowledge Center.
Related
I have two companies using same application running in Oracle Database. Now the companies are merging and making a single company. The databases are huge with an approximate size of 10 TB.
We wanted to have the application in two databases to be merged and have a single application pointed to both the databases with minimal work.
Help is highly appreciated.
Regards
Bjm
Using DB Links feature in Oracle
For more information you can use below link regarding Database Links:
https://docs.oracle.com/cd/B28359_01/server.111/b28310/ds_concepts002.htm#ADMIN12083
It would enable you to build a SQL statement that references tables from the two different databases.
If you want to access the data in instance B from the instance A, you can use below query and edit the details accordingly:
CREATE DATABASE LINK dblink_example
CONNECT TO xxusernamexx IDENTIFIED BY xxpasswordxx
USING
'(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=xxipaddrxx / xxhostxx )
(PORT=xxportxx))
(CONNECT_DATA=
(SID=xxsidxx)))';
Now you can execute the below query to access the table:
SELECT * FROM tablename#dblink_example;
You can perform any operation DML, DDL, DQL using DB Link
So basically I have 2 superusers, postgres and eric, each with their own databases. What I want to do is, while being connected to one of them, access the database (tables to be more precise) of the other. The tables of both databases are in public schema.
I am using this query, which I found on another question on the forum, but without any result:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
I have changed the owners of the databases, offered all the privileges to both roles, but nothing. All I get are only the tables of the database under that user, not both.
Any idea what I could be missing? Thanks.
P.S: I am using PostgreSQL 9.3, and coding in Python 2.7
Superusers can always access everything in the whole cluster.
This sentence makes no sense:
Both databases are in public schema.
Cluster -> database -> schema -> table. Start with the manual here.
While connected to a particular database you can only access tables of this particular database. You have to connect to a different database to work with tables there.
Or you can use dblink or FDW.
PostgreSQL has a firewall between different databases. They might as well be on totally separate servers. You can use dblink or fdw or something like those to bridge between them.
My original question was about whether to keep separate ASPNETDB.MDF from the application database or merge all the tables in one database. Checking the previous questions/answers, I learned that it depends on whether the membership data would be shared across several applications.
Now, my question is this. In case I decide to keep ASPNETDB.MDF separate from the application DB, how can I query 2 tables located in 2 different databases?
Thanks for helping.
If you have two databases/schemas on the same database server, you can query across databases with the following syntax:
select *
from database1.dbo.table1 t1 join database2.dbo.table2 t2 on
t1.field1 = t2.field2
If they are on physically separate servers, you can still do a cross-database query, but you need to link the servers first:
http://msdn.microsoft.com/en-us/library/aa213778(v=sql.80).aspx
You can check your SQL Server version by:
SELECT SERVERPROPERTY('Edition')
If you are using "SQL Azure" you will not be able to use a table from different database. You will get this error:
Reference to database and/or server name in 'DataBase.Schema.Table' is not supported in this version of SQL Server.
Even if you try to Query a different database from your file like this:
USE ANOTHER_DATABASE;
You will get this error:
USE statement is not supported to switch between databases. Use a new connection to connect to a different database.
Ive run into the issue where I need to query 2 separate databases(same instance) in one query.
I am used to doing this with mysql, but Im not sure how to do it with DB2.
In mySQL it would be something like:
SELECT user_info.*, game.*
FROM user_info, second_db.game_stats as game
WHERE user_info.uid = game.uid
So the question is how i translate a query like that into DB2 syntax?
Equivalent of this
Is there a reason why you have the tables in a separate database? MySQL doesn't support the concept of schemas, because in MySQL a "schema" is the same thing as a "database". In DB2, a schema is simply a collection of named objects that lets you group them together.
In DB2, a single database is much closer to an entire MySQL server, as each DB2 database can have multiple schemas. With multiple schemas inside the same database, your query can run more or less unchanged from how it is written.
However, if you really have 2 separate DB2 databases (and, for some reason, don't want to migrate to a single database with multiple schemas): You can do this by defining a nickname in your first database.
This requires a somewhat convoluted process of defining a wrapper (CREATE WRAPPER), a server (CREATE SERVER), user mapping(s) (CREATE USER MAPPING) and finally the nickname (CREATE NICKNAME). It is generally easiest to do these tasks using the Control Center GUI because it will walk you through the process of defining each of these.
I have an application that needs to query two different DB2 databases for the exact same data. Is there any way to create a view that takes my query, executes it against both databases, combines the results, and send them back to my application?
Yes, sort of. We had to do something similar a few years back because one of our customers split their data across two DB2 instances but still wanted single queries that would get at them both (the reporting tool we were using could only connect to one instance).
From memory, it's a matter of:
turning on federation support (needed for instance-to-instance communication).
creating a wrapper with create wrapper so one DB2 instance knows how to connect to another.
registering the other server with create server.
using create user mapping to set up mapping of credentials between the two instances.
creating an alias in the local instance for the remote table with create nickname.
From there, you would just create your view as something like:
select * from localtable union all select * from nickname;
and you should have rows from both tables.