数据库学习——2.Oracle基础

前言

这个数据库的坑也继续慢慢记录一下吧,用不知道多久的时间慢慢梳理学过的内容
对了,这个适合我自己看,不适合任何新手以及大牛
大学学校开了两次数据库课程,两次都讲的sql server,两次还都是从基础开始讲….Oracle只能自己来
这里只记录我接触到的部分,目前还是个小白

开始

用的软件是pl/sql developer,我这里不记录这个的用法了,反正网上一搜一大堆..

1.查询语句

数据库嘛,就是增删改查,先从查询开始说起吧
最基础的查询语句

select 名字 from

2.算术表达式

比如计算年薪(按14个月算,月薪字段是salary,表是employee)

slect salary*14 from employee

3.别名

select salary*14 as '年薪' from employee

4.字符串链接

把姓和名连接在一起(一个字段),但中间用空格隔开(||可以类比城java中的+)

select firstname || ' ' || lastname from employee

5.消除重复行

比如hr想看看职工中的薪水阶层,没必要每个人都看,薪水一样的显示一个就行了

select distinct salary from employee 

6.条件限定

// ><等
select * from employee where salary >5000

//between and
select * from employee where salary between 3000 and 5000

//in
select * from employee where salary in(3000,6000)

/*like用于进行模糊查询
名字里倒数第二个字母是a的员工
%表示任意匹配,有或者没有都可以
_表示有,并且只有一个*/
select * from employee where first_name like '%a_'

//is null
select * from employee where department_id is null

//and
select * from employee where salary>= 3000 and salary<=5000

//or
select * from employee where first_name like '%a_' or first_name like '_a%'

//not
select * from employee where first_name not like '%a_'

7.小练习(1)

1查询所有员工的姓名(last_name+’ ‘+first_name),工资,年终奖金(工资的百分之八十 乘以 commission_pct 在加500)别名(年终奖)。

select first_name || ' ' || last_name,salary,salary*0.8*commission_pct+500 as '年终奖' from employee

2查询所有有人员的部门编号,并且去掉重复行。

select distinct number from employee

3查询员工的姓名,工资,岗位(JOB_ID),要求工资为2000~3000并且JOB_ID以‘ERK’结束

select name,salary,job_id from employee where salary between 2000 and 3000 and job_id like '%erk'

4查询所有有人员的岗位编号,要求岗位(JOB_ID)中包含‘L’同时岗位(JOB_ID)名称以‘N’或‘K’结束,去掉重复行。

select distinct job_number from employee where job_id like '%L%' and (job_id like '%N' or job_id like '%K')  

8.排序查询

两种排序:asc正序和desc倒序

//根据工资排倒序
select * from employee order by salary desc

9.关联查询

关联查询指的是同时从多张表里取数据

比如查询员工的名称,以及员工所在部门的名称
就需要同时从employee表和department表里取数据

并且会使用到表别名

查询员工的名字和部门id,其中e就是表别名

select e.name,e.department_id from employee e

但是我们想知道部门地址,在employee这张表中没有,必须查department这个表

select d.address from department d

但是这样两次查询查出来的是两部分数据,怎么一次查询呢,这时候就要用到关联数据

select e.name,d.address from employee e 
left join department d
on e.department_id = d.department_id

left join 是进行表关联
on 指定关联字段

10.统计函数

常用统计函数有
count 总数
max 最大
min 最小
avg 平均

统计月薪高于5000的有多少人

select count(*) from employee e where e.salary >5000

100部门薪水最高最低是多少

select max(salary) ,min(salary)  from employee e where e.department_id = 100

平均薪资

select avg(salary) from employee e

11.分组查询

分组查询通常和统计函数结合起来使用

比如要查询每个部门的平均工资,如果不用分组查询,则需要单独查询每个部门的平均薪资

select avg(salary) from employee e where e.department_id = 90
select avg(salary) from employee e where e.department_id = 100
select avg(salary) from employee e where e.department_id = 110

但是如果使用分组查询,只需要一条语句就可以

select avg(salary),e.department_id from employee 
group by e.department_id

注意:分组的时候,查询字段,只能是统计函数,或者被分组的字段

select avg(salary),e.name  from employee e group by e.department_id

就会报错,因为根据部门分组,但是部门中每个人的名字不一样,无法查询

12.having

having即是对分组函数进行条件判断

以部门进行分组,找出部门平均薪资高于5000的数据

select avg(salary),e.department_id  from employee e
group by e.department_id 
having avg(salary) >5000

13.小练习(2)

查询部门编号大于等于50小于等于90的部门中工资小于5000的员工的编号、部门编号和工资

select e.id,e.department_id,e.salary from employee e where e.department_id between 50 and 90 and e.salary < 5000

