{"id":649,"date":"2019-09-15T17:43:52","date_gmt":"2019-09-15T12:13:52","guid":{"rendered":"https:\/\/techieshouts.com\/?p=649"},"modified":"2022-08-09T19:05:59","modified_gmt":"2022-08-09T13:35:59","slug":"create-table-using-select-in-teradata","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/create-table-using-select-in-teradata\/","title":{"rendered":"CREATE TABLE using SELECT in Teradata"},"content":{"rendered":"\n<p>In the other post, we saw how to create tables in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Teradata\" target=\"_blank\" rel=\"noopener\">Teradata<\/a> and the corresponding examples for each. Here we will see how to create a table with select from another table. In this post, we will see how we can create a table using select in Teradata. There are different ways in which we can create a table in Teradata using the select query.<\/p>\n\n\n\n<h3>Create table using select (without data and index)<\/h3>\n\n\n\n<h4>Syntax<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE [SET\/MULTISET\/BLANK] TABLE DBTEST.TESTTABLE AS\n(\nSELECT COL1, COL2, COL3 \nFROM\nDBNAME.TABLENAME\n)WITH NO DATA\n[BLANK\/NO INDEX];<\/pre>\n\n\n\n<p>In the above example, please note the word &#8220;WITH NO DATA&#8221; after the &#8216;)&#8217;. This will create a new table with the structure the same as the select statement without copying the data.<\/p>\n\n\n\n<p>Let us see a real-time example for the above syntax. The below code will create an empty table &#8216;dim_channel&#8217; in the &#8216;testdb&#8217;. The structure of the table will be as of the Select statement given inside the brackets. In this case, we are only creating an empty table with no index. Instead of column names, you can also use &#8220;select *&#8221; <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create table tesdtdb.dim_channel as\n(\nselect \nchnl_key, chnl_nm, chnl_type \nfrom prod.dim_channel\n)with no data\n;<\/pre>\n\n\n\n<h3>Create table using select without data with index<\/h3>\n\n\n\n<p>Similar to the above method, we can create a table from another table with the index also. You can specify the primary index.<\/p>\n\n\n\n<h4>Syntax<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE [SET\/MULTISET\/BLANK] TABLE DBTEST.TESTTABLE AS\n(\nSELECT COL1, COL2, COL3 \nFROM\nDBNAME.TABLENAME\n)WITH NO DATA\nPRIMARY INDEX(index col name);<\/pre>\n\n\n\n<p>In the above syntax, you can see the inclusion of &#8216;PRIMARY INDEX&#8217; at the end of the statement. This will create the new table along with the index column.<\/p>\n\n\n\n<p>Let us see an example for the create table using select along with index but without data. The below code will create the &#8216;dim_channel&#8217; table under &#8216;testdb&#8217; without any data. But the table will have a primary index on the &#8216;chnl_key&#8217; column.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create table tesdtdb.dim_channel as\n(\nselect \nchnl_key, chnl_nm, chnl_type \nfrom prod.dim_channel\n)with no data\nprimary index(chnl_key);<\/pre>\n\n\n\n<p>If you do not give any option like SET or MULTISET, it will take MULTISET by default. So when you try to create a SET table with no index, it will throw the below error.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Executed as Single statement.  Failed [3706 : 42000] Syntax error: Cannot create a NoPI table as a SET table.<\/pre>\n\n\n\n<h3>Creating table using select with data but no index<\/h3>\n\n\n\n<p>While creating the table if you want to load the data also, you can use the &#8220;WITH DATA&#8221; option at the end of the statement. <\/p>\n\n\n\n<p>Let us see an example for the case where you want to create a table on the fly using a select statement along with the data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create table tesdtdb.dim_channel as\n(\nselect \nchnl_key, chnl_nm, chnl_type \nfrom prod.dim_channel\n)with data;<\/pre>\n\n\n\n<p>The above code will create the table &#8216;dim_channel&#8217; along with the data from the production table. Here, since we did not specify any index option, there won&#8217;t be any index created.<\/p>\n\n\n\n<h3>Create table using select with data and index<\/h3>\n\n\n\n<p>There are cases when you create a table on the fly along with the index for the proper distribution of the data. You can use &#8216;PRIMARY&#8217; or &#8216;UNIQUE PRIMARY INDEX&#8217; for that matter. This can be combined with the &#8216;WITH DATA&#8217; option so that the target table will be created along with the data and index.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create table tesdtdb.dim_channel as\n(\nselect \nchnl_key, chnl_nm, chnl_type \nfrom prod.dim_channel\n)with data\nprimary index(chnl_key);<\/pre>\n\n\n\n<p>The point here is if you want to create a SET table, you have to create an index. Otherwise, it will throw the error.<\/p>\n\n\n\n<p>Also check &#8220;<a href=\"https:\/\/techieshouts.com\/create-table-types-in-teradata-with-examples\/\">Creating tables in Teradata<\/a>&#8220;, &#8220;<a href=\"https:\/\/techieshouts.com\/home\/creating-a-table-in-td-using-another-table\/\">Creating table in Teradata using another table<\/a>&#8220;<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the other post, we saw how to create tables in Teradata and the corresponding examples for each. Here we will see how to create a table with select from another table. In this post, we will see how we can create a table using select in Teradata. There are different ways in which we\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/create-table-using-select-in-teradata\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,9],"tags":[69,68,204,67,42,66,72,203,147,146,148],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/649"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":9,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/649\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}