天天看点

使用create table ...as创建表时要注意的问题

    1.hive中用CTAS 创建表,所创建的表统一都是非分区表,不管源表是否是分区表。所以对于分区表的创建使用create table ..as一定要注意分区功能的丢失。当然创建表以后可以添加分区,成为分区表。注意如果源表是非分区表则没有这个问题。

    2.如果使用create table as  select  *  创建表时源表是分区表,则新建的表会多字段,具体多的字段个数和名称就是源表分区的个数和名称。当然如果select选择的是指定的列则不会有这种问题。

    3.如果源表的存储格式不是TEXTFILE。则使用CTAS创建的表存储格式会变成默认的格式textfile。比如这里源表是RCFILE。而新建的表则是TEXTFILE。当然可以在使用create table ....as创建表时指定存储格式和解析格式,甚至是列的名称等属性。具体参考博客:hive使用create as创建表指定存储格式等属性

   4.使用CTAS方式创建的表不能是外部表。

   5.使用CTAS创建的表不能分桶表。

查询官网发现原来使as创建表会有如下问题:

Oracle Database automatically defines on columns in the new table any  NOT NULL  constraints that were explicitly created on the corresponding columns of the selected table if the subquery selects the column rather than an expression containing the column. If any rows violate the constraint, then the database does not create the table and returns an error.

       1.显示的NOT NULL约束自动会带到新表。

NOT NULL  constraints that were implicitly created by Oracle Database on columns of the selected table (for example, for primary keys) are not carried over to the new table.

       2.隐式的NOT NULL约束不会带到新表,如主键。

In addition, primary keys, unique keys, foreign keys, check constraints, partitioning criteria, indexes, and column default values are not carried over to the new table.

      3.另外最关键的是,主键,唯一,外键,check约束,分区,索引以及列的默认值不会带到新表。

If the selected table is partitioned, then you can choose whether the new table will be partitioned the same way, partitioned differently, or not partitioned. Partitioning is not carried over to the new table. Specify any desired partitioning as part of the  CREATE TABLE  statement before the  AS subquery  clause.

继续阅读