SQLite Insert or Replace Where - sql

i have a table with 3 columns, ID, Description and Key where ID is not a primary key!
What i want is to insert or update/replace a current record.
Example:
decryptionKeys
ID Description Key
999 Birthday 24.12.1988
I tried this but it won't work:
INSERT OR REPLACE INTO decryptionKeys VALUES ("999","Birthday","25.12.1988") WHERE ID="999" AND Description="Birthday"

leave thr where clause
INSERT OR REPLACE INTO decryptionKeys VALUES ("999","Birthday","25.12.1988")

Related

INSERT + SELECT data type mismatch on similar fields

I'm running the following SQLite workaround to add a primary key to a table that did not have one. I am getting a datatype mismatch on
INSERT INTO cities
SELECT id, name FROM old_cities;
However, the fields have exactly the same type. Is it possible that his happens due to running the queries from DbBrowser for SQLite?
CREATE table cities (
id INTEGER NOT NULL,
name TEXT NOT NULL
);
INSERT INTO cities (id, name)
VALUES ('pan', 'doul');
END TRANSACTION;
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE cities RENAME TO old_cities;
--CREATE TABLE cities (
-- id INTEGER NOT NULL PRIMARY KEY,
-- name TEXT NOT NULL
--);
CREATE TABLE cities (
id INTEGER NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (id)
);
SELECT * FROM old_cities;
INSERT INTO cities
SELECT id, name FROM old_cities;
DROP TABLE old_cities;
COMMIT;
You have defined the column id of the table cities to be INTEGER, but with this:
INSERT INTO cities (id, name) VALUES ('pan', 'doul');
you insert the string 'pan' as id.
SQLite does not do any type checking in this case and allows it.
Did you mean to insert 2 rows each having the names 'pan' and 'doul'?
If so, you should do something like:
INSERT INTO cities (id, name) VALUES (1, 'pan'), (2, 'doul');
Later you rename the table cities to old_cities and you recreate cities but you do something different: you define id as INTEGER and PRIMARY KEY.
This definition is the only one that forces type checking in SQLite.
So, when you try to insert the rows from old_cities to cities you get an error because 'pan' is not allowed in the column id as it is defined now.

How auto increment id and insert 2 rows

I have two table with one to one relation and I want to insert two rows to the tables with the same auto increment id. Is it possible?
create table first
(
id bigint primary key,
value varchar(100) not null
);
create table second
(
id bigint references first (id),
sign boolean
);
insert into first(id, value)
values (-- autoincremented, 'some_value');
insert into second(id, sign)
values (-- the same autoincremented, true);
Your id column must be defined as an "auto increment" one before you can use that:
create table first
(
id bigint generated always as identity primary key,
value varchar(100) not null
);
Then you can use lastval() to get the last generated id:
insert into first(id, value)
values (default, 'some_value');
insert into second(id, sign)
values (lastval(), true);
Or if you want to be explicit:
insert into first(id, value)
values (default, 'some_value');
insert into second(id, sign)
values (currval(pg_get_serial_sequence('first','id')), true);
One option uses a cte with the returning clause:
with i as (
insert into first(value) values('some_value')
returning id
)
insert into second(id, sign)
select i.id, true from i
This performs the two inserts at once; the id of the first insert is auto-generated, and then used in the second insert.
For this to work, you need the id of the first table to be defined as serial.

INSERT INTO with default values for a single column

I have a problem to insert the data into 1 table with 1 column
Name: user_id
Column: id
I am trying to add 1 line in this column with this query:
INSERT INTO user_id (id) VALUES ()
The problem is the above is invalid, I want the id take the last value id +1
This is not a syntax problem because this query works:
INSERT INTO user_id (id) VALUES (4)
So, I do not really know how to solve this problem.
Assuming the id column is defined as serial or identity you can specify a column list and set the column value to default:
insert into user_id (id) values (default);
This also works if you have more columns, e.g:
insert into users (id, firstname, lastname)
values (default, 'Arthur', 'Dent');
Or you can leave out the column list completely and request the default value(s) for all columns:
insert into user_id default values;
SQL supports the default values statement.
So this will work:
create table t (id serial primary key);
insert into t
default values;
The syntax is described in the documentation.

Oracle update one table in relation to entering data into another table

the scenario is simple
CREATE TABLE one
(ID NUMBER CONSTRAINT
pk_id PRIMARY KEY,
Number_of_relations INTEGER)
CREATE TABLE two
(mock_id NUMBER,
CONSTRAINT fk_id FOREIGN KEY (mock_id) REFERENCES one(ID)
text VARCHAR)
is it possible to update Number_of_relations every time you enter a new row into table two. so if i enter ID in table one as '1', at the moment Number_of_relations is at '0', but if i add ('1', 'hello') and ('1', 'helloagain') into table two, now id '1' has two texts to it, but i want the number_of_relations to update (automatically if possible) in table one to '2'. is it possible?, thanks in advance.
This trigger does it:
create or replace trigger ins_two after insert on two
for each row
update one set number_of_relations = number_of_relations + 1
where one.id = :new.mock_id
Test:
insert into two values (1, 'hello');
insert into two values (1, 'hello again');
insert into two values (2, 'hello');
select * from one
ID NUMBER_OF_RELATIONS
---------- -------------------
1 2
2 1
This is example for INSERT action, you can add also sections for DELETE and UPDATE.
In "DELETE" case number_of_relations has to be decremented,
in "UPDATE" - it depends what column was updated, but logic is similar.
Triggers documentation and examples.

In PostgreSQL how do you insert into a table with only one identity column?

For instance:
{create table Participant ( id serial, primary key(id) );}
How do you insert into table in this case?
If you create the table like above,
You can use default in following way to insert:
INSERT INTO Participant values(default);
Check out SQLFIDDLE.
Another way to insert is:
INSERT INTO Participant values(NEXTVAL('Participant_id_seq'));
CREATE TABLE will create implicit sequence "Participant_id_seq" for serial column "Participant.id".
You can get the sequence for the table using pg_get_serial_sequence function in following way:
pg_get_serial_sequence('Participant', 'id')
It will take new value from sequence using NEXTVAL().
Check out SQLFIDDLE
insert into Participant values (default);