How to create a database table in MySQL?

To create a MySQL database table, you need to use the CREATE TABLE statement, with the syntax format as follows:

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

其中,table_name represents the desired name of the table to be created, column1, column2, …, columnN refer to the columns of the table, datatype indicates the data type of each column, and constraint specifies any constraints for the columns (such as primary key, unique key, not null, etc.).

For instance, to create a table named “users” with columns id, name, and age, where id is the primary key and name and age cannot be null, you can use the following statement to create the table:

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

This will create a table named “users” with three columns: id, name, and age. The id column is the primary key, and the name and age columns cannot be empty.

bannerAds