小工具知识网

当前位置: 首页 > 行业知识 > MySQL之表的记录操作

行业知识

MySQL之表的记录操作

2026-11-12 15:00:26 来源:互联网转载

前言

���数据不是目的,目的是能够将存起来的数据取出来或者查出来,并且能够对数据进行增删改查操作,本文将详细介绍表中记录的增删改查操作。对记录的操作属于DML数据库操作语言,可以通过SQL实现对数据的操作,包括实现向表中插入数据(insert),对表中数据进行更新(update),删除表中数据(delete)以及查询数据(select)。

增 - insert

表准备

create table info ( id int primary key auto_increment, name varchar(6), age int, gender enum ( 'male', 'female' ) defaule 'male' );

最标准的insert语句

-- INSERT INTO 表名(字段1, 字段2,...) VALUES (字段1对应的值, 字段2对应的值...);mysql> insert into info(name, age, gender) values ('xu', 18, 'male');mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  1 | xu   |   18 | male   |+----+------+------+--------+1 row in set (0.00 sec)

省事的写法,按照所有字段顺序进行插入数据

-- INSERT INTO 表名 VALUES (字段1对应的值, 字段2对应的值...);mysql> insert into info values (2, 'lili', 20, 'female');Query OK, 1 row affected (0.00 sec)mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  1 | xu   |   18 | male   ||  2 | lili |   20 | female |+----+------+------+--------+2 rows in set (0.00 sec) 

针对性的录入数据

-- INSERT INTO 表名 (字段1, 字段2...) VALUES (字段1对应的值, 字段2对应的值...)mysql> insert into info (name, age) values ('jack', 30);Query OK, 1 row affected (0.00 sec)mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  1 | xu   |   18 | male   ||  2 | lili |   20 | female ||  3 | jack |   30 | male   |+----+------+------+--------+3 rows in set (0.00 sec)

同时录入多行数据

-- INSERT INTO 表名 (字段1, 字段2...) VALUES (字段1对应的值, 字段2对应的值...), (字段1对应的值, 字段2对应的值...)...;mysql> insert into info (name, age) values ('python', 30),('java', 40),('go', 50);Query OK, 3 rows affected (0.00 sec)Records: 3  Duplicates: 0  Warnings: 0mysql> select * from info;+----+--------+------+--------+| id | NAME   | age  | gender |+----+--------+------+--------+|  1 | xu     |   18 | male   ||  2 | lili   |   20 | female ||  3 | jack   |   30 | male   ||  4 | python |   30 | male   ||  5 | java   |   40 | male   ||  6 | go     |   50 | male   |+----+--------+------+--------+6 rows in set (0.00 sec)

删 - delete

删除操作一定要慎重慎重再慎重!!!!

 
-- 删除表中的所有数据,但是主键的自增不会停止delete from 表名;  -- 清空表数据并重置主键truncate 表名;  -- 删除表中某一条或几条数据:delete from 表名 where 条件; (where条件在介绍查询时会详细介绍)mysql> delete from info where id=1;Query OK, 1 row affected (0.00 sec)mysql> select * from info;+----+--------+------+--------+| id | NAME   | age  | gender |+----+--------+------+--------+|  2 | lili   |   20 | female ||  3 | jack   |   30 | male   ||  4 | python |   30 | male   ||  5 | java   |   40 | male   ||  6 | go     |   50 | male   |+----+--------+------+--------+5 rows in set (0.00 sec)mysql> delete from info where age=30;Query OK, 2 rows affected (0.00 sec)mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  2 | lili |   20 | female ||  5 | java |   40 | male   ||  6 | go   |   50 | male   |+----+------+------+--------+3 rows in set (0.00 sec)

改 - update

对表中已经存在的记录进行修改。

 
-- update 表名 set 字段1=值1, 字段2=值2 where 条件;-- 如果不加where条件会将表中所有记录都修改mysql> update info set name='xxx', age=100;Query OK, 3 rows affected (0.00 sec)Rows matched: 3  Changed: 3  Warnings: 0mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  2 | xxx  |  100 | female ||  5 | xxx  |  100 | male   ||  6 | xxx  |  100 | male   |+----+------+------+--------+3 rows in set (0.00 sec)-- 如果不想修改全部的记录就需要使用where条件mysql> update info set name='test', age=10 where id=2;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from info;+----+------+------+--------+| id | NAME | age  | gender |+----+------+------+--------+|  2 | test |   10 | female ||  5 | xxx  |  100 | male   ||  6 | xxx  |  100 | male   |+----+------+------+--------+3 rows in set (0.00 sec)

查 - select

表与表之间有时是有关系的,尤其是在同一个项目中的表,因此查询数据就分为单表查询和多表查询。这里师门使用MySQL官方提供的MySQL示例进行介绍查询操作,打开压缩包之后是world.sql文件,如何将这个文件导入到MySQL中呢,可以通过下面的语法:

 
-- source .sql文件的路径mysql> source  C:\Users801\Desktop\world.sqlmysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || book_manage        || mysql              || performance_schema || stu                || test               || world              |+--------------------+7 rows in set (0.00 sec)

单表查询

查询语句的标准用法是需要select配合其他子句进行查询,因为在实际项目开发中并不是每次都需要查询所有的数据,而是需要一些过滤条件进行过滤,查询出需要的数据,并且在实际开发中不推荐查询所有数据,因为如果一张表中数据太多的话,内存可能会不够用哦~会导致电脑卡***...

首先和select配合使用的子句有以下几个select /from/where/group by/having/order by/limit,上面这些子句的书写顺序也是默认的SQL书写顺序。

from子句

是从哪张表中查询数据。

-- select 列名 from 表名;-- 全表扫描,查询country表中的所有数据,不建议这样操作,数据过多可能导致电脑卡***mysql>select * from country;  -- 只查询表中部分列的所有值,查询country表中的code、name列的所有值mysql> select code, name from country;

where子句

where子句就需要和过滤条件配合使用,过滤条件包括= >

 
-- 查询中国所有城市信息select * from city where countrycode='CHN';-- 中国人口大于1000000的城市信息select * from city countrycode='CHN' and population>5000000; -- 查询省的名字前面带guang开头的,注意:like语句时,%不能放在前面,因为不走索引(索引会在后续文章中介绍,这里就简单理解索引就是书的目录)select * from city where district like 'guang%';-- 查询中国和美国所有城市信息select * from city where countrycode in ('CHN' ,'USA');-- 查询世界上人口数量大于100w小于200w的城市信息select * from city where population >1000000 and population 

上一篇:His系统模板编辑详解,轻松成为编辑高手!

下一篇:MySQL默认值(DEFAULT)和MySQL非空约束(NOT NULL)