Invisible columns are a neat trick in SQL. They let you add new data to your database without messing up existing queries. This is great when updating an old system. The new columns stay hidden unless you ask for them.
They also boost security. Sensitive data stays out of sight in standard queries. If someone gets unauthorized access, they won’t see the hidden data unless they know how to find it. You control who sees what. This keeps your important information safe.
Benefits of Invisible Columns
- Layered Security: Invisible columns add an extra layer of security. If someone gains unauthorized access, they won’t see the most sensitive data unless they know to query it specifically.
- Controlled Access: These columns don’t appear in
SELECT *
queries. Only specific queries can access them, allowing you to control who can see this data. - Simplify Auditing: Use invisible columns to store audit trails, like
created_by
orlast_accessed_by
. This keeps audit data secure and out of everyday queries, lowering the risk of tampering. - Update Old Systems: If you’re working on a legacy system, you might need to add new data fields. But you don’t want to break everything else. Invisible columns let you add these fields without changing how the old system works.
- Limit Data Exposure: Invisible columns keep sensitive information, like personal identifiers or financial data, out of standard queries. This reduces the risk of accidentally exposing this data.
Note: Before using invisible columns, think about whether they’re the right fit for your needs.
Databases That Support Invisible Columns
- MySQL: Supported in MySQL 8.0.23 and later.
- Oracle Database: Supported in Oracle 12c Release 1 (12.1) and later.
- MariaDB: Supported in MariaDB 10.3 and later.
- PostgreSQL: Not supported (use views or conditional logic as alternatives).
- SQL Server: Not supported (use views or computed columns as alternatives).
Employees table with an invisible column
Let’s create an employees
table where the salary
is an invisible column.
Note: At least one column in a table must be visible. If you try to make all columns invisible, it will result in an error.
Inserting data into the employees
table:
Now, if you run the query SELECT * FROM employees;
, the output would look like this.
Notice that the salary
column, which is marked as invisible, does not appear in the output.
This is the power of invisible columns. They let you hide specific data from your query results, keeping things clean and focused.
The data is still there, but it won’t show up unless you specifically ask for it. This helps reduce clutter in your results and keeps sensitive information out of sight. It’s a simple way to control what data gets exposed in your queries.
The Importance of Default Values
Invisible columns can be declared as NOT NULL
. However, if they are, you must provide a default value.
status
is invisible, cannot be null
, and has a default value of 'Pending'
. If you try to insert data without specifying the status
, the default value is used.
This query will return just order_id
, but the status column still exists and is set to 'Pending'
.
- Only the
order_id
column is visible because thestatus
column is invisible. - The
status
column has a default value of'Pending'
, but it does not appear in the output of theSELECT *
query.
Retrieving Invisible Columns
Invisible columns don’t disappear completely—they are simply hidden. To retrieve them, you must explicitly ask for them in your query.
Now, if you explicitly query for the invisible status column like this:
- Both
order_id
andstatus
will appear in the results. - The status for
order_id
1 and 2 is shown as'Pending'
because it uses the default value. - The status for
order_id
3 is'Shipped'
, as it was explicitly set during the insert.
Surrogate Primary Keys
Imagine you have a table without a primary key, and you want to add one without disrupting the existing structure.
You can add an invisible surrogate primary key to your table, which is a unique identifier for each row.
This primary key won’t clutter your standard query results but will still uniquely identify each row behind the scenes. This is particularly useful when you want to maintain a clean and simple output for your queries but still need a reliable way to reference each record individually.
Now, every row will have a unique primary key (pkid
).
- The
pkid
column, which is marked as invisible, does not appear in the results. - Only the visible columns (
customer_id
,name
, andemail
) are shown.
Remember, to retrieve the invisible pkid
column, you must explicitly include it in your SELECT
query. Even though it’s hidden by default, you can still access it by specifying it in the list of columns you want to retrieve.
Surrogate Foreign Keys
Let’s say you have two tables: orders
and customers
. The orders table references the customers
table through a customer_id
foreign key.
customers
table with an invisible surrogate primary key.
AUTO_INCREMENT
automatically generates a unique value for thepkid
column.PRIMARY KEY
ensures thatpkid
is the primary key.INVISIBLE
makes the pkid column invisible by default in queries likeSELECT *
.
Create the orders table with a surrogate foreign key that references the invisible primary key (pkid
) from the customers table.
Inserting dummy data into the customers
table.
customer_pkid
is a foreign key referencing thepkid
in thecustomers
table and is also invisible.
Inserting dummy data into the orders
table.
When you run a SELECT * FROM orders;
query, the output will only show the visible columns:
The customer_pkid
column, which acts as the foreign key, is invisible by default and doesn’t appear in this output. To retrieve the foreign key along with the order details, you can explicitly include it in your query.
Joining Tables Using Surrogate Keys
If you want to join the orders and customers tables to get more information, you can do so using the surrogate keys.
Invisible columns are a handy tool for managing data in SQL. They’re perfect for adding features to an old system without breaking anything or for keeping your queries clean and simple. Understanding how to create and use these columns gives you more control over your database.
Next time you’re working on a database, think about whether some columns should be invisible. It’s a small feature, but it can make a big difference.