How to create a new database and table in MySQL?
To create a new database and table in MySQL, you can follow these steps:
- Connect to the MySQL server. You can use command-line tools like MySQL Shell, MySQL command-line client, or graphical interface tools like phpMyAdmin, MySQL Workbench, etc.
- Create a new database:
- In the command line tool, use the following syntax to create a database: CREATE DATABASE database_name;
Here, database_name is the name of the database you want to create.
- In graphical interface tools, there is usually an option for “Create New Database.” Simply click on this option and fill in the database name.
- Switch to the newly created database.
- Switch to the created database using the following syntax in the command line tool: USE database_name;
Here, database_name refers to the name of the database you want to switch to.
- In the graphical interface tool, you can select the database you created in places like “Select Database” or “Connection Properties”.
- Create a new table:
- To create a table in the command line tool, use the following syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
…
);Here, table_name is the name of the table to be created, column1, column2, … are the column names of the table, datatype is the data type of the column, and constraint is the constraint of the column (such as primary key, unique, not null, etc).
- In graphic interface tools, there is usually an option for “creating a new table”. Clicking on this option and filling in the table’s name, column names, data types, etc. will suffice.
The above is the method for creating a new database and table in MySQL. Choose the appropriate tools and methods based on the specific situation.