数据库学习笔记day01+day02

--表示系统时间
select sysdate from dual
 
--表是关系型数据库的基本结构
--表是二维的,由行和列组成
--行称为记录,列称为字段
--创建第一张表
create table hw(
name varchar2(20),          --Java中的int数据类型在这里用varchar2,number(6,2)6位中有2位小数
age number(2),
address varchar2(50)
);
--查看创建的表
desc hw;
--删除表(实际上是删除表结构)
drop table hw;
--修改表结构alter table name modify();
alter table hw modify(name varchar2(15));
alter table hw drop(name);
alter table hw add(Xingming varchar2(20));
--修改表名
rename hw to hw1;
--插入一条数据
insert into hw (name,age,address) values('李',22,'长清区紫薇阁')
insert into hw (name) values('陈')
--查看表数据
select * from hw;
--删除表中的数据delete [from] hw where xxx=xxx;  delete 用来删除表中的数据,drop删除表头
delete hw where name='李';
--修改数据update name set col='' where col=''
update hw set age=21 where age is null;
update hw set name='再见' where age is not null;
update hw set name='' where age=null;
char(1)--表示一个字符,常常表示性别
--DDL:数据定义语言,操作表头。create table name();drop table name;alter table name motify();
--DML:数据操作语言,操作数据 insert into name() values();delete[from]hw where xxx=xxx;update name set xxx=   where yyy=null;
 
--drop table emp :删除表emp;
--char&varchar2:char :定长字符,声明多少占用多少,不够的补空格。varchar2变长字符,实际用多少占用多少。
--char最大2000字节,varchar2最大4000个字节,long最大2G,clob最大4G,一张表只能有一个clob。
--concat:将两个字段合成一个字段
--||连接符
 
实例:
--创建一个表 emp
create table emp(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
rngr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0)
);
--向表中插入数据
insert into emp values(7369,'smith','clerk',7902,to_date('1980/12/17','yyyy-mm-dd'),800.00,null,20);
insert into emp values(7499,'allen','salesman',7698,to_date('1981/12/20','yyyy-mm-dd'),1600.00,300.00,30);
insert into emp values(7521,'ward','salesman',7698,to_date('1982/2/22','yyyy-mm-dd'),1250.00,500.00,30);
insert into emp values(7566,'jones','manager',7839,to_date('1981/4/2','yyyy-mm-dd'),2975.00,null,20);
insert into emp values(7654,'martin','manager',7698,to_date('1981/9/28','yyyy-mm-dd'),1250.00,1400.00,30);
insert into emp values(7698,'blake','manager',7839,to_date('1981/5/1','yyyy-mm-dd'),2850.00,null,30);
insert into emp values(7782,'clark','manager',7839,to_date('1981/6/9','yyyy-mm-dd'),2450.00,null,10);
insert into emp values(7788,'scott','analyst',7566,to_date('1987/4/19','yyyy-mm-dd'),3000.00,null,20);
insert into emp values(7839,'king','president',null,to_date('1981/11/17','yyyy-mm-dd'),5000.00,null,10);
insert into emp values(7844,'turner','salesman',7698,to_date('1981/9/8','yyyy-mm-dd'),1500.00,0.00,30);
insert into emp values(7876,'adamas','clerk',7788,to_date('1987/5/23','yyyy-mm-dd'),1100.00,null,20);
insert into emp values(7900,'james','clerk',7698,to_date('1981/12/3','yyyy-mm-dd'),950.00,null,30);
insert into emp values(7902,'ford','analyst',7566,to_date('1981/12/3','yyyy-mm-dd'),1300.00,null,20);
insert into emp values(7934,'miller','clerk',7782,to_date('1982/1/23','yyyy-mm-dd'),1300.00,null,10);
commit              --提交
select *from emp;         --查看表中的内容
--||连接符
select concat(concat(ename,':'),sal)from emp;
select ename||':'||sal from emp;      --查看表中enama和sal,并且连接起来
--length 返回字符串的长度
select ename,length(ename)from emp;   --查看表中ename表头下的长度
--lower,upper,initcap:转换大小写或者首字母大写。
select lower(ename)from emp;        --将enanme属性下的转换为小写并列出
select upper(ename)from emp;        --转换为大写
select initcap(ename)from emp;        --转换为首字符大写
--截去子串/左截去/右截去 : trim/ltrim/rtrim
select trim(7 from empno)from emp;      --截去7个
select ltrim('qwer','q')from dual;        --从左边截去q
select rtrim('qwer','r')from dual;        --从右边截去r
--补位函数 lpad/rpad
select sal from emp;
select lpad(sal,5,'+') from emp;      --sal补为五位,sal本身不够的补+
--截取字符 substr('',m,n)从第m个开始,截取n个字符
select substr('Following the track of the gale,I am chasing the sun.',33,25) from dual; --得到I am chasing the sun.
--instr(char1,char2)反回char2在char1中的位置(第几个)
select instr('qwer','w') from dual;
 
