SQL Create Table - How to Create a Table In SQL

.

Create Table In SQL- SQL CREATE STATEMENT


create table in SQL with 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.

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


Create Table Example
An example of 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 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 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

Tags: Create table in SQL , SQL create table with example 
.

Post a Comment

0 Comments