登录
首页 >  数据库 >  MySQL

MySQL-浅谈存储引擎

来源:SegmentFault

时间:2023-02-16 15:15:47 344浏览 收藏

哈喽!今天心血来潮给大家带来了《MySQL-浅谈存储引擎》,想必大家应该对数据库都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到MySQL,若是你正在学习数据库,千万别错过这篇文章~希望能帮助到你!

MySQL中的存储引擎

一、前言

数据库作为存储数据的仓库,可能大家会想,关系型数据库把我们的数据一行一行地存储不就完事了吗?哪里又冒出个存储引擎呢?

之所以有多个存储引擎,是因为我们对表的使用场景并不总是相同的,有时候可能我们需要频繁的

show create table userinfo \G;  # \G格式化输出

上面的命令,使用查看建表语句,可以查看数据表使用的引擎,当然我们默认的都是

drop table if exists bookmarket;
    create table bookmarket(
    bookname varchar(50),
    price double,
    primary key(bookname)
);

drop table if exists bookstore;
    create table bookstore(
    id int primary key auto_increment,
    bookname varchar(50),
    price double,
    constraint `fk_bookname` foreign key(bookname) references bookmarket(bookname)
);

下面我们来复习以下外键相关的知识:

  1. 如果我们想在从表中插入数据,那么
    insert bookmarket values('book_1',23.42),('book_2',22.2),('book_3',44.1);
    
    insert bookstore values(null,'book_1',30),(null,'book_2',32.2);

    然后,我们以

    alter table bookstore drop foreign key `fk_bookname`;
    
    alter table bookstore add constraint `bookstore_fk_bookname` foreign key(bookname) references 
    bookmarket(bookname) on delete restrict on update restrict;

    然后,我们试图删除主表的一行数据:

    这里提示,不能删除数据,因为数据被外键所引用。顺便一提,默认情况下,外键会采用

    set forign_key_checks=0;
    ...
    ...
    set forign_key_checks=1;
    1.3 存储方式

    该点水平不够。

    2、
    drop table if exists isam_t;
        create table isam_t(
        id int,
        name char(12)
    )engine=myisam charset=utf8;

    接下来,我们插入数据并验证。

    insert isam_t values(2,' tom '),(3,'  kim'),(4,' jol   ');
    select id,name,length(name) from isam_t;

    我们可以看出,

    drop table if exists bill_2016;
        create table bill_2016(
        id int primary key,
        time date,
        cost double
    )engine=myisam;
    
    drop table if exists bill_2017;
        create table bill_2017(
        id int primary key,
        time date,
        cost double
    )engine=myisam;
    
    drop table if exists bill_merge;
        create table bill_merge(
        id int primary key,
        time date,
        cost double
    )engine=merge union=(bill_2016,bill_2017) insert_method=last;

    然后我们往2张分表中插入数据。

    insert bill_2016 values(1,'2016-1-2',22.4),(2,'2016-2-3',44.2);
    insert bill_2017 values(100,'2017-4-2',55.2),(102,'2017-6-3',88);
    
    select * from bill_2016;
    select * from bill_2017;

    我们查看

    create table emp_memory engine=memory
    select * from bookmarket;

    我们在生成索引的时候,可以选择

    create index idx_bookname using hash on emp_memory(bookname);
    show index from emp_memory;

    当我们不需要

    MEMORY
    表的时候,我们应该删除从而释放内存。

    可以是

    delete from 
    truncate table
    drop table
    等。

    今天带大家了解了MySQL的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>