statement ok
SET experimental_enable_unique_without_index_constraints = true

statement ok
CREATE TABLE test_kv(k INT PRIMARY KEY, v INT, w DECIMAL);
  CREATE UNIQUE INDEX test_v_idx ON test_kv(v);
  CREATE INDEX test_v_idx2 ON test_kv(v DESC) STORING(w);
  CREATE INDEX test_v_idx3 ON test_kv(w) STORING(v);
  CREATE TABLE test_kvr1(k INT PRIMARY KEY REFERENCES test_kv(k));
  CREATE TABLE test_kvr2(k INT, v INT UNIQUE REFERENCES test_kv(k));
  CREATE TABLE test_kvr3(k INT, v INT UNIQUE REFERENCES test_kv(v));
  CREATE TABLE test_kvi1(k INT PRIMARY KEY);
  CREATE TABLE test_kvi2(k INT PRIMARY KEY, v INT);
  CREATE UNIQUE INDEX test_kvi2_idx ON test_kvi2(v);
  CREATE VIEW test_v1 AS SELECT v FROM test_kv;
  CREATE VIEW test_v2 AS SELECT v FROM test_v1;
  CREATE TABLE test_uwi_parent(a INT UNIQUE WITHOUT INDEX);
  CREATE TABLE test_uwi_child(a INT REFERENCES test_uwi_parent(a));

query ITITTBTB colnames
SELECT * FROM crdb_internal.table_columns WHERE descriptor_name LIKE 'test_%' ORDER BY descriptor_id, column_id
----
descriptor_id  descriptor_name  column_id  column_name  column_type                                                                                              nullable  default_expr    hidden
106            test_kv          1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     NULL            false
106            test_kv          2          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
106            test_kv          3          w            family:DecimalFamily width:0 precision:0 locale:"" visible_type:0 oid:1700 time_precision_is_set:false   true      NULL            false
107            test_kvr1        1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     NULL            false
108            test_kvr2        1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
108            test_kvr2        2          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
108            test_kvr2        3          rowid        family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     unique_rowid()  true
109            test_kvr3        1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
109            test_kvr3        2          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
109            test_kvr3        3          rowid        family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     unique_rowid()  true
110            test_kvi1        1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     NULL            false
111            test_kvi2        1          k            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     NULL            false
111            test_kvi2        2          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
112            test_v1          1          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
113            test_v2          1          v            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
114            test_uwi_parent  1          a            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
114            test_uwi_parent  2          rowid        family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     unique_rowid()  true
115            test_uwi_child   1          a            family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        true      NULL            false
115            test_uwi_child   2          rowid        family:IntFamily width:64 precision:0 locale:"" visible_type:0 oid:20 time_precision_is_set:false        false     unique_rowid()  true

query ITITTBBBI colnames
  SELECT descriptor_id, descriptor_name, index_id, index_name, index_type, is_unique, is_inverted, is_sharded, shard_bucket_count
    FROM crdb_internal.table_indexes
   WHERE descriptor_name LIKE 'test_%'
ORDER BY descriptor_id, index_id
----
descriptor_id  descriptor_name  index_id  index_name            index_type  is_unique  is_inverted  is_sharded  shard_bucket_count
106            test_kv          1         test_kv_pkey          primary     true       false        false       NULL
106            test_kv          2         test_v_idx            secondary   true       false        false       NULL
106            test_kv          4         test_v_idx2           secondary   false      false        false       NULL
106            test_kv          6         test_v_idx3           secondary   false      false        false       NULL
107            test_kvr1        1         test_kvr1_pkey        primary     true       false        false       NULL
108            test_kvr2        1         test_kvr2_pkey        primary     true       false        false       NULL
108            test_kvr2        2         test_kvr2_v_key       secondary   true       false        false       NULL
109            test_kvr3        1         test_kvr3_pkey        primary     true       false        false       NULL
109            test_kvr3        2         test_kvr3_v_key       secondary   true       false        false       NULL
110            test_kvi1        1         test_kvi1_pkey        primary     true       false        false       NULL
111            test_kvi2        1         test_kvi2_pkey        primary     true       false        false       NULL
111            test_kvi2        2         test_kvi2_idx         secondary   true       false        false       NULL
112            test_v1          0         ·                     primary     false      false        false       NULL
113            test_v2          0         ·                     primary     false      false        false       NULL
114            test_uwi_parent  1         test_uwi_parent_pkey  primary     true       false        false       NULL
115            test_uwi_child   1         test_uwi_child_pkey   primary     true       false        false       NULL

