Hello Googlers,
I’m looking for a metadata field that shows when BigQuery table schema was last changed.
I’ve tried INFORMATION_SCHEMA.TABLES and TABLES but they don’t give me what I’m looking for. INFORMATION_SCHEMA.TABLES has a creation_time field that only gets set when you create the table…not updated on ALTER. TABLES has a last_modified_time field which gets set/updated any time you create or alter a table but it also gets updated whenever you insert data into the table.
I haven’t found any other promising fields in documentation or this forum. Does anyone know if what I’m looking for can be found?
Here’s a sample script showing what I’ve tried:
DROP TABLE IF EXISTS your_project.your_dataset.your_table;
SELECT CURRENT_TIMESTAMP();
--2024-02-28 19:48:32.661
SELECT table_id, TIMESTAMP_MILLIS(last_modified_time) as fmt_last_modified_time, last_modified_time
from your_project.your_dataset.__TABLES__
where table_id = 'your_table' ;
-- No data
CREATE TABLE your_project.your_dataset.your_table (LastName STRING(100), FirstName STRING(100));
SELECT table_id, TIMESTAMP_MILLIS(last_modified_time) as fmt_last_modified_time, last_modified_time
from your_project.your_dataset.__TABLES__
where table_id = 'your_table' ;
-- your_table 2024-02-28 19:48:54.487 1709174934487
SELECT table_name, creation_time
FROM your_project.your_dataset.INFORMATION_SCHEMA.TABLES
WHERE table_type = 'BASE TABLE' AND table_name = 'test_metadata';
-- your_table 2024-02-28 19:48:54.487
ALTER TABLE your_project.your_dataset.your_table ADD COLUMN MiddleName STRING(100);
SELECT table_id, TIMESTAMP_MILLIS(last_modified_time) as fmt_last_modified_time, last_modified_time
from your_project.your_dataset.__TABLES__
where table_id = 'your_table' ;
-- your_table 2024-02-28 19:49:54.296 1709174994296
-- changed, GOOD!
SELECT table_name, creation_time
FROM your_project.your_dataset.INFORMATION_SCHEMA.TABLES
WHERE table_type = 'BASE TABLE' AND table_name = 'your_table';
-- your_table 2024-02-28 19:48:54.487
-- no change, BAD!
INSERT INTO your_project.your_dataset.your_table (LastName, FirstName, MiddleName) VALUES ('Krigbaum', 'Michael', 'James');
select * from your_project.your_dataset.your_table;
--Krigbaum Michael James
SELECT table_id, TIMESTAMP_MILLIS(last_modified_time) as fmt_last_modified_time, last_modified_time
from your_project.your_dataset.__TABLES__
where table_id = 'your_table' ;
-- your_table 2024-02-28 19:51:48.009 1709175108009
-- changed, BAD!