Mysql基础

基础概念

  • Mysql:关系型数据库管理系统,它是一种开源软件,用于存储、管理和操作大规模数据
  • 数据库(Database):存储数据的仓库。简称DB
  • 数据库管理系统(DataBase Management System):意为关系型数据库管理系统。简称DBMS
  • SQL(Structured Query Language):操作关系型数据库的编程语言。简称SQL
  • 表(Table):数据库中的数据组织方式,由多行(记录)和多列(字段)构成。
  • 记录(Record):表中的一行,包含多个字段。
  • 字段(Field):表中的一列,用于存储特定类型的数据。
  • 主键(Primary Key):表中用于唯一标识每个记录的字段或字段组合。
  • 外键(Foreign Key):关联两个表的字段,它指向另一个表中的主键。
  • 索引(Index):提高查询效率的一种数据结构,用于快速查找某个字段的值。
  • 视图(View):基于一个或多个表的查询结果,类似于虚拟表格。
  • 存储过程(Stored Procedure):预定义的一组SQL语句,可以多次重复使用。
  • 触发器(Trigger):在表上执行INSERT、UPDATE或DELETE操作时自动触发的一种特殊类型的存储过程。
  • 事务(Transaction):由一组SQL语句组成的单一逻辑工作单元,要么全部成功执行,要么全部回滚。

常见的数据库

关系型数据库:MySQL、Oracle、Microsoft SQL Server

非关系型数据库:MongoDB、Redis、Cassandra

数据类型

  • 数据所具有的类型或格式,例如整数、浮点数、字符串、日期时间等。数据类型决定了数据在数据库中的存储方式以及能够进行的操作

数值类型

  • 用于存储数值数据,包括整数、浮点数、小数等等。数值类型可以进行算术运算和比较操作,通常用于对数据进行计算和统计分析。
类型大小范围(有符号)范围(无符号)用途
TINYINT1 Bytes(-128,127)(0,255)小整数值
SMALLINT2 Bytes(-32 768,32 767)(0,65 535)大整数值
MEDIUMINT3 Bytes(-8 388 608,8 388 607)(0,16 777 215)大整数值
INT或INTEGER4 Bytes(-2 147 483 648,2 147 483 647)(0,4 294 967 295)大整数值
BIGINT8 Bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)极大整数值
FLOAT4 Bytes(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)0,(1.175 494 351 E-38,3.402 823 466 E+38)单精度 浮点数值
DOUBLE8 Bytes(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)双精度 浮点数值
DECIMAL对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2依赖于M和D的值依赖于M和D的值小数值

日期和时间类型

  • 用于存储日期和时间数据,包括年份、月份、日份、小时、分钟、秒钟等等。日期和时间类型支持对日期和时间进行计算、比较和格式化等操作,通常用于记录时间戳或者处理时序数据。
类型大小(bytes)范围格式用途
DATE31000-01-01/9999-12-31YYYY-MM-DD日期值
TIME3'-838:59:59'/'838:59:59'HH:MM:SS时间值或持续时间
YEAR11901/2155YYYY年份值
DATETIME8'1000-01-01 00:00:00' 到 '9999-12-31 23:59:59'YYYY-MM-DD hh:mm:ss混合日期和时间值
TIMESTAMP4'1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07YYYY-MM-DD hh:mm:ss混合日期和时间值,时间戳

字符串类型

  • 用于存储文本数据,包括字符、单词、句子等等。字符串类型支持文本匹配、搜索、替换和格式化等操作,通常用于存储和处理文本数据。
类型大小用途
CHAR0-255 bytes定长字符串
VARCHAR0-65535 bytes变长字符串
TINYBLOB0-255 bytes不超过 255 个字符的二进制字符串
TINYTEXT0-255 bytes短文本字符串
BLOB0-65 535 bytes二进制形式的长文本数据
TEXT0-65 535 bytes长文本数据
MEDIUMBLOB0-16 777 215 bytes二进制形式的中等长度文本数据
MEDIUMTEXT0-16 777 215 bytes中等长度文本数据
LONGBLOB0-4 294 967 295 bytes二进制形式的极大文本数据
LONGTEXT0-4 294 967 295 bytes极大文本数据

DDl操作数据库

  • DDL:数据定义语言,用来定义数据库对象:数据库,表,列等
属性说明
mysql -h 主机地址 -u 用户名 -p密码连接数据库
show databases;查看数据库列表
create database 库名;创建数据库
use 库名切换库
drop database 库名删除库
select database();查询所在的库
flush privileges刷新数据库
//查看数据库 show databases
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

