Kaley's (mostly) Oracle Database Blog
Hi! I'm Kaley Crum, Oracle DBA.
Welcome to my (mostly) Oracle database blog.

Useful Queries and Code

Query to Examine Blocking

 

See All Grants Necessary for Selecting From a View

Ran into a frustrating problem where schema “A” couldn’t select from a view that schema “B” owned.  This was because schema “B” referenced tables that were owned by schema “C,” and schema “B” was granted “SELECT” privileges only on those tables, where it should have been granted “SELECT…WITH GRANT OPTION.”

This is a bit like killing a fly with a sledgehammer, so it should be used with thoughtful consideration to the exact privileges that are being granted.  But this SQL should list out all the necessary grants for a user to be able to select from a view.  Just fill in the VIEW_OWNER, VIEW_NAME, and GRANT_SELECT_TO pieces.

 

See Privileges Granted Through Roles

Here’s a couple of queries that can be used to see privileges granted through roles.

System Privileges:

Object Privileges:

 

Oracle Drop If Exists Procedure

One of the things I’ve always wanted in Oracle was drop if exists functionality.  Inevitably, I’ll be creating a script that creates a bunch of tables and, for one reason or another, I’ll need to re-run the script because a data type in the table has changed or the length of a field needs to be tweaked, so I start dropping tables and I’ll miss one, or I’ll miss and index, and as a result the script dies halfway through with a ORA-00955 name is already used by an existing object error.

The solution, I’ve determined, was to roll my own drop_if_exists() procedure.

Continue Reading

See All Tables Foreign-Keyed to a Parent Table in Oracle

The below SQL query will find all children tables of any specified parent table.  If you do not have access to DBA_CONSTRAINTS/DBA_CONS_COLUMNS data dictionary view, use ALL_CONSTRAINTS/ALL_CONS_COLUMNS instead.  Or ask your DBA very nicely for the SELECT ANY DICTIONARY system privilege (or SELECT_CATALOG_ROLE).

 

Creating a Database Link in Another User’s Schema

Awhile back, I was tasked with creating a database link in another user’s schema, when I wasn’t able to login as the user.  I searched the internet–the majority of articles said that it couldn’t be done, and they encouraged people to create public database links instead of private database links.

Well, I found a working method–here it is.  Just replace INSERT_USERNAME  with your intended target schema.

 

Copying a User in Oracle

When you don’t care so much about copying the contents of the schema…and you’re more concerned about just copying a user’s account, this PL/SQL block will allow you to make a user account and model it after an existing account.  (If you are more interested in copying the schema contents…tables, indexes, etc., check out this blog post: How to Copy a Schema in Oracle)

 

Searching for Junction Tables in Oracle

If you have two tables that can be joined in a many-to-many fashion (for example, if you have a MOVIES table and an ACTORS table), and you want to find the table that joins the two, it’s easy if the architect used foreign keys.  Below is a modified version of the query used to search for child tables of a given parent table in a foreign-key relationship.  As always, if you don’t have permission to select from DBA_CONSTRAINTS, ask your DBA nicely for the SELECT ANY DICTIONARY system privilege or the SELECT_ANY_CATALOG role, or use ALL_CONSTRAINTS instead.

 

10 Cool Under-Used Oracle Features

The Multi-In List

Cool because it prevents you from writing a long series of “and”s and “or”s

Continue Reading