MySQL基础

博客聚焦于MySQL关系型数据库,介绍了其DDL、DML、DQL操作,其中DML和DQL为重点内容,还提及了事务和索引相关知识,这些都是信息技术领域数据库操作的关键部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql(关系型数据库)

DDL

	作用:创建数据库、创建数据库表和字段
	数据库的相关操作:
--新建数据库:-----create database 【schema】 数据库名
create database db1;

--删除数据库:-----drop database 数据库名
drop database db1;

--使用数据库:----use 数据库名
use  db1;

--查看所有的数据库:-----show databases
show databases ;
	创建数据库表:
		创建的语法:---create table  表名( 字段名 类型【约束】【注释】)
		数据类型:数值型、字符串、时间
		约束:非空约束【not null】、唯一约束【unique】、主键约束【primary key,auto_increment自动增长】、默认约束【defaule ‘默认值’】、外键约束【foreign key】
#emp为数据库表名
#comment属于描述信息
create table emp
(
    id          int unsigned primary key auto_increment comment 'ID',
    username    varchar(20)      not null unique comment '用户名',
    password    varchar(32) default '123456' comment '密码',
    name        varchar(10)      not null comment '姓名',
    gender      tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
    image       varchar(300) comment '图像',
    job         tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',
    entrydate   date comment '入职时间',
    create_time datetime         not null comment '创建时间',
    update_time datetime         not null comment '修改时间'
) comment '员工表';

DML(重点)

	概念:对数据表进行增删改
	分类:
--增加:insert into 表名(字段1,字段2....)values(xxx,xxx..)
#此为添加一条数据库信息,添加多条可重复复制修改即可
INSERT INTO emp
(id, username, password, name, gender, image, job, entrydate, create_time, update_time)
VALUES (1, 'jinyong', '123456', '金庸', 1, '1.jpg', 4, '2000-01-01', now(), now())

--删除:delete form 表名 where 条件
#根据id删除员工
delete from emp where id=1;

--修改:update 表名 set 字段=值,字段=值.. where 条件
#将id为1的名字修改为张三,可根据条件修改多个字段
update emp set name='张三' where id=1;

DQL(重点)

	概念:用于查询表数据
	分类:
		基本查询:select 字段.. forn 表名
--  =================== 基本查询 ======================
-- 1.查询指定字段 name并返回
select name from  emp;
-- 2.查询返回所有字段方式一:  写出所有字段     推荐, 效率高, 更直观
select id, username, password, name, image from emp;
-- 2.查询返回所有字段方式二: * 表示所有字段
select * from emp;
-- 3.查询所有员工的 name, 并起别名(姓名)  --- as 关键字可以省略
select name 姓名 from emp;
-- 4.查询员工姓名(不要重复) -- distinct
select distinct name from emp;
	--条件查询:select 字段.. from 表名 where 条件....
--  =================== 条件查询 ======================
-- 1.查询 姓名 为 杨逍 的员工
select * from emp where name='杨逍';

-- 2.查询 id小于等于5 的员工信息
select * from emp where id<=5;

-- 3.查询 没有分配职位 的员工信息  -- 判断 null , 用 is null
select * from emp where job is null ;

-- 4.查询 有职位 的员工信息  -- 判断 不是null , 用 is not null
select * from emp where job is not null ;

-- 5.查询 密码不等于 '123456' 的员工信息
select * from  emp where password !='123456';

-- 6.查询入职日期 在 '2005-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息

select * from emp where entrydate between '2005-01-01' and '2010-01-01';
-- 7.查询 入职时间 在 '2005-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from emp where entrydate between '2005-01-01' and '2010-01-01' and gender=2;

-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息
select * from emp where  job=2 or job=3 or job=4;
select * from emp where job in (2,3,4);

-- 9.查询姓名为两个字的员工信息
select * from emp where name like '__';
-- 10.查询姓 '张' 的员工信息
select * from emp where name like '张%';

-- 11.查询姓名中包含 '三' 的员工信息
select * from emp where name like '%三%';

-- 查询名字包含张, 性别为1, 入职日期在 '2000-01-01' 到 '2010-01-01'
select * from emp where name like '%张%' and gender='1' and entrydate between '2000-01-01' and '2010-01-01';
		聚合函数:select 聚合函数(字段/*)from 表名
			概念:把一列的数据做纵向计算,得到是一个值
			分类:总数:【count(字段/*)】、最大值:max(字段)、最小值:min(字段)、平均值:avg(字段)、求和:sum(字段)
--  =================== 聚合函数 ======================
-- 1.统计该企业员工数量, count(字段)
select count(username) from emp;

-- null值不参与聚合函数运算
select count(job) from emp;

-- B.count(*)
select count(*) from emp;

-- C.count(值)  1无意义
select count(1) from emp;

-- 2.统计该企业员工 ID 的平均值
select avg(id)  from emp;

-- 3.统计该企业最早入职的员工的入职日期
select min(entrydate) from emp;

-- 4.统计该企业最近入职的员工的入职日期
select max(entrydate) from emp;

-- 5.统计该企业员工的 ID 之和
select sum(id) from emp;
	--分组查询:select 分组字段、聚合函数 from 表名  where 条件  group by 分组字段  having 分组之后的条件
--  =================== 分组查询 ======================
-- 1.根据性别分组, 统计男性和女性员工的数量  -- count
select count(gender),gender from emp group by gender;

-- 2.查询入职时间在 '2015-01-01' (包含) 以前的员工, 并对结果根据职位分组, 获取员工数量大于等于2的职位
select job, count(job) from emp where entrydate<='2015-01-01' group by job having count(job)>='2';
--  =================== 排序查询 ======================
-- order by 字段 asc | desc
-- asc升序
-- desc降序
-- 1.根据入职时间, 对员工进行升序排序
select * from emp order by entrydate asc ;

-- 2.根据入职时间, 对员工进行降序排序

select * from emp order by entrydate desc ;
--  =================== 分页查询 ======================
#公式:查询页数=(当前页数-1)*条数,条数(显示多少条)
-- 1.每页展示10条记录, 查询第1页, 跳过0条, 获取10条
select * from emp limit 0,10;

-- 2.每页展示10条记录, 查询第2页, 跳过10条, 获取10条
select * from emp limit 10,10;

-- 3.每页展示10条记录, 查询第3页, 跳过20条, 获取10条
select * from emp limit 20,10;

事务

	作用:保证我们进行多个sql操作的一致性
	特点:四大特性(ACID)
		原子性:要么都成功,要么都失败
		一致性:要么数据都被修改,要么都不被修改
		隔离性:不同的窗口会有不同的事物,相互不影响
		持久性:对数据库的操作是永久的
	开启事务:start transaction;begin
	提交事务:commit;---都成功的时候才提交
	回滚:rollback---出现失败的操作

索引

	作用:高效查询数据的数据结构
	数据接口:b+树
	索引的操作:
		创建:create index 索引名 on 表(字段);
		查看所有的索引:show index from table;
		删除索引:drop index 索引名 on table
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值