分类标签归档:Mysql

[ 数据库字段长度截取 ]


可以,SQL 层面可以直接截取 body 字段的前 N 个字符,避免拉取全部内容再截断。 不同数据库有不同的字符串截取函数:

MySQL 用 LEFT(body, 200) PostgreSQL 用 SUBSTRING(body FROM 1 FOR 200)

golang

    sql := fmt.Sprintf(`
        SELECT 
            id,
            title,
            LEFT(body, 300) AS body,
            created_time,
            last_mod_ti

< Read more >

[ Sql题目 ]


第二高的薪水

已解答 中等 相关标签 相关企业 SQL Schema Pandas Schema Employee 表:

Column Name Type
id int
salary int

在 SQL 中,id 是这个表的主键。 表的每一行包含员工的工资信息。

查询并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null(Pandas 则返回 None) 。 查询结果如下例所示。

示例 1:

输入: Employee 表:

id salary
1 100
2 200
3 300

输出:

Second

< Read more >

[ mysql修改事务隔离级别 ]


mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)

mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
  • 与乐观锁配合使用READ-COMMITTED 读取

< Read more >

[ msyql异常 ]


1. group by语句问题

问题:

SQL 错误 [1055] [42000]: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'pymysql.users.password' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决:

ONLY_FUL

< Read more >

[ Mysql面试题目 ]


1. 聚簇索引, 和 非聚簇索引

聚簇索引是由每张表的主键来建立的, 而非聚簇索引是按照非组件列来建立的。
在innodb中没张表只有一个聚簇索引, 而可以有多改非聚簇索引。
聚簇索引的叶子节点存储的是整行的数据, 而非聚簇索引叶子节点储存的是主键的值和对应行的指针

2. 聚簇索引是怎么构建的

在mysql中, 当你指定了表的主键时, mysql会自动为该主键创建聚簇索引。如果没有指定主键,则会自动为第一个非空唯一索引创建聚簇索引。如果没有这样的索引, mysql会自动为innodb表创建一个cen_clust_index的隐藏的聚簇索引。

3. 回表索引和索引覆盖

回表指的是, my

< Read more >

[ Mysql测试用 ]


INSERT INTO `tb_user` (`password`, `last_login`, `is_superuser`, `username`, `first_name`, `last_name`, `email`, `is_staff`, `is_active`, `date_joined`, `create_time`, `update_time`, `version`, `delete_token`, `phone`, `gender`, `token`, `status`)
VALUES ('pbkdf2_sha256$390000$GUWtpz0MfJtNvRGzVI

< Read more >

[ Mysql8用户连接配置 ]


  1. 先使用mysql数据库 use mysql;

  2. 运行远程连接 GRANT ALL ON . TO 'root'@'%'; GRANT REPLICATION SLAVE ON . TO 'root'@'%';

  3. 查看所允许的访问地址

  4. 修改所允许的访问地址,即可 update user set host='%' where user='root';

  5. 查看密码加密方式 select host, user, plugin from user;

  6. 修改密码的加密方式 alter user 'root'@'%' identified with mysql_native_passwor

< Read more >

[ Mysql事务 ]


事务嵌套的两种方法

参考事务操作

-- 第一个事务 --
BEGIN;
    update yyy;
        -- 第两个事务 ---
        begin;
            update xxx;
        commit;
    update zzz;
commit;

注意事项:

正常情况下mysql不存在事务的嵌套 当第一个事务执行, 遇到了第二个事务begin等代码 就会自动把第一个事务提交,去执行第二个事务 执行完第二个事务, 后面的代码相当于是没有事务了 导致割裂

场景1

  1. 融入外界的事务

    相当于把内部事务的begin, commit删除了 相

< Read more >

[ Mysql数据库备份 ]


基本操作

mysqldump -u root -p[密码] --databases 库名1 [库名2] ... > /备份路径/备份文件名.sql   
#导出的就是数据库脚本文件

例:
mysqldump -u root -p --databases liu > /opt/kgc.sql       #备份一个kgc库
mysqldump -u root -p --databases mysql li > /opt/mysql-kgc.sql    #备份mysql与 kgc两个库

尝试使用sh脚本备份docker里的mysql

# 保留10天数据,
# mysql8

< Read more >

[ Mysql多对多SQL写法 ]


多对多(权限abrc)

表结构

1. 用户表 user

2. 用户&角色-中间表 user_role

3. 角色表 role

4. 角色&权限-中间表 role_power

5. 权限表 power

数据库查询**

使用内join查询(5连表)

  • user左连
  • user_role连
  • role连
  • role_power连
  • power
select `user`.id as user_id, `user`.username as 用户名, `role`.name as 角色, `power`.name as 权限
from `user`  
    left join (`user

< Read more >