显示员工姓名加起来一共15个字符的员工

select * from employee e where e.first_name || ' ' || e.last_name like '_______________'

显示不带有“ R ”的员工的姓名

select * from employee e where e.first_name || ' ' || e.last_name not like '%R%'

查询所有员工的部门的平均工资,要求显示部门编号,部门名,部门所在地(需要多表关联查询: employees, department, location)

select e.avg(salary),e.department_id,d.department_name,l.department_address from employee e
left join department d
on e.department_id = d.department_id
left location l
on d.location_id = l.location_id
group by  e.department_id,d.department_name,l.department_address

14.子查询

子查询即可以简单的理解成同时执行多条sql语句
将一条sql语句的查询结果作为条件来执行另一条sql语句

查询出Bruce的月薪,得到数据是6000

select e.salary from employee e where e.first_name= 'Bruce'

统计月薪高于6000的人数

select count(*) from hr.employees e where e.salary > 6000.00

这需要两句sql语句
通过子查询,一句sql语句就足够了:

select count(*) from employee e where e.salary > 
(
 select e.salary from employee e where e.first_name= 'Bruce'
)

15.分页查询

只查出10条数据

select * from hr.employees e where row<=10

16.小练习(3)

1.查询所在部门所在城市为’South San Francisco’的员工

select e.first_name, d.department_name,l.city from employee e
left join department d
on e.department_id = d.department_id
left join location l
on d.location_id = l.location_id
where l.city = 'South San Francisco'

2.查找和143在同一个部门(job_id)和同一个岗位(employee_id)的比他工资高的员工。

select * from employee e2 where e2.job_id =
    (select e.job_id from employee e where e.employee_id = 143)
and e2.salary >
     (select e.salary from employee e where e.employee_id = 143)

3.查询总工资最多的部门,显示部门编号(department_id),部门名(department_name)

select d.department_id, d.department_name
  from hr.departments d
 where d.department_id =
       (select x2.department_id
          from (select rownum r, x.*
                  from (select sum(e.salary) as sumSalary, e.department_id
                          from hr.employees e
                         group by e.department_id
                         order by sumSalary desc) x) x2
         where x2.r = 1)

3题的思路:
1.先从员工表中根据部门id分组查询总工资。得到一个表A

select sum(e.salary) as sunSalary,e.department_id from employee e 
group by e.department_id
 order by sumSalary desc

2.在根据分页查询这个表A,得到一条数据的表B(就是工资最高的那个部门的数据)

select rownum r,x.* from A x 

3.对这个B的department_id进行rownum为1的分页查询(其实这里是接上面的,2和3最好一起),得到一个id为C

select x2.department_id from B x2 where x2.r =1;

4.从表C中查询题目要的条件

select d.department_id, d.department_name
  from hr.departments d
 where d.department_id = C

——————————————————————————————————————oracle的查询就写到这

——————————————————————————————————————

17.创建表

create table hero(
  id number,
  name varchar2(30),
  hp number,
  mp number,
  damage number,
  armor number,
  speed number
)

hero即表名
number表示数字类型
varchar2(30)表示可变长字符类型,最长30
这里写图片描述

varchar和varchar2的区别
两种都是变长的,对于汉字(unicode)的处理有区别
varchar如果放的是英文和数字,就用一个字节存放,如果是汉字(unicode),就用两个字节
varchar2 统一使用两个字节
一般都直接使用varchar2,使用varchar很少了

18.插入数据

commit;的意思是提交这一次插入行为,否则数据库不会生效

insert into hero (id,name,hp,mp,damage,armor,speed) values(1,'炸弹人',450,200,45,3,300);
commit;

19.序列

序列即sequence通常被用来作为id插入到表中
1.创建了一个名字为hero_seq的序列
这个序列从1开始,每次增加1,最大值是9999999

create sequence hero_seq
  increment by 1 
  start with 1 
  maxvalue 9999999

2.使用sequence
获取hero_seq的下一个值

select hero_seq.nextval from dual

获取hero_seq的当前值

select hero_seq.currval from dual

3.作为id插入表中
执行下面代码多次,再执行查询语句

 insert into hero (id,name,hp,mp,damage,armor,speed) values(hero_seq.nextval,'炸弹人',450,200,45,3,300);
select * from hero

可以看到后面再增加的数据的id就是自动增加的

20.修改数据

修改一个字段

update hero set damage = 46 where id = 22;

修改多个字段

update hero set damage = 48, armor=4 where id = 22;

21.删除数据

删除id是21的记录
delete操作时可以回滚的,只要在提交之前,进行回滚就可以把删除的数据还原

delete from hero where id = 21