--round(num,int): 四舍五入,保留int位小数,int+1位四舍五入
select round(3.1415926,2) from dual;
select round(46.33,-1)from dual;
 
--trunc(num,int):无条件舍弃int位小数后面的数
select trunc(123456,-2)from dual;--个位为-1,十位为-2.
 
--mod(m,n)返回m余n
select mod(5,3)from dual;
select sal,job from emp;
 
--ceil/floor:向上/向下取整
select ceil(3.18)from dual;
select floor(3.18)from dual;
 
--to_date():将字符串格式转换为系统格式
--查看81年以后入职的都有谁
select *from emp where hiredate>to_date('1987-01-01','yyyy-mm-dd');
 
--to_char:转换为字符串格式
select sal,ename from emp ;
select ename,job,to_char(hiredate,'yyyy"年"mm"月"dd"日"') from emp;
 
--last_day(time):返回time所在月的最后一天
select last_day(sysdate) from dual;
--查看每个员工入职月的最后一天
select last_day(hiredate)from emp;
 
--add_months(d,i)返回日期d过了i个月的时间是哪天
select add_months(sysdate,10)from dual;
 
--查看员工入职二十周年纪念日
select add_months(hiredate,12*20)from emp;
--months_between(time1,time2):计算time1与time2隔了多少个月

--香港回归了多久
select round(months_between(sysdate,to_date('1997-07-1','yyyy-mm-dd')))from dual;
 
--计算我的一百周年诞辰
select round(months_between(to_date('2097-11-6','yyyy-mm-dd'),sysdate))from dual;
select round(months_between(add_months(to_date('1997-11-6','yyyy-mm-dd'),12*100),sysdate))from dual;
 
--next_day():返回下一个周几,要看这个周的周几有没有过,过了就是指下周,没过就是指本周,
select next_day(sysdate,1)from dual;
 
--nvl(arg1,arg2):若第一个参数为null,则转换为第二个参数。
--查看员工每个月领走多少钱

select ename,sal,comm,sal+nvl(comm,0) from emp;
--nvl2(arg1,arg2,arg3):判断第一个参数是否为null,若第一个参数为null则返回arg3,否则返回arg2
select ename,sal,comm,nvl2(comm,sal+comm,sal)from emp;
 
--别名
select ename  e,job  j from emp;
 
--where一般用来添加条件

--查看十号部门的人
select * from emp;
select *from emp where deptno=10 ;
--谁是经理

select *from emp where job='manager';

--谁的薪资大于两千
select *from emp where sal>2000;
 
--and&or用在条件中,表示并且&或者的意思
select *from emp where sal>2000 and job='manager';

--查看入职时间是81年以后,并且在20部门的人
select *from emp where deptno=20 and hiredate>to_date('1981-01-01','yyyy-mm-dd');     ---**
 
--like 模糊查询
select *from emp where ename like '_a%';
select *from emp where job like '%na%';
 
--in&not in 在&不在什么内
select * from emp where job not in ('manager','clerk');
select * from emp where job !='manager' and job !='clerk';
 
--between a and b:查看在a和b范围内有撒
select *from emp where hiredate between to_date('1981-01-01','yyyy-mm-dd')and to_date('1982-12-31','yyyy-mm-dd');
 
--  >any大于最小  <any 小于最大  >all大于最大   <all小于最小
select *from emp where sal >any(1500,3000)and sal <any(1500,3000);
 
--distinct(): 消除重复
select distinct(ename) from emp;

猜你喜欢

转载自www.cnblogs.com/cgwjava/p/11420421.html
今日推荐