一.现有的数据存储的缺点
a) 内存存储:无法进行数据持久化存储
b) 文本:存储数据的量级比较小。存在IO并发访问。不安全,不存在备份,没有数据恢复的机制。存储数据的类型比较单一。
二.数据库的优点
a) 数据是存储在磁盘/硬盘上的,可实现数据持久化的存储。
b) 存储量级大。
c) 允许多用户同时访问
d) 可以备份,恢复
e) 提供了丰富的数据类型
三.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.
六.SQL(Struct Query Language):结构化查询语言,不区分大小写。
a) 全表查询
i. 查询所有:select * from 表名;*:所有的列。
ii. 定列查询:select 列名1,列名2 from 表名; 注意:列名之间使用逗号隔开。
iii. 对列的内容进行运算:计算所有员工的年薪。Select last_name,salary*12 from employees;
iv. 给列起别名:select 列名 as 别名 from 表名;注意:as关键字可以省略
v. 多列内容进行连接:select 列名1 ||列名2 as 新列名 from 表名;
b) 条件查询(where)
i.注意:sql中的字符串可以直接使用等号(=)来判断,字符串是用单引号括起来的。
--查询John的薪资是多少?
select first_name,salary from employees where first_name='John';
ii. 逻辑运算(and,or): and逻辑于 or逻辑或
--查找last_name为King且薪资大于20000的员工。
select * from employees where last_name='King' and salary>=10000;
iii. 特殊谓词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);
iv. between and
--查询薪资在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;
v. 模糊查询(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) 函数
i. 内置函数sysdate
1. sysdate:获得当前系统时间
2. dual:哑表,专门用于和sysdate结合使用。
3. Select sysdate from dual;
ii. 内置函数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');
iii. 内置函数to_date(‘字符串’,‘日期格式’):将一个字符串转换成日期类型的数据。
--将‘2017-08-28’转成date类型的数据
select to_date('2017-08-28','yyyy-mm-dd') from dual;
e) 组函数:默认(没有分组)情况下,会将一张表划分为一组。
i. max(列名):获取最大值
ii. min():最小值
iii. avg():平均值
iv. sum():和
v. count(*): select count(*) from employees; 返回表中的总行数。