MariaDB 插入&更新&删除数据(8)
存储在系统中的数据是数据库管理系统(DBMS)的核心,数据库被设计用来管理数据的存储、访问和维护数据的完整性,MariaDB中提供了功能丰富的数据库管理语句,包括有效地向数据库中插入数据的INSERT语句,更新数据的UPDATE语句以及当数据不再使用时删除数据的DELETE语句,本小结将依次来介绍这些命令的使用方法和技巧.
MariaDB 插入数据
MariaDB中使用INSERT语句插入数据,可以插入的方式有:插入完整记录,插入记录的部分,插入多条记录,插入另一个查询的结果,废话不多说,老样子先来看一下插入语句的写法吧:
INSERT INTO 表名称(字段1,字段2,字段3,.....) VALUES(数值1,数值2,数值3....)
为了方便后续的练习,我们先来创建一个表结构,SQL语句如下:
MariaDB [lyshark]> create table person
-> (
-> id int unsigned not null auto_increment,
-> name char(50) not null default '',
-> age int not null default 0,
-> info char(50) null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.00 sec)
◆在所有字段插入数据◆
在person
表中,插入一条新记录id=1,name=LyShark,age=22,info=Lawyer
,SQL语句如下:
MariaDB [lyshark]> select * from person;
Empty set (0.00 sec)
MariaDB [lyshark]> insert into person(id,name,age,info) values(1,'LyShark',22,'Lawyer');
Query OK, 1 row affected (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------+
| id | name | age | info |
+----+---------+-----+--------+
| 1 | LyShark | 22 | Lawyer |
+----+---------+-----+--------+
1 row in set (0.00 sec)
MariaDB [lyshark]>
◆在指定字段插入数据◆
在person
表中,插入一条新记录,name=Willam,age=18,info=sports
,我们不给其指定ID,SQL语句如下:
MariaDB [lyshark]> desc person;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(50) | NO | | | |
| age | int(11) | NO | | 0 | |
| info | char(50) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(name,age,info) values('Willam',18,'sports man');
Query OK, 1 row affected (0.04 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
+----+---------+-----+------------+
2 rows in set (0.00 sec)
MariaDB [lyshark]>
◆同时为表插入多条记录◆
在person
表中,同时插入3条新记录,有多条只需要在每一条的后面加,
即可,SQL语句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
+----+---------+-----+------------+
2 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(name,age,info) values('Evans',27,'secretary'),
-> ('Dale',22,'cook'),
-> ('Edison',28,'singer');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
+----+---------+-----+------------+
5 rows in set (0.00 sec)
◆将查询结果插入到表中◆
为了实现将另一个表中的记录插入到本表中,我们新建一个person_old
表,其表结构和person相同,我们将person_old
表中的内容全部迁移到person
中去,SQL语句如下:
1.创建一个person_old
表,并插入测试字段:
MariaDB [lyshark]> create table person_old
-> (
-> id int unsigned not null auto_increment,
-> name char(50) not null default '',
-> age int not null default 0,
-> info char(50) null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [lyshark]> insert into person_old
-> values(11,'harry',20,'student'),(12,'Beckham',33,'police');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
2.接下来我们将person_old
表中的内容迁移到person
中去
MariaDB [lyshark]> select * from person_old;
+----+---------+-----+---------+
| id | name | age | info |
+----+---------+-----+---------+
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+---------+
2 rows in set (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
+----+---------+-----+------------+
5 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(id,name,age,info)
-> select id,name,age,info from person_old;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
## MariaDB 更新数据
表中有数据之后,接下来我们可以对数据进行更新操作,MariaDB中使用UPDATE
语句更新表中的记录,可以更新特定的行或同时更新所有的行,基本语句结构如下:
UPDATE 表名称
SET 字段1=修改值,字段2=修改值,字段3=修改值
where (限定条件);
◆更新表中指定字段◆
修改person
表中数据,将id=11
的name字段
的值改为xxxx
,age字段改为200
,SQL语句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> update person set age=200,name='xxxx' where id=11; #更新单个字段
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | xxxx | 200 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
◆更新表的一个范围◆
更新person表中的记录,将1-12
的info字段
全部改为lyshark blog
,SQL语句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | xxxx | 200 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> update person set info='lyshark blog' where age between 1 and 200; #指定修改的字段
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
| 12 | Beckham | 33 | lyshark blog |
+----+---------+-----+--------------+
7 rows in set (0.00 sec)
## MariaDB 删除数据
◆删除表中指定记录◆
通过id号,删除表中指定列,此处删除第id=12
号,这条记录,SQL语句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
| 12 | Beckham | 33 | lyshark blog |
+----+---------+-----+--------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person where id=12; #通过id号,删除表中指定列
Query OK, 1 row affected (0.05 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+---------+-----+--------------+
6 rows in set (0.00 sec)
◆删除表的一个范围◆
在person表
中,删除age字段值
在19-22
的记录,SQL语句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+---------+-----+--------------+
6 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person where age between 19 and 22; #指定范围删除
Query OK, 2 rows affected (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+--------+-----+--------------+
| id | name | age | info |
+----+--------+-----+--------------+
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+--------+-----+--------------+
4 rows in set (0.00 sec)
◆清空表中所有记录◆
MariaDB [lyshark]> select * from person;
+----+--------+-----+--------------+
| id | name | age | info |
+----+--------+-----+--------------+
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+--------+-----+--------------+
4 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person; #清空表中所有记录
Query OK, 4 rows affected (0.00 sec)
MariaDB [lyshark]> select * from person;
Empty set (0.00 sec)
参考文献:mysql5.7从入门到精通
MariaDB 插入&更新&删除数据(8)的更多相关文章
- oracle插入,更新,删除数据
插入,更新,删除数据 oracle提供了功能丰富的数据库管理语句 包括有效的向数据库中插入数据的insert语句 更新数据的update语句 以及当数据不再使用时删除数据的delete语句 更改数据之 ...
- 我的MYSQL学习心得(八) 插入 更新 删除
我的MYSQL学习心得(八) 插入 更新 删除 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得( ...
- sqlserver 插入 更新 删除 语句中的 output子句
官方文档镇楼: https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/ms177564(v=sql.100) 从 ...
- MySQL基本SQL语句之数据插入、删除数据和更新数据
一.INSERT插入数据: 方法一:批量插入 基本语法: INSERT INTO tb_name (col1, col2, ...) VALUES (val1, val2, ...)[,(val1, ...
- Hibernate更新删除数据后,再查询数据依然存在的解决办法
删除数据后,重新查询了数据库,DB中记录已经删除了,但是数据依然能查询到,网上都说是Hibernate的缓冲问题. 我对session进行了clear,flush,并且在事务和查询中都对session ...
- Mysql添加更新删除数据-表
例如 此处拥有一个表名为 uuser 为表添加新数据 ,'); ,'); ,'); 假如只想添加uid和uname ,'小张'); 那么pas自动填充为NULL. 为表更新数据 这里把小王的pas改成 ...
- MySQL 向表中插入、删除数据
一.向表中插入一条信息 1.查看表中的数据 mysql> SELECT * FROM user; +----+---------+----------+ | id | account | pas ...
- 数据库SQL语言学习--上机练习3(插入 更新 删除)
上机练习3 . 将一个新学生记录(学号::姓名:陈冬:性别:男:所在系:信息系:年龄:20岁)插入到Student表中: ALTER TABLE Student ,); UPDATE Student ...
- ORACLE no1 存储过程插入更新表数据
CREATE OR REPLACE PROCEDURE sp_cust_main_data_yx(InStrDate IN VARCHAR2, ...
随机推荐
- 【NIFI】 Apache NiFI 与 SQL 操作
本里需要基础知识:[NIFI] Apache NiFI 安装及简单的使用 查询SQL 1.拖入一个 Processor:ExecuteSQLRecord(执行sql记录) 2.配置,SETTINGS的 ...
- ManageEngine卓豪 IT管理峰会圆满结束
- Python开课复习-10/10
1. 什么时匿名函数def 定义 的是有名函数:特点是可以通过名字重复调用 def func(): #func = 函数的内存地址 pass匿名函数就是没有名字的函数:特点是只能在定义时使用一次 2. ...
- 2018.10.26 NOIP训练 数数树(换根dp)
传送门 换根dpdpdp傻逼题好像不好码啊. 考虑直接把每一个二进制位拆开处理. 先dfsdfsdfs出每个点到1的异或距离. 然后分类讨论一波: 如果一个点如果当前二进制位到根节点异或距离为1,那么 ...
- jrebel热部署
一,JRebel 插件 获取与安装 1,JRebel 官网下载地址https://zeroturnaround.com/software/jrebel/download/#!/free-trial P ...
- The XOR Largest Pair(Tire字典树应用)
题目链接:传送门 思路:建立一个32位的字典树,对每一个要插入的数字查找它异或的最大值(就是尽量全部二进制的值都相反), 然后获得两个数异或的最大值. #include<iostream> ...
- NIO原理解析
NIO中主要包括几大组件,selector.channel.buffer.selector后面介绍,channel则类似于BIO中的流,但是流的读取是单向的,例如只能读,或只能写,但是channel则 ...
- JWT 理解
概念: JWT是json web token缩写.它将用户信息加密到token里,服务器不保存任何用户信息.服务器通过使用保存的密钥验证token的正确性,只要正确即通过验证. 优点是在分布式系统中, ...
- Java数组的初始化
1.动态初始化 数据类型 [] 变量名 = new 数据类型 [数组大小]; //数组的动态初始化 int [] arr = new int [3]; 2.静态初始化 数据类型 [] 变量名 = {元 ...
- 深度优先搜索DFS和广度优先搜索BFS
DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...