如何在Tarantool SQL中检查表是否已存在?
只使用 SQL 设施,可以这样做:
SELECT EXISTS (select true from "_space" where "name" = 'table_name')
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]
...
tarantool> box.space.T1 ~= nil
---
- true
...
tarantool> box.space.T ~= nil
---
- false
...
nil
进行比较只有在您明确想要布尔类型或想要特别表达检查存在意图时才是必要的。否则,您可以只需执行if box.space.T1 then print 'yes' else print 'no' end
或print(box.space.T1 and 'yes' or 'no')
:D - DarkWiiPlayer