Categories: DBMS

SQL: create command

The SQL CREATE command is a type of DDL command that is primarily used for creating databases and tables. To create databases or tables with the desired structure, the CREATE command has a specific syntax that must be followed.
Before we can perform any other functions, we must first create a database, which is the first step in learning SQL.

Syntax: for creating a Database
CREATE Database db_name;

where db_name is the name of the database.

Syntax: for creating a Table
CREATE table table_name
(
column1 datatype (size),
column2 datatype (size),
.
.
columnN datatype(size)
);

where table_name is name of the table, and column is the name of the column.

Example:
CREATE TABLE SCHOOL;

Here we have created a database SCHOOL.

The following code block is an example, which creates a STUDENT table with a ROLL as a primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table −

CREATE TABLE STUDENT(
   ROLL   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   CONTACT  BIGINT              NOT NULL,
   ADDRESS  CHAR (25) ,       
   PRIMARY KEY (ID)
);

Note: We can check if your table was successfully created by looking at the message displayed by the SQL server, or using the DESC command as shown below.

DESC STUDENT;

Output:

+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ROLL    | int(5)        | NO   | PRI |         |       |
| NAME    | varchar(20)   | NO   |     |         |       |
| CONTACT | bigint(10)    | NO   |     |         |       |
| ADDRESS | char(25)      | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Note: also read about Introduction to SQL

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago