Home > Database > Mysql Tutorial > body text

How to insert data in mysql

PHPz
Release: 2023-04-17 17:28:57
Original
30292 people have browsed it

MySQL is a commonly used relational database management system. Through MySQL, we can easily manage data. When operating a MySQL database, data insertion is a basic operation and a frequently used operation. This article will introduce you how to insert data in MySQL, I hope it will be helpful to beginners.

1. Data table creation

Before you start inserting data, you first need to create the data table into which you want to insert data. In MySQL, we can create data tables through the CREATE TABLE statement. The following is an example:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

In the above code, we created a data table named "users". The data table contains three fields: id, name and age, where id is the primary key and the data type is int; name is a string type, the length is 255, and cannot be empty; age is an integer type, and cannot be empty.

2. Data insertion

  1. Insert a row of data

We can use the INSERT INTO statement to insert a row of data. The following is the common format of this statement:

INSERT INTO `users` (`name`, `age`) VALUES ('Tom', 18);
Copy after login

In the above code, we inserted a row of data into the users data table. The data contains two fields, name and age, with values ​​​​"Tom" and 18 respectively.

  1. Insert multiple rows of data

If you need to insert multiple rows of data into the data table at the same time, it is also very simple. We only need to add multiple sets of values ​​after VALUES. Can. The following is an example:

INSERT INTO `users` (`name`, `age`) VALUES
('Tom', 18),
('Jack', 19),
('Lily', 20);
Copy after login

In the above code, we inserted three rows of data into the users data table, each row of data contains two fields: name and age.

3. Summary

Through the above introduction, we can find that inserting data in MySQL is a very simple operation. We only need to create the data table first, and then use the INSERT INTO statement to insert data. I hope this article can help beginners better master the basic operations of the MySQL database.

The above is the detailed content of How to insert data 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
Popular Tutorials
More>
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!