hive的数据库操作
规则语法
大小写规则:
1. hive的数据库名、表名都不区分大小写
2. 建议关键字大写
命名规则:
1.名字不能使用数字开头
2.不能使用关键字
3.尽量不使用特殊符号
库操作语法
hive> create database test;
hive> create database if not exist test;
hive> create database if not exist test comment "this is a comment";
hive> select * from DBS; # 查看数据库
hive> select * from TBLS; # 查看表
hive> show database;
hive> use mydb; #切换数据库
hive> desc database test1; # 查看数据库信息
hive> drop database test1 #删除数据库 只能删除空库
hive> drop database test1 cascade; # 强制删除
表的基本操作
hive> create table t_user(id int,name string);
hive> select current_database(); #查看当前使用的数据库是哪个
hive> create table mydb.t_user(id int,name string); #指定mydb数据库
hive> show tables in mydb; #查看另外一个数据库中的表
hive> create table if not exists emp(
eno int,
uname string,
job string,
mgr int,
hiredate int,
salary int,
comm int,
deptno int
)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;
hive> desc t1 # 查看表结构
hive> desc extended t1; #查看表结构 更详细
hive> alter table t1 rename to t2 #改表名
hive> alter table t2 change column uname sname string; #改列名
hive> alter table t2 change column english english int after chinese; # 修改列的位置
hive> alter table t2 change column english english int first; # 修改列的位置
hive> alter table t2 add column (sex int,...); # 增加字段
hive> alter table t2 replace columns(
id int,
name int,
size int,
pic string
); #删除字段
# 注意:实际上是保留小括号内的字段
hive> drop table t2; #删除表
本文详细介绍了Hive的数据库操作,包括创建、查看、切换、删除数据库的语法,以及表的操作,如创建、查看、修改、删除表的详细步骤。此外,还涵盖了表的重命名、列的增删改等高级操作。内容全面,适合Hive初学者和进阶者参考。
5484

被折叠的 条评论
为什么被折叠?



