Home  >  Article  >  Database  >  How to create a table in mysql

How to create a table in mysql

下次还敢
下次还敢Original
2024-04-14 18:33:56354browse

Creating a table in MySQL involves five steps: 1. Open the MySQL command line; 2. Connect to the database; 3. Use the CREATE TABLE syntax to specify the column name and data type to create the table; 4. Execute the query; 5. Use the SHOW TABLES query to verify that the table has been created.

How to create a table in mysql

Steps to create a table in MySQL

The process of creating a table in MySQL is simple and straightforward, including the following steps:

1. Open the MySQL command line

Use a terminal or command prompt to open the MySQL command line interface.

2. Connect to the database

Enter the following command to connect to the database where you want to create the table:

<code>mysql -u username -p password -D database_name</code>

Among them:

  • username is your MySQL username
  • password is your password
  • database_name is the name of the database you want to connect

Press Enter to connect .

3. Create a table

Use the following syntax to create a table:

<code>CREATE TABLE table_name (
  column1_name data_type,
  column2_name data_type,
  ...
);</code>

Where:

  • table_name is the value you want The created table name
  • column1_name, column2_name, etc. are the column names you want to create
  • data_type is the data type you want to store in the column

For example, To create a table named "users" with 3 columns: "id" (integer), "name" (string), and "age" (integer), you can use the following command:

<code>CREATE TABLE users (
  id INT,
  name VARCHAR(255),
  age INT
);</code>

4. Execute the query

Press the Enter key to execute the query. If successful, MySQL will display the following message:

<code>Query OK, 0 rows affected</code>

5. Verify the table

Verify that the table was created using the following query:

<code>SHOW TABLES;</code>

This will Displays all existing tables, including the one you just created.

The above is the detailed content of How to create a table in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn