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;
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
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);
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.
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);
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!