insert knowledge MySQL database application (8) DML statements of

A, inserting data into a table

    1, the command syntax:

    insert into <table name> [(<field name 1> [, .. <field name n>])] values ​​(value 1) [, (n-value)]

    2, create a simple test table test

create table `test`(
`id` int(4) not null auto_increment,
`name` char(20) not null,
primary key (`id`)
);

    3, to the table into a different example of the syntax of the data:

        1) according to the rules specified all column names, and each column values ​​is inserted

mysql> insert into test(id,name) values(1,'oldboy');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | oldboy |
+----+--------+
1 row in set (0.00 sec)

        2) Since the increment as id, all may be inserted in the column name value only

mysql> insert into test(name) values('oldgirl');
Query OK, 1 row affected (0.11 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
+----+---------+
2 rows in set (0.00 sec)

        3) If a column does not know, it is necessary according to the rules for each column is inserted a proper value

mysql> insert into test values(3,'inca');
Query OK, 1 row affected (0.36 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
+----+---------+
3 rows in set (0.00 sec)

        4) Bulk data insertion method, improve efficiency

MySQL> INSERT INTO Test values ( . 4 , ' Zuma ' ), ( . 5 , ' Kaka ' ); # bulk insert two records, improve efficiency 
Query the OK, 2 rows affected ( 0.35 sec) 
Records: 2   Duplicates: 0   Warnings: 0 

MySQL > the SELECT * from the Test;
 + ---- + --------- + 
| the above mentioned id | name | 
+ ---- + --------- + 
|   1 | Oldboy | 
|   2 | oldgirl | 
|   3 | INCA | 
|   4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

insert into `test` values (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');

    4, insert data practical demonstration:

mysql> delete from test;
Query OK, 5 rows affected (0.36 sec)

mysql> select * from test;
Empty set (0.00 sec)

mysql> insert into `test` values (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');
Query OK, 5 rows affected (0.10 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

     5, the test is completed, out of the database, and then back above data, keep spare:

[root@localhost ~]# mysqldump -uroot -pdubin -B oldboy >/opt/oldboy_bak.sql
[root@localhost ~]# ls -l /opt/oldboy_bak.sql 
-rw-r--r--. 1 root root 2826 9月  20 08:42 /opt/oldboy_bak.sql
[root@localhost ~]# 

    6, sql backup check contents data backup: filter useless information

[root@localhost ~]# grep -E -v "#|\/|^$|--" /opt/oldboy_bak.sql 
USE `oldboy`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  `age` tinyint(2) NOT NULL DEFAULT '0',
  `dept` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_inde_name` (`name`),
  KEY `index_dept` (`dept`(8)),
  KEY `ind_name_dept` (`name`(8),`dept`(10))
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `student` WRITE;
UNLOCK TABLES;
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
LOCK TABLES `test` WRITE;
INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');
UNLOCK TABLES;

    7, a backup error case: If you do not check the backup data may result in data not want.

[root @ localhost ~] # mysqldump-uroot--pdubin -A -B Oldboy> / opt / oldboy_bak1.sql 
[root @ localhost ~] # grep -E -v " # | \ / | ^ $ | - " / opt / oldboy_bak1.sql 
the Usage: the mysqldump [the OPTIONS] Database [Tables] 
[the root @ localhost ~] # CAT / opt / oldboy_bak1.sql 
the Usage: the mysqldump [the OPTIONS] Database [Tables] 
OR the mysqldump [the OPTIONS] - Databases [the OPTIONS] DBl [the DB2 DB3 ...] 
OR mysqldump [the OPTIONS] --all- Databases [the OPTIONS] 
the For More Options, use mysqldump --help 

tips:
1, something wrong -A represents all libraries, behind the library can not specify oldboy
2 , version 5.1.68:
[root@localhost ~]# mysqldump -uroot -pdubin -A -B --event >/tmp/oldboy_bak.sql

     8, make deposit emphasize: We usually visit the website postings, Bowen, are essentially calling program web site connect to the MySQL database, through the above statement to insert data into the database Post Bowen's.

Guess you like

Origin www.cnblogs.com/cnxy168/p/11594362.html