query ITITTITTB colnames
SELECT * FROM crdb_internal.index_columns WHERE descriptor_name LIKE 'test_%' ORDER BY descriptor_id, index_id, column_type, column_id
----
descriptor_id  descriptor_name  index_id  index_name            column_type  column_id  column_name  column_direction  implicit
106            test_kv          1         test_kv_pkey          key          1          k            ASC               false
106            test_kv          2         test_v_idx            extra        1          NULL         NULL              false
106            test_kv          2         test_v_idx            key          2          v            ASC               false
106            test_kv          4         test_v_idx2           extra        1          NULL         NULL              false
106            test_kv          4         test_v_idx2           key          2          v            DESC              false
106            test_kv          4         test_v_idx2           storing      3          NULL         NULL              false
106            test_kv          6         test_v_idx3           composite    3          NULL         NULL              false
106            test_kv          6         test_v_idx3           extra        1          NULL         NULL              false
106            test_kv          6         test_v_idx3           key          3          w            ASC               false
106            test_kv          6         test_v_idx3           storing      2          NULL         NULL              false
107            test_kvr1        1         test_kvr1_pkey        key          1          k            ASC               false
108            test_kvr2        1         test_kvr2_pkey        key          3          rowid        ASC               false
108            test_kvr2        2         test_kvr2_v_key       extra        3          NULL         NULL              false
108            test_kvr2        2         test_kvr2_v_key       key          2          v            ASC               false
109            test_kvr3        1         test_kvr3_pkey        key          3          rowid        ASC               false
109            test_kvr3        2         test_kvr3_v_key       extra        3          NULL         NULL              false
109            test_kvr3        2         test_kvr3_v_key       key          2          v            ASC               false
110            test_kvi1        1         test_kvi1_pkey        key          1          k            ASC               false
111            test_kvi2        1         test_kvi2_pkey        key          1          k            ASC               false
111            test_kvi2        2         test_kvi2_idx         extra        1          NULL         NULL              false
111            test_kvi2        2         test_kvi2_idx         key          2          v            ASC               false
114            test_uwi_parent  1         test_uwi_parent_pkey  key          2          rowid        ASC               false
115            test_uwi_child   1         test_uwi_child_pkey   key          2          rowid        ASC               false

query ITIIITITT colnames
SELECT * FROM crdb_internal.backward_dependencies WHERE descriptor_name LIKE 'test_%' ORDER BY descriptor_id, index_id, dependson_type, dependson_id, dependson_index_id
----
descriptor_id  descriptor_name  index_id  column_id  dependson_id  dependson_type  dependson_index_id  dependson_name         dependson_details
107            test_kvr1        NULL      NULL       106           fk              1                   test_kvr1_k_fkey       NULL
108            test_kvr2        NULL      NULL       106           fk              1                   test_kvr2_v_fkey       NULL
109            test_kvr3        NULL      NULL       106           fk              2                   test_kvr3_v_fkey       NULL
112            test_v1          NULL      NULL       106           view            NULL                NULL                   NULL
113            test_v2          NULL      NULL       112           view            NULL                NULL                   NULL
115            test_uwi_child   NULL      NULL       114           fk              0                   test_uwi_child_a_fkey  NULL

query ITIITITT colnames
SELECT * FROM crdb_internal.forward_dependencies WHERE descriptor_name LIKE 'test_%' ORDER BY descriptor_id, index_id, dependedonby_type, dependedonby_id, dependedonby_index_id
----
descriptor_id  descriptor_name  index_id  dependedonby_id  dependedonby_type  dependedonby_index_id  dependedonby_name  dependedonby_details
106            test_kv          NULL      107              fk                 NULL                   NULL               NULL
106            test_kv          NULL      108              fk                 NULL                   NULL               NULL
106            test_kv          NULL      109              fk                 NULL                   NULL               NULL
106            test_kv          NULL      112              view               0                      NULL               Columns: [2]
112            test_v1          NULL      113              view               0                      NULL               Columns: [1]
114            test_uwi_parent  NULL      115              fk                 NULL                   NULL               NULL

# Checks view dependencies (#17306)
statement ok
CREATE TABLE moretest_t(k INT, v INT);
  CREATE VIEW moretest_v AS SELECT v FROM moretest_t WHERE FALSE

query ITIIITITT colnames
SELECT * FROM crdb_internal.backward_dependencies WHERE descriptor_name LIKE 'moretest_%' ORDER BY descriptor_id, index_id, dependson_type, dependson_id, dependson_index_id
----
descriptor_id  descriptor_name  index_id  column_id  dependson_id  dependson_type  dependson_index_id  dependson_name  dependson_details
117            moretest_v       NULL      NULL       116           view            NULL                NULL            NULL

query ITIITITT colnames
SELECT * FROM crdb_internal.forward_dependencies WHERE descriptor_name LIKE 'moretest_%' ORDER BY descriptor_id, index_id, dependedonby_type, dependedonby_id, dependedonby_index_id
----
descriptor_id  descriptor_name  index_id  dependedonby_id  dependedonby_type  dependedonby_index_id  dependedonby_name  dependedonby_details
116            moretest_t       NULL      117              view               0                      NULL               Columns: [2]

# Check sequence dependencies.

statement ok
CREATE SEQUENCE blog_posts_id_seq

statement ok
CREATE TABLE blog_posts (id INT PRIMARY KEY DEFAULT nextval('blog_posts_id_seq'), title text)

query ITIIITITT colnames
SELECT * FROM crdb_internal.backward_dependencies WHERE descriptor_name LIKE 'blog_posts'
----
descriptor_id  descriptor_name  index_id  column_id  dependson_id  dependson_type  dependson_index_id  dependson_name  dependson_details
119            blog_posts       NULL      1          118           sequence        NULL                NULL            NULL

query ITIITITT colnames
SELECT * FROM crdb_internal.forward_dependencies WHERE descriptor_name LIKE 'blog_posts%'
----
descriptor_id  descriptor_name    index_id  dependedonby_id  dependedonby_type  dependedonby_index_id  dependedonby_name  dependedonby_details
118            blog_posts_id_seq  NULL      119              sequence           0                      NULL               Columns: [1]
