SQL Create Table - How To Create a Table In SQL

.

Create Table In SQL- SQL CREATE STATEMENT


Create a table in SQL with an Example



In SQL CREATE TABLE Query is used to create a new table in a database.
If You Want to Create a table in a Database then You Have To Define the Name of the Table, the Column name, and Its Data Types.

Syntax of Create Table 
The basic syntax of creating the table is given below
 
CREATE TABLE table_name (column1 datatype, column2 datatype,column3 datatype,  ....);


Example of Create Table 
An example of a CREATE Table is given below.

CREATE TABLE STUDENTS (  ID INT  NOT NULL,  NAME VARCHAR (20) NOT NULL,  AGE INT  NOT NULL,  ADDRESS CHAR (25),  PRIMARY KEY (ID)  ); 


Here, the ID column is int type which means it will accept the integer and the data type (Not Null) Means We can not leave the ID column Blank
Here name Column is Varchar (20) It Means It accepts numeric and alpha .and the limit of characters is 20.

  • How To Create a Table in SQL with an Example?

Example: If we want to create an employee table with employee id, employee name,  employee email, address, and state then by using the below SQL Statement we can create an employee table.


CREATE TABLE Employee  ( EmployeeID int,  FirstName varchar(50),  LastName varchar(50),  Email varchar(50),  Address varchar(100),  State  varchar(50)); 

  • Create Table Using Existing Table

We can Create a new table by using an existing table with the Following Command.
It Will Copy all names and data types of the Existing Table And create a new table With Another name


Syntax

CREATE TABLE new_table_name AS SELECT column1, column2,... FROM existing_table_name WHERE ....;

Example

CREATE TABLE Test Table AS SELECT customername, contactname FROM customers;

Hope!!! The above tutorial on " SQL CREATE statement with examples is helpful for you...

Team,
QA acharya

Post a Comment

0 Comments