How to build a database in MySQL

PHPz
Release: 2023-05-23 09:49:07
Original
37320 people have browsed it

MySQL is a very popular relational database management system. Whether in business or personal projects, MySQL databases are often used. Before using MySQL, you need to create a database first. This article will introduce how to set up a MySQL database.

1. Install MySQL

Before you start to create a MySQL database, you need to install MySQL first. You can download the MySQL installation package and follow the prompts to install it. During the installation process, you need to set the password of the root user. It is recommended to set a strong password to protect the security of the MySQL database.

2. Use the command line tool to connect to MySQL

After the installation is complete, you can use the command line tool to connect to MySQL. Enter the following command in the command line window:

mysql -u root -p
Copy after login

where "root" is the MySQL super administrator account. After executing this command, you will be prompted to enter a password. Enter the password of the root user you set. After the connection is successful, you will see the MySQL command line tool.

3. Create a database

In the command line tool connected to MySQL, execute the following command to create a new database:

CREATE DATABASE database_name;
Copy after login

where "database_name" is the name of the new database The name can be modified according to actual needs.

4. Create a table

In the database, you need to create a table to store data. The following is the command to create a table named "users":

CREATE TABLE users ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(30) NOT NULL, password VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL, reg_date TIMESTAMP, PRIMARY KEY (id) );
Copy after login

This table contains 4 fields: id, username, password and email, which are user ID, username, password and email address respectively. . The id field is of integer type and is automatically incremented; the other three fields are of VARCHAR type. The reg_date field is a timestamp type and is used to record the user's registration time. PRIMARY KEY specifies the id field as the primary key.

5. Insert data

After the table is created, you can add data to the table. The following is a command to add a row of data to the users table:

INSERT INTO users (username, password, email, reg_date) VALUES ('user1', 'password1', 'user1@example.com', NOW());
Copy after login

This command inserts a row of data to the users table, including the values of 4 fields, namely: 'user1', 'password1', ' user1@example.com' and the current time.

6. Query data

After adding data to the table, you can use the SELECT command to query and obtain the data. The following is a command to query all row data in the users table:

SELECT * FROM users;
Copy after login

This command will return all row data in the users table.

7. Summary

MySQL is a popular relational database management system. You need to create a database before using it. You can connect to MySQL through the command line tool, and execute a series of commands to create databases, create tables, insert and query data, and other operations. It is very important for developers to be proficient in the use of MySQL.

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

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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!