不跟where条件,即删除一个表所有数据

delete from hero

22.含去表

truncate 指令 立即删除表里所有的记录
并且 不能回滚!

truncate table hero

truncate 比delete更快,更彻底,并且不能回复,不能回滚
所以只有在确定要删除一个表所有记录的时候,才能执行这个操作
其实不建议用这个,万一出事了就只能跑路了

23.修改表结构

这里写图片描述

1.增加新的一列

alter table hero add (kills number)

2.修改列

alter table hero modify(name varchar2(300))

使得name字段的类型从原来的30长度的变长字符变成300长度的变长字符

3.删除列

alter table hero drop column kills

执行这句话需要权限才行

24.删除表

drop table hero

不能回滚,所以删除表一定要非常谨慎
不建议使用,可以用delete删除内容

25.小练习(4)

1.为hr方案创建表“USERS”,要求有列:USER_ID,USER_NAME,BIRTHDATE,AGE,SALARY,photo,USER_COMMENT(个人履历)。

create table USERS(
    unser_id number,
    user_name varchar2(30),
    birthdate date,
    age number,
    salary number,
    photo varchar2(20),
    user_comment varchar2(300),

)

2.为“USERS” 表添加一列 “DEPARTMENT_ID”;

alert table USERS add(department_id number)

3.删除表“USERS”中的age列

alert table USERS drop column age

4.删除表“users”

drop table USERS

26.约束constraint

约束(constraint)种类
唯一约束 unique 不可重复
非空约束 not null 不能为空必须制定值
主键约束 primary key 通常是给id使用的,同时具备了unique和not null约束
外键约束 foreign key

1.创建表的时候设置约束

create table hero(
   id number,
   name varchar2(30) unique,
   hp number,
   mp number,
   damage number,
   armor number,
   speed number
)

通过关键字unique给name加上了唯一约束
增加数据的时候,name 就不能重复了
这个时候重复sql语句,加入相同的name的数据就会提示出错

2.给约束命名

create table hero(
   id number,
   name varchar2(30),
   hp number,
   mp number,
   damage number,
   armor number,
   speed number,
   constraint uk_hero_name unique (name)
)

uk_hero_name即指定的约束名称。
uk 代表 unique
hero 代表表名
name 代表字段
约束名称是自定义的,但是易读性高的约束命名方式,更加便于调试与维护
当加入name存在的数据的时候,就会提示违反uk_hero_name这个约束

3.给已经存在的表增加约束
给已经存在的表hero的id字段加上主键约束

alter table hero add constraint pk_hero_id  primary key (id)

4.删除约束

alter table hero drop constraint uk_hero_name

5.创建外键约束
外键指的是本表的某一个字段,指向另一张表的主键(或者是有唯一约束的字段)

击杀记录表,用于记录英雄彼此之间的击杀数量

create table kill_record(
   id number,
   killerid number,
   killedid number,
   number_ number,
   constraint fk_killer_hero foreign key (killerid) references hero(id),
   constraint fk_killed_hero foreign key (killedid) references hero(id)
)

killerid 存放击杀英雄的id
killedid 存放被击杀英雄的id
number_ 存放击杀了多少次(因为number是关键字,所以使用number_作为字段名)

27.视图View

1.创建一个视图


create view herosimple as (
    select id, name from hero
)
select * from herosimple;

这样就可以对herosimple像操作普通表一样进行操作了

2.修改table会导致view里看到的数据发生变化

update hero set name=’炸弹人1’ where id = 1
select * from herosimple

3.修改视图不会导致表中的数据发生变化

28.小练习(5)

1.为users表的user_id添加主键约束。

alert table users add constraint pk_users_id primary key (user_id)

2.为users表的user_name添加非空和唯一约束。

alert table modify(user_name varchar2(300) not null)
alert table users add constraint uk_users_name unique(user_name)

3.创建一个表department( department_id, department_name)

create table department(
    department_id number,
    department_name varchar2(30)
)

4.为users表的DEPARTMENT_ID添加外键约束

alert table users add constraint fk_users_department foreign key (department_id) references departments(department_id)

5.删除表hr.users后重新创建它,并在一条SQL语句中一并添加上述的约束条件。

drop table hr.users;

create table hr.users (
  user_id number primary key,
  user_name varchar2(30) not null unique,
  BIRTHDATE date,
  AGE number,
  SALARY number,
  photo varchar2(30),
  USER_COMMENT varchar2(3000),
  department_id ,
  constraint fk_users_department foreign key (department_id) references hr.department(department_id)
)

后记

基础的其实就这么多,后续的学习全靠自己继续啦

猜你喜欢

转载自blog.csdn.net/zyw644451/article/details/81700134