Oracle and Mysql

--oracle 语法
SELECT DISTINCT
  <select_list>
FROM
  <left_table> <join_type>
JOIN <right_table> ON <join_condition>
WHERE
  <where_condition>
GROUP BY
  <group_by_list>
HAVING
  <having_condition>
UNION 
  <condition>
ORDER BY
  <order_by_condition>



--mysql
SELECT DISTINCT
  <select_list>
FROM
  <left_table> <join_type>
JOIN <right_table> ON <join_condition>
WHERE
  <where_condition>
GROUP BY
  <group_by_list>
HAVING
  <having_condition>
ORDER BY
  <order_by_condition>
LIMIT <limit_number>











 

-- union all去重   union  不去重复    对应 mysql中的 inner  join 
select * from emp where sal <1800
union all 
select * from emp where sal >1800
--minus  求差集   对应mysql中的 outer join
select * from emp where sal <1800
minus 
select * from emp where sal >1800

--求交集 intersect   对应mysql中的  outer join

select * from emp where sal <1800
intersect 
select * from emp where sal >1800

--字段的连接   类似于 mysql中的  concat
select  empno|| sal from emp
--单行函数 
-- (1)日期函数
select  sysdate  from dual
--(2)数字函数   100的 平方

select  sqrt(100) from dual
-- (3)字符函数  大小写转换
select lower('DDD') from dual
select super('aa') from dual
--(4) 转换函数  to_char() to_date()  to_number()
select to_char(sysdate,'yyyy"年"/MM"月"-dd"日"')  from dual
select to_char(12398,'$999,999') from dual  --数字转字符转
select  to_date('2017/6/28','yyyy-MM-dd') from dual--  日期转字符串

--  其他没有分类的函数   nvl   nvl2  decode
select ename ,nvl(comm,0) 
from emp  ( 判断是否为空)

select ename ,nvl2(comm,sal+conm,sal)  (判断是否为空类似于三元)
from emp

--分析函数  rank  dense_rank  row_unmber
 
select ename ,deptno,sal,
row_number() over(partition by deptno order by sal desc) as "ROW_NUMBER"
from  emp



-- 表空间分类   

--永久性表空间   system  users  一般用于保存表,视图,过程和索引等数据
--临时性表空间    temp    保存系统中短期的数据
--撤销表空间      undo   用来 帮助退回未提交的事务数据


--  alter tablespace tp_hr readonly  修改为只读

--创建一个序列
create sequence s_person4
start with 1000 --默认从几开始
increment by 2  --默认增加几
maxvalue 9999
minvalue 1000
noycle
cache 10  --缓存
select s_person3.nextval from dual
select s_person3.currval from dual


--在并发的情况下 我们使用SYS_GUID
select sys_GUID() from dual
--在不并行的时候 使用 GUID

--同义词的使用

(私有同义词)

create or peplace  synonym  别名 for scott.emp;
(共有同义词)
create public   synonym  别名 for scott.emp;

删除 drop  synonym 名字


create synonym e for scott.emp   -- 私有同义词

select * from emp


select * 
from dept 
left outer join e 
on dept.deptid=emp.deptid

-- oracle 数据库的索引
分区或者非分区 , 
B树索引
正向或者反向索引
位图索引 

--逻辑索引
 单列或者组合索引
 唯一或者非唯一索引
 基于函数索引

-- 创建  反向索引的语法
CREATE  [unique]INDEX 索引名 ON 表名 (列名,列名2)reverse
TABLESPACE 表空间名;

-- 创建  (B+树)正向索引的语法
CREATE  [unique]INDEX 索引名 ON 表名 (列名,列名2)reverse
TABLESPACE 表空间名;

--创建位图索引的语法


create BitMAP  Index indexName on tableName(colunm)


--注意  B树索引  使用  or  效率低   行级锁定   存储多
--位图索引,对于每一个值 通过 or查询效率高,位图 端锁

