{"id":119,"date":"2018-12-06T08:27:48","date_gmt":"2018-12-06T08:27:48","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/06\/sql-create-table\/"},"modified":"2024-03-03T18:02:08","modified_gmt":"2024-03-03T18:02:08","slug":"sql-create-table","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-create-table\/","title":{"rendered":"SQL CREATE Table"},"content":{"rendered":"<h2>The SQL CREATE TABLE Statement<\/h2>\n<p>The CREATE TABLE statement is used to create a new table in a database.<\/p>\n<h3>Syntax<\/h3>\n<blockquote>\n<div class=\"w3-example\">\n<div class=\"w3-code notranslate sqlHigh\">CREATE\u00a0TABLE\u00a0<em>table_name\u00a0<\/em>(<br \/>\n<em>\u00a0\u00a0\u00a0 column1 datatype<\/em>,<br \/>\n<em>\u00a0\u00a0\u00a0 column2 datatype<\/em>,<br \/>\n<em>\u00a0\u00a0\u00a0 column3 datatype<\/em>,<br \/>\n&#8230;.<br \/>\n);<\/div>\n<\/div>\n<\/blockquote>\n<p>The column parameters specify the names of the columns of the table.<br \/>\nThe datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).<\/p>\n<p><strong>Tip:<\/strong>\u00a0For an overview of the available data types, go to our complete\u00a0<a href=\"https:\/\/barkhane.com\/sql\/sql-data-types\/\">Data Types<\/a> Reference.<\/p>\n<hr \/>\n<h2>SQL CREATE TABLE Example<\/h2>\n<p>The following example creates a table called \\&#8221;Persons\\&#8221; that contains five columns: PersonID, LastName, FirstName, Address, and City:<\/p>\n<div class=\"w3-example\">\n<h3>Example<\/h3>\n<p><code><\/code><\/p>\n<pre class=\"w3-code notranslate sqlHigh\">CREATE\u00a0TABLE\u00a0Persons (\nPersonID int,\nLastName varchar(255),\nFirstName varchar(255),\nAddress varchar(255),\nCity varchar(255)\n);<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>The PersonID column is of type int and will hold an integer.<\/p>\n<p>The LastName, FirstName, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters.<\/p>\n<p>The empty \\&#8221;Persons\\&#8221; table will now look like this:<\/p>\n<div class=\"w3-responsive\">\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th>PersonID<\/th>\n<th>LastName<\/th>\n<th>FirstName<\/th>\n<th>Address<\/th>\n<th>City<\/th>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>Tip:<\/strong>\u00a0The empty \\&#8221;Persons\\&#8221; table can now be filled with data with the SQL\u00a0<a href=\"https:\/\/www.w3schools.com\/sql\/sql_insert.asp\">INSERT INTO<\/a>\u00a0statement.<\/p>\n<hr \/>\n<h2>Create Table Using Another Table<\/h2>\n<p>A copy of an existing table can also be created using CREATE TABLE.<\/p>\n<p>The new table gets the same column definitions. All columns or specific columns can be selected.<\/p>\n<p>If you create a new table using an existing table, the new table will be filled with the existing values from the old table.<\/p>\n<h3>Syntax<\/h3>\n<div class=\"w3-example\">\n<code><\/p>\n<pre class=\"w3-code notranslate sqlHigh\">CREATE\u00a0TABLE\u00a0<em>new_table_name<\/em>\u00a0AS\nSELECT\u00a0<em>column1, column2,...<\/em>\nFROM\u00a0<em>existing_table_name<\/em>\nWHERE\u00a0....;<\/pre>\n<p><\/code>\n<\/div>\n<p>The following SQL creates a new table called \\&#8221;TestTables\\&#8221; (which is a copy of the \\&#8221;Customers\\&#8221; table):<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}