Sunday, September 30, 2007

愿那灵火复兴我

愿那灵火复兴我,我很喜欢这首歌,我也渴望那灵火大大复兴我!

今天教会去领诗,由于好几次没有来,牧师,长老和兄弟们都语重心长地批评了我。我也觉的很惭愧,信主这么多年了,可是就是不能很好地为主做工。回来和LP谈谈,要相互鼓励。

我们所有的一切都是上帝所赐,我们有何理由不竭力,尽力去侍奉主呢?!

周五的诗班要参加,10月5号晚7:30。
下一次的婚姻家庭是在10月14号主日。

特此警醒自己,活着为耶稣。阿门!

Friday, September 28, 2007

姚明 & 神奇玛塔

中国男篮75-66悉尼国王
这是一场气氛颇为火爆的比赛,即使在最后时刻双方也在锱铢必较。最终中国队成功逆转以75-66战胜悉尼国王,姚明轰下全场最高的37分12篮板,易建联贡献13分7篮板4助攻……


桑巴热舞。巴西队4-0击败美国。终场哨响,光芒万丈的玛塔(Marta)望向天空,兴高采烈的队友们则跑过来为最伟大的个人表演喝彩。中国刚刚度过他们的传统节日中秋节,在这一天,月亮最圆也最明亮。如果有人拉下开关让杭州黄龙体育场陷入一片黑暗,21岁的玛塔会依然光彩夺目。她是真正的明星。

Thursday, September 27, 2007

ORCL - Blank Line

My colleage asked me if there is a way to have empty blink line inside SQL select to seperate the totals. Actullay you can use CASE to check some column (this column maybe generated in the subquery).

I tried the following statement, and it's pretty cool, just an idea to be fancy:))

Connected to Oracle9i Release 9.2.0.6.0
Connected as advance
SQL>
select CASE
WHEN receipt_number IS NULL THEN
''
ELSE
donor_id
END id_no,
receipt_number,
CASE
WHEN receipt_number IS NULL THEN
''
ELSE
to_char(amount)
END
from (select g.donor_id,
g.receipt_number,
sum(g.some_amount) amount
from table_name g
group by rollup(g.donor_id, g.receipt_number))

Wednesday, September 26, 2007

ORCL- Table Function(2)

From Oracle online docs:

Example: Querying a Table Function

The following example shows a table function GetBooks that takes a CLOB as input and returns an instance of the collection type BookSet_t. The CLOB column stores a catalog listing of books in some format (either proprietary or following a standard such as XML). The table function returns all the catalogs and their corresponding book listings.

The collection type BookSet_t is defined as:
CREATE TYPE Book_t AS OBJECT
( name VARCHAR2(100),
author VARCHAR2(30),
abstract VARCHAR2(1000));

CREATE TYPE BookSet_t AS TABLE OF Book_t;

The CLOBs are stored in a table Catalogs:CREATE TABLE Catalogs
( name VARCHAR2(30),
cat CLOB);

Function GetBooks is defined as follows:
CREATE FUNCTION GetBooks(a CLOB) RETURN BookSet_t;

The query below returns all the catalogs and their corresponding book listings.
SELECT c.name, Book.name, Book.author, Book.abstract
FROM Catalogs c, TABLE(GetBooks(c.cat)) Book;

Example: Assigning the Result of a Table Function
The following example shows how you can assign the result of a table function to a PL/SQL collection variable. Because the table function is called from the SELECT list of the query, you do not need the TABLE keyword.create type numset_t as table of number;
/

create function f1(x number) return numset_t pipelined is
begin
for i in 1..x loop
pipe row(i);
end loop;
return;
end;
/

-- pipelined function in from clause
select * from table(f1(3));

COLUMN_VALUE
------------
1
2
3

3 rows selected.

-- pipelined function in select list

select f1(3) from dual;

F1(3)
---------------------------------
NUMSET_T(1, 2, 3)

-- Since the function returns a collection, we can assign
-- the result to a PL/SQL variable.
declare
func_result numset_t;
begin
select f1(3) into func_result from dual;
end;
/

