Create is a command in SQL DDL, it is used in creating a table or a database in a relational database management system.
The create command is used in creating a database in RDBMS. The syntax below is used;
CREATE DATABASE <DB_NAME>;
Example:
CREATE DATABASE MyDatabase;
The command above will be used to create a database named MyDatabase, which will be an empty schema without any table.
However, to create tables in this newly created database, we can use the create command again.
The create command can also be used in creating tables. However, if you create a table, you are required to specify the details of the columns of the tables as well. The names and data types can be specified for various columns in the create command itself.
Following is the syntax,
CREATE TABLE <TABLE_NAME> (
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);
In general, the create table command will instruct the database system to create a new table with the given table name and column information.
Example:
CREATE TABLE Student(
student_id INT,
name VARCHAR(100),
age INT);
The command above will create a new table with the name Student in the current database with 3 columns, namely student_id, name, and age. However, the column ‘student_id' is allowed to only store integers; the ‘name’ will hold up to 100 characters, and the ‘age’ can only store an integer value.
In the case where you are currently not logged into your database in which you want to create the table, you can then add the database name along with the table name, using a dot operator.
For instance, if you already have a database with the name ‘MyDatabase’ and you want to create a table with the name 'Student’ in it, then you can do so using the query shown below:
CREATE TABLE MyDatabase.Student(
student_id INT,
name VARCHAR(100),
age INT);
Below is a list of some of the commonly used data types used for columns in tables.
Datatype | Use |
---|---|
INT | It is used for columns that will store integer values. |
FLOAT | It is used for columns that will store float values. |
DOUBLE | It is used for columns that will store float values. |
VARCHAR | It is used for columns which will be used to store characters and integers, basically a string. |
CHAR | It is used for columns that will store char values(single characters). |
DATE | used for columns which will store date values. |
TEXT | It is used for columns that will store text which is generally long. For example, if you create a table for storing profile information of a social networking website, then for the about me section |