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, ...
随机推荐
- canvas 实现微信小游戏
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); var timer; var iS ...
- tms web core 与 kbmmw 第一次亲密接触
最近,tms 经过1年多,集合了数十名高手大牛,开发出了一个跨时代的产品,就是tms web core. 具体的介绍详见官网,https://www.tmssoftware.com/site/tmsw ...
- centos 7 禁止root登录及更改ssh端口号
vim /etc/ssh/sshd_config PermitRootLogin yes => PermitRootLogin no systemctl restart sshd.service ...
- ORACLE 导入的问题
1.导入报错 我将ORACLE12.2 导出的文件,导入到ORACLE12.1 . IMP-00010: 不是有效的导出文件, 标头验证失败 解决办法: 修改 dmp 文件版本,使用UEDITOR打开 ...
- springboot xml声明式事务管理方案
在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现. 使用注解 在启动类上添加注解 @EnableTransactionManagement 在需要事务控制的方法添加@Tran ...
- Linux 的虚拟文件系统(强烈推荐)
1 引言 Linux 中允许众多不同的文件系统共存,如 ext2, ext3, vfat 等.通过使用同一套文件 I/O 系统 调用即可对 Linux 中的任意文件进行操作而无需考虑其所在的具体文件系 ...
- python的6种基本数据类型--集合
特征 1.确定性(元素必须可hash) 2.互异性(去重) 3.无序性(集合中的元素没有顺序,先后之分) >>> s = {1,1,1,2,2,3,4,5,6,7} # 创建 > ...
- eclipse 创建servlet 出现继承 HttpServlet 报红线
eclipse创建servlet出现红线: 解决方案1,鼠标右键项目 -> 鼠标右击项目——>Build Path——> 点击comfigure Build Path进入-----& ...
- 如何比较两个xml 的异同
http://www.xmlunit.org/ <dependency> <groupId>org.xmlunit</groupId> <ar ...
- java代码执行顺序
class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println(&quo ...