频繁搜索,排序,分组的列可以作为索引
影厂用作连接的列(主键,外键)可以作为索引
定期重建索引
包含几个不同值的列建议使用位图索引
不要在仅包含几行的表中创建索引
alert  index indexname rebuild noreverse






--oracle 的分区表
--
(1)允许用户将一个表划分成多个分区
(2)用户可以执行查询,只访问表中的特定分区
(3)将不同的分区存储到不同的磁盘,提高访问的性能。
(4)可以独立的恢复和备份数据;

范围分区:将表中的一列或者多列 按照范围进行划分  
partition by range(column_name)
(
     partition part1 value less than (range1),
     partition part1 value less than (range2),
     partition part3 value less than (maxvalue),     

);

创建一张表  同时划分分区
create table testll 
partition by range(sal)(
     partition part1 values less than (1000),
     partition part2 values less than (2000),
     partition part3 values less than (maxvalue)     

)
as select * from test11 partition()
--按照范围分区进行查询(选择的时候有些 列是没有分区的)
select * from testll partition(part1)


--间隔分区

partition by range(column_name)
(
     -- n 代表 指定的值   interval_until 代表的单位
     interval (numtoyminterval(n,"interval_until"))
     partition part1 value less than (range1),
     partition part1 value less than (range2),
     partition part3 value less than (maxvalue),     

);

--数据库链
create [public] database like like_name
connect to username identified by password
using 'servername/serverurl'; 



show user :--查看当前的用户
show parameter db_name



-- 创建表空间
create tablespace  tablename  -- 创建表空间
datafile 'd:\data.dbf' --存放路径
size 100m -- 表空间大小
autoextend on --表空间可以自动扩展
next 10m ;

-- 删除表空间
drop tablespace tablename

-- 创建用户   被谁创建
create user  itrip   --创建一个用户
identified by 123456  -- 创建一个用户密码
default tablespace tablename  -- 指定该用户出现的表空间

-- 给用户授权我们才可以登录  oracle 数据库中常用的角色  
--connect-- 连接角色 基本角色   resource --开发者角色  dba-- 超级管理员角色

-- 给itrip 用户授予权限 (resource  connect  dba )
grant dba to itrip
--切换用户
conn itrip

--创建一张表 
-- dataType  varchar   char
-- number(n)
--varchar2 会自动截取长度 ,但是不会自己扩展 
--  表示一个整数
--number(m,n)表示一个小数,总长度是 m 小数是n 整数是m-n
-- 日期类型 data 
timestamp 精确到小数点后6位
--大对象  CLOB  4个G  存储文本
---Blob    存储二进制 可存4G   视频  图片

create  table person3(
        pid number(20),
        pname varchar2(10)
);

--修改表结构  

--(1)添加一列
alter table person3 add name2 number(1);

--修改列类型 

alter table person3 modify sex char(1) 

--修改列的名称

alter table person2 rename column gender to sex

--删除一列
alter  table person2 drop column sex;

-- 数据的增加

insert  into person3 (pid,pname) values (1, '小明');
commit;

--修改数据

update person3 set pname ='小马 where pid =1;

--删除操作
delete from person;

--删除表结构
drop table person
--先删除表,在创建表,在数据量大情况下,尤其是在表中带有索引,执行效率比较高
--索引提高查询效率,会影响怎删改效率
truncate table person


--序列 (oracle 默认不会给我们生成主键):默认从1开始依次递增
--主要用来赋值, 默认不属于任何一张表 

--dull 是
create sequence s_person3
increment by 2  --默认增加几
start with n --默认从几开始
cycle noclycle  循环
ccache n  --缓存
select s_person3.nextval from dual
select s_person3.currval from dual

insert  into person3 (pid,pname) values (s_person3.nextval,'小明');

select * from a where id =1
union 
select * from a where id =2

-- 单行函数  作用于一行,返回一个值
(1)字符函数  大小写转换
select upper('yes') from dual
select lower('yes') from dual 
--字符串拼接
select CONCAT(a,B) from dual  




--多行函数  作用域多行,返回一个值





 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值