SQL Create Table


Create Table In SQL


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

Create table Syntax-

SQL>CREATE TABLE table_name (column1 datatype, column2 datatype,column3 datatype,  ....);



Create Table Example-

SQL> 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 it means it will accept the integer and data type (Not Null) Means We can not leave the ID column Blank
Here name Column is Varchar (20) It Means It accept the numeric and alpha .and limit of character is 20.



Create Table Real Time Example-


SQL>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 anew table by using a existing table with Following Command
It Will Copy all name and data type of Existing Table And create a new table With Another name


Syntax-

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

Example-

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

Post a Comment

0 Comments