views:

85

answers:

2

I am a PHP developer with little Oracle experience who is tasked to work with an Oracle database.

The first thing I have noticed is that the tables don't seem to have an auto number index as I am used to seeing in MySQL. Instead they seem to create an index out of two fields.

For example I noticed that one of the indexes is a combination of a Date Field and foreign key ID field. The Date field seems to store the entire date and timestamp so the combination is fairly unique.

If the index name was PLAYER_TABLE_IDX how would I go about using this index in my PHP code?

I want to reference a unique record by this index (rather than using two AND clauses in the WHERE portion of my SQL query)

Any advice Oracle/PHP gurus?

+12  A: 

I want to reference a unique record by this index (rather than using two AND clauses in the WHERE portion of my SQL query)

There's no way around that you have to use reference all the columns in a composite primary key to get a unique row.

You can't use an index directly in a SQL query.
In Oracle, you use the hint syntax to suggestion an index that should be used, but the only means of hoping to use an index is by specifying the column(s) associated with it in the SELECT, JOIN, WHERE and ORDER BY clauses.

The first thing I have noticed is that the tables don't seem to have an auto number index as I am used to seeing in MySQL.

Oracle (and PostgreSQL) have what are called "sequences". They're separate objects from the table, but are used for functionality similar to MySQL's auto_increment. Unlike MySQL's auto_increment, you can have more than one sequence used per table (they're never associated), and can control each one individually.

Instead they seem to create an index out of two fields.

That's what the table design was, nothing specifically Oracle about it.

But I think it's time to address that an index has different meaning in a database than how you are using the term. An index is an additional step to make SELECTing data out of a table faster (but makes INSERT/UPDATE/DELETE slower because of maintaining them).

What you're talking about is actually called a primary key, and in this example it'd be called a composite key because it involves more than one column. One of the columns, either the DATE (consider it DATETIME) or the foreign key, can have duplicates in this case. But because of the key being based on both columns, it's the combination of the two values that makes them the key to a unique record in the table.

OMG Ponies
very thorough - I'd give extra "pluses" for the correction of terminology if I could.
sql_mommy
@sql_mommy: Thx, I can be rather pedantic
OMG Ponies
Answer much appreciated.
Joseph U.
A: 

http://use-the-index-luke.com/ is my Web-Book that explains how to use indexes in Oracle.

It's an overkill to your question, however, it is probably worth reading if you want to understand how things work.

Markus Winand