博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 更改列为计算列_INSERT语句上mysql中的计算列
阅读量:5013 次
发布时间:2019-06-12

本文共 914 字,大约阅读时间需要 3 分钟。

bd96500e110b49cbb3cd949968f18be7.png

Let's say that I want to have a table that logs the date and the number of columns in some other table (or really any sort of math / string concat etc).

CREATE TABLE `log` (

`id` INTEGER NOT NULL AUTO_INCREMENT ,

`date` DATETIME NOT NULL ,

`count` INTEGER NOT NULL ,

PRIMARY KEY (`id`)

);

Is it possible to have the count column calculated for me whenever I do an insert?

e.g. do something like:

INSERT INTO log (date='foo');

and have count calculated by mysql.

Obviously I could do it myself by doing a query to get the count and inserting it, but this would be better.

解决方案

Triggers are the best tool for annotating data when a table is changed by insert, update or delete.

To automatically set the date column of a new row in the log with the current date, you'd create a trigger that looked something like this:

create trigger log_date before insert on log

for each row begin

set new.date = current_date()

end;

转载地址:http://zoggp.baihongyu.com/

你可能感兴趣的文章
笔记_小结
查看>>
Linux lsof命令 umount U盘
查看>>
自定义Font
查看>>
linux svn 服务端搭建
查看>>
maven用途、核心概念、用法、常用参数和命令、扩展
查看>>
linux时间同步ntp服务的安装与配置
查看>>
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法...
查看>>
网络编程-socket并发-粘包问题
查看>>
python 中安装pandas
查看>>
Hibernate 的<generator class="native"></generator>的不同属性含义
查看>>
linux修改root账户的用户名所得的教训
查看>>
【LeetCode】Flatten Binary Tree to Linked List
查看>>
读后感-浮生六纪
查看>>
执行指定路径的程序文件
查看>>
Leetcode-950 Reveal Cards In Increasing Order(按递增顺序显示卡牌)
查看>>
[Linux] 在 Linux CLI 使用 ssh-keygen 生成 RSA 密钥
查看>>
14款下载有用脚本的超酷网站
查看>>
LXC-Linux Containers介绍
查看>>
7.31实习培训日志-docker sql
查看>>
c#中使用servicestackredis操作redis
查看>>