SQL: create command

  • March 12, 2023
  • DBMS
SQL Views

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

Leave a Reply

Your email address will not be published. Required fields are marked *