Creating a Table
For storing data into an SQL-based DBMS, we first create and select a database instance. We can do so by using the following query:
CREATE DATABASE db_name;
Where db_name
refers to the name of the database which we want to create.
Note
There can be more than one database instance in a DBMS, each containing their own set of tables.
Once the database is created, we can create a table using the following syntax:
CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
...
);
The table_name
indicates the name of the table which is to be created. Inside the brackets (...)
, we write the column names that are to be included in the table, followed by their respective data types. Each column-datatype pair is separated by commas (,
).
Following is a simple CREATE TABLE
query, which creates a table called students
with three columns, namely: first_name
, last_name
, age
:
CREATE TABLE students (
first_name VARCHAR(24),
last_name VARCHAR(24),
age INT
);
Following are the common datatypes in SQL:
Demonstration:
Good Practice:
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 1.72
Creating a Table
Swipe to show menu
For storing data into an SQL-based DBMS, we first create and select a database instance. We can do so by using the following query:
CREATE DATABASE db_name;
Where db_name
refers to the name of the database which we want to create.
Note
There can be more than one database instance in a DBMS, each containing their own set of tables.
Once the database is created, we can create a table using the following syntax:
CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
...
);
The table_name
indicates the name of the table which is to be created. Inside the brackets (...)
, we write the column names that are to be included in the table, followed by their respective data types. Each column-datatype pair is separated by commas (,
).
Following is a simple CREATE TABLE
query, which creates a table called students
with three columns, namely: first_name
, last_name
, age
:
CREATE TABLE students (
first_name VARCHAR(24),
last_name VARCHAR(24),
age INT
);
Following are the common datatypes in SQL:
Demonstration:
Good Practice:
Thanks for your feedback!