1.创建数据库
格式
create database [if not exits] database_name [comment "说明内容"]
[location '指定路劲' ] [ with dbproperties( 'create_date'=' 2023-3-13')] #可以任意的
案例:
create database if not exist student location '/user/hive/datas/student';
查看数据信息
desc [extended|formatted] database_name;
删除数据库
语法: drop database database_name;
案例:
drop database student;
2.创建数据表
注:
temporary 内部表 ;external 管理表 \外部表
案例一
创建数据表 默认内部表
create table if not exits student(
id int comment '学生学号',
name string comment 'student姓名')
row format delimited files terminated by '\t'
location '/user/hive/student'
删除数据表
drop table student;
当删除内部表时 hadoop上hdfs的数据表路径和数据表不存在 一起删除
创建数据表 默认外部表
create external table if not exits student(
id int comment '学生学号',
name string comment 'student姓名')
row format delimited files terminated by '\t'
location '/user/hive/student' 当删除外部表时 hadoop上hdfs的数据表路径和数据表还存在
serde:
row format delimited[files terminated by '列分隔符']
[lines terminated by '行分隔符']
同时创建数据表和数据
案例01:
Create table student_001 As select * from student;
复制表结构和将查询的数据插入到新的数据表
案例02
Create table student_002 like student; (student是存在的数据表)
将查询的数据插入到新的数据表
insert into student_002 select * from student;
删除数据表
Drop table table_name;
清空数据表内容
truncate table table_name;
查询数据
selcet * from student;
修改数据表表名
** Alter table** student_001 rename to student001;
修改列信息
1.增加列
alter table table_name add columns (colu_name col_type ...)[comment '说明']
案例:
alter table student add columns(sex string);
2.更新列
alter table table_name change col_old_name col_new_name col_new_type
[first/after old_col_name]
案例:将hive中的强制转换类型关闭
set hive.metastore.disallow.incompatible.col.type.changes=false;
alter table student change sex sex varchar(16) after phone;
3.替换列(重新定义数据表的列)
alter table table_name replace columns (col_name col_type...) [comment '说明']
案例:
alter table student replace columns (name string,id int,address string, phone int);