How to create a new table in mysql?

To create a new table in MySQL, you can use the CREATE TABLE statement. Here is an example of creating a table:

CREATE TABLE 表名 (
    列1 数据类型,
    列2 数据类型,
    列3 数据类型,
    ...
);

In this case, “table name” refers to the name of the table to be created, “column 1”, “column 2”, “column 3”, etc. refer to the column names of the table, and “data type” refers to the data type required for each column.

For example, to create a table named “users” with three columns – id, name, and age, you can use the following code:

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

In the above code, the data type for the id column is INT, the data type for the name column is VARCHAR, and the data type for the age column is INT. Additionally, the id column is also set as the primary key.

It is important to select the appropriate data types when creating a table, in order to ensure that the table can store the data and set appropriate constraints (such as primary keys, foreign keys, etc.) according to the requirements.

After creating a table, you can use the INSERT INTO statement to insert data into the table, the SELECT statement to query data from the table, and the ALTER TABLE statement to modify the table structure.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds