–查看所有库
sqlshow databases;
– 使用库
use log;
–查看所有表
show tables;
–创建库
–语法
create database [if not exists] databaseName [comment '注释'];
create database if not exists log;
create database if not exists logtemp comment 'this is log database';
–查询数据库信息
describe database [extended] databaseName;
desc database [extended] databaseName;
desc database hadoop2;
–创建表
create table if not exists student(
id int,
name string
)
row format delimited fields terminated by '\t'
stored as textfile;
--utf-8
--建表时最后一行添加
CHARSET=utf8
–删除表
–语法
drop table tableName;
drop table if exists student;
–删除库
drop database log;
drop database log cascade;
–修改表
– 关键字 alter
–修改表名
alter table student rename to teacher;
–查看表结构
desc teacher;
desc formatted teacher;
–修改字段名
alter table teacher change column id myid string comment 'alter id';
alter table teacher change column name name string comment '' after myid;
–增加字段名 在最后添加
alter table teacher add columns(
sex string,
age int comment 'this is teacher age'
);
–加载 本地 数据
load data local inpath '/home/hadoop/data/studentInfo.txt' into table teacher;
–创建表
--like 只创建表结构
create table if not exists student like teacher;
--as 创建表结构 并且 复制数据
create table if not exists student as select * from teacher;
--加载 HDFS 数据
load data inpath '/data/studentInfo.txt' into table teacher;
–更新和覆盖数据
--追加
load data local inpath '/home/hadoop/data/studentInfo.txt' into table teacher;
--覆盖
load data local inpath '/home/hadoop/data/studentInfo.txt' overwrite into table teacher;
–查询
select * from teacher limit 5;