Oracle中操作如下:
1、表t1:
SQL>
select *
from t1;
NAME PASSWORD ADDRESS
---------- ---------- ------------------------------
wangwu 3 shanghai
zhangsan 1 shanghai
lisi 2 shanghai
zhaoliu 4 shanghai
zhan 5 shanghai
jin 6 shanghai
sin 7 shanghai
NAME PASSWORD ADDRESS
---------- ---------- ------------------------------
wangwu 3 shanghai
zhangsan 1 shanghai
lisi 2 shanghai
zhaoliu 4 shanghai
zhan 5 shanghai
jin 6 shanghai
sin 7 shanghai
2、利用表t1中的某些字段创建表t2, 将表t1中的字段name、转换为2进制后的password选出来创建表t2, 且只取password小于5的记录。
SQL>
create
table t2
as
select
name, PKG_NUMBER_TRANS.F_DEC_TO_BIN(PASSWORD)
as password
from t1
where password < 5;
Table created.
SQL> select * from t2;
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
Table created.
SQL> select * from t2;
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
利用表t1中的某些字段创建表t3, 将表t1中的字段name、转换为2进制后的password选出来创建表t3, 且只取t1表的前5行。
SQL>
create
table t3
as
select
name, PKG_NUMBER_TRANS.F_DEC_TO_BIN(PASSWORD)
as password
from t1
where rownum< =5;
Table created.
SQL> select * from t3;
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
zhan 101
Table created.
SQL> select * from t3;
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
zhan 101
3、取字段的某部分内容, 使用substr
SQL>
select *
from t3;
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
zhan 101
SQL> select substr(password, -1) from t3; --取最后一位
SUBSTR(PASSWORD,-1)
--------------------
1
1
0
0
1
SQL> select substr(password, 1,2) from t3; --从第1位开始取,取2位
SUBSTR(PASSWORD,1,2)
--------------------
11
1
10
10
10
NAME PASSWORD
---------- ----------
wangwu 11
zhangsan 1
lisi 10
zhaoliu 100
zhan 101
SQL> select substr(password, -1) from t3; --取最后一位
SUBSTR(PASSWORD,-1)
--------------------
1
1
0
0
1
SQL> select substr(password, 1,2) from t3; --从第1位开始取,取2位
SUBSTR(PASSWORD,1,2)
--------------------
11
1
10
10
10
4、创建新表,取部分字段, 且取字段的某部分位作为新表t5的内容
QL>
create
table t5
as
select
name, substr(PKG_NUMBER_TRANS.F_DEC_TO_BIN(PASSWORD),1,2)
as password
from t1
where password < 6
order
by password;
Table created.
SQL> select * from t5;
NAME PASSWORD
---------- ----------
zhangsan 1
lisi 10
zhan 10
zhaoliu 10
wangwu 11
Table created.
SQL> select * from t5;
NAME PASSWORD
---------- ----------
zhangsan 1
lisi 10
zhan 10
zhaoliu 10
wangwu 11
5、创建100万的数据
SQL>
create
or
replace
procedure sp_stan1
as
2 v_num number:=1;
3 begin
4 loop
5 insert into t1 values( 'zhangsan', v_num);
6 exit when v_num=1000000;
7 if mod(v_num, 10000)=0
8 then
9 commit;
10 end if;
11 v_num:=v_num+1;
12 end loop;
13 end;
14 /
Procedure created.
SQL> exec sp_stan1
2 v_num number:=1;
3 begin
4 loop
5 insert into t1 values( 'zhangsan', v_num);
6 exit when v_num=1000000;
7 if mod(v_num, 10000)=0
8 then
9 commit;
10 end if;
11 v_num:=v_num+1;
12 end loop;
13 end;
14 /
Procedure created.
SQL> exec sp_stan1
6、根据条件创建新表
create
table t2
as
select
name, PKG_NUMBER_TRANS.F_DEC_TO_BIN(empno)
as empno
from t1
where substr(PKG_NUMBER_TRANS.F_DEC_TO_BIN(empno),-1)=1;
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/271720,如需转载请自行联系原作者