赞
踩
目录
show databases;
use database_name;
- create database [if not exists] database_name
- [comment database_comment] --注释
- [location hdfs_path] --存储在hdfs上的位置
desc database db_name;
- drop database [if exists] database_name [restrict|cascade]
- 默认为restrict,如果数据库内有表,想要一并删除可以使用cascade
- create [temporary] [external] table student(
- id int,
- name string,
- age int)
- [row format delimited] --分隔符设置开始语句
- [fields terminated by ‘\t’] --单引号内填分隔符
- [stored as textfile] --文件保存的类型
- [location hdfs_path]; --指定表的存储位置
- create table table_name(
- id int,
- name string)
- row format delimited fields terminated by ‘\t’;
- create external table student(
- id int,
- name string)
- row format delimited fields terminated by ‘\t’
- location ‘/hive/emp_external’;
- create table student(
- id int,
- name string)
- partitioned by(score string) --创建一个表,并设置以”score”字段分区
- (临时表仅对当前对话可见(session),临时表数据存储在用户的暂存目录,并在对话结束后删除。如果哦临时表与永久表表名相同,则在此对话中对表名的引用全部指向临时表。)
- 限制:不支持分区,不支持创建索引
-
- create temporary table student(
- id int,
- name string);
create table table_name as select * from student where id < ‘20’;
- creeate [temprary] [external] table [if not exists] [db_name.]table_name --创建表表名
- like old_table_name --被复制表的表名
- [location hdfs_path];
示例:
create table student2 like student1;
- -- 加载数据到 student表中
- load data [local] inpath “/data/student.csv” intp table student; --如果路径时linux就加local关键字
alter table table_name rename to new_table_name;
- alter table table_name change col_old_name col_new_name col_type
- [first|after column_name];
- --修改字段名和类型
- alter table student change id id_new int;
- --修改字段id的名称,并将其放在name字段后
- alter table student change id id_new int after name;
alter table table_name add columns (score int comment ‘1-100’);
- -- 清空整个表或表指定分区中的数据
- truncate table table_name [partition (partition_column = partition_col_value, ...)];
- truncate table student;
- truncate table student partition(score = 100);
- drop table [if exists] table_name
- 删除内部表的元数据和存储在hdfs上的数据,外部表只会删除元数据
show create table table_name;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。