Home > Database > Mysql Tutorial > How to create a table in mysql

How to create a table in mysql

下次还敢
Release: 2024-04-14 18:33:56
Original
382 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

5. Verify the table

Verify that the table was created using the following query:

<code>SHOW TABLES;</code>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template