巨杉SequoiaDB 5.0.1 最简单的执行增删改查等操作例子

本文介绍,在巨杉SequoiaDB 5上做最简单的增删改查操作的例子。

创建CS

[sdbadmin@gbase_rh7_001 ~]$ sdb
Welcome to SequoiaDB shell!
help() for help, Ctrl+c or quit to exit
> var db=new Sdb()
Takes 0.011562s.
> var cs=db.createCS('emp')
Takes 0.008918s.

创建CL

> db.emp.createCL("employees")
localhost:11810.emp.employees
Takes 0.316113s.

插入数据

通过insert

注意2行数据,name是和第1行数据相,但多了birth数据。

> db.emp.employees.insert({id:1,name:"First"})
{
  "InsertedNum": 1,
  "DuplicatedNum": 0
}
Takes 0.003210s.
> db.emp.employees.insert({id:2,name:"First",birth:"1990-12-13"})
{
  "InsertedNum": 1,
  "DuplicatedNum": 0
}
Takes 0.001932s.

通过execUpdate的SQL格式

insert into () values ()

> db.execUpdate("insert into emp.employees (id,name) values(3,'second')");
{
  "InsertedNum": 1,
  "DuplicatedNum": 0
}
Takes 0.019914s.
> db.exec("select * from emp.employees where id='3'");
Return 0 row(s).
Takes 0.001557s.
> db.exec("select * from emp.employees where id=3");
{
  "_id": {
    "$oid": "5ffee5c77606f131940aa9e3"
  },
  "id": 3,
  "name": "second"
}
Return 1 row(s).
Takes 0.001759s.
>

查询

全表查询

> db.exec('select * from emp.employees');
{
  "_id": {
    "$oid": "5ffef97795d25e56d34d79d7"
  },
  "id": 1,
  "name": "First"
}
{
  "_id": {
    "$oid": "5ffef9e295d25e56d34d79d8"
  },
  "id": 2,
  "name": "First",
  "birth": "1990-12-13"
}
Return 2 row(s).
Takes 0.003467s.

带条件查询

注意条件是有类型的,比如=1是数字,=’1‘是字符串,结果不一定相等。

> db.exec('select * from emp.employees where id=1');
{
  "_id": {
    "$oid": "5ffef97795d25e56d34d79d7"
  },
  "id": 1,
  "name": "First"
}
Return 1 row(s).
Takes 0.002669s.
> db.exec('select * from emp.employees where name="First"');
{
  "_id": {
    "$oid": "5ffef97795d25e56d34d79d7"
  },
  "id": 1,
  "name": "First"
}
{
  "_id": {
    "$oid": "5ffef9e295d25e56d34d79d8"
  },
  "id": 2,
  "name": "First",
  "birth": "1990-12-13"
}
Return 2 row(s).
Takes 0.002355s.
>

查询唯一的id

> db.emp.employees.find({id:1})
{
  "_id": {
    "$oid": "5ffef97795d25e56d34d79d7"
  },
  "id": 1,
  "name": "First"
}
Return 1 row(s).
Takes 0.001672s.

查询有重复的Name

> db.emp.employees.find({name:"First"})
{
  "_id": {
    "$oid": "5ffef97795d25e56d34d79d7"
  },
  "id": 1,
  "name": "First"
}
{
  "_id": {
    "$oid": "5ffef9e295d25e56d34d79d8"
  },
  "id": 2,
  "name": "First",
  "birth": "1990-12-13"
}
Return 2 row(s).
Takes 0.001782s.

更新

更新操作本身,是可以增加额外属性的,等同于增加了字段。

如下update时给id=3的记录增加了birth字段,在前面insert into时是没有的。

> db.execUpdate("update emp.employees set birth='2020-12-13' where id=3");
{
  "UpdatedNum": 1,
  "ModifiedNum": 1,
  "InsertedNum": 0
}
Takes 0.002411s.
> db.exec("select * from emp.employees where id=3");
{
  "_id": {
    "$oid": "5ffee5c77606f131940aa9e3"
  },
  "birth": "2020-12-13",
  "id": 3,
  "name": "second"
}
Return 1 row(s).
Takes 0.002100s.

删除

标准的delete语法,再次查询已经没有记录。

> db.execUpdate("delete from emp.employees where id=3");
{
  "DeletedNum": 1
}
Takes 0.002337s.
> db.exec("select * from emp.employees where id=3");
Return 0 row(s).
Takes 0.001677s.

总结

通过SQL类型的操作,进行数据的增删改查。其中exec用于查询,execUpdate用于增加,更新和删除。语法与标准SQL一致。