--创建表(最简单)createtableifnotexists person(
id int,
name string
)row format delimited fieldsterminatedby'\t';--创建表(复杂字段类型,定义怎么分割,并且是外部表)createtable test(
name string,
friends array<string>,
children map<string,int>,
address struct<street:string,
city:string,
email:int>)row format delimited fieldsterminatedby','
collection items terminatedby'_'
map keysterminatedby'_'linesterminatedby'\n';--创建外部表(external定义为外部表,comment 为字段描述,tblproperties为表的属性)create external tableifnotexists person(
id intcomment'this is id',
name string comment'this is name')comment'this is table'row format delimited fieldsterminatedby' '
location '/demo'
tblproperties('version'='5.1');--查看创建表的语句showcreatetable 表名;--查看表信息(字段,字段类型,字段的描述信息)desc 表名;--查看表的详细信息desc formatted 表名;--内部表和外部表的转换:'EXTERNAL'='TRUE'必须为大写--EXTERNAL的值为TRUE-外部表/FALSE-内部表altertable 表名 set tblproperties('EXTERNAL'='TRUE');--修改表名称altertable 表名 renameto 新表名
--修改字段的名称或类型--注意:字段的类型和原来的类型是否可以隐式转换如果不能则会失败altertable person change column id newId intCOMMENT'this is id';--添加和替换列的字段ALTERTABLE table_name ADD|REPLACECOLUMNS.......--注意(REPLACE):-- 1.替换的字段的类型和原来的类型是否可以隐式转换如果不能则会失败-- 2.替换的时候是从第一个字段开始依次替换。如果替换的字段少于被替换的字段-- 那么原字段将缺失。--删除表droptable dept;--清空表,只能清空内部表truncatetable 表名