前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

MySQL操作基础之DML语句,数据操纵语言(增删改)

qiguaw 2024-11-10 10:47:16 资源文章 12 ℃ 0 评论

MySQL基本操做之DDL(数据定义语言),DML(数据操纵语言),DQL(数据查询语言),DCL(数据控制语言)

这里介绍DML语句(数据操纵语言),用于数据的增加删除更改,也就是我们常说的增删改

示例表:

示例数据库:testDatabase,示例表:myTable

mysql root@(none):testDatabase> select * from `myTable`
+----+----------------+----------------+----------+-------------+
| id | name | phone | country | numberrange |
+----+----------------+----------------+----------+-------------+
| 1 | Wade Sykes | 1-917-342-3132 | Turkey | 3 |
| 2 | Barrett Boyer | 1-264-304-0665 | Germany | 9 |
| 3 | Alana Kaufman | (213) 254-4997 | India | 0 |
| 4 | Emmanuel Lopez | (543) 493-0137 | Germany | 9 |
| 5 | Timon Bauer | 1-269-448-2772 | Pakistan | 6 |
| 7 | ABC | 1-917-342-3123 | KHMER | 6 |
+----+----------------+----------------+----------+-------------+
6 rows in set
Time: 0.121s


一:增(Insert)

格式:

insert [into] <数据库>.<数据表>(字段名) values (字段值); 
-- into可省略

示例:

1)只插入某一列的值

insert into myTable(name) values("zhangsan");

2)按规矩指定所有列名,并且每列都插入值

注意数据类型,如字符类型values值需要加 `" "`

insert into myTable(name,phone,country,numberrange) values("zhangsan",42132513,"China",5);

显示如下:

mysql > insert into myTable(name,phone,country,numberrange) values("zhangsan",42132513,"China",5);
Query OK, 1 row affected
Time: 0.042s
mysql > select * from myTable;
+----+----------------+----------------+----------+-------------+
| id | name | phone | country | numberrange |
+----+----------------+----------------+----------+-------------+
| 1 | Wade Sykes | 1-917-342-3132 | Turkey | 3 |
| 2 | Barrett Boyer | 1-264-304-0665 | Germany | 9 |
| 3 | Alana Kaufman | (213) 254-4997 | India | 0 |
| 4 | Emmanuel Lopez | (543) 493-0137 | Germany | 9 |
| 5 | Timon Bauer | 1-269-448-2772 | Pakistan | 6 |
| 7 | ABC | 1-917-342-3123 | KHMER | 6 |
| 8 | zhangsan | 42132513 | China | 5 |
+----+----------------+----------------+----------+-------------+
7 rows in set
Time: 0.008s

3)如果不知道列,就要按规矩为每列都插入恰当的值

示例:

注: 这种方式,需要注意自增项的列(id),也需要赋值,可以自定义id值,也可以null,让自增。

insert myTable values(null,"lisi",3213123,"uk",6);

4)批量插入数据的方法,提升效率

insert myTable values(null,"lisi",3213123,"uk",6),(null,"wangwu",2313123,"usa",8);

二:删(Delete)

Delete,意为删除,顾名思义就是删除表中已有的某些记录。

格式:

delete from <数据库>.<数据表> where <条件>;

示例:

delete from myTable where name = 'lisi';

注:以上会删除所有同名为“lisi”的列,所有实际中以唯一量的值删除,如id。

例:

删除id为9和12的列。

delete from `myTable` where id in (9,12);

三:改(UPDATE)

Update,意为更新,顾名思义,就是修改表中原有的数据。

格式:

update <数据库>.<数据表> set <字段名=新字段值> where <条件>;

示例:

update myTable set name="wangwu" where id=15;

或者改多项值:

update myTable set name="wangwu",phone=123323434 where id=15;

以上是基本的DML(数据操纵语言)语句。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表