新建表 & 插入数据
create table family(
stu_no varchar2(12) not null primary key, <u>名字用varchar2</u>
stu_name varchar2(20),
sex char(2),
groupName varchar(20),
age number(3),
emp_time date <u>date 代表日期格式</u> <u>sysdate是系统日期时间</u>
)
insert into emp(emp_name,emp_no,emp_sex,emp_time,emp_sal,emp_pos,dept_id)
values('小张',1,'男',sysdate,8888.88,'产品经理',1);
备份一张表:
create table student_bak as select * from student;
插入信息
insert into family(stu_no,stu_name,sex,groupname,age)
values('19920506','边伯贤','1','EXO',33);
查询语句
select * from family;
select * from student where stu_sex <>'男';
or 指两种满足一种即可 ; and 两边的情况都要满足
select * from student where stu_name = '沈雅博' or stu_name = '邓越';
select * from student where stu_age >= 15 and stu_age <=19;
between…and… 是左闭右闭,都可以取到的
select * from student where stu_age between 15 and 19;
%是代表后面任意的字符 ‘%邓%’可以表示字符中只要有邓的字符,就都查询出来,用like来写
select * from student where stu_name like '%邓%' ;
_下划线只等匹配一个字符
select * from student where stu_name like '邓_' ;
这里的女 占3个字符
select stu_sex from student where stu_sex ='女';
distinct 可以给字符去重(查询时)
select distinct stu_age,stu_name from student;
sysdate 获取当前系统的时间
select sysdate from dual;
/*通过sql语句查出100*100等于多少*/
select 100*100 from dual; (dual代表空表)
as 代表别名,就是把最后得结果赋予名字,更或者把查询出来的表格名变成你赋予的
select 100*100 as res from dual;
找出年龄最大最小的同学
关键字:order by 作用是排序 asc升序排序 desc降序排序 order by 默认是升序排序,有时不用写asc
格式:order by + 字段名
select * from student order by stu_age desc;//可以说为 查询学生表的所有,通过年龄降序排序
select * from student order by stu_age asc;
找出某几位同学之间 哪位金额最多 order by可以多字段查询
格式:order by + 题目中所含有的字段名
select * from student order by stu_age,money desc;
删除语句
删除具体数据
delete from family where stu_no='19920728';
删除表
drop table student;
修改表中语句
update 表名 set 修改成的字段 where 系统识别的字段
update student set stu_no = 20202 where stu_name = '林殷庆' ;
计算总条数:count(你要计算的那个字段)
select count(*) from emp;
count函数作用于某一列时,会自动把空值忽略掉
select count(emp_name) from emp;
分组函数(sum、avg、min、max、count)
sum 求和
select sum(emp_sal) from emp;
avg 平均数
select avg(emp_sal) from emp;
min 求最小值 格式:min(需要求最小值的字段名)
select min(emp_sal) from emp;
max 求最大值
select max(emp_sal) from emp;
to_date可以把字符串转成日期格式
to_date(你需要转成日期格式的字符串,你传入的格式)
insert into emp values('小赵',8,'女',to_date('2020-02-21','yyyy-MM-dd'),10000,'测试',1)
转换成字符串
select to_char(emp_sal) from emp;
nvl 将你查询时,将空值设为待定,或者直接为0
select nvl(emp_pos,'待定') from emp;
找出所有小伙伴中工资最高的那个的信息
先进行排序 → order by
select * from emp order by emp_sal desc;
rownum 分页操作 且 Oracle中没有limit
select * from emp where rownum = 1 order by emp_sal desc; //查出来是8888.88那一条
select * from emp where max(emp_sal); //这样是错误的,这里不许使用max分组函数
要运用子查询
select * from emp where emp_sal = (select max(emp_sal) from emp) and rownum = 1;
统计公司员工的工资 分成男 女统计(要分组,需要group by;要统计,就要count)
group by
select count(emp_sex),emp_sex from emp group by emp_sex;
统计每一个部门有多少人
select count(dept_id),dept_id from emp group by dept_id;
展示部门人数多余2的部门,其余的不展示
having 条件,里面可以使用分组函数,相当于where
select count(dept_id),dept_id from emp group by dept_id having count(dept_id) >=2;
//
在排序 order by 里面可以用聚合函数
select count(dept_id),dept_id from emp group by dept_id having count(dept_id) >=2 order by count(dept_id);
求每个部门的平均工资,将平均工资小于6000的不展示
select avg(emp_sal),dept_id from emp group by dept_id having avg(emp_sal) >= 6000;
只保留两位
select round(avg(emp_sal),2),dept_id from emp group by dept_id having avg(emp_sal) >= 6000;
round 确定小数位数,不是分组函数
select round(avg(emp_sal),2) from emp;
表连接
如果直接搜索两张表,就会像笛卡尔积,会有很多冗余的数据
select * from emp,dept;
用 !类链接 !
通过外键关联
格式:from 两张表名 where 两张表相同的地方(将两张表连接起来的地方)
select * from emp,dept where emp.dept_id = dept.dept_id;
通过inner
格式:from 表名 inner join 另一个表名 on 两张表相同的地方(将两张表连接起来的地方)
select * from emp inner join dept on emp.dept_id = dept.dept_id;