brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: MariaDB INVISIBLE columns stay out of SELECT * and plain INSERTs

Mariadb
~/tips/tip-mariadb-invisible-columns

ALTER TABLE t ADD COLUMN z INT INVISIBLE;

Hides a column from SELECT * and from inserts that do not name it.

Adding a column to a table that old code reads with SELECT * is risky. A new column can change what that code sees. MariaDB lets you add a column that old queries simply do not notice.

CREATE TABLE t (x INT, y INT INVISIBLE);
ALTER TABLE t ADD COLUMN z INT INVISIBLE;
ALTER TABLE t MODIFY x INT INVISIBLE;

An invisible column is not returned by SELECT *, but you can still read it by naming it: SELECT x, y FROM t. An INSERT without a column list does not need a value for it, while an insert that names the column can set it.

That makes it useful for audit or housekeeping columns, or for a column an application will start using later. It also helps when you migrate: the new column can exist before every query has been updated.

Restrictions from the docs: a table must keep at least one visible column. An invisible column may be NOT NULL, but then it needs a DEFAULT. A view or table built with SELECT * leaves invisible columns out, and a column you select by name is carried over without the invisible attribute.

One more thing I would remember: any tool or ORM that relies on SELECT * will not see the column, so check that it can read it by name before you put real data there.

Further reading: MariaDB invisible columns

More Mariadb tips CREATE SEQUENCE s START WITH 100 INCREMENT BY 10; CREATE OR REPLACE TABLE scratch ( mariadb --init-command="SET SESSION sql_mode='STRICT_ALL_TABLES'" mydb mariadb-admin -u root -p extended-status --relative --sleep=10 \ ALTER USER 'app'@'localhost' ACCOUNT LOCK; All Mariadb tips (15) All topics

Link