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.
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:
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:
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!