Log In

How to Create Tables in MySQL

How to Create Tables in MySQL
27.06.2024
Reading time: 13 min
Hostman Team
Technical writer

Data starts with creating tables. Relational tables must follow certain rules. In MySQL, special queries are used to create tables, specifying the attributes (fields) of the table and the data types for each field and optionally providing field descriptions.

In this article, we will describe how to work with tables in MySQL, explain the CREATE TABLE syntax, and show you how to create tables and properly enter data. 

Creating Tables in MySQL

There are three main ways to create tables.

  1. If the table is created from scratch, the first commands must describe all fields. For this, MySQL uses the CREATE TABLE statement with the following syntax: 

CREATE TEMPORARY  TABLE IF NOT EXISTS table_name

column_name_1 data_type_1,
column_name_2 data_type_2, 
...,
column_name_N data_type_N,
) ;

Required parameters: 

  • table_name is the name of the table being created (must be unique, and for convenience, the name should describe what the table represents); 

  • column_name_1 is the name of the column; 

  • data_type_1 defines the type of data that will be stored in the column. 

Optional parameters: 

  • TEMPORARY. If used in the statement, a temporary table is created. It exists only in the current session and disappears when reconnecting to the database. Temporary tables are usually used to store intermediate data during complex selections. Only the user who created it has access to the temporary table. 

  • IF NOT EXISTS prevents an error if a table with that name already exists. It does not check if the structure of the existing table matches the one we tried to create; it only checks the name.

  1. The second way to create a table in MySQL is based on a query: 
CREATE TEMPORARY  TABLE IF NOT EXISTS table_name

column_name_1 data_type_1,
column_name_2 data_type_2, 
...,
column_name_N data_type_N,

IGNORE | REPLACE
AS query_expression;

Required parameter: 

  • query_expression is an SQL query based on the results of which the table is created. For example, the query might look like this: 
SELECT id, column_1 FROM table WHERE id > 15 

Optional parameter: 

  • IGNORE | REPLACE specifies how to handle rows that duplicate unique key values. The full query statement might look like this: 
CREATE TABLE movies_copy 
(id INT, title CHAR(50) UNIQUE, year YEAR, summary TEXT)
IGNORE
SELECT id, title, year, storyline as summary FROM movies;
  1. The third way is when a new table is created based on the structure of another table. We can say that we are copying another table:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table_name
LIKE old_tbl_name;
  • old_tbl_name is the name of the table we want to "clone". 

This method creates a table with the same structure as the original but does not copy the data stored in it. We get the same table but empty. 

Data Types in MySQL

MySQL supports various data types. Choosing the right type significantly affects the performance of the database, especially with large sizes. All data types can be divided into several groups. 

Numeric Data Types

Numeric types are divided into integer and floating-point.

To store integers, data types TINYINT, SMALLINT, MEDIUMINT, INT, or BIGINT are used. Each of them allows storing values in the range from -2(N-1) to 2(N-1)-1, where N is the number of bits required for storage. If necessary, it is possible to use the UNSIGNED attribute to disallow negative values. In this case, the value range can be shifted to start from 0, for example, from 0 to 255 for TINYINT. The most common data type is INT.

MySQL uses three data types for floating-point numbers:

  • FLOAT (uses 4 bytes) — stores up to 24 decimal places;

  • DOUBLE (uses 8 bytes) — stores up to 54 decimal places;

  • DECIMAL (the number of bytes depends on the chosen precision).

The DECIMAL data type is used to store exact fractional numbers. For example, it is used for storing financial data where precise results are needed during calculations. Synonyms (aliases) for DECIMAL include NUMERIC, DEC, and FIXED. For DECIMAL, you can choose the number of values you want to see before and after the decimal point, which determines the number of bytes it will use. DECIMAL can take two parameters DECIMAL(precision, scale). The precision parameter specifies the maximum number of digits the number can store, ranging from 1 to 65. The scale parameter specifies the maximum number of digits the number can contain after the decimal point, ranging from 0 to the precision value, with a default of 0. For example, with parameters DECIMAL(5,2), you can store the number 69839.12 or 71468.2.

Character (String) Data Types

  • CHAR has a fixed length of up to 255 characters. It is useful when you need to store short strings or when all data is approximately the same length. Since the length is fixed, the allocated space is also fixed. If the table is created with CHAR(10) (10 is the length of the string to be stored), all strings will have a length of 10. If fewer characters are entered, the remaining space will be filled with spaces, meaning that space in the database is allocated for nothing.
  • VARCHAR stores variable-length character strings from 0 to 65535 characters, provided the MySQL version is higher than 5.0.3. It is useful when you do not know the length of the text to be stored. It uses only the necessary amount of space for the data.
  • TEXT is intended for storing large amounts of character data. There are 4 types of TEXT: TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. They differ in the maximum length of the string they can contain. Due to its size, it is often stored separately, sorted differently, and not indexed to full length. The BLOB family has similar features. These data types can store large amounts of information in binary form. The BLOB family is very similar to TEXT. BLOB types include TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.
  • ENUM stores a predefined set of string values specified when the column is created. It is suitable if a field frequently repeats certain values that are few and the list doesn't need frequent updates, such as car body types or planets in the solar system. All these values are stored as integers and take up less space than strings, using 1-2 bytes.
  • SET is a string object that can contain 0 or more values, each chosen from a predefined list specified when the table is created (maximum size is 64 elements). For example, to store book genres in a column with a SET type. SET stores values as integers, using 1-8 bytes.

