Query table from another ORACLE database - sql

I have two different data base, one is DEVORADB which i use for development, and another one is UATORADB which tester use for testing. UATORADB have the most updated data which is not in development. I want to query tables from UATORADB database in DEVORADB. I was writing in DEVORADB in such a way but not getting the result:
SELECT * FROM TABLE_NAME#UATDEVORADB.

For Oracle,
CREATE DATABASE LINK ...
e.g.
With a database link created and tested, you can do a query (of the style you showed) to retrieve rows from a remote database.
Reference: http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_5005.htm#SQLRF01205
FOLLOWUP
NOTE: In Oracle, the term "database" refers to the datafiles and logfiles associated with an Oracle "instance". To retrieve data from a second "database" means you need a second connection to the other database. Oracle provides a facility called a "database link". That allows a session(connection) to one database instance to connect to another database instance. (Without this facility, a client would need to create two separate connections, and would need to query the two databases separately.)
If this question is regarding querying from two separate "schemas" within the same database, as long as the user has sufficient privileges on objects in the second schema, the identifier can be qualified with the name of the schema, e.g.
SELECT * FROM UATDEVORADB.TABLE_NAME
To access data on a separate database, a database link can be used...
CREATE DATABASE LINK UADEVORADB
CONNECT TO user
IDENTIFIED BY password
USING 'uadevoradb' ;
(This will require an appropriate matching entry in the tnsnames.ora file on the Oracle server, or the oracle names server, or the connection details can be spelled out in place of a tnsnames.ora entry, something like:
CREATE DATABASE LINK UADEVORADB
CONNECT TO user IDENTIFIED BY password
USING '(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=uadevorahost1)(PORT=1521))
(CONNECT_DATA=(SERVICE_NAME=uadevoradb.domaindb)))'
If the "user" specified in the database link differs from the "owner" of the table on the remote system, and there's no synonym that references the table, the table identifier will need to be qualified with the owner...
SELECT * FROM OWNER.TABLE_NAME#UADEVORADB ;

Related

Query table with compound primary keys

I'm using pyodbc to connect to a machine database, and query a number of tables in that database using
pandas.read_sql(tbl,cnxn), where tbl = "SELECT * FROM TABLE", cnxn is pyodbc.connect('DSN=DATASOURCE;UID=USERID;PWD=PASSWORD').
It works on most tables, but some tables return:
DatabaseError: Execution failed on sql 'SELECT * FROM TABLE': ('42S02', '[42S02] [Microsoft][ODBC driver for Oracle][Oracle]ORA-00942: table or view does not exist (942) (SQLExecDirectW)')
These tables that return an error, when opened in MS Access, have multiple columns with a key icon on the left when opened in design view (thus a compound primary key, made up from multiple columns).
Is this is the reason I'm having the error described above? How can I solve this?
Edit: as shown in this screenshot, there are multiple columns marked as making up the primary key in design view:
Edit2:Thanks for the feedbacks. After checking ODBC Data Source Administrator window, this data source is on 32-bit platform, and its driver is Microsoft ODBC for Oracle.
I don't think table's name is the issue, because other tables worked and they have the same naming convention (table name is in this format NAME_OF_THE_TABLE). Trying to avoid showing the table name because working on a company project.
I did research the concept of primary key and realized that there can only be one for a table, but as shown in the screenshot attached, there are a five fields shows a key icon on the left.
Before anything, understand MS Access is a unique, GUI tool that maintains its own default database, JET/ACE Engine, but can connect to other databases as well including Oracle, SQL Server, Postgres, etc. via OLEDB/ODBC connections. Essentially, both MS Access and Python are doing the same thing: make an ODBC connection to Oracle (the actual backend database).
Because all linked tables connect fine in MS Access, try matching connections and queries in Python. Likely, the issue involves table names, schema connection, or user access.
Table Name: Your table contains misspellings or reserved words, or a mix of upper or lower cases as defined in their CREATE TABLE setup causing case sensitivity, so Table as defined with CREATE TABLE "Table" is not the same as TABLE. For this reason, use the exact name in the MS Access linked table and wrap with double quotes.
pandas.read_sql('SELECT * FROM "Table"', cnxn)
(Do note: double quotes in SQL is entirely different meaning than double quotes in Python and are not interchangeable with single quotes.)
Connected Schema/User: Incorrect schema. Because schemas in Oracle are more or less users, you may have multiple connections for your MS Access linked tables. Though they all point to same database server with same ODBC driver, the user differs each with different underlying tables. To resolve, match the Python ODBC connection with the MS Access ODBC connection:
You can locate the MS Access connection string under: Table Design (from Navigation Pane) > Property Sheet (from Ribbon) > Description. Use this in the pyodbc.connect(...) call. Likely only the uid and pwd would differ if working across schemas.
Unprivileged User: The connected user does not have select privilege on that table.

Query across two instances

