Confusion in Oracle naming - oracle

I wanted to form oracle database URL for my JDBC conection and few websites say SID should be part of URL and few say schema name.
Please compare schema, service name and SID in ORACLE? And how they are related?
is it like SID:schema is 1:n as in one SID can point to n schemas?
Please explain with the relation between them.

A SID is the unique identifier for a database, or if it's a RAC system for an instance of a database.
A Service is an identifier for a service offered by the database, and the database should be configured for services such as "BILLING_APP" or "CUST_WEBSITE". http://docs.oracle.com/cd/B28359_01/server.111/b28320/initparams217.htm
Don't confuse a database and an instance, by the way. A database is the set of data and control (etc) files, the instance is the memory areas and processes that access it. RAC has multiple instances per database, each of which can register with the listener to offer all or a subset of the total services offered by the database.
A schema is effectively the username that owns the objects, and there can be multiple schemas in the database.
So an application should be referencing a SERVICE to connect to, through a listener on a certain host and port. The listener resolves this to a database (possibly one of many that register to offer that service). The connection is made to the database as a user who might or might not be the owner of the schema. Typically the connection is not made as the schema owner for security reasons -- in fact the best level of security is to connect as User_A, calling code in User_B's schema, whicxh references tables in other users' schemas.

A SID is a Service ID and refers to a single Oracle database instance
A service name is very like a SID but multiple SIDs can be referenced by one Service Name
E.g. in a Dataguard scenario, the primary instance might be SID_01 and ORA_SRV. SID_02 and SID_03 are running somewhere on the network and receiving and applying the log files. SID_01 fails and SID_02 is brought up (Mounted and Opened) and now exposes itself ont he network as ORA_SRV.
Similarly with RAC multiple SIDs make up a single Service.
A schema is in effect a user.
A JDBC connection will be in some form of "HOST:PORT:(SID or Service name)". You will have to connect to the DB and will do so with a username and password. The username will (almost always) connect you to a schema of that name. You can can reference objects in another schema with dotted notation in your sql, e.g. SCHEMA.OBJECT_NAME

Related

Specifying a database in JDBC URL for Netezza

When connecting to Netezza via JDBC, the database in the URL is a required field - e.g. the value sales in the example URL jdbc:netezza://main:5490/sales;user=admin;password=password
The first time I connect to a Netezza instance, I don't (necessarily) know the name of a database on the appliance. Guessing a database name is a very time consuming exercise.
In this scenario, what value should I provide? Is there a "use default" option, a way to list databases, or a specific database name that will always work (e.g. a system database)?
Netezza will always have one main database: system.
You cannot delete the system database.
I would suggest that you connect to jdbc:netezza://hostname:5480/system.
Once connected, you can list the databases that your user has access to, using
select database
from _v_database;
I can't answer definitively, but it appears that every Netezza appliance by default has a database named TESTDB. This is at least true for all the appliances to which I have access.
Users or administrators may be able to delete this database, but it's a good bet if you need a database to which to connect.

How can I set a JBOSS data source to an Oracle database use a different schema to the one used for authentication

I have a Java webapp (WAR) that is to be run in JBOSS.
That webapp is to create connections to an Oracle database using a username/password for a user that is given read-only permissions.
The webapp queries tables belonging to a different schema. I do this by qualifying each table name in my SQL queries.
However, I would like to parameterise this in my datasource, since the schema names can be different in different environments.
Is there a way to define a JBOSS data source which logs in as User A for each connection, but uses Schema B for all queries?
One way to do it is to use the new-connection-sql or check-valid-connection-sql datasource properties to execute ALTER SESSION SET CURRENT_SCHEMA=yourschema, which will change the default schema for each connection.
Recommended way is to create synonyms in Oracle for your User A to access tables in schema owned by User B. This way you can even grant specific privileges to user A to select, update, insert on tables owned by the other UserB.

what differences SID and Service Name when use Oracle SQLDeveloper?