ORCL - TABLE function(1)

I have to say that I am very happy to work on Oracle DB again. I found sometimes the stored procedure we created is not very efficient. We try to avoid to use temp tables and some queries are very long and complex.

Then I thought about the Oracle9i new table function. Again from the expert James Koopmann:

So What Are Table Functions?
Table functions are a new feature in Oracle9i that allow you to define a set of PL/SQL statements that will, when queried, behave just as a regular query to table would. The added benefit to having a table function is that you can perform transformations to the data in question before it is returned in the result set. This is of great use when performing ETL (Extraction Transformation Load) operations.

The Pieces
CREATE OBJECT
Here we create our own object type called IND_TAB_PRIVS. Then we create a table of IND_TAB_PRIVS called IND_TAB_PRIVS_TABLE. The table IND_TAB_PRIVS_TABLE is what we will use to return the rows from the table function within a simple select statement.

CREATE TYPE ind_tab_privs AS OBJECT
(ITP_USER VARCHAR2(50),
ITP_USER_ROLE VARCHAR2(50),
ITP_GRANTEE VARCHAR2(50),
ITP_PRIVILEGE VARCHAR2(50),
ITP_OWNER VARCHAR2(50),
ITP_TABLE_NAME VARCHAR2(50),
ITP_GRANTED_ROLE VARCHAR2(50));
/
CREATE TYPE ind_tab_privs_table AS TABLE OF ind_tab_privs;
/
CREATE FUNCTION
I have created a set of PL/SQL statements in here or check at the bottom. There is nothing particularly interesting here but the additional clauses and statements are explained in further detail in the article. Specifically the PIPELINED, PIPE ROW, and RETURN clause are discussed.

PIPELINED Clause
Within the CREATE FUNCTION clause, there is a new option called PIPELINED. This option tells Oracle to return the results of the function as they are processed, and not wait for a complete execution or completion of the result set. This pipelining of the result set to one row at a time has the immediate advantage of not requiring excessive memory or disk staging resources.

PIPE ROW(out_rec)
The PIPE ROW statement is the interface or mechanism to send a piped row through the PIPELINED option through to the caller of the function.

RETURN Clause
This statement is only required because we are writing a function here and it is expected to RETURN something. You do not need to supply an argument to return since you are returning results through the PIPE ROW statement. Actually, the only real purpose of the RETURN clause is to give control back to the caller and allow for a NO_DATA_FOUND exception.

The SELECT Statement
Nothing too exciting here except for the TABLE operator and function call. Our newly created function ITP_GRANTS just requires you to send the owner and table name. I have supplied SCOTT' and EMP' for demonstration purposes only. You should supply your own owner and table name for which you wish to get the table permissions.

Issue at the SQL/Plus Prompt the following:
SQL> SELECT * from TABLE(itp_grants('SCOTT','EMP'));

