跳转至

SQL 海洋


概念小記

  • 数据库database
  • table
  • column
  • row
  • 主键primary key
  • 关键字

增删改查

  • 注释:
    • 单行注释:--#开头,这两个字符开头的行会被忽略(# 有些数据库软件会不支持
    • 多行注释:/* 开头,*/ 结尾

  • SQL 对于大小写不敏感,但约定成俗“关键字大写,列名小写(即字段)”
  • SQL 语句普遍以 ; 结尾(有些数据软件不写也行
  • SQL 语句如果过长,一般建议写成多行,便于阅读

  • 语法简记
    • SHOW:SHOW 什么; 如查看数据库:SHOW DATABASES;
    • SELECT:SELECT 列名 FROM 表名; 多个列名用 , 分隔
    • UPDTATE:
    • DROP:
    • INSET:
    • WHERE:WHERE 都是结合前面语句使用,用于条件过滤,比如 SELECT 列名 FROM 表名 WHERE 条件
  • 查看数据库用户列表
    • select Host,User,PassWord from mysql.user;
    • select * from mysql.user;
  • 查看数据库
    • show databases;
  • 查看数据库中的表
    • show tables from DB_name;
  • 求和
    • sum()
    • SELECT SUM(字段) AS value FROM table_name WHERE 条件
      SELECT 'lt0001' AS id,SUM(suttle_adjust) AS VALUE,TIME FROM `dbsx_cgcx` WHERE (re_company_id = '3330' AND inv_code IN ('300013','300012','5071589'))
      
  • select 结果合并

    • UNION
      SELECT 'lt0001' AS id,SUM(suttle_adjust) AS VALUE,TIME FROM `dbsx_cgcx` WHERE (re_company_id = '3330' AND inv_code IN ('300013','300012','5071589'))
      UNION
      SELECT 'jy0001' AS id,SUM(suttle_adjust) AS VALUE,TIME FROM `dbsx_cgcx` WHERE (re_company_id = '3330' AND inv_code IN ('5110564','5110561','5089102'))
      UNION
      SELECT inv_code AS id,suttle_adjust,TIME FROM `dbsx_cgcx` WHERE (re_company_id = '3330' AND inv_code='5062994')
      
  • 查询某个特定值在整个数据库中所在的表和字段(已知数据库和字段值,查询表明和字段名

    DROP TABLE  IF EXISTS temp;
    CREATE TABLE temp(tablename VARCHAR(255),LieName VARCHAR(255));
    DELIMITER $$
    DROP PROCEDURE IF EXISTS getDataByDbName $$
    CREATE PROCEDURE getDataByDbName(IN dbName VARCHAR(255),IN ziduan VARCHAR(255))
    BEGIN
    DECLARE num INT;
    SET @STMT =CONCAT("SELECT COUNT(*) FROM ",dbName," WHERE `",ziduan,"` LIKE BINARY '%特定的值%' INTO @num;");
    PREPARE STMT FROM @STMT;
    EXECUTE STMT;
    IF(@num>0) THEN
        INSERT INTO temp VALUES (dbName,ziduan);
    END IF;
    END$$
    DELIMITER ;
    DELIMITER $$
    DROP PROCEDURE IF EXISTS processquanjusou$$
    CREATE PROCEDURE processquanjusou()
    BEGIN
    DECLARE done BOOLEAN DEFAULT 0;
    DECLARE biao VARCHAR (255);
    DECLARE ziduan VARCHAR (255);
    DECLARE indexss CURSOR
    FOR
    SELECT TABLE_NAME,COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='数据库名' ;
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
    OPEN indexss;
    REPEAT
    FETCH indexss INTO biao,ziduan;
    CALL  getDataByDbName(biao,ziduan);
    UNTIL done END REPEAT;
    CLOSE indexss;
    END$$
    DELIMITER ;
    CALL processquanjusou();
    SELECT * FROM temp;
    

备份&恢复

  • 备份

    mysqldump -u root -p --databases database_name > file_name.sql
    

  • 恢复

    mysql -u root -p database_name < file_name.sql
    

  • 拷贝整个数据文件目录(版本需要统一,否则容易出错)

  • 借助第三方数据工具:


社区推荐


日常FAQ

  • ERROR 1449 (HY000) at line 8667: The user specified as a definer ('root'@'%') does not exist
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY ".";
    FLUSH PRIVILEGES;
    

By anYun 2022.01.20


  1. 本文版权归IT小圈所有,受中华人民共和国相关法律保护 

  2. 任何组织和个人未经允许不得私自复制传播以及商业性分享