//创建数据库blogdx create database blogdx
mysql> create database blogdx;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| blogdx             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

//切换到blogdx库里面 use
mysql> use blogdx;
Database changed

//查询当前所在的库 select database
mysql> select database();
+------------+
| database() |
+------------+
| blogdx     |
+------------+
1 row in set (0.00 sec)

//删除数据库blogdx drop database;
mysql> drop database blogdx;
Query OK, 0 rows affected (0.01 sec)

//查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

DML操作表

  • DML:数据操作语言,用来对数据库中表的数据进行增删改
属性说明
show tables;查看所有的表
create table 表名(字段1 数据类型,字段2,数据类型);创建表
desc 表名查看表结构
drop table 表名删除表
show create table 表名查询指定表名的创建语句
//创建表
mysql> create table student(
    -> id int,
    -> name varchar(20),
    -> gender varchar(20),
    -> birth date,
    -> address varchar(20),
    -> score double);


//查看表结构  
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

DDl修改表结构

属性说明
alter table 表名 add 字段名(列名)类型(长度);添加列(添加字段)
alter table 表名 change 旧列名 新列名 类型;修改列(修改字段)
alter table 表名 drop 列名;删除列(删除字段)
rename table 表名 to 新表名;修改表名
//查看表结构
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

//添加一个列名为age
mysql> alter table student add age int;


mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
| age     | int         | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

//将age字段修改为age1
mysql> alter table student change age age1 int;


mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
| age1    | int         | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

//删除列age1
mysql> alter table student drop age1;


mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

//将student重命名为person
mysql> rename table student to person;


mysql> desc person;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

DML操作数据

  • 对数据库进行读取、修改、插入和删除数据的操作。
  • 常见的DML操作包括select(查询数据)、insert(插入新数据)、update(更新已有数据)和delete(删除数据)

DML数据插入

属性说明
insert into 表名(列名1,列名2,3,4....)values(值1,2,3,4,5...)数据的插入
insert into 表名 values(值1,2,3,4,5..)向表插入所有的列
//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 上海    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 北京    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 河北    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 北京    |  87.6 |
+------+-----------+--------+------------+---------+-------+

//向id字段(列)对id和gender、score添加数据
mysql> insert into student(id,gender,score) values('1006','男','90');

//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 上海    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 北京    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 河北    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 北京    |  87.6 |
| 1006 | NULL      | 男     | NULL       | NULL    |    90 |
+------+-----------+--------+------------+---------+-------+

//对字段id添加1007
mysql> insert into student(id) values(1007);

//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 上海    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 北京    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 河北    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 北京    |  87.6 |
| 1006 | NULL      | 男     | NULL       | NULL    |    90 |
| 1007 | NULL      | NULL   | NULL       | NULL    |  NULL |
+------+-----------+--------+------------+---------+-------+

DML数据更新

属性说明
update 表名 set 字段=值,字段名=值...修改字段中的所有值
update 表名 set 字段=值,字段名=值... where 条件修改字段中的值并使用where条件来判断
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 中国    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 中国    |  87.6 |
| 1006 | NULL      | 男     | NULL       | 中国    |    90 |
| 1007 | NULL      | NULL   | NULL       | 中国    |  NULL |
+------+-----------+--------+------------+---------+-------+

//将id=1005的地址修改为上海
mysql> update student set address = '上海' where id = 1005;


//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 中国    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 上海    |  87.6 |
| 1006 | NULL      | 男     | NULL       | 中国    |    90 |
| 1007 | NULL      | NULL   | NULL       | 中国    |  NULL |
+------+-----------+--------+------------+---------+-------+

//将id=1007的地址修改为河北并且分数修改为100
mysql> update student set address = '河北',score = 100 where id = 1007;


mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |  87.3 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |  91.5 |
| 1003 | 小红      | 女     | 2000-02-14 | 中国    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    90 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 上海    |  87.6 |
| 1006 | NULL      | 男     | NULL       | 中国    |    90 |
| 1007 | NULL      | NULL   | NULL       | 河北    |   100 |
+------+-----------+--------+------------+---------+-------+

DML数据删除

属性说明
delete from 表名 where条件删除字段中的数据
delete from 表名清空表中的所有内容
truncate table 表名清空表中的所有内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |    85 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |    85 |
| 1003 | 小红      | 女     | 2000-02-14 | 中国    |    81 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    85 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 上海    |    85 |
| 1006 | NULL      | 男     | NULL       | 中国    |    85 |
| 1007 | NULL      | NULL   | NULL       | 河北    |    85 |
+------+-----------+--------+------------+---------+-------+

