SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO SELECT

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE command.

While creating a table, we need to specify a table name, a column name, and a data type for each column where data gets stored and column size.

Let’s learn how to use CREATE TABLE command to create a table.

The syntax of the CREATE TABLE is as follows:

CREATE TABLE TABLE-NAME (COLUMNNAME_1 DATATYPE(SIZE), COLUMNNAME_2 DATATYPE(SIZE), COLUMNNAME_3 DATATYPE(SIZE), PRIMARY KEY(COLUMNNAME) );

Here is the CREATE TABLE command we use to create the table.

TABLE-NAME – Name of the table.

COLUMNNAME_1 – Name of the columns.

DATATYPE -Type of data to be stored (integer, varchar, etc.).

SIZE – Size of data we can store in a column. The column can store a maximum of 10 digits in a number. For example, the column name specifies a data type integer with a size of 10.

The example of the CREATE TABLE is as follows:

CREATE TABLE TEAMS (NUMBER_OF_MATCHES INT(10) NOT NULL, TEAM_NAME VARCHAR(50) NOT NULL, HEAD_COACH VARCHAR(50) NOT NULL, CAPTAIN_NAME VARCHAR(50) NOT NULL, NUMBER_MATCHES_WON INT(10) NOT NULL, NUMBER_MATCHES_LOSSINT(10) NOT NULL, PRIMARY KEY(NUMBER_OF_MATCHES) );

In the above example, we created a table with the table name "TEAMS",

NUMBER_OF_MATCHES,       NUMBER_MATCHES_WON, NUMBER_MATCHES_LOSS columns are of integer data type and will hold an integer with size 10.

TEAM_NAME, HEAD_COACH, and CAPTAIN_NAME columns are type varchar and will hold characters with a maximum length of 50.

NUMBER_OF_MATCHES is a Primary key in a table.

After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:

DESC TEAMS;

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NOPRINULL 
TEAM_NAMEvarchar(50)NO NULL 
HEAD_COACHvarchar(50)NO NULL 
CAPTAIN_NAMEvarchar(50)NO NULL 
NUMBER_MATCHES_WONint(10)NO NULL 
NUMBER_MATCHES_LOSSint(10)NO NULL 
SQL CREATE TABLE

Now you have the TEAMS table in the database.

Now you can store data in table TEAMS.

Let's take another example by creating a new table for better understanding.

The example of the CREATE TABLE is as follows:

CREATE TABLE PLAYER( NUMBER_OF_MATCHES INT(10) NOT NULL, NUMBER_OF_INNINGSINT(10) NOT NULL, PLAYER_NAME VARCHAR(50) NOT NULL, TEAM_NAMEVARCHAR(50) NOT NULL, RUNS INT(10) NOT NULL, HALF_CENTURY INT(10) NOT NULL, CENTURY INT(10) NOT NULL, HIGESHT_SCORE INT(10) NOT NULL, PRIMARY KEY(NUMBER_OF_MATCHES) );

We have created another table name, "PLAYER," with the same command CREATE TABLE.

NUMBER_OF_MATCHES, RUNS, HALF_CENTURY, CENTURY, HIGESHT_SCORE columns are of integer data type and will hold an integer with size 10.

PLAYER_NAME, TEAM_NAME columns are of type varchar and will hold characters with a maximum length of 50.

NUMBER_OF_MATCHES is a Primary key in a table.

After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:

DESC TEAMS;

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NOPRINULL 
NUMBER_OF_INNINGSint(10)NO NULL 
PLAYER_NAMEvarchar(50)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
RUNSint(10)NO NULL 
HALF_CENTURYint(10)NO NULL 
CENTURYint(10)NO NULL 
HIGHEST_SCOREint(10)NO NULL 
SQL CREATE TABLE

CREATE TABLE USING AN EXISTING (ANOTHER) TABLE:  

If you want a copy of an existing table, we can create using the CREATE TABLE command.

The new table will get the same column definitions.

The new table will have all columns of an existing table or can select the specified columns.

If we created a new table from an existing table, the new table would have all the old values of an existing table.

The syntax of the create table using an existing table is as follows:

CREATE TABLE TABLE_NAME AS SELECT * FROM EXISITING_TABLE_NAME;

Above syntax creates a new table from an existing table, selecting all the columns defined in an existing table.

TABLE_NAME – the new name of the table

EXISITING_TABLE_NAME - old table

The example of the CREATE TABLE USING AN EXISTING TABLE is as follows:

CREATE TABLE ODIS AS SELECT * FROM ODI;

We already have an ODI table in our system. To create a copy table of an “ODI," we run the above query to create a copy of an existing table “ODI”

ODIS - NEW_TABLE_NAME.

ODI - OLD TABLE NAME

After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:

DESC ODIS;

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
Matchesint(11)YES NULL 
P_Namechar(200)YES NULL 
SQL CREATE TABLE

Let’s create another new table from an existing table.

The example of the CREATE TABLE USING AN EXISTING TABLE is as follows:

CREATE TABLE TEAM AS SELECT * FROM TEAMS;

The above query creates a new table, "TEAM," from an existing table, "TEAMS," which defines all the existing table's columns into the new table.

After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:

DESC TEAM;

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
HEAD_COACHvarchar(50)NO NULL 
CAPTAIN_NAMEvarchar(50)NO NULL 
NUMBER_MATCHES_WONint(10)NO NULL 
NUMBER_MATCHES_LOSSint(10)NO NULL 
SQL CREATE TABLE

If you want only a selected column into a new table from an existing table, then we will use the below query as follows: -

The syntax is as follows:

CREATE TABLE NEW_TABLE AS SELECT COLUMNNAME_1, COLUMNNAME_2, FROM EXISITING_TABLE_NAME;

The example is as follows:

CREATE TABLE PLAYERS AS SELECT NUMBER_OF_MATCHES, NUMBER_OF_INNINGS, PLAYER_NAME, TEAM_NAME FROM PLAYER;

The above query creates a new table "PLAYERS” from an existing table “PLAYER," which has only selected columns from an existing table into a new table (NUMBER_OF_MATCHES, NUMBER_OF_INNINGS, PLAYER_NAME, TEAM_NAME)

After creating the table successfully, if you want to check again or verify, you can use DESC commands as follows:

DESC PLAYERS;

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
NUMBER_OF_MATCHESint(10)NO NULL 
NUMBER_OF_INNINGSint(10)NO NULL 
PLAYER_NAMEvarchar(50)NO NULL 
TEAM_NAMEvarchar(50)NO NULL 
SQL CREATE TABLE



ADVERTISEMENT
ADVERTISEMENT