SQL operations in CockroachDB

1. Insert multiple rows of data

 

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

 If the corresponding column has no value, replace it with NULL or use 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 an index

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

query index

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     |
+----------+-------------+--------+-----+---------+-----------+---------+----------+

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154511&siteId=291194637