Oracle Day01
一.现有的数据存储的缺点
a) 内存存储:无法进行数据持久化存储
b) 文本:存储数据的量级比较小。存在IO并发访问。不安全,不存在备份,没有数据恢复的机制。存储数据的类型比较单一。
二.数据库的优点
a) 数据是存储在磁盘/硬盘上的,可实现数据持久化的存储。
b) 存储量级大。
c) 允许多用户同时访问
d) 可以备份,恢复
e) 提供了丰富的数据类型
三.数据库的安装和卸载
a) Oracle的安装
- 安装目录不可以有中文
- System用户为超级管理员用户
b) Oracle的卸载
i.重新运行安装程序,选择卸载选项
C)数据库服务:必须开启的服务
四.数据库的使用
a) 环境变量的配置:C:oraclexeapporacleproduct10.2.0serverBIN将oracle安装目录的BIN文件夹的路径添加到Path环境变量中即可:
b) 激活一个普通/测试用户供我们学习使用(hr):alter user hr account unlock;解锁完毕之后可以使用hr用户进行登录,hr用户的默认密码为hr。
c) 测试hr用户是否有数据:select * from employees;
五.Oracle数据库的存储结构
a) Table(表格):用于存储数据的结构(类)
b) Row:具体数据的内容。(对象)
c) Column:用于存储/规范数据的属性(特点)(属性)
d) Primary Key(主键):唯一标识一行数据
e) Foreign Key(外键):多表之间的关联
六.数据库的分类
a) 关系型数据库(RDB):以Table表格的形式进行数据的存储。Oracle,Mysql...
b) 对象型数据库(ODB):存储的数据是对象。
c) NOSQL:Rdis
七.Oracle数据库的访问方式
a) Cmd窗口:sqlplus。录入oracle的用户名和密码。
b) 网页形式:尽量不要使用该形式访问数据库。
c) 第三方工具:PL/Sql Developer.
注意:安装目录为C:Program Files
八.SQL(Struct Query Language):结构化查询语言,不区分大小写。
a) 全表查询
- 查询所有:select * from 表名;*:所有的列。
- 定列查询:select 列名1,列名2 from 表名; 注意:列名之间使用逗号隔开。
- 对列的内容进行运算:计算所有员工的年薪。Select last_name,salary*12 from employees;
- 给列起别名:select 列名 as 别名 from 表名;注意:as关键字可以省略
- 多列内容进行连接:select 列名1 ||列名2 as 新列名 from 表名;
b) 条件查询(where)
- --查询John的薪资是多少?select first_name,salary from employees where first_name='John';注意:sql中的字符串可以直接使用等号(=)来判断,字符串是用单引号括起来的。
- 逻辑运算(and,or): and逻辑于 or逻辑或
--查找last_name为King且薪资大于20000的员工。
select * from employees where last_name='King' and salary>=10000;
- 特殊谓词in ,not in:
--查询工资为9k或者6k或者12k的员工姓名和工资
select last_name,salary from employees where salary in(9000,6000,12000);
select last_name,salary from employees where salary=9000 or salary=6000 or salary=12000;
--查询工资为非9k或者6k或者12k的员工姓名和工资
select last_name,salary from employees where salary not in(9000,6000,12000);
--查询薪资在10000-20000之间员工的姓名和薪资
select first_name||last_name as name,salary from employees where salary>=10000 and salary<=20000;
select first_name||last_name as name,salary from employees where salary between 10000 and 20000;
- 模糊查询(like):
--模糊查询:查询last_name为‘K’开头的员工信息
select * from employees where last_name like 'K____';
注意:%表示的是多个字符。_表示任意单个字符。
c) 排序(Order by)
--根据薪资进行排序(order by)默认情况下做的升序排列
select * from employees order by salary;
--跟last_name进行排序
select last_name from employees order by last_name;
--desc:降序 asc:升序
select * from employees order by salary desc;
--注意:order by子句一定必须放在sql语句末尾
select * from employees where salary>10000 order by salary desc;
--多列排序
select * from employees order by salary,first_name;
d) 函数
- 内置函数sysdate
- sysdate:获得当前系统时间
- dual:哑表,专门用于和sysdate结合使用。
- Select sysdate from dual;
- 内置函数to_char(日期,“日期格式”):将一个时间转换成字符串的形式。
--将当前系统时间转换成字符串的形式
--yyyy:年 mm:月 dd:天 day:星期 hh:小时 mi:分钟 ss:秒
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
--查询4月份入职的员工姓名,薪资,和工种。
select first_name||last_name as name,salary,job_id,hire_date from employees
where to_char(hire_date,'mm')='04';
--查询今天(月-日)入职的员工
select first_name||last_name as name,salary,job_id,hire_date from employees
where to_char(sysdate,'mm-dd')=to_char(hire_date,'mm-dd');
- 内置函数to_date(‘字符串’,‘日期格式’):将一个字符串转换成日期类型的数据。
--将‘2017-08-28’转成date类型的数据
select to_date('2017-08-28','yyyy-mm-dd') from dual;
e) 组函数:默认(没有分组)情况下,会将一张表划分为一组。
- max(列名):获取最大值
- min():最小值
- avg():平均值
- sum():和
- count(*): select count(*) from employees; 返回表中的总行数。
代码体现:
select * from employees; --查询employees表中所有的数据
select last_name,salary from employees;
select last_name,salary*12 from employees; --计算每个员工的年薪
select job_id as GongZhong from employees; --给job_id起别名为GongZhong
select first_name||last_name as name from employees;--将两列连接/合并显示
--查询John的薪资是多少?
select first_name,salary from employees where first_name='John';
--查询薪资大于10000的员工姓名和工种和薪资。
select first_name || last_name as name,job_id,salary from employees where salary>=10000;
--查找last_name为King且薪资大于20000的员工。
select * from employees where last_name='King' or salary>=10000 or job_id='fdsfds';
--查询工资为9k或者6k或者12k的员工姓名和工资
select last_name,salary from employees where salary in(9000,6000,12000);
select last_name,salary from employees where salary=9000 or salary=6000 or salary=12000;
--查询工资为非9k或者6k或者12k的员工姓名和工资
select last_name,salary from employees where salary not in(9000,6000,12000);
--查询薪资在10000-20000之间员工的姓名和薪资
select first_name||last_name as name,salary from employees where salary>=10000 and salary<=20000;
select first_name||last_name as name,salary from employees where salary between 10000 and 20000;
--模糊查询:查询last_name为‘K’开头的员工信息
select * from employees where last_name like 'K____';
--根据薪资进行排序(order by)默认情况下做的升序排列
select * from employees order by salary;
--跟last_name进行排序
select last_name from employees order by last_name;
--desc:降序 asc:升序
select * from employees order by salary desc;
--注意:order by子句一定必须放在sql语句末尾
select * from employees where salary>10000 order by salary desc;
--多列排序
select * from employees order by salary,first_name;
--获得当前系统时间:sysdate
select sysdate from dual;
--将当前系统时间转换成字符串的形式
--yyyy:年 mm:月 dd:天 day:星期 hh:小时 mi:分钟 ss:秒
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
--查询4月份入职的员工姓名,薪资,和工种。
select first_name||last_name as name,salary,job_id,hire_date from employees
where to_char(hire_date,'mm')='04';
--查询今天(月-日)入职的员工
select first_name||last_name as name,salary,job_id,hire_date from employees
where to_char(sysdate,'mm-dd')=to_char(hire_date,'mm-dd');
--将‘2017-08-28’转成date类型的数据
select to_date('2017-08-28','yyyy-mm-dd') from dual;
--获取employees表中最高薪资
select max(salary) from employees;