一、常用语句
1、更新:update 表名 set [字段1]=[value1],[字段2]=[value2] where [条件]
2、插入:insert into 表名 ([字段1],[字段2],[字段3]) values([value1],[value2],[value3])
3、删除数据:delete from 表名 where 条件 ——删除记录并不能释放oracle里被占用的数据块表空间. 它只把那些被删除的数据块标成unused
truncate table 表名 ——可以释放占用的数据块表空间,但是不可回退
删除表结构:drop table 表名 ——drop语句将删除表的结构,被依赖的约束(constrain),触发器(trigger),索引(index); 依赖于该表的存储过程/函数将保留,但是变为invalid状态
区别:truncate 会释放占用空间,而drop 和 delete不会。
速度:drop>truncate>dalete
4、创建视图
create or replace view 视图名 as sql语句
二、常用函数
日期转字符串
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual
字符串转日期
select to_date('2018-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual
trunc()函数处理日期
select trunc(sysdate) from dual;--2018/9/30 上午 12:00:00(00:00:00),返回当前时间
select trunc(sysdate,'y|yy|yyy|yyyy') from dual;--2018/1/1,返回当年第一天
select trunc(sysdate,'mm') from dual;--2018/9/1,返回当月的第一天
select trunc(sysdate,'d') from dual;--2018/9/30,返回当前星期的第一天,即星期天
select trunc(sysdate,'dd') from dual;--2018/9/30,返回当前日期,今天是2017/2/13
select trunc(sysdate ,'HH24') from dual;--2018/9/30 14:00:00,返回本小时的开始时间
select trunc(sysdate ,'MI') from dual;--2018/9/30 14:17:00,返回本分钟的开始时间
trunc函数处理number类型数字
select trunc(123.567,2) from dual;--123.56,将小数点右边指定位数后面的截去;
select trunc(123.567,-2) from dual;--100,第二个参数可以为负数,表示将小数点左边指定位数后面的部分截去,即均以0记;
select trunc(123.567) from dual;--123,默认截去小数点后面的部分;
查询当天的数据
select * from where time>=trunc(sysdate)
查询当一天的数据
select * from where time>=trunc(sysdate-1)
order排序
select t.name from 表名 order by decode(t.name,'a','1','b','2','c','3',t.name)