Date and Time Data Types

Several types are available for date and time.

  • DATE stores only the date in 'YYYY-MM-DD' format and uses 3 bytes.
  • TIME stores only the time in 'hh:mm:ss' format and uses 3 bytes, with a range from '-838:59:59.00' to '838:59:59.00'.
  • YEAR stores the year, with a range of 1901-2155, using 1 byte.
  • DATETIME uses 8 bytes. It allows storing values from the year 1001 to 9999 with a precision of 1 second in the format: 'YYYY-MM-DD hh:mm:ss'. It is not dependent on the time zone.
  • TIMESTAMP uses 4 bytes, thus having a much smaller date range: from '1970-01-01 00:00:01' to '2038-01-19 03:14:07'. It is stored as the number of seconds elapsed since the Unix epoch (January 1, 1970, GMT). The displayed value depends on the time zone. Later versions added support for storing time in microseconds.

When both date and time are needed, two types are required: DATETIME and TIMESTAMP.

Constraints in MySQL

To ensure database integrity, MySQL uses specific constraints (CONSTRAINT). They can be divided into two types: column-level and table-level constraints (applied to a specific column or the entire table). Constraints are declared during table creation. They include:

  • NOT NULL — indicates that the column cannot contain a NULL value.

  • UNIQUE — prevents duplicate values (all values in the column must be unique).

  • PRIMARY KEY — only unique non-NULL values can be stored in the column. Only one such column can exist in a table.

  • FOREIGN KEY — creates a relationship between two tables based on a specific column.

  • CHECK — controls the values in the column, checking if they are acceptable.

  • DEFAULT — sets a default value for the column. If the field is omitted when entering a record, the default value will be inserted.

Index Types in MySQL

An index is a structure that stores the value of a table column (or several columns) and references the rows where these values are located. Creating indexes helps increase MySQL's efficiency, significantly speeding up queries. Most indexes have a tree-like data structure (are B-tree indexes). Indexes take up memory space, so typically only the fields that are used for data retrieval are indexed. Without an index, a query search goes through all table records, which can be time-consuming and computationally intensive. 

Indexes in MySQL speed up operations:

  • Searching for rows matching a WHERE query;

  • Retrieving rows during joins;

  • Finding minimum (MIN()) and maximum (MAX()) values of a specific indexed column;

  • Sorting or grouping tables, provided the operation uses the leftmost prefix of the used index.

There are following types of Indexes in MySQL

  • Primary key — This column uniquely identifies each row in the table. Usually specified when creating the table. If not done manually, MySQL will create a hidden key. The primary key contains unique values. If it consists of several columns, the combination of values must be unique. The primary key cannot have NULL values. A table can have only one primary key.

  • Unique index — Ensures the uniqueness of values in one or more columns. Unlike the primary key, you can create many unique indexes. It can have NULL values.

  • Composite index — An index on multiple columns. MySQL allows creating composite indexes containing up to 16 columns. Typically used to speed up queries that need to retrieve data from multiple fields.

Indexes are not recommended for small tables. The improvement from using indexes will not be noticeable. Indexes should be created primarily for slow queries or the most frequently used ones. Collect query performance statistics and conduct an assessment to determine this. Creating indexes for everything is not a good idea.

Example 1: Creating a Table

Let's create a MySQL table from scratch to store a collection of movies:

CREATE TABLE movies (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title CHAR(100) NOT NULL,
year YEAR NOT NULL,
running_time_min INT NOT NULL,
storyline TEXT
);

This table includes the following columns:

  • id: Unique identifier for the movie.

  • title: The movie's title.

  • year: The release year of the movie.

  • running_time_min: The movie's duration in minutes.

  • storyline: A brief description of the movie's plot.

All fields have the NOT NULL constraint. The primary key is the id field. Initially, the table only has columns with specified data types. 

Now let's insert some data into the table. For this, MySQL uses the INSERT INTO statement with VALUES.  

INSERT movies3(title, year, running_time_min, storyline) 
VALUES ('Harry Potter and the Philosophers Stone', 2001, 152, "An orphaned boy enrolls in a school of wizardry, where he learns the truth about himself, his family and the terrible evil that haunts the magical world."),
('Harry Potter and the Chamber of Secrets', 2002, 162,"An ancient prophecy seems to be coming true when a mysterious presence begins stalking the corridors of a school of magic and leaving its victims paralyzed."),
('The Green Mile', 1999, 188,'Death Row guards at a penitentiary, in the 1930s, have a moral dilemma with their job when they discover one of their prisoners, a convicted murderer, has a special gift.'),
('Forrest Gump', 1994, 142,"The presidencies of Kennedy and Johnson, the Vietnam War, the Watergate scandal and other historical events unfold from the perspective of an Alabama man with an IQ of 75, whose only desire is to be reunited with his childhood sweetheart."),
('Cast Away', 2000, 143,"A FedEx executive undergoes a physical and emotional transformation after crash landing on a deserted island.");

