MySQL CREATE TABLE: Create a New Table with Columns and Constraints

Use MySQL CREATE TABLE to define a new table, its columns, datatypes, default values, keys, and other constraints.

CREATE TABLE student (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    class VARCHAR(20) NOT NULL,
    mark TINYINT UNSIGNED NOT NULL DEFAULT 0,
    gender VARCHAR(10) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

This creates a table named student with five columns and an auto-incrementing primary key. Later examples in this page describe alternative tables and designs: run them separately in a test database, not as one script recreating student repeatedly.

CREATE TABLE defines structure. It does not insert application records. Use INSERT after the table exists.
Follow the sample table through the series: After creating the student table above, review PRIMARY KEY, then INSERT sample students and SELECT their rows. You need not repeat CREATE TABLE when the table already exists.
On this page — Show page sectionsHide page sections

Basic CREATE TABLE Syntax

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);

Each column definition normally includes a column name, a datatype, and any constraints required by the application.

Create a Simple Table

Here is an example of creating a table with one required column:

CREATE TABLE sample_tb (
    empno VARCHAR(6) NOT NULL
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

This creates sample_tb with one required text column named empno.

IF NOT EXISTS

If the table might already exist, add IF NOT EXISTS:

CREATE TABLE IF NOT EXISTS sample_tb (
    empno VARCHAR(6) NOT NULL
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

This avoids an error for an existing table with the same name.

IF NOT EXISTS does not verify that the existing table has the structure you want. It only prevents creation from failing because the table name already exists.

Columns and Datatypes

Choose datatypes based on the values a column must store.

CREATE TABLE employee (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    empno VARCHAR(10) NOT NULL,
    name VARCHAR(80) NOT NULL,
    joining_date DATE,
    salary DECIMAL(10,2),
    PRIMARY KEY (id)
);

Useful datatype references:

Older MySQL examples often used display widths such as INT(2) or INT(3). Do not use those as a size limit for integer values. Choose the integer datatype itself according to the required numeric range.

NOT NULL and NULL

NOT NULL means a row must contain a non-NULL value for that column unless another mechanism supplies one.

CREATE TABLE customer (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(80) NOT NULL,
    phone VARCHAR(20) NULL,
    PRIMARY KEY (id)
);

Use NULL only when "value unknown or not provided" is a valid state for that column. See SQL NULL values.

DEFAULT Values

A DEFAULT supplies a value when an INSERT omits that column:

CREATE TABLE exam_result (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    student_name VARCHAR(80) NOT NULL,
    mark TINYINT UNSIGNED NOT NULL DEFAULT 0,
    status VARCHAR(20) NOT NULL DEFAULT 'Pending',
    PRIMARY KEY (id)
);

Choose defaults that represent a real application state. Do not use an empty string or zero merely because a column is NOT NULL.

PRIMARY KEY

A primary key uniquely identifies each row. It cannot contain NULL values.

CREATE TABLE department (
    department_id INT UNSIGNED NOT NULL,
    department_name VARCHAR(80) NOT NULL,
    PRIMARY KEY (department_id)
);

See PRIMARY KEY constraints.

AUTO_INCREMENT

For a generated numeric identifier, combine an integer key with AUTO_INCREMENT:

CREATE TABLE student (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    class VARCHAR(20) NOT NULL,
    mark TINYINT UNSIGNED NOT NULL DEFAULT 0,
    gender VARCHAR(10) NOT NULL,
    PRIMARY KEY (id)
);

See MySQL AUTO_INCREMENT.

InnoDB and utf8mb4

For typical modern MySQL applications, InnoDB and utf8mb4 are good defaults:

CREATE TABLE messages (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    message VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

InnoDB supports transactions and foreign keys. utf8mb4 supports the full Unicode character range.

Check Whether the Table Exists

Use SHOW TABLES to check whether a table exists in the current database:

SHOW TABLES LIKE 'sample_tb';

If a row is returned, a table or view with that matching name is visible to the current MySQL account.

Use INFORMATION_SCHEMA when more detail is needed

SELECT TABLE_NAME,
       TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'sample_tb';

This lets you inspect metadata rather than relying only on a display command.

SHOW CREATE TABLE

To inspect the table definition MySQL currently stores:

SHOW CREATE TABLE student;

This is useful for checking engine, columns, indexes, defaults, and table options. See copying tables and SHOW CREATE TABLE.

Create a Table with PHP PDO

The SQL concept should come first. If an application genuinely needs to create a table at runtime, PDO can execute the DDL statement.

<?php
$sql="CREATE TABLE IF NOT EXISTS sample_tb (
    empno VARCHAR(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

try{
    $dbo->exec($sql);
    echo 'Table is ready.';
}catch(PDOException $e){
    error_log($e->getMessage());
    echo 'Unable to create the table.';
}

See PDO connection for the database connection setup.

Do not build table names or column definitions directly from untrusted user input. Prepared-statement placeholders are for data values, not SQL identifiers or schema syntax.

Should You DROP the Table First?

You may need to drop and recreate a table when testing a new table structure in a disposable database. That is appropriate only when deleting the existing structure and all of its data is genuinely intended.

DROP TABLE IF EXISTS sample_tb;

CREATE TABLE sample_tb (
    empno VARCHAR(6) NOT NULL
);
DROP TABLE is destructive. It removes the table and its data. Do not use DROP merely to avoid a "table already exists" message. Use CREATE TABLE IF NOT EXISTS when preserving an existing table is the requirement.

See DROP TABLE.

Common CREATE TABLE Mistakes

Using old integer display widths as value limits

INT(2) and INT(3) do not mean the column can store only two- or three-digit numbers. Choose the numeric datatype based on its range.

Mixing table and column character sets unnecessarily

Use a consistent table-level utf8mb4 default unless a specific column genuinely requires another character set or collation.

Using contradictory old charset settings

Avoid mixing character sets unnecessarily. Use a consistent character set such as utf8mb4 unless specific requirements call for otherwise. Keep character-set choices consistent.

Dropping a table just because it already exists

DROP destroys data. Use IF NOT EXISTS when the goal is simply to avoid a duplicate-table error.

Assuming IF NOT EXISTS validates the schema

It does not compare the existing table definition with the CREATE TABLE statement.

Using empty-string defaults without a data reason

An empty string is a real value, not the same as unknown or absent data. Choose defaults deliberately.

Creating application tables dynamically for every request

Permanent schema changes are usually deployment or migration operations, not routine request-time application behavior.

Frequently Asked Questions

Q1: What does CREATE TABLE do in MySQL?

It creates a new table and defines its columns, datatypes, keys, defaults, and other constraints.

Q2: What does CREATE TABLE IF NOT EXISTS do?

It avoids a table-already-exists error, but it does not verify that the existing table has the same structure as the CREATE statement.

Q3: Should I DROP a table before CREATE TABLE?

Only when deleting the existing table and all of its data is intentional. Otherwise use IF NOT EXISTS or ALTER TABLE as appropriate.

Q4: How do I check whether a table exists?

Use SHOW TABLES LIKE 'table_name' or query INFORMATION_SCHEMA.TABLES.

Q5: How do I make an auto-incrementing primary key?

Use an integer column with NOT NULL AUTO_INCREMENT and define it as the PRIMARY KEY.

Q6: Should I use INT(3) for a three-digit number?

No. Integer display width does not limit values to three digits. Choose the integer type according to the numeric range you need.

Q7: Can I create a table from PHP?

Yes. PDO can execute CREATE TABLE, but permanent schema changes are usually better handled through deployment or migration processes rather than normal page requests.


Subqueries ALTER TABLE SQL References

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com
mobo

03-12-2009

can you please tell me how can i copy a table from one user to another in mysqlplus. Many thanks
dan

02-06-2012

Thank you very very much!




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer