Home >Database >Mysql Tutorial >How to create a table in mysql database
In the MySQL database, the steps to create a table include: Connect to the database and write the CREATE TABLE statement, which includes the table name, column name, data type and primary key (optional) Execute the statement and use SHOW TABLES to verify the table creation Use the INSERT statement to insert data and the SELECT statement to query data
Creating a table in a MySQL database is a Important task, it allows you to store and manage data. Here are the steps to create a table:
First, you need to connect to the MySQL database where you want to create the table. You can do this using the MySQL command line client or any GUI tool that supports MySQL.
Next, you need to write a CREATE TABLE
statement to define the table structure. The statement includes:
For example, the following statement Create a table named users
with columns name
(VARCHAR), age
(INT), and id
(INT):
<code>CREATE TABLE users ( name VARCHAR(255) NOT NULL, age INT NOT NULL, id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) );</code>
Once you have written the CREATE TABLE
statement, you can execute it. This can be achieved by running it in the MySQL command line client or GUI tool.
After the table is created, you can use the SHOW TABLES
statement to verify whether it exists:
<code>SHOW TABLES;</code>
Next, you can use the INSERT
statement to insert data into the newly created table. For example, the following statement inserts a record into the users
table:
<code>INSERT INTO users (name, age) VALUES ('John Doe', 30);</code>
Finally, you can use the SELECT
statement to retrieve data from the table Query data. For example, the following statement selects all records from the users
table:
<code>SELECT * FROM users;</code>
The above is the detailed content of How to create a table in mysql database. For more information, please follow other related articles on the PHP Chinese website!