Yogesh Chauhan's Blog

How to CREATE TABLE in SQL with and without using Another Table?

in SQL/MySQL on April 29, 2020

The SQL CREATE TABLE Statement

Creates a new table in a database.

Syntax


CREATE TABLE table_name (
    column1_name datatype,
    column2_name datatype,
    column3_name datatype,
   ....
);

Where the datatype parameter specifies the type of data the column can hold for e.g. varchar, integer, date, binary, text, longtext etc.

SQL CREATE TABLE Example


CREATE TABLE Users (
    UserID int,
    LastName varchar(255),
    FirstName varchar(255),
    Email varchar(255),
    Address varchar(255)
);

Whenever we add varchar as datatype, we are required to specify length of the field. It can take up to 255 characters.

Create Table Using Another Table

We can create a copy of an existing table using CREATE TABLE.

The new table gets the same column definitions. We can select all columns or some if we want.

Note: If you create a new table using an existing table, the new table will be filled with the existing values from the old table.

Syntax


CREATE TABLE new_table AS
    SELECT column1_name, column2_name,...
    FROM current_table
    WHERE <add condition>;

Example


CREATE TABLE marketing AS
SELECT username, email
FROM customers;

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What are Conditional Tags in WordPress?WordPressThe 7 Security Objectives of Any Organization for IT and Network SecurityMiscellaneous4 different Ways to Get JavaScript OutputJavaScriptA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSWhat are partials in SCSS (Sass)?SCSSHow to Check If a Variable is an Integer, a Float, a Number, NaN, an Infinite or a Numeric in PHP?PHPDISTINCT ON: The confusing, unique and useful feature in PostgresPostgresHow to add before after image effect using pure CSS?CSSHow to add a Pie Chart in Angular App?AngularPHP __construct() functionPHPDid you get an error while displaying Special Characters in JavaScript?JavaScriptIs PHP still good for back-end programming?PHP