How can I check in Tarantool SQL if a table already exists?
Asked
Active
Viewed 244 times
1 Answers
4
Using only SQL facilities it can be done this way:
SELECT EXISTS (select true from "_space" where "name" = 'table_name')
For instance:
tarantool> SELECT EXISTS (select true from "_space" where "name" = 'T1')
---
- metadata:
- name: EXISTS (select true from "_space" where "name" = 'T1')
type: boolean
rows:
- [true]
...
tarantool> SELECT EXISTS (select true from "_space" where "name" = 'T')
---
- metadata:
- name: EXISTS (select true from "_space" where "name" = 'T')
type: boolean
rows:
- [false]
...
In Lua mode:
tarantool> box.space.T1 ~= nil
---
- true
...
tarantool> box.space.T ~= nil
---
- false
...

NikitaRock
- 353
- 2
- 9
-
Comparing to `nil` is only necessary if you explicitly want a boolean type or want to specifically express then and there the intent of checking for existance. Otherwise you can just do `if box.space.T1 then print 'yes' else print 'no' end` or `print(box.space.T1 and 'yes' or 'no')` :D – DarkWiiPlayer Jul 23 '19 at 11:37