欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CockroachDB中SQL操作

程序员文章站 2022-03-22 17:31:17
...

1、多行数据插入

 

INSERT INTO accounts VALUES
    (3, 8100.73),
    (4, 9400.10);

 对应列没有值以NULL代替或使用DEFAULT

 

INSERT INTO accounts (id, balance) VALUES    (5);
INSERT INTO accounts (id, balance) VALUES    (6, DEFAULT);
SELECT * FROM accounts WHERE id in (5, 6);
+----+---------+
| id | balance |
+----+---------+
|  5 | NULL    |
|  6 | NULL    |
+----+---------+
(2 rows)

 

2、创建索引

CREATE INDEX balance_idx ON accounts (balance DESC);
或
CREATE TABLE accounts (
    id INT PRIMARY KEY,
    balance DECIMAL,
    INDEX balance_idx (balance)
);

查询索引

SHOW INDEX FROM accounts;

+----------+-------------+--------+-----+---------+-----------+---------+----------+
|  Table   |    Name     | Unique | Seq | Column  | Direction | Storing | Implicit |
+----------+-------------+--------+-----+---------+-----------+---------+----------+
| accounts | primary     | true   |   1 | id      | ASC       | false   | false    |
| accounts | balance_idx | false  |   1 | balance | DESC      | false   | false    |
| accounts | balance_idx | false  |   2 | id      | ASC       | false   | true     |
+----------+-------------+--------+-----+---------+-----------+---------+----------+

 

 

 

相关标签: CockroachDB sql