SQLite CREATE TABLE AS但重命名列并保留原始数据类型,键?

时间:2018-03-02 19:23:24

标签: sql sqlite

SQLite目标

我想从一个连接(或一系列连接/子查询)创建一个新表。我希望新表保留原始表中的数据类型以及唯一索引。

另外,我想重命名过程中的一些列。要在SQLite中执行此操作,我的印象是我需要执行以下步骤:

  
      
  1. 将联接表创建为临时表
  2.   
  3. 使用所需的列名,数据类型和索引
  4. 创建一个空的新表   
  5. 将临时表插入新表。
  6.   

问题:是否有更好或更简洁的方法来完成此任务?

上述过程的代码示例:

--create joined table as tmp
DROP TABLE if exists tmp;

CREATE TABLE tmp AS
SELECT 
    individual_contributions.cmte_id, 
    cmte_pty_affiliation, 
    name, 
    employer, 
    transaction_dt,
    transaction_amt,
    file_num,
    cand_id,
    sub_id 
    FROM individual_contributions LEFT JOIN committee_master 
    ON individual_contributions.cmte_id=committee_master.cmte_id
    WHERE employer LIKE "Goldman Sachs"
    GROUP BY sub_id;
--create new table with desired specs
DROP TABLE if exists cm_indiv;

CREATE TABLE cm_indiv (
    cmte_id TEXT NOT NULL,
    cmte_pty_affiliation TEXT,
    contributor_name TEXT,
    contributor_employer TEXT,
    contributor_transaction_dt TEXT,
    cand_id TEXT,
    transaction_amt TEXT,
    file_num NUMERIC,
    sub_id NUMERIC NOT NULL
);

CREATE UNIQUE INDEX idx_cm_indiv ON cm_indiv (sub_id);
--insert joined table into new table
INSERT INTO cm_indiv (
    cmte_id,
    cmte_pty_affiliation,
    contributor_name,
    contributor_employer,
    contributor_transaction_dt,
    cand_id,
    transaction_amt,
    file_num,
    sub_id
    )
SELECT 
    cmte_id,
    cmte_pty_affiliation,
    name,
    employer,
    transaction_dt,
    cand_id,
    transaction_amt,
    file_num,
    sub_id
    FROM tmp;

2 个答案:

答案 0 :(得分:1)

这个CTAS使用SQL Fiddle(SQLite-WebSQL)为我工作。因此,在创建表时,请在select语句中使用首选列名,如下所示。

create table tbl( id int);

insert into tbl values (1);

create table new_tbl 
as 
select id as new_id 
from tbl;

select new_id from new_tbl;

Result: 
new_id
1

答案 1 :(得分:0)

首先创建新表,然后直接插入已连接的数据:

<img alt="#yeşil  #manzara #doğa  
#yayla #nature #naturelovers #adventuretime #adventures #mountainstaries 
#picture #şehirdenuzak  #tatil #holiday #cow  #potography #view #kütükev 
#naturelife #animal #amazing  #kar #winter #winteriscomming #mapavr1 #artvin 
#tulumile #insaatr #tulumci #rize 
class="_2di5p" sizes="171px" srcset="https://scontent-mxp11.cdninstagram.com/vp/c883e0c4267c003843fafeda255f1329/5A9D3C97/t51.2885-15/s150x150/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg 150w,
https://scontent-mxp1-1.cdninstagram.com/vp/6a3480f8658b50c691bcc100a96cc6f0/5A9CC9DC/t51.2885-15/s240x240/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg 240w,
https://scontent-mxp1-1.cdninstagram.com/vp/461c138e15f52420c3fbc075fab027eb/5A9DD808/t51.2885-15/s320x320/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg 320w,
https://scontent-mxp1-1.cdninstagram.com/vp/ad5d67f1c9ea77d78d145501e73c2ea0/5A9CAF9D/t51.2885-15/s480x480/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg 480w,
https://scontent-mxp1-1.cdninstagram.com/vp/e0636f79adc1ae53f7321d10fe60f275/5A9CD134/t51.2885-15/s640x640/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg 640w" 
src="https://scontent-mxp1-1.cdninstagram.com/vp/e0636f79adc1ae53f7321d10fe60f275/5A9CD134/t51.2885-15/s640x640/e15/c0.90.720.720/28154674_2016914221854461_991623208941649920_n.jpg" style="">