SQL - CREATE Table


1 August 2025

Welcome to the SQL magic show, where we transform data using the CREATE TABLE statement! 🎩✨


Unveiling the CREATE TABLE Spell

In the enchanted world of relational databases, tables are where the real magic happens. Let's cast the CREATE TABLE spell and see the wonders unfold:

Syntax Enchantment:

CREATE TABLE table_name

 ( column1 datatype, 

column2 datatype, 

 column3 datatype, 

 ... 

 columnN datatype, 

 PRIMARY KEY(one or more columns));

Here, we define the name of our mystical table and the essence of each column within it. The PRIMARY KEY is like the secret key to unlock the true power of your table.


Example Magic Ritual:

CREATE TABLE CUSTOMERS (

ID INT NOT NULL,

NAME VARCHAR(20) NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR(25),

SALARY DECIMAL(18, 2),

PRIMARY KEY(ID)

);

Behold, the CUSTOMERS table arises! 🏰✨


Verification Spell:

DESC CUSTOMERS;

And the oracle reveals:

objective Copy code

Field.               Type             Null             Key          Default         Extra

ID                   int(11).           NO             PRI             NULL

NAME.          varchar(20)     NO                                NULL

AGE               int(11)            NO                                NULL

ADDRESS    char(25)         YES                                 NULL

SALARY     decimal(18,2)   YES                                 NULL

Your table is alive and kicking!


Handling the Unknown with IF NOT EXISTS

Sometimes, in the vast realms of SQL, tables may already exist. Fear not! We wield the magic words:


CREATE TABLE IF NOT EXISTS CUSTOMERS (

ID INT NOT NULL,

NAME VARCHAR(20) NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR(25),

SALARY DECIMAL(18, 2),

PRIMARY KEY(ID)

);

This spell creates the table only if it doesn't already exist. No errors, just a smooth journey!


The Art of Duplication

Ever wanted to clone a table? With SQL, you can create a new table from an existing one!

Duplication Incantation:


CREATE TABLE NEW_TABLE_NAME AS

SELECT [column1, column2...columnN]

FROM EXISTING_TABLE_NAME

WHERE Condition;


For instance:


CREATE TABLE SALARY AS

SELECT ID, SALARY

FROM CUSTOMERS;


A new table named SALARY is born, inheriting the magic of ID and SALARY from the CUSTOMERS table.

And there you have it – the SQL CREATE TABLE spellbook! Now go forth, create tables, and let the data magic flow! 🌟🔮





Sajan Tonge
Founder & CEO

My Analytics School