Creating a table in MySQL
Start your free 7-days trial now!
We can create a table in MySQL using the following general syntax:
CREATE TABLE table_name (column1 datatype1, column2 datatype2, column3 datatype3, ...);
Where:
table_name
: the name to give to the table created.
column1
: the name of a column that is to be part of the newly created table.
datatype1
: the data type of column1
.
Example
Basic usage
Consider the following information about some people:
id | name | age | hobby |
---|---|---|---|
1 | alex | 30 | Programming |
2 | bob | 15 | Programming |
3 | cathy | 20 | Swimming |
To create the above table in MySQL and call it info
:
CREATE TABLE info (id INT, name VARCHAR(20), age INT, hobby VARCHAR(20));
To add records for the three people into the table info
:
INSERT INTO info (id, name, age, hobby) VALUES (1, 'alex', 30, 'Programming');INSERT INTO info (id, name, age, hobby) VALUES (2, 'bob', 15, 'Programming');INSERT INTO info (id, name, age, hobby) VALUES (3, 'cathy', 20, 'Swimming');
SELECT * FROM info;
+------+-------+------+-------------+| id | name | age | hobby |+------+-------+------+-------------+| 1 | alex | 30 | Programming || 2 | bob | 15 | Programming || 3 | cathy | 20 | Swimming |+------+-------+------+-------------+
The table name must be unique within the database that the table resides in. This means that if you have two databases, we are allowed to have a table named info
in each one of these databases.
Primary key
To specify the id
column as a primary key that uniquely identifies each row:
CREATE TABLE info ( id INT, name VARCHAR(20), age INT, hobby VARCHAR(20), PRIMARY KEY (id));
Auto-increment
To set the id
column as an auto-increment column (i.e. automatically generate a unique number when a new record is inserted into the table):
CREATE TABLE info ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(20), age INT, hobby VARCHAR(20));
The NOT NULL
simply specifies that the id
column should not accept NULL
values.
Column data types
When we define the columns of a table, we need to specify the type of data that is to occupy each column. To store text, we can use the type VARCHAR(M)
. In the case of the above example, we have used VARCHAR(20)
, which means that each data stored in the name
column will be less than or equal to 20 characters.
If we attempt to store data that violates the format that we have pre-specified, we will encounter an error.
Naming convention
Although there is no one standard that satisfies all developers, typically table names are all lowercase and singular.
For instance, a table that contains data about films should be called film
instead of films
. This might be surprising at first since a table is essentially a collection of records (i.e. data), and so making it plural should be more intuitive. However, pluralizing the table name brings forth a number of problems such as irregular plural nouns (e.g. goose/geese, person/people, sheep/sheep).
It is common to use snake_case for table names. Moreover, the table name is not case-sensitive (i.e. abc is the same as ABC).