SQL stands for Structured Query Language, a programming language used to communicate with and manipulate databases. In this article, TipsMake will introduce the CREATE command in SQL to help you understand its syntax and usage.
To learn more about what SQL is, you can refer to TipsMake's article "What is SQL ?". This article will focus solely on the CREATE command in SQL.
The CREATE command in SQL
SQL has two built-in CREATE commands , including:
- Create Database
- CREATE TABLE
CREATE DATABASE command in SQL
In SQL, a database is defined as a sorted collection of data. Therefore, the first step in SQL to store sorted data is to create a database. The CREATE DATABASE command is used to create a new database in SQL.
Syntax:
CREATE DATABASE database_name;
In there:
database_name : the name of the database.
Example query:
This query will create a new database in SQL and name the database my_database:
CREATE DATABASE my_database;
The CREATE TABLE command in SQL
As TipsMake mentioned above, the CREATE DATABASE command is used to create a new database in SQL. To store data in SQL, you need a data table. The CREATE TABLE command is used to create a table in SQL.
A table consists of rows and columns. Therefore, when creating a table, you must provide information to SQL including column names, the type of data to be stored in the columns, and the size of the data.
Below is the syntax for the CREATE TABLE command to create a table in SQL.
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
.
);
In there:
table_name : the name of the table.
column1: the name of the first column.
data_type : The type of data you want to store in that specific column. For example, int for integer data.
- Size: The size of data we can store in a specific column. For example, if the data_type column is defined as int and the size is 10, then this column can store an integer, up to 10 digits.
Example query:
The query below creates a table named Students, which includes three columns: ROLL_NO, NAME, and SUBJECT:
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
The above query will create the Students table. The ROLL_NO field is of type int and can store integers with a size of 3. The next two columns, NAME and SUBJECT, are of type varchar, can store characters, and have a size of 20, meaning these fields can contain a maximum of 20 characters.
The article above, TipsMake has just introduced you to the CREATE command in SQL. Basically, there are two CREATE commands in SQL: CREATE DATABASE and CREATE TABLE. The next article, TipsMake will introduce you to the DELETE command in SQL .
If you have any questions or need clarification, please leave your comments below the article, and TipsMake will answer your questions as soon as possible.