MySQL backup, views, triggers,

MySQL Backup

  • The need for database backup
    • Ensure that important data is not lost
    • Data transfer
  • MySQL database backup method
    • mysqldump backup tool
    • Database management tools, such as SQLyog
    • Direct copy of the database files and related configuration files

mysqldump client

Role:

  • Dump database
  • Collection database backup
  • Transferring the data to another SQL server, not necessarily a MySQL server

Syntax:

img

-- 导出
1. 导出一张表
  mysqldump -u用户名 -p密码 库名 表名 > 文件名(D:/a.sql)
2. 导出多张表
  mysqldump -u用户名 -p密码 库名 表123 > 文件名(D:/a.sql)
3. 导出所有表
  mysqldump -u用户名 -p密码 库名 > 文件名(D:/a.sql)
4. 导出一个库 
  mysqldump -u用户名 -p密码 -B 库名 > 文件名(D:/a.sql)

可以-w携带备份条件

-- 导入
1. 在登录mysql的情况下:
  source  备份文件
2. 在不登录的情况下
  mysql -u用户名 -p密码 库名 < 备份文件

view

What is the view:
A view is a virtual table, the contents of which is defined by the query. With real tables, view contains a series of columns and rows of data with a name. However, in view there is no set of data values stored in the database. Data consisting of rows and columns to define the table referenced by the query view, and dynamically generated when the reference view.
View file having a table structure, but the data file does not exist.
The underlying tables cited therein, the role of view similar to screening. Screening may be defined from the current view or a plurality of other databases or tables, or other views. Query through a view without any restrictions, and few restrictions when data is modified by them.
View sql statement query is stored in the database, it is mainly for two reasons: For security reasons, the view can hide some data, such as: social insurance fund tables, can display only the name, address with a view, without displaying Social Security number and the number of wages, etc. another reason is that make complex queries easy to understand and use.

-- 创建视图
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement
    - 视图名必须唯一,同时不能与表重名。
    - 视图可以使用select语句查询到的列名,也可以自己指定相应的列名。
    - 可以指定视图执行的算法,通过ALGORITHM指定。
    - column_list如果存在,则数目必须等于SELECT语句检索的列数

-- 查看结构
    SHOW CREATE VIEW view_name 

-- 删除视图
    - 删除视图后,数据依然存在。
    - 可同时删除多个视图。
    DROP VIEW [IF EXISTS] view_name ...

-- 修改视图结构
    - 一般不修改视图,因为不是所有的更新视图都会映射到表上。
    ALTER VIEW view_name [(column_list)] AS select_statement

-- 视图作用
    1. 简化业务逻辑
    2. 对客户端隐藏真实的表结构

-- 视图算法(ALGORITHM)
    MERGE        合并
        将视图的查询语句,与外部查询需要先合并再执行!
    TEMPTABLE    临时表
        将视图执行完毕后,形成临时表,再做外层查询!
    UNDEFINED    未定义(默认),指的是MySQL自主去选择相应的算法。

trigger

/* 锁表 */
表锁定只用于防止其它客户端进行不正当地读取和写入
MyISAM 支持表锁,InnoDB 支持行锁
-- 锁定
    LOCK TABLES tbl_name [AS alias]
-- 解锁
    UNLOCK TABLES


/* 触发器 */ ------------------
    触发程序是与表有关的命名数据库对象,当该表出现特定事件时,将激活该对象
    监听:记录的增加、修改、删除。

-- 创建触发器
CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt
    参数:
    trigger_time是触发程序的动作时间。它可以是 before 或 after,以指明触发程序是在激活它的语句之前或之后触发。
    trigger_event指明了激活触发程序的语句的类型
        INSERT:将新行插入表时激活触发程序
        UPDATE:更改某一行时激活触发程序
        DELETE:从表中删除某一行时激活触发程序
    tbl_name:监听的表,必须是永久性的表,不能将触发程序与TEMPORARY表或视图关联起来。
    trigger_stmt:当触发程序激活时执行的语句。执行多个语句,可使用BEGIN...END复合语句结构

-- 删除
DROP TRIGGER [schema_name.]trigger_name

可以使用old和new代替旧的和新的数据
    更新操作,更新前是old,更新后是new.
    删除操作,只有old.
    增加操作,只有new.

-- 注意
    1. 对于具有相同触发程序动作时间和事件的给定表,不能有两个触发程序。


-- 字符连接函数
concat(str1[, str2,...])

-- 分支语句
if 条件 then
    执行语句
elseif 条件 then
    执行语句
else
    执行语句
end if;

-- 修改最外层语句结束符
delimiter 自定义结束符号
    SQL语句
自定义结束符号

delimiter ;        -- 修改回原来的分号

-- 语句块包裹
begin
    语句块
end

-- 特殊的执行
1. 只要添加记录,就会触发程序。
2. Insert into on duplicate key update 语法会触发:
    如果没有重复记录,会触发 before insert, after insert;
    如果有重复记录并更新,会触发 before insert, before update, after update;
    如果有重复记录但是没有发生更新,则触发 before insert, before update
3. Replace 语法 如果有记录,则执行 before insert, before delete, after delete, after insert
Published 21 original articles · won praise 0 · Views 107

Guess you like

Origin blog.csdn.net/qq_44776943/article/details/104436666