2020-02-19数据库操作语言DML

往表中插入数据
  语法
insert into <表名> (<字段名1> ,<字段名n>) values (值1),(值2)
mysql> CREATE TABLE 'test' (
    -> id int(4) NOT NULL AUTO_INCREMENT,
    -> name char(20) NOT NULL,
    -> PRIMARY KEY(`id`)
    -> );

常规的插入方法
1. 指定所有列名,并且每个列都插入值
'''
mysql> insert into test(id,name) values(1,'scott');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+-------+
| id | name  |
+----+-------+
|  1 | scott |
+----+-------+
1 row in set (0.00 sec)
'''

2. 由于id列为自增的,所以,可以只在name列插入值
'''
mysql> insert into test(name) values('scott');
Query OK, 1 row affected (0.00 sec)

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

3. 如果不指定列,就要按规矩为每个列都插入适当的值
'''
mysql> insert into test values(4,'scott_boy');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | scott     |
|  2 | scott     |
|  4 | scott_boy |
+----+-----------+
3 rows in set (0.00 sec)

'''

4. 批量插入数据的方法,提升效率,
'''
插入两个值
mysql> insert into test values(3,'scott_boy'),(5,'kaka');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | scott     |
|  2 | scott     |
|  3 | scott_boy |
|  4 | scott_boy |
|  5 | kaka      |
+----+-----------+
5 rows in set (0.00 sec)
'''

删除数据
'''
mysql> delete from test;
Query OK, 5 rows affected (0.00 sec)

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

mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(4)   | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

'''

插入多条数据
'''
mysql> insert into test values(1,'scott'),(2,'scot'),(4,'sco'),(3,'scott_boy'),(5,'kaka')
    -> ;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | scott     |
|  2 | scot      |
|  3 | scott_boy |
|  4 | sco       |
|  5 | kaka      |
+----+-----------+
5 rows in set (0.00 sec)
'''
mysqldump -uroot -p -B scott > ./scott.sql

-A 指定所有的库
[root@mangodb ~]# grep -E -v "#|\/|'$|--" scott.sql

你可能感兴趣的:(2020-02-19数据库操作语言DML)