CREATE FUNCTION itp_grants (owner VARCHAR2, table_name VARCHAR2)
RETURN ind_tab_privs_table PIPELINED IS
TYPE ref0 IS REF CURSOR;
cur0 ref0;
TYPE ref1 IS REF CURSOR;
cur1 ref1;
v_owner VARCHAR2(50);
v_table_name VARCHAR2(50);
out_rec ind_tab_privs
:= ind_tab_privs(NULL,NULL,NULL,NULL,NULL,NULL,NULL);
BEGIN
v_owner := owner;
v_table_name := table_name;
--return simple grants on the object where the grantee is not a role
OPEN cur0 FOR
'SELECT grantee "user", NULL "user_role", grantee, '
'privilege, owner,table_name, NULL granted_role '
'FROM (SELECT grantee, privilege, owner, table_name '
'FROM dba_tab_privs '
'WHERE owner = :1 AND table_name = :2 '
'AND grantee NOT IN (SELECT role FROM dba_roles)) '
USING v_owner, v_table_name;
LOOP
FETCH cur0 INTO out_rec.itp_user, out_rec.itp_user_role,
out_rec.itp_grantee, out_rec.itp_privilege,
out_rec.itp_owner, out_rec.itp_table_name,
out_rec.itp_granted_role;
EXIT WHEN cur0%NOTFOUND;
PIPE ROW(out_rec);
END LOOP;
CLOSE cur0;
--return grants on the object where the grantee is a role
--get the roles
OPEN cur0 FOR
'SELECT grantee "role", NULL "user_role", grantee, '
'privilege, owner, table_name, NULL granted_role '
'FROM (SELECT grantee, privilege, owner, table_name '
'FROM dba_tab_privs '
'WHERE owner = :1 AND table_name = :2 '
'AND grantee IN (SELECT role FROM dba_roles)) '
USING v_owner, v_table_name;
LOOP
FETCH cur0 INTO out_rec.itp_user, out_rec.itp_user_role,
out_rec.itp_grantee, out_rec.itp_privilege,
out_rec.itp_owner, out_rec.itp_table_name,
out_rec.itp_granted_role;
IF cur0%NOTFOUND THEN
RETURN;
END IF;
-- evaluate for each role and get all users
OPEN cur1 FOR
'SELECT user_role, granted_role '
'FROM ( SELECT grantee user_role, granted_role '
'FROM dba_role_privs '
'CONNECT BY PRIOR grantee = granted_role '
'START WITH granted_role = :1 ) '
'WHERE user_role NOT IN (SELECT role FROM dba_roles) '
USING out_rec.itp_user;
LOOP
FETCH cur1 INTO out_rec.itp_user,
out_rec.itp_granted_role;
EXIT WHEN cur1%NOTFOUND;
IF out_rec.itp_user != v_owner THEN
PIPE ROW(out_rec);
END IF;
END LOOP;
END LOOP;
CLOSE cur0;
CLOSE cur1;
RETURN;
END itp_grants;
/

next blog I may study more about this feature, and see if we can use this for our daily job.

Tuesday, September 25, 2007

ORCL Interview Qs

Today we are talking about the interview questions we had during interview for Oracle jobs, I found some great questions/answers from James F. Koopmann(Database Expert) :

Here are the next 30 in this section. Depending on the mood of the interview and your ability to elaborate on the answer, try to give some insight that you know more than just the simple answer to some of these questions. Also, be sensitive to the interviewer getting tired of you talking too much. Well here they are.

21. How would you determine the time zone under which a database was operating?
select DBTIMEZONE from dual;

22. Explain the use of setting GLOBAL_NAMES equal to TRUE.
Setting GLOBAL_NAMES dictates how you might connect to a database. This variable is either TRUE or FALSE and if it is set to TRUE it enforces database links to have the same name as the remote database to which they are linking.

23. What command would you use to encrypt a PL/SQL application?
WRAP

24. Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.
A function and procedure are the same in that they are intended to be a collection of PL/SQL code that carries a single task. While a procedure does not have to return any values to the calling application, a function will return a single value. A package on the other hand is a collection of functions and procedures that are grouped together based on their commonality to a business function or application.

25. Explain the use of table functions.

Table functions are designed to return a set of rows through PL/SQL logic but are intended to be used as a normal table or view in a SQL statement. They are also used to pipeline information in an ETL process.

26. Name three advisory statistics you can collect.
Buffer Cache Advice, Segment Level Statistics, & Timed Statistics

27. Where in the Oracle directory tree structure are audit traces placed?
In unix $ORACLE_HOME/rdbms/audit, in Windows the event viewer

28. Explain materialized views and how they are used.
Materialized views are objects that are reduced sets of information that have been summarized, grouped, or aggregated from base tables. They are typically used in data warehouse or decision support systems.

29. When a user process fails, what background process cleans up after it?
PMON

30. What background process refreshes materialized views?
The Job Queue Processes.

31. How would you determine what sessions are connected and what resources they are waiting for?
Use of V$SESSION and V$SESSION_WAIT

32. Describe what redo logs are.
Redo logs are logical and physical structures that are designed to hold all the changes made to a database and are intended to aid in the recovery of a database.

33. How would you force a log switch?
ALTER SYSTEM SWITCH LOGFILE;

