Sqlite: Insert new record by copying from old entry when inserting in the ID field - sqlite

I want to create a duplicated entry when I insert a new record with an existing id number.
i.e. when I enter a new row with 2 in the ID field, it will automatically create a new entry by copying all the fields from the row with ID=2
I am using the below trigger but it is not working.
1) the trigger fails to work when inserting an existing number in the ID column
2) when I changed new.ID to other field like new.col_2, the trigger works but an extra entry is also created after the trigger which I don't want it.
How can I solve these two issues or are there any workarounds for them?
Thanks.
CREATE TRIGGER "main"."NewTrigger" BEFORE INSERT ON "record" FOR EACH ROW WHEN new.ID is not NULL
BEGIN
INSERT INTO record (col_1, col_2)
SELECT col_1, col_2 FROM record
WHERE ID = new.ID;
END;
e.g.
original table named "record"
ID | col_1 | col_2
1 | Ada | 20
2 | Ben | 56
3 | Candy | 82
when I insert a new entry like this:
ID | col_1 | col_2
2
the resulting table "record" will become:
ID | col_1 | col_2
1 | Ada | 20
2 | Ben | 56
3 | Candy | 82
4 | Ben | 56

The INSERT statements inserts a new row.
This is not what you want because you are already inserting a row.
What you need to do is to change the values in the row after it has been inserted:
CREATE TRIGGER NewTrigger
AFTER INSERT ON record
FOR EACH ROW
WHEN NEW.ID IS NOT NULL AND NEW.Col1 IS NULL
BEGIN
UPDATE record
SET (Col1, Col2) = (SELECT Col1, Col2
FROM record
WHERE ID = NEW.ID
AND Col1 IS NOT NULL);
WHERE ID = NEW.ID
AND Col1 IS NULL;
END;
(Using a row value requires SQLite 3.15.0 or later.)

Related

Sqlite INSERT OR REPLACE is incrementing rowid

I am using the INSERT OR REPLACE syntax to either insert or update a row that may exist based on a unique column constraint within the table.
The table structure is
| Name | Age | CustomerId (unique) |
When I print out the table the first time, I get something like
| Name | Age | CustomerId | rowid |
|"Bob" | 22 | 5 | 1 |
Then I run INSERT OR REPLACE INTO MyTable(Name, Age, CustomerId) VALUES ("Bob", 23, 5);"
Without fail, this will increment the rowid column each time it is run. So now the result is
| Name | Age | CustomerId | rowid |
|"Bob" | 23 | 5 | 2 |
How do I prevent rowid from incrementing?
This is how INSERT OR REPLACE works.
If a violation of a unique index/constraint occurs then the existing row is deleted and the new row is inserted, so a new rowid is assigned for the new row.
If you don't want this to happen you must use UPSERT:
insert into tablename(Name, Age, CustomerId) values ('Bob', 23, 5)
on conflict(CustomerId) do update
set Name = excluded.Name,
Age = excluded.Age;
See the demo.

Update SQL column with SELECT statement data

I am currently working with DB Browser and I have a problem with updating columns.
I have a table which contains two columns: one is filled with strings and the other one is completely empty.
I have trimmed these strings with rtrim():
SELECT rtrim(column1, 'e') FROM table1
Now I want to place these trimmed strings into the empty column:
UPDATE table1 SET column2 = (SELECT rtrim(column1, 'e') FROM table1)
But now it fills the empty column with only the first trimmed string of column1, because it won't see the SELECT statement as a column, but as a string.
I also tried:
INSERT INTO table1 (column2) SELECT rtrim(column1, 'e') FROM table1
But this just doubles the amount of rows, as column2 is not 'empty', but filled with null.
I want to get something like
| column1 | column2 |
---------------------
| apple | appl |
| orange | orang |
| grape | grap |
Any ideas on how to solve this?

How to get ID from table A to table B in trigger?

I have two tables, A & B:
TABLE A
id | name
TABLE B
id | name | fk_idA
I want to create trigger AFTER INSERT in TABLE B which updates fk_idA appropriate with the newest id from table A.
An example:
TABLE A
id | name
1 | Andrew
2 | David
TABLE B
id | name | fk_idA
1 | Photo1 | 2
If the ID column is autoincrementing, the latest is the largest one, i.e., the one returned by MAX:
CREATE TRIGGER DefaultAIsLatest
AFTER INSERT ON TableB
FOR EACH ROW
WHEN NEW.fk_idA IS NULL
BEGIN
UPDATE TableB
SET fk_idA = (SELECT MAX(id)
FROM TableA)
WHERE id = NEW.id;
END;

selecting a row based on a number of column values in SQLite

I have a table with this structure:
id | IDs | Name | Type
1 | 10 | A | 1
2 | 11 | B | 1
3 | 12 | C | 2
4 | 13 | D | 3
except id nothing else is a FOREIGN or PRIMARY KEY. I want to select a row based on it's column values that are not PRIMARY KEY. I have tried the following syntax but it yields no results.
SELECT * FROM MyTable WHERE Name = 'A', Type = 1;
what am I doing wrong? What is exactly returned by a SELECT statement? I'm totally new to Data Base and I'm currently experimenting and trying to learn it. so far my search has not yield any results regarding this case.
Use and to add multiple conditions to your query
SELECT *
FROM MyTable
WHERE Name = 'A'
AND Type = 1;

Table name in another table using PostgreSQL

I have one table in which I have name of all the tables.
Table
ID | Name | table_name|
1 | A | abc
2 | B | xyz
3 | C | 123
Now I have tables abc, xyz and 123 on the basis of name I want to get the table name and then from that table I want it complete data
SELECT *
FROM (SELECT table_name FROM Table 1 WHERE Table1.Name = 'A')
This query is not working in PostgreSQL
Please try after modifying the query as follows:
SELECT *
FROM (SELECT table_name FROM Table 1 WHERE Table1.Name = 'A') As TblName

Resources