Вы находитесь на странице: 1из 2

Oracle SQL tricks

Here are some useful SQL tricks, tested on an Oracle database.

1. The Oracle 7.3, and 8.0.3, and 8.1.5 manuals are not supposed to be online.
The 9i manuals are supposed to be online (free username/password required)
2. A PL/SQL package to generate random numbers

What if you want a random number in the where clause, like, so you can choose
1/10 of the rows at random? Use a hash instead.

select * from emp where


dbms_utility.get_hash_value(dump(rowid),0,100)<10;

3. The biggest difference between Oracle and everyone else is the locking
mechanism. Everyone else gets share locks on data when they query it. That
means other queries can also see the data, but queries block writes and
writes block queries. (Update: the default for everyone else, for for example
for DB2, is for queries not to block writes. Instead they show any data
committed at the time the query internally fetches the data. This means each
query result can be internally inconsistent, but it allows greater concurrency.)
Oracle doesn't have share locks. Instead it uses snapshots, which are points
in time. A query looks at the data as of some point in time. If someone
changes the data later, that's OK, the query still sees the old version of the
data. Each query result is internally consistent, but consecutive queries may
not be consistent with one another. Queries don't block writes and writes
don't block queries. Writes still block writes. Both share locks and snapshots
are reasonable concurrency models, but they aren't the same. It's wrong to
think in terms of one when working with the other.
4. Derived tables (from-clause subqueries, inline views):
5. select * from (select * from emp);
6. insert into (select deptno from dept)
7. select trunc(a) from (select sum(empno)/5000 a from emp);

This has the same effect as if you used a view. It is useful for queries that
have a repeated complex expression; for example

select x from (select avg(sin(empno)) x from emp group by deptno)


where x > 0;

It's also useful when you want to use one of the values found by a subquery,
for example when deleting all outdated entries from a log:

delete from log where rowid in


(select rowid from log
minus
select a.rowid
from log a, (select id, max(time) mtime from log group by id) b
where a.id = b.id and a.time = b.mtime);

As of 9i, inline views can also be expressed with a "WITH clause", for example
with sub1 as (select deptno, avg(sin(empno)) x from emp group by
deptno)
select a.deptno, a.x, b.x from sub1 a, sub1 b where a.deptno >
b.deptno;

8. When should varchar2 be used instead of char? Always. Varchar2 is always


faster, more space efficient, less buggy, and its comparison rules are more
likely to be what you expect.
9. Is SQL case sensitive? No. Can I give tables case-sensitive names with weird
characters in them? Yes. Quote them, like so:
10. create table "Table" ("My column is KEWL!!!" int);
11.How do I insert data into an nvarchar2 column? From 9i onwards, just like any
other character column, and the character set is guaranteed to hold the
Unicode repertoire and measure characters using UCS2 codepoints. There is
implicit conversion between char and nchar. Before 9i, avoid nvarchar2. If
you really must use it, you insert like so: "insert into nemp (ename) values
(n'SCOTT')".
12.Building a big bogus table:
13. create table a (a1 number, a2 varchar2(50));
14. begin
15. for i in 1..10000 loop
16. insert into a values (i, 'I am a unique and extraordinary
individual');
17. end loop;
18. commit;
19. end;
20. /
21. alter table a add constraint apk primary key (a1);
22. select count(*) from a; -- 10000 rows
23.Like the making of sausage and politics, it is best not to understand the
making of Oracle data types.

Вам также может понравиться