update users set newColumn = PID;
SQL> commit;
SQL> alter table users drop column PID cascade constraints;
SQL> alter table users rename column newColumn to PID;
SQL> commit;
SQL> desc users;
名称 是否为空 类型
----------------------------------------- -------- ----------------------------
NAME NOT NULL VARCHAR2(10)
AGE NUMBER
PID VARCHAR2(20)
SQL> select * from users;
NAME AGE PID
---------- ---------- --------------------
赵霞 3201231988001010101
李莉 3201231988001010102
创建序列:
SQL> create sequence seq_PID
start with 3201231988001010101
maxvalue 3201231999999999999
minvalue 3201231988001010101
nocycle
cache 100;
创建触发器:
SQL> create or replace trigger trigger_users
before insert on users
for each row
begin
select seq_PID.nextval into :new.PID from dual;
end;
/
查看用户序列
SQL> select sequence_name from USER_SEQUENCES;
SEQUENCE_NAME
------------------------------
SEQ_ID
查看用户触发器
SQL> select * from user_triggers where table_name = upper('job');
通过group by 语句使用rollup运算符汇总ID列
SQL> select ID,avg(age),sum(age),max(age),count(*)
2 from job group by rollup(ID);
ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1583 25 25 25 1
1584 24 24 24 1
1585 25 25 25 1
1586 25 25 25 1
1587 23 23 23 1
1683 22 22 22 1
1684 24 24 24 1
1685 25 25 25 1
1686 24 24 24 1
1687 22 22 22 1
1688 28 28 28 1
ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1689 26 26 26 1
1690 26 26 26 1
1783 35 35 35 1
1784 1
1883 24 24 24 1
1884 1
25.2 378 35 17
having子句与group by 子句一般一起使用,可以在汇总相关数据后进一步筛选汇总的数据。
SQL> select ID,avg(age),sum(age),max(age),count(*)
2 from job group by id having avg(age)>25;
ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1690 26 26 26 1
1783 35 35 35 1
1689 26 26 26 1
1688 28 28 28 1
多表连接查询
SQL> select job.name,job.id,job.job,job.age,users.name,users.age from job,users
2 where job.name=users.name;
NAME ID JOB AGE NAME AGE
-------- ---------- ---------- ---------- ---------- ----------
陈灵灵 1783 KTV服务员 35 陈灵灵 38
李莉 1587 IT程序员 23 李莉
赵霞 1683 空姐 22 赵霞
desc 在oracle数据库里有两种用法,一是查询表的结构 格式:desc table_name
二是在用排序时(order by)是降序的关键字 格式 :order by column_name desc;
五、查看、管理ORACLE常用的参数、配置等
查看控制文件:
SQL> select name from v$controlfile;
查看重做日志文件,简称日志文件:
SQL> select * from v$logfile;
查看表的索引:
SQL>select index_name,index_type,table_name from user_indexes where table_name='表名';
修改oracle时间格式:
SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
指定开启某个监听:
SQL>lsnrctl start orc5_lisenter(此处是当初建监听时创建的监听名)
数据库SCN及时间查询。
SQL> select sysdate from dual; 查看数据库时间
SYSDATE
-----------------
20131216 23:52:55
SQL> select dbms_flashback.get_system_change_number from dual; 查看当前数据库的SCN号
GET_SYSTEM_CHANGE_NUMBER
------------------------
1583042
SQL> select to_char(scn_to_timestamp(1583042),'yyyy-mm-dd hh24:mi:ss') from dual; 根据数据库的SCN号查找对应的数据库时间
TO_CHAR(SCN_TO_TIME
-------------------
2013-12-16 23:45:38
SQL> select timestamp_to_scn(to_date('2013-12-16 15:30:19','yyyy-mm-dd hh24:mi:ss')) from dual; 根据数据库的时间查找对应的数据库SCN号
TIMESTAMP_TO_SCN(TO_DATE('2013-12-1615:30:19','YYYY-MM-DDHH24:MI:SS'))
----------------------------------------------------------------------
1519388
SQL> select * from (select time_dp,scn from smon_scn_time order by time_dp desc)where rownum<10; 条件查找,查找rownum小于10的SCN以对应的时间。
TIME_DP SCN
----------------- ----------
20131216 15:53:02 1584574
20131216 15:46:45 1583098
2013121