DEV Community

Michael
Michael

Posted on • Originally published at gbase8.cn

Checking Whether a View's Underlying Tables Still Exist in GBase 8a

When you create a view in GBase 8a, all referenced tables or other views must exist. However, if those underlying objects are dropped later, querying the view will fail. GBase 8a provides the CHECK TABLE command to detect whether a view's references are still valid, making dependency troubleshooting much easier in a gbase database.

Syntax

CHECK TABLE [vc.][db.]VIEW_NAME
Enter fullscreen mode Exit fullscreen mode

Example

We'll create a base table a, a view v_a that depends on it, and then see what CHECK TABLE reports before and after the table is dropped.

1. Create the Table and View

gbase> CREATE TABLE a(id INT);
Query OK, 0 rows affected

gbase> INSERT INTO a VALUES(8);
Query OK, 1 row affected

gbase> CREATE VIEW v_a AS SELECT * FROM a;
Query OK, 0 rows affected

gbase> SELECT * FROM v_a;
+------+
| id   |
+------+
|    8 |
+------+
Enter fullscreen mode Exit fullscreen mode

2. CHECK TABLE While the Base Table Exists

gbase> CHECK TABLE v_a;
+----------+-------+----------+----------------------------------------------------------------------------------+
| Table    | Op    | Msg_type | Msg_text                                                                         |
+----------+-------+----------+----------------------------------------------------------------------------------+
| test.v_a | check | Error    | (GBA-02SC-1001) The query includes syntax that is not supported by the gcluster. |
| test.v_a | check | status   | OK                                                                               |
+----------+-------+----------+----------------------------------------------------------------------------------+
Enter fullscreen mode Exit fullscreen mode

The view itself is structurally sound; the status column reports OK.

3. Drop the Base Table and Check Again

gbase> DROP TABLE a;
Query OK, 0 rows affected

gbase> CHECK TABLE v_a;
+----------+-------+----------+----------------------------------------------------------------------------------------------------------------------------+
| Table    | Op    | Msg_type | Msg_text                                                                                                                   |
+----------+-------+----------+----------------------------------------------------------------------------------------------------------------------------+
| test.v_a | check | Error    | Table 'vc1.test.a' doesn't exist                                                                                           |
| test.v_a | check | Error    | View 'test.v_a' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them |
| test.v_a | check | error    | Corrupt                                                                                                                    |
+----------+-------+----------+----------------------------------------------------------------------------------------------------------------------------+
Enter fullscreen mode Exit fullscreen mode

CHECK TABLE now explicitly names the missing table and marks the view as Corrupt.

When to Use This

Run CHECK TABLE whenever a view query suddenly fails with a cryptic error. It will quickly tell you whether the root cause is a missing underlying object, saving you from manually tracing dependencies across your schema. This is an essential tool for maintaining view integrity in any gbase database environment.

Top comments (0)