search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Creating a table in MySQL

schedule Aug 11, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 |
+------+-------+------+-------------+
NOTE

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.

WARNING

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).

NOTE

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).

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!