--复习上节内容 select * from user_tables; rename myemp_dk to myemp; ALTER TABLE myemp RENAME TO myemp_dk; select * from myemp_dk; alter table myemp_dk modify( JOB varchar2(50) default 'MANAGER' ); alter table myemp_dk drop(job); insert into myemp_dk (id, name, gender, birth, salary, job, deptno) values (1004, 'shiy1', 'F', to_date('2008-01-28', 'yyyy-mm-dd'), 8000, 'MANAGER', 20 ); select * from myemp_dk; update myemp_dk set gender = 'M' where id = 1004; delete from myemp_dk where id = 1004; --查询指定表中所有字段 --select * from table_name select * from myemp_dk; --查看指定字段的信息 select 与 from 之间指定的字段可以包括: 1:表中的字段 2:一个表达式 3:一个函数 s select name,job,deptno from myemp_dk; --字符串函数: --连接字符串CONCAT(id,name) select concat(id,name) from myemp_dk; --连接字符串操作符"||" select name||'-'||deptno from myemp_dk; --获取字符串长度:length()函数 select name,length(name) from myemp_dk; upper,lower,initcap 函数:大小写转换 dual:伪表,当查询的数据与任何表没有关系时,可以使用伪表 select upper('helloworld') from dual; select initcap('helloworld') from dual; select name, job, salary from myemp_dk where name = lower('ROSE'); trim,ltrim,rtrim 函数 select trim( 'e' from 'eeeeeliteeeee') from dual; select ltrim('eeeeeeeeliteeeee','e') from dual; select rtrim('eeeeeeeeliteeeee','e') from dual; select trim('ttt ooop ') from dual; lpad,rpad 函数:补位函数 select lpad(salary,8,'$') from myemp_dk; select lpad(null,8,'$') from dual; substr函数:截取字符串: 数据库中下标从1开始! 参数:从指定字符串的指定位置开始连续取若干字符。第三个参数 可以不传入,不传入则是取到末尾,若第三个参数的值大于实际可 以获取的字符长度时也默认为取到字符串末尾。 select substr('thinking in java',10,2) from dual; select substr('thinking in java',10) from dual; select substr('thinking in java',10,100) from dual; --数字函数: round 函数:四舍五入 参数2表示保留到小数点后多少位,0则是保留到 整数位,负数则是10位以上的单位。 select round(45.678,2) from dual; select round(45.678,0) from dual; select round(55.678,-2) from dual; trunc 函数:截取数字 参数2表示保留到小数点后多少位,0则是保留到 整数位,负数则是10位以上的单位。 select trunc(45.678,2) from dual; select trunc(45.678,0) from dual; select trunc(55.678,-1) from dual; mod() 求余 select mod(11,3) from dual; ceil 、floor 函数 向上取整、向下取整 select ceil(45.678) from dual; select floor(45.678) from dual; --日期类型: date 与 timestamp date :表示日期,精度到秒,7个字节分别表示世纪年月日时分秒 timestamp 时间戳:表示日期,精度到纳秒,前7个字节与date一致 ,后4个字节记录纳秒以下的精度 select sysdate from dual; select systimestamp from dual; --日期的计算 数据库中,日期是可以计算的,对一个日期类型的值加上一个指定的 数字,等于加上了指定的天数,返回的日期是计算后的日期,减去也同理。 两日期类型的值相减,差是相差的天数。 日期越晚的越大。 查看每个员工到今天为止入职多少天? select sysdate-hiredate from emp; 在日期格式字符串中,除了英文与符号外的其他字符,都应当使用双引号括起来 select to_date('2008年08月8日 20:08:08', 'YYYY"年"MM"月"DD"日" HH24:mi:ss' ) from dual; to_char 函数: 将指定的日期安照指定的日期格式转换为字符串 select ename,to_char(hiredate,'YYYY-MM-DD') from emp; RR 是2位数字表示的年的日期格式的关键字 RR与YY的区别在于,RR会根据当前系统时间自动判定世纪 select to_char(to_date('99-12-01', 'RR-MM-DD'), 'yyyy-MM-DD') from dual; 根据自己的生日计算到今天为止,共活了多少天? select sysdate-to_date('1992-08-03','yyyy-MM-dd') from dual; LAST_DAY()函数: 返回给定日期所在月的月底 select last_day(sysdate) from dual; add_month() 函数: 对指定的日期加上指定的月数 select ename,add_months(hiredate,3) from emp; months_between(date1,date2)函数: 计算两个日期之间相差的月数,计算是用date1-date2的结果换算的 select months_between(sysdate, to_date('2018-01-01', 'yyyy-MM-DD')) from dual; 查看每个员工至今入职多少月? select ename,months_between(sysdate,hiredate) from emp; next_day(date,i) 返回的是距离给定的date最近的还没有过的周几 返回给定日期之后一周内的周几 数字表示周几时使用1-7之间的数字。 1表示周日,2表示周一,以此类推。 select next_day(sysdate,1) from dual; --null值: create table student_t( id number(4), name char(20), gender char(1) ); insert into student_t values(1000,'李莫愁','F'); insert into student_t values(1001,'林平之',null); insert into student_t(id,name) values(1002,'张无忌'); select * from student_t 更新null值 update student_t set gender = null; 判断null条件 要使用 is null 或 is not null 判断 update student_t set gender = 'M' where gender is null; null的计算 null 与字符串拼接等于什么都没做 null 与任何数字计算结果还是 null select ename,sal,comm,sal+comm from emp; select 'aaa'||null from dual; --空值函数 nvl(p1,p2) 若p1的值是 null 则函数返回p2,否则返回 p1 select ename,sal,comm,sal+nvl(comm,0) from emp nvl2(p1,p2,p3) 若p1的值不为null时函数返回P2,若为null则返回p3 p2与P3的数据类型相同,但不是必须与p1同种类型。 select ename,comm,nvl2(comm,'有奖金','没奖金') from emp;
数据库开发基础-Oracle-SQL基础-002
猜你喜欢
转载自blog.csdn.net/coder_boy_/article/details/80517514
今日推荐
周排行