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, ...
随机推荐
- MongoDB相关记录
win10中zip安装 下载地址:http://dl.mongodb.org/dl/win32/x86_64 首先解压至某文件夹, 使用管理员权限打开cmd或者powershell, 进入指定目录中的 ...
- mysql之表的查看操作
一 补充一些杂碎的知识 1 插入数据: create table 新表名(字段 数据类型[约束条间]...) select 字段... from 旧表名 create table 新表名(字段 数据类 ...
- 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)
传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi表示第i ni~ ni n位第一个 ...
- 2018.10.25 uestc上天的卿学姐(计数dp)
传送门 看了DZYODZYODZYO的题解之后发现自己又sbsbsb了啊. 直接dpdpdp是O(2d)O(2^d)O(2d)更新,O(1)O(1)O(1)查询或者O(1)O(1)O(1)更新,O(2 ...
- spring+springMVC+mybatis+maven+mysql环境搭建(二)
上一篇整合了spring+mybatis,基本上还不是web工程,接下来接入springMVC,Let's go! 一.工程转换成Web工程 首先右击项目-->properties-->p ...
- Codeforces Round #544 (Div. 3) dp + 双指针
https://codeforces.com/contest/1133/problem/E 题意 给你n个数(n<=5000),你需要对其挑选并进行分组,总组数不能超过k(k<=5000) ...
- AttributeError: type object 'testClass' has no attribute 'testMothod'
点击"Unittest for test_post_API.testClass"按钮,点击”Edit configuration...“,弹出对话框Run/Debug config ...
- c++中typedef、define、const、inline之间的区别
1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...
- 第09章:MongoDB-CRUD操作--文档--修改--update
①语法 db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boole ...
- altera FIFO知识点
虽然是很常用的IP,但经常用时还要看下文档,某些知识点如果能记住的话,还是可以节省很多时间的. (1)输入输出位宽不相等 这里以输入16位数据输出为8位数据为例,写入两16位的数据,读出4个8位的 ...