//删除student表中分数小于85的内容
mysql> delete from student where score<85;

//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |    85 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |    85 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    85 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 上海    |    85 |
| 1006 | NULL      | 男     | NULL       | 中国    |    85 |
| 1007 | NULL      | NULL   | NULL       | 河北    |    85 |
+------+-----------+--------+------------+---------+-------+


//删除student表中id是1007的内容  
mysql> delete from student where id = 1007;

//查看表内容
mysql> select * from student;
+------+-----------+--------+------------+---------+-------+
| id   | name      | gender | birth      | address | score |
+------+-----------+--------+------------+---------+-------+
| 1001 | 张三      | 男     | 2002-10-14 | 中国    |    85 |
| 1002 | 李四      | 男     | 2001-10-17 | 中国    |    85 |
| 1004 | 王五      | 男     | 2002-09-17 | 中国    |    85 |
| 1005 | 张三丰    | 男     | 2001-10-19 | 上海    |    85 |
| 1006 | NULL      | 男     | NULL       | 中国    |    85 |
+------+-----------+--------+------------+---------+-------+
5 rows in set (0.00 sec)

//清空表中所有的数据  
mysql> delete from student;


//查看表结构
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| gender  | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
| score   | double      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

DML总结

约束constraint

  • 在数据库中,约束是用于限制表中数据的规则或条件。这些约束可以应用于列、行或整个表,并确保数据的完整性、正确性和一致性。常见的约束类型包括主键、唯一约束、非空约束、默认值约束和外键约束等。

主键约束primary key

  • 唯一性:主键的值必须唯一,不允许重复。
  • 非空性:主键的值不能为NULL。
  • 稳定性:主键的值在记录被插入后不可更改。
  • 简洁性:主键应该使用简单的数据类型和尽可能少的列来定义
属性说明
create table 表(字段1 primary key,字段2...)添加单列主键
create table 表(字段1,字段2,字段3,primary key(字段1,字段2))添加联合主键
alter table 表 drop primary key删除主键
//对id字段添加主键
mysql>create table stu(id int primary key,name varchar(20));

//查看表结构
mysql> desc stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

//插入数据
mysql> insert into stu values(1,'张三');

//插入数据不可重复
mysql> insert into stu values(1,'李四');
ERROR 1062 (23000): Duplicate entry '1' for key 'stu.PRIMARY'
  
  
//联合主键  
mysql> create table student(
    -> id int,
    -> name varchar(20),
    -> gender varchar(20),
    -> primary key(id,name));


mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   | PRI | NULL    |       |
| name   | varchar(20) | NO   | PRI | NULL    |       |
| gender | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


mysql> insert into student values(1,'张三','男');
mysql> insert into student values(2,'张三','男');

mysql> insert into student values(2,'张三','女');
ERROR 1062 (23000): Duplicate entry '2-张三' for key 'student.PRIMARY'
  
mysql> create table student(id int,
    -> name varchar(20),
    -> gender varchar(20));


mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

//添加单列主键 多列主键就在后面写字段 primary key(id,name);
mysql> alter table student  add primary key(id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   | PRI | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

  
//删除主键                           alter drop table 表 drop primary key
  
mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   | PRI | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+


mysql> alter table student drop primary key;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | varchar(20) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

自增长约束auto_increment

  • 通常用于标识主键字段,以确保每个记录都有唯一的标识符。当插入新记录时,数据库会自动将该列的值设置为当前最大值加1,从而避免重复值的出现。
  • 使用delete清空数据在插入数据的时候自增长不会被清空
  • 使用truncate清空数据在插入数据的时候自增长会清空
属性说明
create table 表(字段 主键 自增长,字段2,字段3...);添加一个主键并设置为自增长约束
delete from 表;清空表中的所有内容
truncate 表;清空表并创建一个新的表
//创建一个学生表并添加主键和自增长约束
mysql> create table student(id int primary key auto_increment,name varchar(20));


//给指定的字段name添加数据李四
mysql> insert into student(name) values('李四');

//查询表内容
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 李四   |
+----+--------+

//给指定的字段name添加数据王五
mysql> insert into student(name) values('王五');


//查询表内容
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 李四   |
|  2 | 王五   |
+----+--------+

//清空所有的数据
mysql> delete from student;

//给指定的字段name添加数据王五
mysql> insert into student(name) values('王五');

//查询表内容
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  3 | 王五   |
+----+--------+

//清空表的内容并创建新的表
mysql> truncate student;

//给指定的字段添加内容王五
mysql> insert into student(name) values('王五');


//查询表内容
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 王五   |
+----+--------+

非空约束not null

  • 非空约束是指在数据库设计中对某些属性或字段设置不允许为空值的限制。也就是说,这些属性必须至少包含一个值,否则将无法在该字段上执行任何操作。
属性说明
create table 表(字段1,字段2 not null,字段3...);添加非空约束
alter table 表modify 字段 类型 not null;添加非空约束
alter table 表modify 字段 类型;删除非空约束
//创建一个学生表 给学生表name字段添加非空约束
mysql> create table student(
    -> id int,
    -> name varchar(20) not null,
    -> address varchar(20) not null
    -> );


//查看表结构 NULL字段为NO表示添加数据不可以为空
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int         | YES  |     | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| address | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

//添加一个数据
mysql> insert into student values(1,'张三','中国');

//查询表内容
mysql> select * from student;
+------+--------+---------+
| id   | name   | address |
+------+--------+---------+
|    1 | 张三   | 中国    |
+------+--------+---------+

  
//方式二
//创建一个user表
mysql> create table user(
    -> id int, 
    -> name varchar(20)
    -> );

//给指定的字段添加非空约束
mysql> alter table user modify name varchar(20) not null;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

//查看表结构  
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+


//给指定字段删除非空约束
mysql> alter table user modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

//查看表结构
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

唯一约束unique

  • 主键约束必须定义在每个表中,并且每个表只能有一个主键约束,而唯一约束可以定义在任何列上,每个表可以有多个唯一约束。
  • 确保表中的一个或多个列具有唯一值,不允许重复
属性说明
create table 表(字段1,字段2 unique,字段3...);添加唯一约束
alter table 表 drop index 唯一约束名(字段);删除约束名
//创建一个person表 对id设置为主键约束和自增加约束 name字段设置为非空约束和唯一约束
mysql> create table person(
    -> id int primary key auto_increment,
    -> name varchar(20) not null unique
    -> );


mysql> insert into person(name) values('李四1');


mysql> insert into person(name) values('李四2');


mysql> select * from person;
+----+---------+
| id | name    |
+----+---------+
|  1 | 李四1   |
|  2 | 李四2   |
+----+---------+


mysql> insert into person(name) values('李四3');

mysql> select * from person;
+----+---------+
| id | name    |
+----+---------+
|  1 | 李四1   |
|  2 | 李四2   |
|  3 | 李四3   |
+----+---------+
  
  
mysql> desc person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int         | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   | UNI | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

//删除唯一约束
mysql> alter table person drop index name;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

默认约束default

  • 用于在表的某个列中自动提供默认值。如果在插入新行时未为该列提供值,则会使用默认值。
属性说明
create table 表(字段1,default 默认值,字段2,字段3...);添加默认约束
alter table 表 modify 字段 类型 defult null;给指定字段添加默认约束
//创建一个学生表id为主键约束和自增长约束
//name字段为非空约束和 唯一约束
//address字段为默认约束 默认值是中国
mysql> create table student(
    -> id int primary key auto_increment,
    -> name varchar(20) not null unique,
    -> address varchar(20) default '中国'
    -> );

//查看学生表结构
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int         | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   | UNI | NULL    |                |
| address | varchar(20) | YES  |     | 中国    |                |
+---------+-------------+------+-----+---------+----------------+

//对name字段插入数据张三
mysql> insert into student(name) values('张三');

//查看表内容
mysql> select * from student;
+----+--------+---------+
| id | name   | address |
+----+--------+---------+
|  1 | 张三   | 中国    |
+----+--------+---------+
  
//给address字段手动添加一个默认约束 默认值为中国  
mysql> alter table student modify address varchar(20) default '中国';
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
  
//查看表结构
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int         | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   | UNI | NULL    |                |
| address | varchar(20) | YES  |     | 中国    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

//删除默认约束
mysql> alter table student modify address varchar(20) default null;


mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int         | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   | UNI | NULL    |                |
| address | varchar(20) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

Mysql约束总结

DQL数据查询

  • DQL:数据查询语言,用来查询数据库中表的记录(数据)

简单查询

属性说明
select * from 表;查询所有
select 字段1,字段2 from 表;查询指定字段
select * from 表 as 重命名;表别名
select 字段 as 重命名 from 表;字段别名
select distinct 字段 from 表;去掉重复值
//创建一个商品表product 对pid字段进行主键约束 自增长约束 pname进行非空约束
mysql> create table product(
    -> pid int primary key auto_increment,  //主键约束 自增长约束
    -> pname varchar(20) not null, --非空约束
    -> price double,
    -> category_id varchar(20)
    -> );

mysql> insert into product values(null,'海尔洗衣机',5000,'c001'),
    -> (null,'美的冰箱',3000,'c001'),
    -> (null,'格力空调',5000,'c001'),
    -> (null,'九阳电饭煲',5000,'c001');

mysql> insert into product values(null,'啄米鸟衬衣',300,'c002'),
    -> (null,'恒源祥西裤',800,'c002'),
    -> (null,'花花公子夹克',266,'c002'),
    -> (null,'劲霸休闲裤',266,'c002'),
    -> (null,'海澜之家卫衣',180,'c002'),
    -> (null,'杰克琼斯运动裤',430,'c002');

mysql> insert into product values(null,'兰蔻面霜',300,'c003'),
    -> (null,'雅诗兰黛精华水',200,'c003'),
    -> (null,'香奈儿香水',350,'c003'),
    -> (null,'SK-||神仙水',350,'c003'),
    -> (null,'资生堂粉底液',180,'c003');

mysql> insert into product values(null,'老北京方便面',56,'c004'),
    -> (null,'良品铺子海带丝',17,'c004'),
    -> (null,'三只松鼠坚果',88,null),;
//查询所有商品
mysql> select * from product;
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   1 | 海尔洗衣机            |  5000 | c001        |
|   2 | 美的冰箱              |  3000 | c001        |
|   3 | 格力空调              |  5000 | c001        |
|   4 | 九阳电饭煲            |  5000 | c001        |
|   5 | 啄米鸟衬衣            |   300 | c002        |
|   6 | 恒源祥西裤            |   800 | c002        |
|   7 | 花花公子夹克          |   266 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|   9 | 海澜之家卫衣          |   180 | c002        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
|  11 | 兰蔻面霜              |   300 | c003        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
|  13 | 香奈儿香水            |   350 | c003        |
|  14 | SK-||神仙水           |   350 | c003        |
|  15 | 资生堂粉底液          |   180 | c003        |
|  16 | 老北京方便面          |    56 | c004        |
|  17 | 良品铺子海带丝        |    17 | c004        |
|  18 | 三只松鼠坚果          |    88 | NULL        |
+-----+-----------------------+-------+-------------+


//查询商品名称和商品价格  
mysql> select pname,price from product;
+-----------------------+-------+
| pname                 | price |
+-----------------------+-------+
| 海尔洗衣机            |  5000 |
| 美的冰箱              |  3000 |
| 格力空调              |  5000 |
| 九阳电饭煲            |  5000 |
| 啄米鸟衬衣            |   300 |
| 恒源祥西裤            |   800 |
| 花花公子夹克          |   266 |
| 劲霸休闲裤            |   266 |
| 海澜之家卫衣          |   180 |
| 杰克琼斯运动裤        |   430 |
| 兰蔻面霜              |   300 |
| 雅诗兰黛精华水        |   200 |
| 香奈儿香水            |   350 |
| SK-||神仙水           |   350 |
| 资生堂粉底液          |   180 |
| 老北京方便面          |    56 |
| 良品铺子海带丝        |    17 |
| 三只松鼠坚果          |    88 |
+-----------------------+-------+

//查询商品名称和商品价格并进行重命名
mysql> select pname as name,price as new_price from product;
+-----------------------+-----------+
| name                  | new_price |
+-----------------------+-----------+
| 海尔洗衣机            |      5000 |
| 美的冰箱              |      3000 |
| 格力空调              |      5000 |
| 九阳电饭煲            |      5000 |
| 啄米鸟衬衣            |       300 |
| 恒源祥西裤            |       800 |
| 花花公子夹克          |       266 |
| 劲霸休闲裤            |       266 |
| 海澜之家卫衣          |       180 |
| 杰克琼斯运动裤        |       430 |
| 兰蔻面霜              |       300 |
| 雅诗兰黛精华水        |       200 |
| 香奈儿香水            |       350 |
| SK-||神仙水           |       350 |
| 资生堂粉底液          |       180 |
| 老北京方便面          |        56 |
| 良品铺子海带丝        |        17 |
| 三只松鼠坚果          |        88 |
+-----------------------+-----------+

//将所有的商品价格加价10元进行显示
mysql> select pname,price+10 as new_price from product;
+-----------------------+-----------+
| pname                 | new_price |
+-----------------------+-----------+
| 海尔洗衣机            |      5010 |
| 美的冰箱              |      3010 |
| 格力空调              |      5010 |
| 九阳电饭煲            |      5010 |
| 啄米鸟衬衣            |       310 |
| 恒源祥西裤            |       810 |
| 花花公子夹克          |       276 |
| 劲霸休闲裤            |       276 |
| 海澜之家卫衣          |       190 |
| 杰克琼斯运动裤        |       440 |
| 兰蔻面霜              |       310 |
| 雅诗兰黛精华水        |       210 |
| 香奈儿香水            |       360 |
| SK-||神仙水           |       360 |
| 资生堂粉底液          |       190 |
| 老北京方便面          |        66 |
| 良品铺子海带丝        |        27 |
| 三只松鼠坚果          |        98 |
+-----------------------+-----------+

算数运算

//将所有商品价格加10元
mysql> select price+10 as new_price from product;
+-----------+
| new_price |
+-----------+
|      5010 |
|      3010 |
|      5010 |
|      5010 |
|       310 |
|       810 |
|       276 |
|       276 |
|       190 |
|       440 |
|       310 |
|       210 |
|       360 |
|       360 |
|       190 |
|        66 |
|        27 |
|        98 |
+-----------+
  
mysql> select price-10 as new_price from product;
mysql> select price*10 as new_price from product;
mysql> select price/10 as new_price from product;

比较运算

属性说明
=等于
>和>=大于和大于等于
<和<=小于和小于等于
!=不等于
isnull 和is null判断一个值是否为null
is not null判断一个值是否不为null
between指定一个范围内的值(类似大于等于并且小于等于)
and和or并且和或者
in和not in判断一个值是in列表中的任意一个值和不是in列表中的任意一个
like和_模糊查询 (%匹配任意多个字符 _任意一个字符)
regexp正则表达式匹配
greatest当有两个或多个参数返回最大值
least当有两个或多个参数返回最小值
//查询商品名称为‘海尔洗衣机’的所有商品信息
mysql> select * from product where pname = '海尔洗衣机';
+-----+-----------------+-------+-------------+
| pid | pname           | price | category_id |
+-----+-----------------+-------+-------------+
|   1 | 海尔洗衣机      |  5000 | c001        |
+-----+-----------------+-------+-------------+

//查询价格为800的所有商品信息
mysql> select * from product  where price = 800;
+-----+-----------------+-------+-------------+
| pid | pname           | price | category_id |
+-----+-----------------+-------+-------------+
|   6 | 恒源祥西裤      |   800 | c002        |
+-----+-----------------+-------+-------------+

//查看商品价格不等于800的所有信息
mysql> select * from product  where price !=800;
mysql> select * from product  where price <>800;
mysql> select * from product  where not (price = 800);

//查看商品价格大于60的所有信息
mysql> select * from product  where price >60;

//查看商品价格在200到1000之间所有的商品
mysql> select * from product where price between 200 and 1000;
mysql> select * from product where  price >= 200 and price <= 1000;
mysql> select * from product where  price >= 200 && price <= 1000; //逻辑与

+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   5 | 啄米鸟衬衣            |   300 | c002        |
|   6 | 恒源祥西裤            |   800 | c002        |
|   7 | 花花公子夹克          |   266 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
|  11 | 兰蔻面霜              |   300 | c003        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
|  13 | 香奈儿香水            |   350 | c003        |
|  14 | SK-||神仙水           |   350 | c003        |
+-----+-----------------------+-------+-------------+
  
//查看商品价格是200或者800的所有商品
mysql> select * from product where price in(200,800);
mysql> select * from product where price=200 or price =800;
mysql> select * from product where price=200 || price =800;
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   6 | 恒源祥西裤            |   800 | c002        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
+-----+-----------------------+-------+-------------+
  
  
//查看商品包含子的所有
mysql> select * from product where pname like'%子%';   //匹配所有
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   7 | 花花公子夹克          |   266 | c002        |
|  17 | 良品铺子海带丝        |    17 | c004        |
+-----+-----------------------+-------+-------------+
  
//查看商品以裤结尾的所有
mysql> select * from product where pname like'%裤';   //匹配结尾
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   6 | 恒源祥西裤            |   800 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
+-----+-----------------------+-------+-------------+
  
//查看商品以S开头的所有
mysql> select * from product where pname like's%';   //匹配开头
+-----+----------------+-------+-------------+
| pid | pname          | price | category_id |
+-----+----------------+-------+-------------+
|  14 | SK-||神仙水    |   350 | c003        |
+-----+----------------+-------+-------------+

//查看商品第二个是花字的所有
mysql> select * from product where pname like'_花%';  //第二个字是花的
+-----+--------------------+-------+-------------+
| pid | pname              | price | category_id |
+-----+--------------------+-------+-------------+
|   7 | 花花公子夹克       |   266 | c002        |
+-----+--------------------+-------+-------------+
  
//查看编号为null的所有
mysql> select * from product where category_id is null;
+-----+--------------------+-------+-------------+
| pid | pname              | price | category_id |
+-----+--------------------+-------+-------------+
|  18 | 三只松鼠坚果       |    88 | NULL        |
+-----+--------------------+-------+-------------+
  
//查看编号不是null的所有
mysql> select * from product where category_id is not null;


//求最大值
mysql> select greatest(10,12,20) as big;    //这个求大小值 如果有null不会进行比较 直接返回null
+-----+
| big |
+-----+
|  20 |
+-----+

//求最小值
mysql> select least(10,12,20) as small;
+-------+
| small |
+-------+
|    10 |
+-------+

排序查询

属性说明
select 字段 from 表 order by 字段名 asc升序
select 字段 from 表 order by 字段名 desc降序
//对价格进行降序查看所有
mysql> select * from product order by price desc;
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   1 | 海尔洗衣机            |  5000 | c001        |
|   3 | 格力空调              |  5000 | c001        |
|   4 | 九阳电饭煲            |  5000 | c001        |
|   2 | 美的冰箱              |  3000 | c001        |
|   6 | 恒源祥西裤            |   800 | c002        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
|  13 | 香奈儿香水            |   350 | c003        |
|  14 | SK-||神仙水           |   350 | c003        |
|   5 | 啄米鸟衬衣            |   300 | c002        |
|  11 | 兰蔻面霜              |   300 | c003        |
|   7 | 花花公子夹克          |   266 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
|   9 | 海澜之家卫衣          |   180 | c002        |
|  15 | 资生堂粉底液          |   180 | c003        |
|  18 | 三只松鼠坚果          |    88 | NULL        |
|  16 | 老北京方便面          |    56 | c004        |
|  17 | 良品铺子海带丝        |    17 | c004        |
+-----+-----------------------+-------+-------------+

//对价格进行升序查看所有 不写就是默认升序
mysql> select * from product order by price asc;
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|  17 | 良品铺子海带丝        |    17 | c004        |
|  16 | 老北京方便面          |    56 | c004        |
|  18 | 三只松鼠坚果          |    88 | NULL        |
|   9 | 海澜之家卫衣          |   180 | c002        |
|  15 | 资生堂粉底液          |   180 | c003        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
|   7 | 花花公子夹克          |   266 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|   5 | 啄米鸟衬衣            |   300 | c002        |
|  11 | 兰蔻面霜              |   300 | c003        |
|  13 | 香奈儿香水            |   350 | c003        |
|  14 | SK-||神仙水           |   350 | c003        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
|   6 | 恒源祥西裤            |   800 | c002        |
|   2 | 美的冰箱              |  3000 | c001        |
|   1 | 海尔洗衣机            |  5000 | c001        |
|   3 | 格力空调              |  5000 | c001        |
|   4 | 九阳电饭煲            |  5000 | c001        |
+-----+-----------------------+-------+-------------+

聚合查询

  • 对表中的数据进行统计、分组和计算等聚合操作,以便获取有用的信息摘要
  • 可以使用聚合查询来计算最大值、最小值、平均值、总和或计数等汇总数据,并且可以按照一个或多个列进行分组,以便更详细地了解数据。
  • null不参与聚合函数的运算
属性说明
count记录行数
sum求和
max最大
min最小
avg平均值
//查看商品的总条数
mysql> select count(pid) from product;
+------------+
| count(pid) |
+------------+
|         18 |
+------------+
  
//查看价格大于200商品的总条数
mysql> select count(pid) from product where price>200;
+------------+
| count(pid) |
+------------+
|         12 |
+------------+
  
//查询分类为c002所有商品的总和
mysql> select sum(price) from product where category_id='c002';
+------------+
| sum(price) |
+------------+
|       2242 |
+------------+
  
//查询商品的最小价格和最大价格
mysql> select min(price) as min, max(price) as max from product;
+------+------+
| min  | max  |
+------+------+
|   17 | 5000 |
+------+------+
  
//查询分类c002商品的平均价格
mysql> select avg(price) from product where category_id='c002';
+-------------------+
| avg(price)        |
+-------------------+
| 373.6666666666667 |
+-------------------+

分组查询

属性说明
select 字段列表 from 表 where 表达式 group by 字段 having 条件表达式(指定分组的条件);固定格式
select 字段列表,聚合函数 from 表 group by 分组字段;固定格式
//查看表内容
mysql> select * from product;
+-----+-----------------------+-------+-------------+
| pid | pname                 | price | category_id |
+-----+-----------------------+-------+-------------+
|   1 | 海尔洗衣机            |  5000 | c001        |
|   2 | 美的冰箱              |  3000 | c001        |
|   3 | 格力空调              |  5000 | c001        |
|   4 | 九阳电饭煲            |  5000 | c001        |
|   5 | 啄米鸟衬衣            |   300 | c002        |
|   6 | 恒源祥西裤            |   800 | c002        |
|   7 | 花花公子夹克          |   266 | c002        |
|   8 | 劲霸休闲裤            |   266 | c002        |
|   9 | 海澜之家卫衣          |   180 | c002        |
|  10 | 杰克琼斯运动裤        |   430 | c002        |
|  11 | 兰蔻面霜              |   300 | c003        |
|  12 | 雅诗兰黛精华水        |   200 | c003        |
|  13 | 香奈儿香水            |   350 | c003        |
|  14 | SK-||神仙水           |   350 | c003        |
|  15 | 资生堂粉底液          |   180 | c003        |
|  16 | 老北京方便面          |    56 | c004        |
|  17 | 良品铺子海带丝        |    17 | c004        |
|  18 | 三只松鼠坚果          |    88 | c004        |
+-----+-----------------------+-------+-------------+

//对category_id字段进行分组
mysql> select category_id from product group by category_id;
+-------------+
| category_id |
+-------------+
| c001        |
| c002        |
| c003        |
| c004        |
+-------------+

//对category_id字段进行分组 count(*):聚合函数用来显示分组后的个数
mysql> select category_id, count(*) from product group by category_id;
+-------------+----------+
| category_id | count(*) |
+-------------+----------+
| c001        |        4 |
| c002        |        6 |
| c003        |        5 |
| c004        |        3 |
+-------------+----------+
  
//统计各个分类商品的个数并且只显示大于4的信息
mysql> select category_id,count(*) 
    -> from product 
    -> group by category_id
    -> having count(*) > 4;

//方式二
mysql> select category_id,count(*) as count
    -> from product 
    -> group by category_id
    -> having count>4;
+-------------+----------+
| category_id | count(*) |
+-------------+----------+
| c002        |        6 |
| c003        |        5 |
+-------------+----------+
  
//统计各个分类商品的个数并且只显示大于4的信息并且升序拍序
//SQL执行顺序:from -> group by -> count(*) -> select -> having ->order by;
mysql> select category_id,count(*) as count from product  group by category_id having count > 4 order by count asc;
+-------------+----------+
| category_id | count(*) |
+-------------+----------+
| c003        |        5 |
| c002        |        6 |
+-------------+----------+

分页查询

属性说明
select 字段 from 表名 limit 起始索引 查询条目数;起始索引为0
//查询前5条数据
mysql> select * from product limit 5;
+-----+-----------------+-------+-------------+
| pid | pname           | price | category_id |
+-----+-----------------+-------+-------------+
|   1 | 海尔洗衣机      |  5000 | c001        |
|   2 | 美的冰箱        |  3000 | c001        |
|   3 | 格力空调        |  5000 | c001        |
|   4 | 九阳电饭煲      |  5000 | c001        |
|   5 | 啄米鸟衬衣      |   300 | c002        |
+-----+-----------------+-------+-------------+

//从第4条开始 显示5条数据
mysql> select * from product limit 3,5;
+-----+--------------------+-------+-------------+
| pid | pname              | price | category_id |
+-----+--------------------+-------+-------------+
|   4 | 九阳电饭煲         |  5000 | c001        |
|   5 | 啄米鸟衬衣         |   300 | c002        |
|   6 | 恒源祥西裤         |   800 | c002        |
|   7 | 花花公子夹克       |   266 | c002        |
|   8 | 劲霸休闲裤         |   266 | c002        |
+-----+--------------------+-------+-------------+

Mysql基本查询总结

最后修改:2023 年 04 月 13 日
如果觉得我的文章对你有用,请随意赞赏