MYSQL

32 阅读5分钟

本地安装数据库

  • 1、下载mysql压缩包,解压到相应的目录
  • 2、配置环境变量
  • 3、初始化:以管理员身份运行命令窗口 mysqld --initialize-insecure --console
  • 4、注册mysql服务 mysqld -install
  • 5、启动 net start mysql 停止net stop mysql
  • 6、以无密码的方式登录mysql -u root -p回车
  • 7、执行sql语句修改密码ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';

企业开发中使用方式

  • 1、连接远程的数据库
  • mysql -h地址 -P端口号 -u用户名 -p

sql语句

DDL数据库

  • 查询所有数据库 show databases
  • 查询当前数据库 select database();
  • 使用/切换数据库 use 数据库名;
  • 创建数据库 create database [if not exists] 数据库名 [default charset utf8mb4]
  • 删除数据库 drop database [if exists] 数据库名

创建表

CREATE TABLE 表名 (
    字段名 数据类型 [约束条件] [comment '备注信息']
) [COMMENT '表备注'];
CREATE TABLE user (
    id int auto_increment primary key comment '主键',
    name varchar(10) not null comment '姓名',
    username varchar(20) not null comment '用户名',
    age int comment '年龄',
    createtime datetime comment '创建时间'
) COMMENT '用户表';

约束条件

微信图片_20260304112931_18246_32.jpg

数据类型

微信图片_20260304112928_18243_32.jpg

微信图片_20260304112929_18244_32.jpg

微信图片_20260304112930_18245_32.jpg

微信图片_20260304112927_18242_32.jpg

-- 查询当前数据库的所有表
show tables ;
-- 查询表结构
desc user;
-- 查询建表语句
show create table user;
-- 创建员工01表
create table user01(
    id int auto_increment primary key comment '主键'
)comment '员工01表';

-- 给user1表添加字段username
alter table user01 add username varchar(10) comment '用户名';
alter table 表名 add 字段名 类型(长度) [comment '注释'] [约束];
-- 修改username字段类型
alter table user01 modify username varchar(20);
alter table 表名 modify 字段名 新数据类型(长度)
-- 修改username名称以及类型
alter table user01 change username username01 varchar(30);
alter table 表名 change 旧字段名 新字段名 类型(长度) [comment '注释'] [约束]
-- 删除字段id
alter table user01 drop column id;
alter table 表名 drop colum 字段名
-- 修改表名
alter table user01 rename to user02;
alter table 旧表名 rename to 新表名
-- 删除表
drop table user02;
drop table 表名

向表中添加数据

-- 向表中添加数据
insert into user(name,username) values ('张三','zhangsan');
insert into 表名(字段1,字段2......) values (值1,值2......)
-- 全部字段添加数据
insert into user values (10,'李四','lisi',18,20260201,'备注');
insert into 表名 values (值1,值2..............)
-- 批量添加数据(指定字段)
insert into user(name,username) values ('王五','wangwu'),('赵六','zhaoliu');
insert into 表名(字段1,字段2) values (值1,值2),(值1,值2);
-- 批量添加数据(全部字段)
insert into user values (20,'姓名1','xingming1',19,20260201,'备注1'),(21,'姓名2','xingming2',20,20200101,'备注2');
insert into 表名 values (值1,值2,......),(值1,值2,........);

修改数据

-- 修改数据
update user set name='张三修改1',age=100 where id=1;
update 表名 set 字段1=值,字段2=值,.... [where 条件]

删除数据

-- 删除数据
delete from user where name='姓名1';
delete form 表名 [where 条件]

基本查询

-- 查找表中所有数据
select * from user;
select * from 表名;
-- 查找表中多个字段的数据
select name,username from user;
select 字段名1,字段名2,.... from 表名;
-- 查询表中多个字段并设置别名
select name 姓名,username 用户名 from user;
select 字段名1 [别名],字段名2 [别名] from 表名;
-- 去除重复记录
select distinct name from user;
select distinct 字段名 from 表名;

条件查询

比较运算符功能
'>'大于
'>='大于等于
'<'小于
'<='小于等于
'='等于
'<>或者!='不等于
'between ... and ...'在某个范围之内(含最小、最大值)
'in(...)'在in之后的列表中的值,多选一
'like 占位符'模糊匹配(_匹配单个字符,%匹配任意个字符)
'is null'是null
'and或&&'并且(多个条件同时成立)
'or或||'或者(多个条件任意一个成立)
'not或!'非,不是
select * from user where name='王五'
select 字段名 from 表名 where 条件列表

分组查询

  • 聚合函数:将一列数据作为一个整体,进行纵向计算
函数功能
count统计数量
max最大值
min最小值
avg平均值
sum求和
-- 查找一共有多少条数据
select count(*) from user;
select count(1) from user;
select count(id) from user;
-- 计算最大值
select max(age) from user;
-- 计算最小值
select min(age) from user;
-- 计算平均值
select avg(age) from user;
-- 求和
select sum(age) from user;
  • select 字段列表 from 表名 [where 条件列表] group by 分组字段名 [having 分组后过滤条件]
  • where和having的区别
  • 执行的时机不同:where是分组之前进行过滤,不满足where不参与分组;而having是分组之后对结果进行过滤
  • 判断条件不同:where不能对聚合函数进行判断,而having可以
  • 注意:分组之后,select后的字段列表不能随意书写,能写的一般是分组字段+聚合函数
-- 根据name分组并统计每个名字的数量
select name,count(*) from user group by name;
-- 查找年龄小于90根据姓名分组过滤名字数量大于1个的数量
select name,count(*) from user where age<90 group by name having count(*)>1

排序查询

  • select 字段列表 form 表名 [where 条件列表] [group by 分组字段名 having 分组后的过滤条件] order by 排序字段 排序方式
  • 排序方式:升序acr 降序desc 默认升序asc,可以不写
  • 可以有多个排序字段用逗号隔开,当第一个字段相同时,才会根据第二个字段进行排序
-- 按照年龄升序排序
select * from user order by age;
-- 按照年龄降序排序
select * from user order by age desc;

分页查询

  • select 字段 from 表名 [where 条件] [group by 分组字段 having 过滤条件] [order by 排序字段 排序条件] limit 起始索引,查询记录数
  • 起始索引=(页面-1)*查询记录数
-- 查询索引为0,每页展示5条
select * from user limit 0,5;