{"id":992,"date":"2021-09-11T21:14:15","date_gmt":"2021-09-11T15:44:15","guid":{"rendered":"https:\/\/techieshouts.com\/home\/?p=992"},"modified":"2022-08-09T19:03:33","modified_gmt":"2022-08-09T13:33:33","slug":"creating-a-table-in-td-using-another-table","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/creating-a-table-in-td-using-another-table\/","title":{"rendered":"Creating a table in Teradata using another table"},"content":{"rendered":"\n<p>Creating a table in <a href=\"https:\/\/www.teradata.com\/\" target=\"_blank\" rel=\"noopener\">Teradata<\/a> can be done in multiple ways. In this post, we will see how to create a table from another table without the select statement. The following examples will show how to create a table from another table in Teradata.<\/p>\n\n\n\n<h2>Creating on the fly with no data<\/h2>\n\n\n\n<p>This is the case for creating a table in Teradata using another table name<\/p>\n\n\n\n<h3>Syntax<\/h3>\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 DATABASE.TABLEA as DATABASE.TABLEB WITH NO DATA;<\/pre>\n\n\n\n<p>Yes, the select statement from the source table is missing but still, the target table will be created with the structure of the source table.<\/p>\n\n\n\n<p>Let us see an example with this syntax<\/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 testdb.newtesttable as testdb.oldtesttable with no data;<\/pre>\n\n\n\n<p>In this example, a new table &#8220;newtesttable&#8221; will be created with the same structure as the &#8220;oldtesttable&#8221;. However, the data from the source table will not get copied to the target table as we have given the &#8220;with no data&#8221; option at the end of the create table statement.<\/p>\n\n\n\n<h2>Creating a table on the fly with data<\/h2>\n\n\n\n<p>This is the case for creating a table in Teradata with data. The data will be available as soon as the statement is executed.<\/p>\n\n\n\n<h3>Syntax<\/h3>\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 DATABASE.TABLEA as DATABASE.TABLEB WITH DATA;<\/pre>\n\n\n\n<p>This method of creating a table will copy the data from the source table to the new target table that is getting created.<\/p>\n\n\n\n<p>Let us see an example using this syntax<\/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 testdb.newtesttable as testdb.oldtesttable with data;<\/pre>\n\n\n\n<p>In this example, the new target table &#8220;newtesttable&#8221; will be created from the source table &#8220;oldtesttable&#8221;. During the table creation, the data from the source table will also get copied to the target table as we have given the option &#8220;with data&#8221; at the end of the create table statement. <\/p>\n\n\n\n<h2>Creating a table on the fly with index but not data<\/h2>\n\n\n\n<h3>Syntax<\/h3>\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 DATABASE.TABLEA as DATABASE.TABLEB WITH NO DATA PRIMARY INDEX(colname);<\/pre>\n\n\n\n<p>This method will create the target table with the structure of the source table without any data being copied. It will also create the &#8220;primary index&#8221; for the specified column<\/p>\n\n\n\n<p>Let us see an example using this syntax<\/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 testdb.newtesttable as testdb.oldtesttable with no data primary index(custid);<\/pre>\n\n\n\n<p>In this example, the target table &#8220;newtesttable&#8221; will get created with the structure of the &#8220;oldtesttable&#8221;. But, since, we have given the option &#8220;with no data&#8221;, the table will only be created as empty. Also, the primary index will be created on one of the columns in the table.<\/p>\n\n\n\n<h2>Creating a table on the fly with index and data<\/h2>\n\n\n\n<h3>Syntax<\/h3>\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 DATABASE.TABLEA as DATABASE.TABLEB WITH DATA PRIMARY INDEX(colname);<\/pre>\n\n\n\n<p>This will help us to create the target table with both the index and data.<\/p>\n\n\n\n<p>Let us see an example using this syntax<\/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 testdb.newtesttable as testdb.oldtesttable with data primary index(custid);<\/pre>\n\n\n\n<p>In this example, the target table &#8220;newtesttable&#8221; will get created with the structure of the &#8220;oldtesttable&#8221;. As we have given the option &#8220;with data&#8221; in the statement, the data from the source table will also get copied to the target table. Also, as specified, the primary index will be created on the &#8220;custid&#8221; column in the table.<\/p>\n\n\n\n<p>Also check,<\/p>\n\n\n\n<ol><li><a href=\"https:\/\/techieshouts.com\/home\/create-table-types-in-teradata-with-examples\/\">Creating tables in Teradata with examples<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/create-table-using-select-in-teradata\/\">Creating tables in Teradata using Select<\/a><\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a table in Teradata can be done in multiple ways. In this post, we will see how to create a table from another table without the select statement. The following examples will show how to create a table from another table in Teradata. Creating on the fly with no data This is the case\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/creating-a-table-in-td-using-another-table\/\">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":[9],"tags":[204,67,66,153,166,203,238,147],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/992"}],"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=992"}],"version-history":[{"count":5,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/992\/revisions"}],"predecessor-version":[{"id":1193,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/992\/revisions\/1193"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}