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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago