In previous SQL post we had discussed about SQL Database and SQL Table, Data Definition Language and Data Manipulation Language Statements.
Now, In this SQL article we will learn DDL CREATE command in detail, how to use CREATE Command for creating new database and table.
CREATE Command – Data Definition Language Statements
The CREATE command is used to create new database in DBMS, and create new table in database. In other word we can say CREATE command used to create new database and table.
After installing Database management system like sql server, oracle on computer, we need to manage database and for create a new database we use CREATE command.
We can create both Database and Table using CREATE command.
CREATE Syntax for Database:
CREATE DatabaseName
CREATE School
database name = SchoolWhen execute above statement it create an empty database named “School” on database server.
After creating the database, the next step to create tables that contains the records or data.
let’s learn how to create new table in database.
When we create new table, the table must contains some columns to store the data. so at the time of creating a new table we must specify the columns with proper datatype and size.
CREATE Syntax for Table:
CREATE TABLE Tablename
(
Columns1 datatype,
Column2 datatype,
Column3 datatype
)
In above statement:
Tablename – is the name of Table
Column1, Column2 – is the name of columns
datatype – is the datatype of columns like int, char, varchar etc.
Create new Table Student with Columns ID, Name, City and Email
CREATE TABLE Student
(ID int NOTNULL,
Name varchar(50) NOTNULL,
City varchar(50) NULL,
Email varchar(100) NULL)
The above statement create a new table named “Student” with four columns student Id, Name, City and Email address.
In above example we can see that, we specify the data type for each columns the ID column data should be integer type and Name, City and Email are varchar data type with specific size.
The Student Table :
ID | Name | City | |
---|---|---|---|
1 | Meera | Bombay | meera@yahoo.com |
2 | Ram | Bombay | ram@yahoo.com |
3 | Jay | Bombay | jay@yahoo.com |
Let’s another example for creating new Table.
Create new table for Branch with columns BranchID, BranchName and Seat.
CREATE TABLE Branch
(
BranchID int NOTNULL,
BranchName varchar(50) NOTNULL,
BranchSeat int NOTNULL)
Create new table from an existing table with records.
The other way to create table in oracle, Create new table from an existing table with all records.
CREATE TABLE temp_Student AS
SELECT * FROM Student
Above statement create new table temp_Student with same columns with records of Student table.
If we want to create table with some of columns from existing table then statement should be like:
CREATE TABLE temp_Student AS
SELECT ID, Name FROM Student
Above statement create new table temp_Student with columns ID and Name with all records from existing table Student
Note : In oracle the datatype integer is represented as “number” and in Sybase it is represented as “int”.