Files
chaisql/sqltests/INSERT/misc.sql
2024-02-18 11:11:37 +04:00

161 lines
4.0 KiB
SQL

-- test: read-only tables
INSERT INTO __chai_catalog (name, namespace) VALUES ('foo', 100);
-- error: cannot write to read-only table
-- test: insert with primary keys
CREATE TABLE testpk (foo INTEGER PRIMARY KEY);
INSERT INTO testpk (bar) VALUES (1);
-- error:
-- test: insert with primary keys: duplicate
CREATE TABLE testpk (foo INTEGER PRIMARY KEY);
INSERT INTO testpk (bar, foo) VALUES (1, 2);
INSERT INTO testpk (bar, foo) VALUES (1, 2);
-- error:
-- test: insert with types constraints
CREATE TABLE test_tc(
b bool, db double,
i bigint, bb blob, byt bytes,
t text
);
INSERT INTO test_tc (i, db, b, bb, byt, t) VALUES (10000000000, 21.21, true, "YmxvYlZhbHVlCg==", "Ynl0ZXNWYWx1ZQ==", "text");
SELECT * FROM test_tc;
/* result:
{
"b": true,
"db": 21.21,
"i": 10000000000,
"bb": CAST("YmxvYlZhbHVlCg==" AS BLOB),
"byt": CAST("Ynl0ZXNWYWx1ZQ==" AS BYTES),
"t": "text"
}
*/
-- test: insert with inferred constraints
CREATE TABLE test_ic(a INTEGER, s.b TEXT);
INSERT INTO test_ic VALUES {s: 1};
-- error:
-- test: insert with on conflict do nothing, pk
CREATE TABLE test_oc(a INTEGER PRIMARY KEY);
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
SELECT * FROM test_oc;
/* result:
{
a: 1
}
*/
-- test: insert with on conflict do nothing, unique
CREATE TABLE test_oc(a INTEGER UNIQUE);
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
SELECT * FROM test_oc;
/* result:
{
a: 1
}
*/
-- test: insert with on conflict do nothing, unique with default
CREATE TABLE test_oc(a INTEGER, b INTEGER UNIQUE DEFAULT 10);
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
INSERT INTO test_oc (a) VALUES (1) ON CONFLICT DO NOTHING;
SELECT * FROM test_oc;
/* result:
{
a: 1,
b: 10
}
*/
-- test: insert with on conflict do nothing, overall
CREATE TABLE test_oc(a INTEGER UNIQUE, b INTEGER PRIMARY KEY, c INTEGER UNIQUE DEFAULT 10);
INSERT INTO test_oc (a, b, c) VALUES (1, 1, 1);
INSERT INTO test_oc (a, b, c) VALUES (1, 2, 3) ON CONFLICT DO NOTHING; -- unique constraint
INSERT INTO test_oc (a, b, c) VALUES (2, 1, 4) ON CONFLICT DO NOTHING; -- primary key
INSERT INTO test_oc (a, b, c) VALUES (2, 2, 1) ON CONFLICT DO NOTHING; -- unique constraint
INSERT INTO test_oc (a, b) VALUES (2, 2); -- should insert
INSERT INTO test_oc (a, b) VALUES (3, 3) ON CONFLICT DO NOTHING; -- should not insert
SELECT * FROM test_oc;
/* result:
{
a: 1,
b: 1,
c: 1
}
{
a: 2,
b: 2,
c: 10
}
*/
-- test: insert with on conflict do replace, pk
CREATE TABLE test_oc(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER);
INSERT INTO test_oc (a, b, c) VALUES (1, 1, 1);
INSERT INTO test_oc (a, b, c) VALUES (1, 2, 3) ON CONFLICT DO REPLACE;
SELECT * FROM test_oc;
/* result:
{
a: 1,
b: 2,
c: 3
}
*/
-- test: insert with on conflict do replace, unique
CREATE TABLE test_oc(a INTEGER UNIQUE, b INTEGER, c INTEGER);
INSERT INTO test_oc (a, b, c) VALUES (1, 1, 1);
INSERT INTO test_oc (a, b, c) VALUES (1, 2, 3) ON CONFLICT DO REPLACE;
SELECT * FROM test_oc;
/* result:
{
a: 1,
b: 2,
c: 3
}
*/
-- test: insert with on conflict do replace, not null
CREATE TABLE test_oc(a INTEGER NOT NULL, b INTEGER, c INTEGER);
INSERT INTO test_oc (b, c) VALUES (1, 1) ON CONFLICT DO REPLACE;
-- error:
-- test: insert with NEXT VALUE FOR
CREATE TABLE test_oc(a INTEGER UNIQUE);
CREATE SEQUENCE test_seq;
INSERT INTO test_oc (a) VALUES (NEXT VALUE FOR test_seq);
INSERT INTO test_oc (a) VALUES (NEXT VALUE FOR test_seq), (NEXT VALUE FOR test_seq);
SELECT * FROM test_oc;
/* result:
{
a: 1
}
{
a: 2
}
{
a: 3
}
*/
-- test: duplicate column names: root
CREATE TABLE test_df;
INSERT INTO test_df(a, a) VALUES (1, 10);
-- error:
-- test: inserts must be silent
CREATE TABLE test (a int);
INSERT INTO test VALUES (1);
/* result:
*/
-- test: inserts must be silent: explain
CREATE TABLE test (a int);
EXPLAIN INSERT INTO test (a) VALUES (1);
/* result:
{plan: "rows.Emit((1)) | table.Validate(\"test\") | table.Insert(\"test\") | discard()"}
*/