I have one Oracle Instance which homes a number of different Databases, I would like to query across all instances as some of the other database contain related information. How do I setup a query to connect to more that one instance as part of my SELECT statement..
I'm using Oracle SQL Developer if that helps.
Prior to Oracle 12.1 the relationship between Oracle Instances and Oracle Databases was 1 to 1. A single instance could house at most 1 database, though a single server could host multiple instances. However, a single database would have multiple schemas each of which could singly or cooperatively host 1 or more database applications.
Access from one Oracle Instance to another is possible via database links. Database links may be either public or private and may be created with commands similar to this (other options exist):
CREATE [PUBLIC] DATABASE LINK LINK_NAME
CONNECT TO SOME_SCHEMA
IDENTIFIED BY SOME_PASSWORD
USING 'SOME_SERVICE_NAME';
Such a DB Link would be used to reference DB objects in the remote instance by appending the link name to the object reference with an at (#) sign, for example:
SELECT * FROM [SCHEMA.]TABLE_NAME#LINK_NAME;
The above select would return data provided the remote schema associated with the DB link (SOME_SCHEMA in the above create db link statement) has sufficient privileges to select from the referenced remote schema.

Can we define procedure or select statement on more than one database?

I need some data of one db and some data of another db and perform some operation. Can we do this in sql server 2008? In my project suppose I want to fetch data of doctor and Chemist database in CRM Database and want to generate operation, How can we do that?
If both db belongs on the same sql server instance you can use full qualified names of objects like [DbName].[scheme].[table] if on different instances then you must create linked server. Of course, providing access as expected.

SQL Server 2005 Linked server not finding tables

I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;

MySQL 'create schema' and 'create database' - Is there any difference

Taking a peek into the information_schema database and peeking at the metadata for one of my pet projects, I'm having a hard time understanding what (if any) differences there are between the create schema command and the create database command for MySQL.
Are there any differences? If not, is this a rather typical pattern of behavior for relational databases (I've heard that for other databases, such as Oracle, a schema exists in a database, rather than being on the same level as a database).
Thanks!
The documentation of MySQL says :
CREATE DATABASE creates a database
with the given name. To use this
statement, you need the CREATE
privilege for the database. CREATE
SCHEMA is a synonym for CREATE
DATABASE as of MySQL 5.0.2.
So, it would seem normal that those two instruction do the same.
Mysql documentation says : CREATE SCHEMA is a synonym for CREATE DATABASE as of MySQL 5.0.2.
this all goes back to an ANSI standard for SQL in the mid-80s.
That standard had a "CREATE SCHEMA" command, and it served to introduce
multiple name spaces for table and view names. All tables and views were
created within a "schema". I do not know whether that version defined
some cross-schema access to tables and views, but I assume it did.
AFAIR, no product (at least back then) really implemented it, that whole
concept was more theory than practice.
OTOH, ISTR this version of the standard did not have the concept of a
"user" or a "CREATE USER" command, so there were products that used the
concept of a "user" (who then had his own name space for tables and
views) to implement their equivalent of "schema".
This is an area where systems differ.
As far as administration is concerned, this should not matter too much,
because here you have differences anyway.
As far as you look at application code, you "only" have to care about
cases where one application accesses tables from multiple name spaces.
AFAIK, all systems support a syntax ".",
and for this it should not matter whether the name space is that of a
user, a "schema", or a "database".
Strictly speaking, the difference between Database and Schema is inexisting in MySql.
However, this is not the case in other database engines such as SQL Server. In SQL server:,
Every table belongs to a grouping of objects in the database called database schema. It's a container or namespace (Querying Microsoft SQL Server 2012)
By default, all the tables in SQL Server belong to a default schema called dbo. When you query a table that hasn't been allocated to any particular schema, you can do something like:
SELECT *
FROM your_table
which is equivalent to:
SELECT *
FROM dbo.your_table
Now, SQL server allows the creation of different schema, which gives you the possibility of grouping tables that share a similar purpose. That helps to organize the database.
For example, you can create an schema called sales, with tables such as invoices, creditorders (and any other related with sales), and another schema called lookup, with tables such as countries, currencies, subscriptiontypes (and any other table used as look up table).
The tables that are allocated to a specific domain are displayed in SQL Server Studio Manager with the schema name prepended to the table name (exactly the same as the tables that belong to the default dbo schema).
There are special schemas in SQL Server. To quote the same book:
There are several built-in database schemas, and they can't be dropped or altered:
1) dbo, the default schema.
2) guest contains objects available to a guest user ("guest user" is a special role in SQL Server lingo, with some default and highly restricted permissions). Rarely used.
3) INFORMATION_SCHEMA, used by the Information Schema Views
4) sys, reserved for SQL Server internal use exclusively
Schemas are not only for grouping. It is actually possible to give different permissions for each schema to different users, as described MSDN.
Doing this way, the schema lookup mentioned above could be made available to any standard user in the database (e.g. SELECT permissions only), whereas a table called supplierbankaccountdetails may be allocated in a different schema called financial, and to give only access to the users in the group accounts (just an example, you get the idea).
Finally, and quoting the same book again:
It isn't the same Database Schema and Table Schema. The former is the namespace of a table, whereas the latter refers to the table definition
CREATE SCHEMA is a synonym for CREATE DATABASE. CREATE DATABASE Syntax
Database is a collection of schemas and schema is a collection of tables. But in MySQL they use it the same way.
So, there is no difference between MySQL "database" and MySQL "schema": these are two names for the same thing - a namespace for tables and other DB objects.
For people with Oracle background:
MySQL "database" a.k.a. MySQL "schema" corresponds to Oracle schema.
The difference between MySQL and Oracle CREATE SCHEMA commands is that in Oracle
the CREATE SCHEMA command does not actually create a schema but rather populates it
with tables and views.
And Oracle's CREATE DATABASE command does a very different thing than its MySQL counterpart.
there is no difference between MySQL "database" and MySQL "schema": these are two names for the same thing