**仅供参考!**
此文章为我个人在当时学习sql时候做的笔记如有错误请尽情提出本人会积极改进。
Day01:
<> 不是 与!=不等于一个道理
---包含 in: 是否包含
---条件 where:
---与 and : 与Java&&一个道理
---或 or : 或java||一个道理
---是 is : 是
---不是 not is: 不是
---俩者之间 between:表达式1-----between----表达式2之间
---过滤空值 nvl : select ename,sal*12+nvl(comm,0) from emp;(此处以scott emp表年薪为例)
---创建用户 create :create(创造) user(用户) 用户名 identified(鉴定) by(通过) 密码;
---修改密码 alter :alter(修改) user(用户)------------------------
---赋予权限 grant :grant(授予) dba(最大权限) to(指向)用户名
---删除用户 drop :drop(使终止) user 用户名
---查询表 select :select(挑选) *(全部) from(来自) emp(scott表名)
---创建表 create :create table 表名
---添加数据 insert :insert(插入) into(到...里) 用户名 values(赋予值)
---日期添加: ('12-12月(月必须带)-1999');
(to_date('1999-09-09 11:11:11','yyyy-MM-dd hh:mi:ss'));
---获取系统时间:select sysdate from emp;(按表数来)
select sysdate from dual;(单行单列)
---消除冗余 dual : select 2+3 from dual(双数的);
select * from dual(得大写X)
---删除表数据 from 可以省略(mysql中不得省略)
delete from demo where id=1;(删除id=1的表数据)
---修改表数据
update demo set(集合) name='李四' where id=1;
---添加表数据
alter table demo add 添加表名 varchar2(20);
---排序 关键字是 order by 默认升序 asc;
select * from emp order by 排序项 asc;
---倒叙 desc
select * from emp order by 排序项 desc;
*******排序时候可以多个字段进行排序************
sleect * from emp order by 排序项 排序类型(不写默认升序),第二排序项 排序类型;
---拼接字符串||
select 拼接1||拼接2
select 拼接1||'字符内容'拼接2
五种聚合函数
---最大值 max
---最小值 min
---平均值 avg
---和 sum
---计数 count
---to_char 转换成字符串类型(转换函数)
select to_char(sal,'&99,999.9999') from emp;
select to_char(sal,'L99,999.9999') from emp;
select to_char(sal,'&00,000.0000') from emp;
select to_char(sal,'L00,000.0000') from emp;
select to_char(sysdate,'yyyy-MM--dd hh24(加24是24小时的意思):mi:ss') from dual;
---消除冗余 关键字 distinct
select distinct 项 from 表名;
Day02:
---包含与否 like
select * from emp where 项 like'%查找区间%';
select * from emp where 项 like'_查找区间(从第二个开始)%';
拓展:
select * from emp where not like '_A%'--(查找第二个字母不是A的表数据)
---为空 is null
---不为空 is not null
*--*-分组 关键字 group by
以scott_emp表为例:
select avg(sal),deptno,ename from emp group by deptno,ename;
注: group by 前有的表项后面必须也得有,而聚合函数不需要.group by后表项有多少不影响.
---having必须和group by 配合使用
用来限制group by 搜索限制 它作用于 group by 创建的组; (0.0.3版改)
&&&&&&*****
连接:
---内连接
inner 可以省略
select *from 表1名 s(别名) inner join 表2名 ss(别名) on s.id=ss.id;
九九: select *from 表1名 s(别名),表2名 ss(别名) where s.id=ss.id;
---左连接
left join;
select * from emp 表1名 s别名,表2名 ss别名 where s.id=ss.id(+);
---右连接
right join
(+)在左
****建议使用outer join (+)号使用在非主表一方 使用where语句时候不能用 outer join
--全连连接
full join
---笛卡儿积(最简单的表连接_错?)
Day03:
---Commit 提交
---Rollback 回滚
---Savepoint 创建回滚点关键字: Savepoint p1(回滚点名);
Rollback to p1 回滚到p1;
5种约束:
---Primary key 主键唯一约束
名 数据类型 primary key
---not null 非空约束
名 数据类型 not null
---check 检查约束
age(名) 数据类型 check(age>0 and age<100 [约束条件] );
---unique 唯一约束
名 数据类型 unique;
---外键约束
创建序列:
---Create Sequence 序列名 (创建序列)
---Start with 值 (初始值)
---Increment by 步长 (步长)
---MaxValue 值 (最大值)
---Cycle (循环)
---MinValue 值 (最小值)
---Cache 值 (缓存)默认值20
缓存值必须小于最大值-最小值/步长(0.0.3改)
---NextVal (查询下个值)
---CurrVal (查询当前值)
********
增加数据: Tools-Data Generator
Owner 用户
Table 表名
Number of records 增加数据条数
---锁----
锁格式:lock table 表名 in 锁定类型 mode;
共享:share
排他:exclusive
---视图****(安全省事)
视图格式:create view 视图名 as 视图项.....
以scott 表为例:
create [ or replace ] view 视图名 as select e.ename 员工姓名,e.sal 员工工资, s.grade 工资等级 from emp e join salgrade s on e.sal between s.losal and s.hisal;
select * form 视图名
--拓展:
复制表: create table 新表名 as select * from 要复制的表名;
只复制表结构
create table 新表名 as select * from 要复制的表名 where 跟(false表达式);
***---导表---
Tools-Export Tables 导出表 以.dmp格式存储
Tools-Import Tables 导入表
Day04:
过滤空值4法
---1) 将 comm 为空的值改为0 ('','');
Select comm,nvl (comm,0) from emp;
---2) 如果comm不为空 原值不变,空值为0 nvl2('','','')
>>>if else
Select comm ,nvl2 (comm,comm,0) from emp;
---3) Decode
Select 更改表项,decode(更改表项,'表值','要替换的值','都不是情况下走这');
替换表值可以是多个;
---4) Case...When...Then...Else...
Select 更改表项 ,Case表项 When'原值' Then '新值' Else '如果空走这' end from emp;
*使用时空格必须打对不然报错
分页:
--- Rownum 关键字 初始值1
Select Rownum rn ,e.* From Emp E;
---Page 页数
---Num 个数
例(Scott_emp表):
select *
from
(
select rownum rn,ename,sal
from
(
select ename,sal
from emp
order by sal desc
)
)
where rn>=6 and rn <=10;
******起始页=(当前页-1)*pageSize()
创建表空间:
---TableSpace 空间名 datafile '物理位置' size 给大小;
---大小给M以上
例:Careate tablespace kjm datafile 'd:/kongjian/kjm.dbf' size 100M;
---删除使用drop (datafiles)
例:drop tablespace 表名 including contents and datafiles;
删除 表空间 表名 包含的内容 和 数据文件
* 有值表的删除法
--- drop user 表名 cascade
* 操作表值时必须大小写分明;
--删除表字段
alter table demo drop(sex);