---json
{
"description": "This Howto will show you how to copy the data definition of a SQL table from another SQL table",
"low_quality_page": "false",
"name": "Create a table from a table",
"page_id": "qotxtxkjlhw885z3nse6b"
}
---
====== Tabul - How to create a table from the definition of another table ======
===== About =====
This [[..:howto|Howto]] will show you how to copy the [[docs:resource:structure|data definition]] of a [[docs:resource:sql_table|SQL table]] from another [[docs:resource:sql_table|SQL table]] by using the [[docs:tabul:data:create|Tabul table create command]].
This command does not copy the data. It just [[docs:op:create|create]] the structure (ie it performs a SQL ''CREATE'' statement). If you want to copy the data, use the [[docs:tabul:data:transfer|tabul data transfer command]] instead.
===== Steps =====
==== Start Postgres ====
The next steps will be using the [[howto:postgres:howto_connection_service|postgres howto connection]].
* You need to [[howto:postgres:howto_connection_service#docker|start the postgres docker image]].
tabul service start postgres
1 service was started
Name Type
-------- ------
postgres docker
* [[docs:tabul:connection:ping|Ping]] your connection to check that the database is up.
tabul connection ping postgres
The connection (postgres) has been pinged successfully
==== Create a table from another table ====
Delete the target destination
tabul data drop --no-strict-selection *@postgres
The below command will create:
* the table ''tpcds_sales'' into the [[howto_connection_service|Postgres Howto connection]]
* from the data definition of the [[docs:system:tpcds|tpcds]] ''store_sales'' table
* with the [[docs:tabul:data:create|tabul data create]] command.
tabul data create store_sales@tpcds tpcds_sales@postgres
where:
* ''store_sales@tpcds'' is a [[docs:flow:data_selector|data selector]]
* and ''tpcds_sales@postgres'' is a [[docs:flow:template_data_uri|target data URI]]
Results of the create operation
input target
----------------- --------------------
store_sales@tpcds tpcds_sales@postgres
==== Check the metadata ====
Check the metadata with the [[docs:tabul:data:describe|tabul data describe]] command
tabul data describe tpcds_sales@postgres
Structure of the resource tpcds_sales@postgres
position name type precision scale primary_key nullable autoincrement comment
-------- --------------------- ------- --------- ----- ----------- -------- ------------- -------
1 ss_sold_date_sk integer 10 0 x
2 ss_sold_time_sk integer 10 0 x
3 ss_item_sk integer 10 0 x
4 ss_customer_sk integer 10 0 x
5 ss_cdemo_sk integer 10 0 x
6 ss_hdemo_sk integer 10 0 x
7 ss_addr_sk integer 10 0 x
8 ss_store_sk integer 10 0 x
9 ss_promo_sk integer 10 0 x
10 ss_ticket_number integer 10 0 x
11 ss_quantity integer 10 0 x
12 ss_wholesale_cost numeric 7 2 x
13 ss_list_price numeric 7 2 x
14 ss_sales_price numeric 7 2 x
15 ss_ext_discount_amt numeric 7 2 x
16 ss_ext_sales_price numeric 7 2 x
17 ss_ext_wholesale_cost numeric 7 2 x
18 ss_ext_list_price numeric 7 2 x
19 ss_ext_tax numeric 7 2 x
20 ss_coupon_amt numeric 7 2 x
21 ss_net_paid numeric 7 2 x
22 ss_net_paid_inc_tax numeric 7 2 x
23 ss_net_profit numeric 7 2 x
==== Next ====
You can also create a table and its dependencies (foreign tables) at once with the [[docs:tabul:data:create|tabul data create command]].
\\
\\
[[howto:sql_schema:create_table_with_dependencies|How to copy a star/snowflake schema ?]]