The result will be this table:

Image1

Example 2: Creating a Table from a Query

Now, let's create a table based on a query. We will select all movies released after 1999:

CREATE TABLE movies_query 
AS
SELECT id, title, year, running_time_min 
FROM movies 
WHERE year > 1999;

The result:

Image3

Example 3: Creating a Table Based on Another Table's Structure

We can create a table based on the structure of another table:

CREATE TABLE movies_copy 
LIKE movies;

This creates an empty table with the same structure as the movies table but without data.

Image2

Foreign Keys

When there are multiple tables in a database, you might need to link them. Foreign keys are used in MySQL for this purpose. A foreign key is a column (or group of columns) that creates a relationship between tables. It refers to the primary key in another table. The table with the primary key is called the parent table, and the table with the foreign key is the child table.

Creating a Foreign Key

CONSTRAINT symbol FOREIGN KEY
index_name (col_name, ...)
REFERENCES tbl_name (col_name,...)
ON DELETE reference_option
ON UPDATE reference_option

Mandatory Parameters:

  • FOREIGN KEY [index_name] (col_name, ...): Specifies the field to be used as a foreign key.

  • index_name: Name of the index.

  • col_name: Name of the column.

  • REFERENCES tbl_name (col_name, ...): Specifies the column of the parent table with which our foreign key will be associated.

  • tbl_name: Name of the table.

  • col_name: Name of the column.

Optional Parameters:

  • CONSTRAINT symbol: Used to create and delete constraints.

  • ON DELETE/ON UPDATE: Defines what to do when the parent table is deleted or updated. Options include:

    • CASCADE: Automatically deletes or updates records in the child table when the parent table's records are deleted or updated.

    • SET NULL: Sets the value to NULL in the child table when the parent table's records are deleted or updated.

    • RESTRICT: Prevents deletion or updating of records in the parent table if they are used in the child table.

Example 4: Creating a Table with a Foreign Key

Let's create a table for movie genres:

CREATE TABLE genres (
id INT AUTO_INCREMENT PRIMARY KEY,
genre VARCHAR(200) UNIQUE NOT NULL
);

Populate the table:

INSERT INTO genres (genre) 
VALUES ('drama'),
('fantasy'),
('sci-fi'),
('cartoon');

We will get the id for each genre.

Example of creating a new table in MySQL with a FOREIGN KEY:

CREATE TABLE movies2 (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title CHAR(100) NOT NULL,
year YEAR NOT NULL,
running_time_min INT NOT NULL,
genre_id INT,
FOREIGN KEY (genre_id) REFERENCES genres (id)
);

Our table:

Image4

To display the genres instead of just numbers, you can use a LEFT JOIN with the genres table:

SELECT movies2.id, title, year, genre 
FROM movies2
LEFT JOIN genres ON genres.id = genre_id;

Image6

Manipulating Tables

Various commands can be used to work with an already created table. Here are some basic MySQL commands for renaming, modifying, deleting, and adding columns, and changing data types.

Renaming a Table

Option 1. Used if we initially indicated which database we were working in:

USE movies_db;
RENAME TABLE movies2 TO cinema;

Option 2. Applies if the database is not specified:

RENAME TABLE movies_db.movies2 TO movies_db.cinema;

You can also use the RENAME TABLE command to move a table from one database to another:

RENAME TABLE movies_db.movies2 TO cinema_db.cinema;

Adding a New Column

In MySQL, the ALTER TABLE statement is used to change a table in some way.

ALTER TABLE cinema
ADD Language VARCHAR(50) NULL;

Deleting a Column

ALTER TABLE cinema
DROP COLUMN Language;

Renaming and Modifying Columns

Use the CHANGE command to rename and redefine a column:

ALTER TABLE cinema CHANGE year date INT NOT NULL;

If you don't want to change the column name, specify the old name twice:

ALTER TABLE cinema CHANGE year year INT NOT NULL;

Use the MODIFY command to change the column definition without renaming it:

ALTER TABLE cinema
MODIFY COLUMN title VARCHAR(100);

Use the RENAME COLUMN command to change a column's name:

ALTER TABLE cinema RENAME COLUMN running_time_min TO running_time;

Deleting Operations

To clear a table of data, use the TRUNCATE TABLE command:

TRUNCATE TABLE cinema;

To completely remove a table from the database, use the DROP TABLE command:

DROP TABLE cinema;

Adding and Removing Foreign Keys

To add a foreign key:

ALTER TABLE cinema
ADD FOREIGN KEY (producer_id) REFERENCES producer (Id);

Conclusion

These operators and examples cover the basic operations with tables, enabling you to perform many useful tasks. These skills can be applied, including for working with MySQL cloud databases deployed on platforms like Hostman.


Share