English is not my native please understand
When I try to login like system/password as sysdba to oracle(linux) by sqldeveloper(window)
It fail when through Service Name and return ORA-0131 :insufficient privileges
but SID? successfully passed
what differences? and how to connect with Service Name?
Thomas Kyte explained the difference beatifully :
A service name is more flexible than a SID would be.
A database can dynamically register with a listener using one or more service names. In fact, more than one database can register with a listener using the same service name (think about a clustered environment where you have multiple instances that all are the same database under the covers).
A database on the other hand has a single SID. And a single SID goes to a single database. It is a pure 1:1 relationship.
A service is a many to many relationship.
Service names are used with dynamic registration - the data registers with the listener after it starts up. Once it does that, you can connect.
With the SID - that is more like telling the listener "I want you to connect to this specific database, I know the 'address', here you go"
With the SERVICE - you are asking the listener to put you in touch with a database that can service your request, a database that registers using that service.
More information here, https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1508737000346067364
Similar question was asked in Stack Overflow 6 years back, How SID is different from Service name in Oracle tnsnames.ora
In your test environment, play around with tnsnames.ora and listener.ora. AFAIK, from 12c, Oracle uses only service_name, since it is flexible and has many to many relationship. I will cross-verify from documention if I find it incorrect.

How do I determine if I a user requires a schema to access a table?

An application needs to access various Oracle database. Some databases have tables in Schemas, some don't - there's no control over this.
If a database has a schema in use, the applicable won't work unless the user enters a schema. I'd like it to be able determine via a SQL query if a schema is required to access the tables so the user can be alerted to this.
I'm aware of the question - How do I obtain a list of schemas that an Oracle user has access to - but that only tells me what schema's can be accessed, not if use of the schema is required to access tables.
Is there an SQL query to one of the system tables that can do this with that user's rights?
Note: The application only has login credentials and doesn't know any table details.
Hope that's clear. Thanks.
Question is confusing. For most part in Oracle, you can consider LOGIN == USER == SCHEMA. When you login into your database with your user, you are able to see and access all objects in that user's schema.
Objects in other schemas (on same database server) can be accesed by SCHEMA2.TABLE1 if connected user has privileges to acces table (there are different privileges...). As already stated in some comments, you do not need to prefix table if synonym exists. Your user can access even tables on some remote server if exists appropriate database link.

Change Oracle Schema at runtime when using SubSonic

In my project, I am using Oracle Database and SubSonic for DAL. I have a problem with SubSonic and Oracle Schema, that is:
When developing, I used a schema DEV in Oracle Database and generate DAL using SubSonic.
After that when release to customer, he used a new schema TEST in Oracle Database and changed the connection string in app.config to connect to Oracle. The error will appear, that is “Table or View does not exist”. I found this error and see that the schema of tables is still DEV.
I do not want re-generate DAL after change schema and when released to the customer. Please help me.
Firstly, your schema should not be DEV. DEV is a user or role.
Your schema name should be related to the data content (eg ACCOUNTS or SALES)
Secondly, consider whether you or the customer is going to decide the schema name. Say you have a product called FLINTSTONE. You may decide that the schema name should be FLINTSTONE. However your customer may want to run two instances of your product (eg one for local sales, the other for international) and use the same database. So they want FS_LOCAL and FS_INTER as the schema names. Is that option a feature of your product ?
Next, decide if your application should connect as the schema owner. There are good security reasons for NOT doing that. For example, the schema owner has privileges to drop tables, which is generally something the application doesn't do and thus, on the principle of least privilege, is something your application shouldn't have privileges to do.
Generally I would recommend some config parameter for the application for the schema name, and after connecting to the database, the app should do an "ALTER SESSION SET CURRENT_SCHEMA = 'whatever was it the config file'". The application database user would need the appropriate insert/update/delete/select/execute privileges on the objects in the application schema. If the application can't do that, you can have a LOGON trigger in the database.
Gary is correct in not using DEV as a schema on your own machine. In using Oracle we typically set up the schema as what the client is going to name their schema. This however does not fix your issue. What you need to do is create a global alias in Oracle that maps say DEV to CLIENTSCHEMA. You should still rename the schema on your machine but this will allow your schema to differ from your clients.

Resources