1、数据库
|--------|-----------------------|-----------------------|
| oracle | hive | 高斯 |
| ORCL | create database 数据库名; | create database test; |
| | show databases; | \l |
| | use 数据库名 | \c 数据库名 |
2、分层
|-------------------------------------------|----------|---|---|
| oracle | hive | 高斯 ||
| user下schema(模式) | 数据库 | 数据库 ||
| alter session set currment_schema = '用户名' | use 数据库名 | \c 数据库名 ||
| 用户名.表名 | 数据库名.表名 | CREATE EXTENSION IF NOT EXISTS dblink; ||
3、建表语句 及 数据类型
|---------------------------------------------------------|---------------------------------------------------------------------------------------|-------------------------------------------------------------|
| oracle | hive | 高斯 |
| create table 表名(字段名 类型,...) | create table 表名(字段名 类型,...) row format delimited | create table 表名(字段名 类型,...) |
| int float varchar(n) varchar2(n) date 字符串单引号, 双引号做名字的强制 | int float varchar(n) string date timestamp , map ,array,struct 字符串单引号 双引号, 反引号做名字 的强制 | int float varchar(n),date,timestamp, 数组 字符串用单引号, 双引号做名字 的强制 |
| user_tables(字典里查看) | show tables | \dt |
| user_tab_columns(字典) | desc 表名 | \d 表名 |
| drop 表名 | drop 表名 | drop 表名 |
4、修改表结构 及 数据类型
|-----------------------------------------|------|---|---|
| oracle | hive | 高斯 ||
| alter table modify|drop |add |rename | 无 | alter table modify|drop|add|rename ||
5、修改删除数据
|---------------------------------------------------------------------------|---|---|---------------------------------------------------------------------------|
| oracle | hive || 高斯 |
| update 表名 set 字段名=值,.. where 条件 delete from 表名 where 条件 truncate table 表名 | insert overwrite || update 表名 set 字段名=值,.. where 条件 delete from 表名 where 条件 truncate table 表名 |
6、运算符
|--------------------------------------------------------------------|------------------------------------------------------------|----------------------------------------------------------------------|
| oracle | hive | 高斯 |
| + - * / mod power | + - * / % ** | + - * / % ^幂运算 |
| > >= < <= = != between ..and in isnull like % _ escape(声明转义字符) | > >= < <= = != between..and in isnull like % _ 转移字符 \ | > >= < <= = != t(真)f(假) between ..and in isnull like % _ 转移字符 \ |
| and or not | and or not | and or not |
7、函数
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| oracle | hive | 高斯 |
| mod power sqrt abs round, floor ceil trunc | mod power sqrt abs round, floor ceil trunc | mod power sqrt abs round, floor ceil trunc |
| length,lengthb, concat ||, instr,substr,replace,upper,lower,lpad,rpad,trim,ltrim ,rtrim | length,lengthb, concat ||, instr,substr,replace,upper,lower,lpad,rpad,trim,ltrim ,rtrim | length,lengthb, concat ||, instr,substr,replace,upper,lower,lpad,rpad,trim,ltrim ,rtrim |
| sysdate, date1-date2 天之差 months_between 月之差 date+n 加减天数 add_months() extract(year/month/day from 日期) trunc(日期,'yyyy 或 mm 或 dd') to_date(日期,'格式yyyyMMdd') date'2025-01-01' to_char(日期,'格式') D周 Q表季度 | current_date, current_timestamp datediff(date1,date2) 天之差 months_between 月之差 date_add(日期,正负n) 加减天数, add_months() year/quarter/month/day(日期) date('2025-1-1') date_format(current_date,'YYYYMMdd') u表周几 Q表季度 | now() date1-date2 ??? date+n 或 date_add 加减天数 select now() + interval 'n hour/day/month/year' 加 减指定的时间单位 extract(year/month/day/hour/minute/second from 日期) cast(日期字符串 as date) 或 date'日期字符串' to_char(日期,'yyyyMMdd') D 表周几 |
| sum, max,min,count,avg wm_concat, listagg | sum,max,min,count,avg collect_set(),collect_list() | sum,max,min,count,avg group_concat([distinct] 字段名) 字符串 array_agg([distinct] 字段名) 数组 |
| 统计: sum,max,min,avg,count 偏移:lead lag first_value 排序:row_number,rank,dense_rank 累计:percent_rank,cume_dist | 统计: sum,max,min,avg,count 偏移:lead lag first_value 排序:row_number,rank,dense_rank 累计:percent_rank,cume_dist | 统计: sum,max,min,avg,count 偏移:lead lag first_value 排序:row_number,rank,dense_rank 累计:percent_rank,cume_dist |
| regexp_count,regexp_substr, regexp_like, regexp_instr regexp_replace | regexp_extract regexp_replace | regexp_subst,regexp_replace regexp_count,regexp_instr regexp_like, regexp_regexp_split_to_array regexp_regexp_split_to_table |
| 无 | size,sort_array,array_contains map_keys,map_values collect_list,collect_set explode ,lateral view explode()虚拟表名 split(字符串) 结果为数组 数组元素访问从0开始 | array_length(数组字段,1) array_agg(普通字段) unnest(数组字段) array_to_string(数组字段,分隔符) array_cat(数组字段1,数组字段2) 数组访问从1开始 |
8、子查询
|--------------------------|-----------------------|--------------------------|
| oracle | hive | 高斯 |
| select from where having | from 后的子查询必须有别名 where | select from where having |
9、表关联
|------------------------|-----------------------------------------|------------------------|
| oracle | hive | 高斯 |
| 笛卡尔积, 内连接,左连 接,右连接,全连接 | 笛卡尔积, 内连接,左连接,右连接,全连 接,左半连接(left semi ) | 笛卡尔积, 内连接,左连 接,右连接,全连接 |
10、select 子句
|----------------------------------------------------------|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
| oracle | hive | 高斯 |
| select distinct from join where group by having order by | select distinct from join where group by having order by limit n 或 m,n 或 n offset m | select distinct from join where group by having order by limit n 或 m,n 或 n offset m |
11、匿名块
|-----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| oracle | hive |
| declare 变量名 类型; begin 语句; end | declare 变量名 类型; begin 语句; end |
| if 条件 then elsif 条件 then else end; | if 条件 then elsif 条件 then else end; |
| 赋值 变量名:=值 select 字段名 into 变量名 from 表 名 | select 字段名 into 变量名 from 表名 变量名:=值 变量名=值 |
| for i in 开始.. 结束 loop end loop; while 循环条件 loop end loop; | for i in 开始.. 结束 loop end loop; while 循环条件 loop end loop; |
| 引用类型 表名.字段名%type 表名 %rowtype, 行内列内容的数据 变量名.列名 | 引用类型 表名.字段名%type 表名%rowtype, 行内列内容的数据 变量名.列名,可以行变量打印 |
| 数组: type 类型名 is table of 元素类型 | declare type new_table is table of int; ids new_table; begin select t_id bulk collect into ids from t1; for i in 1..ids.count loop raise notice '第%个值为%',i,ids(i); end loop; end; 变量名 类型[] 定义数组 访问每个元素时用 变量名[] |
| 游标 cursor 游标名 is select语句 open游标名 fetch 游标名 into 变量名 while 游标名%found loop end loop; close 游标名 | declare cursor cur is select t_id from t1; tid int; begin open cur; fetch cur into tid; while cur%found loop raise notice '%',tid; fetch cur into tid; end loop; close cur; end; |
12、自定义函数 和 存储过程
|------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| oracle | 高斯 |
| create or replace function 函数名(参数名 参数类型,..) return 返回值类型 as begin return 变量名; end ; select 函数名(参数) from 表名 | create or replace function 函数名(参数名 参数类型,..) return 返回值类型 as begin r eturn 变量名; end ; select 函数名(参数) from 表名 |
| create or replace procedure 函数名(参数名 参数类型,..) as begin end ; call 过程名(参数) | create or replace procedure sp_getdate(sid int) as sname varchar(10) cursor cur is select t_name from t1 where t_id>=sid; begin open cur; fetch cur into sname; while cur%found loop raise notice '%',sname; fetch cur into sname; end loop; close cur; end; call sp_getdate(1); |
| 动态sql sql字符串 execute [immediate] sql into 变量 | declare sql_text varchar(100); t_n varchar(10):='t1'; t_names varchar(10)[]; i int; begin --动态sql sql_text:='select array_agg(t_name) from '||t_n|| ' where t_id>1'; --执行动态sql execute immediate sql_text into t_names; --遍历数组 if t_names is not null then for i in 1..array_length(t_names,1) loop raise notice '名字:%',t_names[i]; --高斯数组用方括号[] end loop; end if; end; |