MariaDB虚拟列 - 我可以有散列吗? [英] MariaDB Virtual Columns - Can I have a hash?

查看:320
本文介绍了MariaDB虚拟列 - 我可以有散列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MariaDB具有虚拟列,允许用户自动对其他列数据执行操作。例如,如果您希望以不同的度量单位度量单位来显示数据,则只需简单地除以10或1000,如下表所示: > CREATE TABLE a(
id INT NOT NULL AUTO_INCREMENT,
distance_meters INT(11),
distance_kilometers FLOAT(11,3)AS(distance_meters / 1000)VIRTUAL,
PRIMARY KEY(id)
)ENGINE = InnoDB;

但是,如何创建一个虚拟列作为表列的子集的哈希值?例如。下面的表(不起作用)旨在散列名称和薪水,以便稍后如果通过与之前的散列表进行比较,任何员工姓名或薪水发生了变化,我可以再次轻松检查。

  CREATE TABLE employees(
id INT NOT NULL,
name VARCHAR(200),
salary INT(12),
年龄int(3),
hash VARCHAR(AS(MD5(concat(name,salary))))PERSISTENT ,
PRIMARY KEY(id)
)ENGINE = InnoDB;


解决方案

该表达式对MariaDB虚拟列完全合法。因为它是一个持久列,所以你甚至可以为它建立索引。



顺便说一句,在你的例子中,有一个语法错误,在VARCHAR之后。


MariaDB has virtual columns that allow one to automatically perform actions on other column data. For example, this is good if you want to display data in different units of metric measurement simply by dividing by 10 or 1000 as shown by the table definition below:

CREATE TABLE a (
     id INT NOT NULL AUTO_INCREMENT,
     distance_meters INT (11),
     distance_kilometers FLOAT (11,3) AS (distance_meters / 1000) VIRTUAL,
     PRIMARY KEY ( id )
) ENGINE=InnoDB;

However, how do I create a virtual column that is the hash of a subset of table columns? E.g. The table below (which doesnt work) aims to hash the name and salary, so that I can easily check again later if any of the employees names or salaries have changed by comparing against a previous hash list. I don't want the hash of the entire row.

CREATE TABLE employees (
    id INT NOT NULL,
    name VARCHAR(200),
    salary INT(12),
    age int(3),
    hash VARCHAR(AS (MD5(concat(name, salary)))) PERSISTENT,
    PRIMARY KEY ( id )
) ENGINE=InnoDB;

解决方案

That expression is perfectly legal for MariaDB virtual columns. Since it is a persistent column, you could even index it.

By the way, there is a syntax error in your example, after VARCHAR.

这篇关于MariaDB虚拟列 - 我可以有散列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