Tuesday, December 20, 2005

20 Beginner Oracle Questions

Continuing in my newfound tradition for lists of 20, here is a "cheat sheet" of 20 common beginner Oracle questions.

Before we begin, one simple warning. Very few of these are complete answers, consider them pointers or starting points or else the incomplete understanding could be dangerous.

Edit: This has been highly edited since its original version.

Oracle General:

1. What are the key environment variables?
Among many others, your home and instance:
export ORACLE_HOME=oracle_home_dir
export ORACLE_SID=instance_name

2. How do you shut down or start up an Oracle instance?
Must log on as a sys user:
sqlplus sys/***@instance as sysdba
Then:
shutdown
or
startup

3. How do you start and stop a listener?
lsnrctl status
lsnrctl start
lsnrctl stop
http://www.psoug.org/reference/listener.html

4. What are the key Oracle files?
They are in network/admin folder.
tnsnames.ora: list of database connection information (client/server)
sqlnet.ora: communication parameters setup
listener.ora: list of databases to listen for on this machine

5. How do you connect to the database to execute queries?
sqlplus user/password@server_instance
On a default system, sometimes you can use scott/tiger.

6. How do you see the errors from your recently created view/procedure?
show errors;

SQL and PL/SQL:

7. How do you output a line from PL/SQL?
DBMS_OUTPUT.PUT_LINE('Hello.');

8. How do you get the current date?
SELECT sysdate FROM dual;
SELECT systimestamp FROM dual;

9. What are some other syntax considerations?
Commands must end with a semi-colon;
Strings must be single-quoted

SQLPlus:

10. How do you show the structure of a table?
desc table

11. How do I re-execute the most recent query/command?
/

12. How do I see my most recent query?
l (for "list")

13. How do I see the PL/SQL procedure output?
SET SERVEROUTPUT ON;

14. How do I execute a SQL file?
@filename.sql

More Advanced:

15. How do I see who is currently connected?
SELECT username, program FROM v$session WHERE username IS NOT NULL;

Or, courtesy of a colleague of mine, something like this:

SELECT p.SPID "OS_PID", to_char(s.SID,'999') SID, s.serial#,
SUBSTR(p.USERNAME,1,10) "OS_USER", SUBSTR(s.USERNAME,1,16) "ORACLE_USER",
SUBSTR(TO_CHAR(s.logon_time, 'DD Month YY "at" HH:MI:SS'),1,30) "LOGON TIME", s.program, s.machine
FROM v$process p, v$session s
WHERE s.PADDR=p.ADDR
AND s.username IS NOT NULL
ORDER BY s.logon_time;

16. How do I find all invalid objects?
Query dba_objects for status = 'INVALID', something like this:
SELECT owner, decode(object_type,'PACKAGE BODY','PACKAGE',object_type) OBJECT_TYPE, count(object_name)
FROM dba_objects WHERE status = 'INVALID' GROUP BY owner, object_type;

17. How do I recompile invalid objects?
Must be logged in with privileges, and use this:
@?/rdbms/admin/utlrp.sql

18. How do I compute table and index statistics for a schema?
Answer from Steve Ensslen:
10g: Don't, it can be set up to analyze itself.
Pre-10g: Must be logged in with privileges:
execute DBMS_STATS.GATHER_SCHEMA_STATS('SCOTT');

19. How do I analyze the performance of a query/procedure (query plans, index choice, etc)?
Many ways, one way is SQL Trace with TKPROF, which I have explained here:
http://thinkoracle.blogspot.com/2005/09/analyzing-query-performance.html

20. How do I tell which database am I in?
select name from v$database;
or
select instance_name, host_name from v$instance;
or
SELECT SYS_CONTEXT(‘USERENV’,’DB_NAME’) FROM DUAL;
http://thinkoracle.blogspot.com/2005/07/which-instance-am-i-in.html

Bonus:

21. How do I set up an Oracle client?
http://thinkoracle.blogspot.com/2005/06/oracle-client.html

22. How do I get data into and out of Oracle?
http://thinkoracle.blogspot.com/2005/08/import-export.html

Comments:
On #9 what is getdate()? Do you mean sysdate?

On #18 shouldn't you use a procedure from dbms_stats, instead?
 
I have to disagree with the answer to question 18. "How do I compute table and index statistics for a schema?".

The first answer is that you don't. In 10g the database analyzes itself as needed in the default install.

The second answer would reference DBMS_STATS.GATHER_SCHEMA_STATS which has been the preferred procedure to call since 8.1.7 .
 
Thanks guys, I should have proof-read! Edited original post.
 
"4. What are the key Oracle files?"
had me puzzled. I was thinking about data files, redo, control files, that kind of thing. Turns out TNS listener config files are more important...

"5. How do you connect to the database to execute queries? sqlplus..."
also a bit of a trick question. Who says SQL*Plus is the only way to connect to Oracle?

"6. How do you see the errors from your last query?"
Er, "show errors"? Your last query?

"8. How do you show the structure of a table? desc table"
seems like it should be in the SQL*Plus section, since you have one.

"9. How do you get the current date? SELECT sysdate FROM dual;"
Would I lose points for not mentioning DUAL in my answer? Or SYSDATE?

"12. How do I see my query/command history? l (for 'list')"
Surely that only shows the most recent command, not the whole history.
 
Thanks William, I did another round of editing to fix those errors.

#4: I made a judgment that those files are the first ones a beginner is usually looking for.

#5: I chose SQL*Plus because in my opinion it is the most common and the most likely to be available anywhere Oracle is installed.

#6: Ack, I thought I had fixed that. I had pulled that from a question I got when someone was building a view (aka stored query).

#9: No bonus points, just thought I'd show it that way.

#12: Good catch. In fact, I was planning to blog on workarounds to that.

Cheers
 
Enter: The Pedants.

Q16)

The question is "How do I find all invalid objects?"

The "answer" query only counts objects that are invalid by type.

Q7.)

How do you output a line from PL/SQL?

DBMS_OUTPUT.PUT_LINE("Hello.");

Those double quotes don't work and also contradict your answer to question 9?
 
Howard, thanks for the corrections, I've made the fixes and added a disclaimer.

Yes, you're quite astute - this list came from my old notes which did indeed cover version 7, 8 and sometimes 9. The questions came from casual users who didn't know Kyte from byte.

#5: I have done default installations of version 9 and 10, and scott/tiger was there in both cases.

Cheers.
 
#8 In PL/SQL of course you'd just set a variable with:
l_sysdate := sysdate;

Selecting it from dual would be a waste of resources
 
Hey, I got named in ComputerWorld’s “Best IT Blogs on the Net.” Granted, only in the “buffer overflow” section, but still – that’s pretty cool!

http://www.computerworld.com/blogs/node/1466?source=nlt_blg

I wonder if that is another consequence of the Thomas Kyte Effect?

http://thinkoracle.blogspot.com/2005/12/thomas-kyte-effect.html
 
I received my Oracle PL/SQL certification from brainbench.
Can somebody tell me if this is a well recognised certificatio.
 
it's helpful for a beginners like me.. i need some more help from you..
pls visit my blog and give ur suggestios
my blog is
www.oracle9itraining.blogsppot.com

here, i am trying to educate oracle for a layman..
 
thanks for the info ...

Analenadora
 
This comment has been removed by a blog administrator.
 
It's vry helpful for a beginners...thnx.....
 
Thx for the information.It's vry useful for beginners
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?