阿里十年工作经验总结MySQL笔记
发布日期:2021-04-30 21:10:20 浏览次数:72 分类:精选文章

本文共 3072 字,大约阅读时间需要 10 分钟。

数据库操作语言分类与常用语法

DDL(数据定义语言)

DDL主要用于定义数据库结构,包括表、字段、外键等。常用命令包括:

  • 创建数据库create database if not exists db_name;

  • 创建用户create user admin identified by '123456';

  • 创建表

    create table grade (
    id int(8) not null primary key auto_increment comment '年级ID'
    ) engine=innodb default charset=utf8;
  • 添加外键

    create table student (
    student_id int(8) not null comment '学号',
    grade_id int(8) default null comment '年级',
    primary key (student_id),
    constraint fk_grade_id foreign key (grade_id) references grade (grade_id)
    ) engine=innodb default charset=utf8;
  • 复制表

    create table new_table (select * from student);
  • 添加索引

    create index index_newclass_id on newclass(id);
  • 删除数据库drop database if exists db_name;

  • 删除用户权限revoke delete on panda_auto.* from 'admin'@'%';

  • 删除表drop table if exists table_name;

  • 修改表结构

    alter table student add constraint fk_gradeid foreign key (gradeid) references grade (gradeid);
    alter table a auto_increment=10000;
    alter table student drop foreign key fk_grade_id;
    alter table student drop index fk_grade_id;
    alter table student modify grade_id int(8) default 0 after student_id;
  • 字段修改

    alter table newclass change sex age int(10);
    alter table newclass rename to newclass;

DML(数据操作语言)

DML用于对数据库数据进行操作,常见命令包括:

  • 插入数据
    insert into newclass (grade_id) values(1), (2), (3);
  • 更新数据
    update user set authentication_string=password('123') where user='root';
    update a set b=replace(b,'c','d');
  • 删除数据
    delete from table_name where student_id=1;
  • 批量删除
    truncate grade;

DQL(数据查询语言)

DQL用于查询数据库中的数据,常见查询包括:

  • 查询空值where 字段名 is null
  • 查询非空值where 字段名 is not null
  • 去重查询select distinct 字段名 from 表名
  • 模糊查询
    select student_name from student where address like '' or address is null;
  • 内外连接
    select teacher.teacher_id, teacher.name as teacher_name, timetable.name 
    from timetable left join teacher on teacher.teacher_id = timetable.teacher_id
    where timetable.grade_id = 20187 and section = '第一节';
  • 排序order by 字段名 asc/desc
  • 分页limit 10 5

DCL(数据库控制语言)

DCL用于控制数据库权限和事务管理,常用命令包括:

  • 刷新权限flush privileges;

  • 赋予权限grant all on *.* to 'root'@'%';

  • 创建用户及权限grant select, insert, update, delete, create, drop on panda_auto.* to admin@'%' identified by '123456';

  • 事务管理

    set autocommit = 0; -- 关闭自动提交
    start transaction; -- 开始事务
    commit; -- 提交事务
    rollback; -- 回滚事务
  • 保存点

    savepoint savepoint_name;
    rollback to savepoint savepoint_name;
    release savepoint savepoint_name;

数据表类型

数据库提供多种存储引擎,常见类型包括:

  • MyISAM:节省空间,支持全文索引
  • InnoDB:支持事务,外键约束
  • MEMORY:内存存储,读写速度快
  • CSV:将数据存储为文本文件

数据类型

数据库提供丰富的数据类型,主要包括:

  • 整型数据int, mediumint, longint
  • 浮点型数据float, double, decimal
  • 字符串数据char, varchar, text
  • 日期时间数据date, datetime, time, year

常用函数

数据库函数分为多个类别,常见函数包括:

  • 数据函数abs(x), format(x, d), ceil(x), floor(x)
  • 字符串函数char_length, insert, lower(), left(), replace()
  • 日期时间函数current_date(), now(), date_format()
  • 聚合函数count(), sum(), avg(), max(), min()
  • 加密算法md5()

事务与ACID原则

事务是数据库操作的基本单元,遵循ACID原则,确保数据的原子性、一致性、隔离性和持久性。

  • 原子性:所有操作要么全部完成,要么全部回滚。
  • 一致性:事务后系统状态保持一致。
  • 隔离性:多个事务看似串行执行。
  • 持久性:事务提交后数据持久保存。

索引

索引是优化查询性能的重要手段,分类包括:

  • 主键索引:确保数据唯一性。
  • 唯一索引:避免字段重复。
  • 常规索引:加在常用查询字段上。
  • 全文索引:用于全文检索。

索引的数据结构包括:

  • B-tree索引:适用于InnoDB和MyISAM
  • 哈希索引:适用于Memory存储引擎

数据库管理员应根据查询需求合理设计索引,避免过多或过少。

上一篇:ART视角 | 如何在native内存增长过多时自动触发GC?如何在Java对象回收时触发native内存回收?
下一篇:测试代码难免不会犯错!盘点你会犯的11个错误

发表评论

最新留言

表示我来过!
[***.240.166.169]2026年06月14日 01时39分30秒