1. 程式人生 > >使用create table ...as建立表時要注意的問題

使用create table ...as建立表時要注意的問題

工作中有時候做hive開發了,需要對一張表進行備份。一般都會使用 create table as  ...簡單方便,但是需要注意as建表產生的問題,因為as建表並不會儲存原表樣式。
  create table mytest_tmp1 
   as   
  select *  from FDM_SOR.mytest_deptaddr where statis_date='20180229';
hive中用create table as建立表注意事項:  1.對於hive中用create table as 建立表,所建立的表是非分割槽表,當然建立表以後可以新增分割槽,成為分割槽表。

    2.如果create table as select  from,後面所選擇的是表是非分割槽表,則建立的表結構一樣(不考慮hive中索引的使用,沒意義,hive是批處理系統)。但如果select from後面的表是分割槽表,則建立表和原表結構不一樣,因為分割槽表用select查詢會多一個分割槽欄位,即新建立的表會比原表多一個欄位。

但是對於oracle和mysql用create table as 建立表要注意如下事項:查詢官網發現原來使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.
 在新表上可以選擇是否像像舊錶那樣分割槽,或者不同的分割槽形式,或者建立非分割槽表。在AS subquery句之前指定。    具體like ,as建表相關問題,可以參考部落格:http://blog.csdn.net/qq_26442553/article/details/78805443