Friday, March 29, 2019

Create | DDL Commands

CREATE Command

Create command used to create new table in a database.

Syntax :

CREATE TABLE table_name (column1 datatype, column2 datatype, column3 datatype, column4 datatype,  . . .)

In CREATE command, there is two types of parameters :

1) Column - parameter specify the name of the column.
2) Datatype - parameters specify the type of data of column.

Example :

// CREATE Customer01 table 

CREATE TABLE Customer01 (Cust_Id int,Last_Name varchar(255),
First_Name varchar(255),Address varchar(255),City varchar(255));

  • Above, example creates a table called "Customer01" that contains five columns: Cust_Id, Last_Name, First_Name, Address, and City.
  • The Cust_Id column is of type int and will hold an integer.
  • The Last_Name, First_Name, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 0-255 characters.

Create New Table Using Another Existing (Old) Table

  • We can create a copy of an existing table using CREATE command.
  • The new table gets the same column definitions.
  • We can use all columns or specific selected columns.
  • If we create a new table using an old existing table, the new table will be filled with the existing values from old table.
Syntax :

CREATE TABLE new_table_name AS SELECT [* from] column1, column2, column3,... from existing_table_name [where];

In above syntax WHERE clause is optional.

Examples :

1) CREATE TABLE Customer_Details AS SELECT  Customer_Id,City FROM Customer01;
2) CREATE TABLE Customer_Details AS SELECT * FROM Customer01;
3) CREATE TABLE Customer_Details AS SELECT * FROM Customer01, Salary;
4) CREATE TABLE Customer_Details AS SELECT * FROM Customer01, Salary WHERE Customer_name="Raj" AND Salary_Month="June";

In above examples, 
  • We can select particular column.
  • We can use also * FROM to copy all table columns.
  • We can also select multiple tables.
  • In third and fourth example i am using two different tables Customer01 and Salary.

* * * * * * * * * * * * * * *

In Easy Language (Simplify)

CREATE command used to create new table from existing database.
Using with CREATE command also we can create database.

Previous Post
Next Post

post written by: