Home » SQL

SQL - CREATE Command (to create Database and Table)

SQL - CREATE Command: Here, we will learn how to create a database and a table using CREATE command in SQL?
Submitted by Shubham Singh Rajawat, on August 09, 2017

CREATE is a DDL command used to create table or database in SQL.

1. Creating Database

A new Database can be created in SQL by using the CREATE DATABASE statement. For e.g. to create a new Database of name 'MyDatabase' following statement should be executed.


If a Database of name MyDatabase does not exist it will create a new database else it will show error.

ERROR 1007
Can’t create database ‘MyDatabase’ ; database exists

And Database will not be created unless you change the name of the Database.

2. Creating Table

A new Table can be created in SQL by using the CREATE TABLE statement. A new table can be created by using following syntax.

CREATE TABLE table_name
(column_name  datatype [constraint],
 column_name  datatype [constraint],
 column_name  datatype [constraint],...);

column_name is the name of the column
datatype is the attribute that tells what kind of data a column can store

Following are the data types in SQL

  1. char(size)
    A string of characters of length size. size can have a maximum value of 255 characters.
  2. date
    It stores date
  3. varchar(size)
    A string of character of length size.
  4. numeric(maxsize)
    Number with maximum number of digits maxsize.


constraints are the rules for the column.

Following are the possible values of a constraint can have:

    The column must have some value cannot be left empty.
    No two values can be same inside this column.
    Each record in the table is uniquely identified by this column.

For example: to create a table student with columns name, roll no, age, date of birth, following statement needs to be executed

( roll_no numeric(15) PRIMARY KEY NOT NULL,
name varchar(25),
age numeric(2), 
dob date);

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.