단일 트리거로는이 작업을 수행 할 수 없습니다.
lastChanged
열을 DATETIME
to TIMESTAMP
으로 변경하면 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
기능을 사용하여 삽입 또는 업데이트시 자동으로 값을 설정할 수 있습니다.
TIMESTAMP
데이터 형식은 1970에서 2038까지의 값만 지원할 수 있으므로이 범위를 벗어나는 값을 저장해야하는 경우 사용할 수 없습니다.
가능한 한 간단하게 만들고 모든 테이블에 이미 lastChanged
이라는 열이 있다고 했으므로 information_schema
을 사용하여 필요한 alter table 문을 생성 한 다음 모두에서 실행하십시오 일단. 이 같은
뭔가 :
select concat('alter table ',
t.table_schema,
'.',
t.table_name,
' modify column ',
c.column_name,
' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;') as ddl
into outfile '/tmp/lastChanged.sql'
from information_schema.columns c
inner join information_schema.tables t
on t.table_schema = c.table_schema and t.table_name = c.table_name
where c.table_schema = database()
and c.data_type = 'datetime'
and c.column_name = 'lastChanged'
and t.table_type = 'base table';
\. /tmp/lastChanged.sql