How to create a table in MySQL database?

To create a MySQL database table, you can use the CREATE TABLE statement. Here is the basic syntax for creating a table:

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

In this, table_name refers to the name of the table, column1, column2, … refers to the column names, datatype refers to the data type of the column, and constraint refers to the column constraints.

Here is a practical example: creating a table named “Users” with three columns – id, name, and age.

CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    age INT
);

In the example above, the id column is set as the primary key and uses the INT data type, the name column uses the VARCHAR data type, and is set with a NOT NULL constraint, and the age column uses the INT data type.

Please note that MySQL supports a variety of data types and constraints that can be selected and set based on your needs.

bannerAds