20210806 Oracle 日常

select * from student;
--插入数据
/**/-- to_date函数,把字符串格式换成日期类型,第一个参数是字符串,第二个参数是格式类型
--sysdate系统提供的函数,获取当前系统时间
--对数据进行写操作,会涉及到事务操作,会涉及到事务操作,事务提交或者回滚,读取(查询数据)不会设计事务
insert into student(id,name,username,password,sex,age,birthday,create_time) values(1,'张三','zhansan','123456',1,20,to_date('2001-08-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(2,'张三疯','zhansanfeng','123456',1,20,to_date('1901-08-16','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(3,'李四','lisi','123456',1,20,to_date('2000-08-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(4,'王五','wangwu','123456',1,20,to_date('2003-08-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(5,'李思思','lisisi','123456',1,20,to_date('1996-01-21','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(6,'王百万','wangbaiwan','123456',1,20,to_date('2001-08-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(7,'赵六','zhaoliu','123456',1,20,to_date('2002-10-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(8,'赵流流','zhaoliuliu','123456',1,20,to_date('2001-10-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(9,'吴亦凡','wuyifan','123456',0,19,to_date('2001-10-06','yyyy-MM-dd'),sysdate);
insert into student(id,name,username,password,sex,age,birthday,create_time) values(10,'阿良','aliang','123456',0,18,to_date('2001-10-06','yyyy-MM-dd'),sysdate);
update student set sex =0 where name='张三';
update student set sex =0 where id =8;
commit;--提交事务,把之前所有对数据库的写操作永久写入文件保存
--rollback;--回滚事务
--修改所有学生的密码为888888
update student set password ='888888';
commit;

--修改学生张三的密码为123456
update student set password ='123456' where name ='张三'
commit;
--修改王五学生的密码和性别,修改多个字段,逗号隔开

update student set  password ='123456',sex=0 where name ='王五';
commit;
--查询结果进行修改更新操作
select * from student for update;

--删除编号为1的学生
delete from student where id =1;
commit;

--查询表的所有列的数据
select * from student;
--查询表的所有列的数据,建议此种子写法,把各个列写出来,效率比*要高
select id,name ,username,password,sex,age,birthday,create_time from student;
--表取别名
select s.* from student s;
select s.id ,s.name,s.password,s.age,s.birthday,s.create_time,s.sex from student s ;
--列区别名
select s.id 编号,s.name 姓名,s.username,s.sex,s.age,s.birthday,s.create_time from student s
--查询列运算,通过  ||拼接,相当于java语言中的+号连接符
select '编号:'||s.id||'姓名:'||s.age||'转号:'||s.username||'密码:'||s.password from student s 
--关系运算符使用,查询条件:=、<、>、>=、<=、!=\<>
--查询年龄为20的学生
select s.* from student s where s.id=1;
--查询年龄大于20岁的学生
select s.* from student s where s.age < 20;
--查询年龄小于或者等与20的学生
select s.* from student s where s.age <=20;
--查询年龄不等于20的学生
select s.* from student s where s.age !=20;
select s.* from student s where s.age <>20;

--多个查询条件,逻辑运算符使用,and、or、not、 between and 
--查询年龄小于20岁的女学生
select s.* from student s where s.age <20 and sex=0;
--查询学生编号在1-5的学生
select s.* from student s where id>=1 and id<=5;
--查询学生标号为1、5,7的学生
select s.* from student s where id in(1,5,7)
select s.* from student s where s.id=1 or s.id=5 or s.id =7;

--查询创建时间为null的学生
select s.* from student s where s.create_time is null
select s.* from student s where s.create_time is not null

--查询学生编号部位1、5、7的学生
select s.* from student s where s.id not in(1,5,7);
--查询创建时间不为null的学生
--模糊查询like ,查询所有性王的学生
select s.* from student s where s.name like'王%';--%为占位符,多个字符
--查询账号中包含字符an的学生
select s.* from student s where s.username like'%an%';

--查询姓王X的同学,且姓名只有两个字
select s.* from student s where name like'王_';--占位符,指任意一个字符
--备份表,自动把查询的结果存入到新的表,创建表结构,插入查询到的数据,原有的约束不会创建
select * from student_bank20210806

create table student_bank20210806 as select s.* from student s;
create table student_bak as select s.id,s.name from student s;
select * from student_bak;
--删除表
drop table student_bank20210806;
drop table student_bak01;

--备份表结构,只要结构不需要数据
create table student_bank20210806 as select s.* from student s where 1=1;

select * from student_bank20210806;

--从其他表中查询数据插入到另一张表中
insert into student_bank20210806 select s.* from student s;

select into student_bank20210806(id,name) select s.id,s.name from student s
create table student_bak01  select s.id,s.name from student s where 1=2;
select * from student_bak01;
-- 伪列,用来标识唯一一行
select rowid,s.* from student s;


--伪列rownum,对查询结果进行编号,主要作业分页查询
select rownum,s.* from student s;

--集合运算符,uniom all intersect ,minu
select s.* from student s where age>=20
union -- 合并两个查询结果,去除重复的记录
select s.* from student s where s.sex =0;

select s.* from student s where age>=20
union all -- 合并两个查询结果,不去除重复的记录
select s.* from student s where s.sex =0;

select s.* from student s where age>=20
intersect -- 两个查询结果共同的记录
select s.* from student s where s.sex =0;

select s.* from student s where age>=20
minus -- 从第一个结果中去除在第二个结果集中的记录
select s.* from student s where s.sex =0;
--系统函数演示,使用  oracle  提供的伪表dual不能存放数据,不能存放数据,只有有一列
select sysdate from dual;

--当前时间2个月后
select add_months(sysdate,2) from dual

select sysdate 当前时间 

--计算两个时间之间相差几个月
select months_between(add_months(sysdate,2),sysdate) from dual;

select months_between(sysdate,to_date('2020-01-01','yyyy-MM-dd')) from dual;
--返回某个时间值的当月最后一天
select last_day(to_date('2020-10','yyyy-MM')) from dual;

select * from student;

--学生平均年龄
select avg(s.age) 平均年龄 from student s
--学生最大年龄
select max(s.age) 最大年龄 from student s
--学生最小年龄
select min(s.age) 最小年龄 from student s
--学生年龄总和
select sum(s.age) 年龄总和 from student s
--学生总数
select count(*) 学生总数 from student s
--男女学生总人数分别人数
select s.sex 性别,count(*) 总人数 from student s group by s.sex 

select decode(s.sex,1,'男',0, '女') 性别,count(*) 总人数 from student s group by s.sex 

select decode(s.sex,1,'男',0, '女') 性别,count(*) 总人数 ,max(s.age) 最大年龄,
min(s.age) 最小年龄,avg(s.age) 平均年龄,sum(s.age)年龄总和 from student s group by s.sex 


--having 对分完组后从新筛选
--查询不唯一的年龄,having在group by 之后进行条件筛选
select s.age, count(*) from student s group by s.age having count(*)>1 ;

--oder by 根据年龄降序排序  asc升序(默认),desc降序
select s.* from student s order by s.age desc ;

--根据年龄降序排序,年龄相同,在根据编号降序排序

select s.* from student s order by s.age desc,s.id desc ;
--查询唯一的年龄,并按照年龄降序排序
select s.age, count(*)
  from student s
 where 1 = 1 --查询条件
 group by s.age
having count(*) = 1 --分组条件
 order by s.age desc;--排序条件
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值