34. Give two methods you could use to determine what DDL changes have been made.
You could use Logminer or Streams

35. What does coalescing a tablespace do?
Coalescing is only valid for dictionary-managed tablespaces and de-fragments space by combining neighboring free extents into large single extents.

36. What is the difference between a TEMPORARY tablespace and a PERMANENT tablespace?
A temporary tablespace is used for temporary objects such as sort structures while permanent tablespaces are used to store those objects meant to be used as the true objects of the database.

37. Name a tablespace automatically created when you create a database.
The SYSTEM tablespace.

38. When creating a user, what permissions must you grant to allow them to connect to the database?
Grant the CONNECT to the user.

39. How do you add a data file to a tablespace?
ALTER TABLESPACE ADD DATAFILE SIZE

40. How do you resize a data file?
ALTER DATABASE DATAFILE RESIZE ;

41. What view would you use to look at the size of a data file?
DBA_DATA_FILES

42. What view would you use to determine free space in a tablespace?
DBA_FREE_SPACE

43. How would you determine who has added a row to a table?
Turn on fine grain auditing for the table.

44. How can you rebuild an index?
ALTER INDEX REBUILD;

45. Explain what partitioning is and what its benefit is.
Partitioning is a method of taking large tables and indexes and splitting them into smaller, more manageable pieces.

46. You have just compiled a PL/SQL package but got errors, how would you view the errors?SHOW ERRORS

47. How can you gather statistics on a table?
The ANALYZE command.

48. How can you enable a trace for a session?
Use the DBMS_SESSION.SET_SQL_TRACE orUse ALTER SESSION SET SQL_TRACE = TRUE;

49. What is the difference between the SQL*Loader and IMPORT utilities?
These two Oracle utilities are used for loading data into the database. The difference is that the import utility relies on the data being produced by another Oracle utility EXPORT while the SQL*Loader utility allows data to be loaded that has been produced by other utilities from different data sources just so long as it conforms to ASCII formatted or delimited files.

50. Name two files used for network connection to a database.
TNSNAMES.ORA and SQLNET.ORA
Technical - UNIX

Every DBA should know something about the operating system that the database will be running on. The questions here are related to UNIX but you should equally be able to answer questions related to common Windows environments.

1. How do you list the files in an UNIX directory while also showing hidden files?ls -ltra

2. How do you execute a UNIX command in the background?Use the "&"

3. What UNIX command will control the default file permissions when files are created?Umask

4. Explain the read, write, and execute permissions on a UNIX directory.Read allows you to see and list the directory contents.Write allows you to create, edit and delete files and subdirectories in the directory.Execute gives you the previous read/write permissions plus allows you to change into the directory and execute programs or shells from the directory.

5. the difference between a soft link and a hard link?A symbolic (soft) linked file and the targeted file can be located on the same or different file system while for a hard link they must be located on the same file system.

6. Give the command to display space usage on the UNIX file system.df -lk

7. Explain iostat, vmstat and netstat.Iostat reports on terminal, disk and tape I/O activity.Vmstat reports on virtual memory statistics for processes, disk, tape and CPU activity.Netstat reports on the contents of network data structures.

8. How would you change all occurrences of a value using VI?Use :%s///g

9. Give two UNIX kernel parameters that effect an Oracle installSHMMAX & SHMMNI

10. Briefly, how do you install Oracle software on UNIX.Basically, set up disks, kernel parameters, and run orainst.

I hope that these interview questions were not too hard. Remember these are "core" DBA questions and not necessarily related to the Oracle options that you may encounter in some interviews. Take a close look at the requirements for any job and try to extract questions that interviewers may ask from manuals and real life experiences. For instance, if they are looking for a DBA to run their databases in RAC environments, you should try to determine what hardware and software they are using BEFORE you get to the interview. This would allow you to brush up on particular environments and not be caught off-guard. Good luck!

Way to go!!!

Monday, September 24, 2007

孔子说

子曰:吾,十有五,而志于学,三十而立,四十而不惑,五十而知天命,六十而耳顺,七十而从心所欲,不逾矩。