How to save changes to SQLiteStudio (3.4.3) - sqlite

I am trying to save my changes (code below) in all rows in 1 column by using the export option but my database is not affected. Date in my column is set to September and I successfully changed it to march but just in SQL editor. How do I save these changes?
select name,
DATE(datum, '-6 months')
as datum
from concerts;

If you want to update the column datum then you need an UPDATE statement:
UPDATE concerts
SET datum = date(datum, '-6 months');
If you want to insert the rows in a different table then you need an INSERT statement:
INSERT INTO other_table (name, datum)
SELECT name, date(datum, '-6 months')
FROM concerts;
If you want to create a new table:
CREATE TABLE new_table AS
SELECT name, date(datum, '-6 months')
FROM concerts;

Related

Insert Into Select Case Statement

I am using an Insert Into Select to move a new table into a master table. I need to have either one of two columns [customMessage1] or [customMessage2] in the new table be entered into the master table. The good data in the new table starts 'SO' in either column. [customMessage2] only has a good value if [customMessage1] does not in the imported data.
INSERT INTO [Freight].[dbo].[MASTER]
Select
[transactionType]
,[amount]
,[transactionId]
,CASE
WHEN [customMessage1] LIKE 'SO%'
THEN [customMessage1]
ELSE
[customMessage2]
END
FROM [Freight].[dbo].[NEW]

To INSERT a row into table from another table

I have the 'SchoolYearTeachingDays' table with just one column, in which are dates:
CREATE TABLE SchoolYearTeachingDays (
aDate DATE PRIMARY KEY
UNIQUE
);
I filled it with many dates which are unique. These dates excludes dates for Sundays and for Saturdays.
I have another, the 'TeachingSaturdaysInSchoolYear' table:
CREATE TABLE TeachingSaturdaysInSchoolYear (
id INT PRIMARY KEY
UNIQUE,
aDate DATE,
TimetableForTheDay TEXT
);
This table holds just two dates. These two dates are for two Saturdays. On these two Saturdays we have to teach students.
When I do the following query on this table, I get these two records:
2018-04-14
2018-05-05
I want to INSERT these two dates from the 'TeachingSaturdaysInSchoolYear' table into 'SchoolYearTeachingDays' table.
I am trying with this query:
INSERT INTO SchoolYearTeachingDays
SELECT aDate FROM TeachingSaturdaysInSchoolYear
;
but I get this error:
Error: UNIQUE constraint failed: SchoolYearTeachingDays.aDate
How this query of mine works and why I get this error? How do I accomplish my goal?
Best, Pal
You are violating unique constraint by inserting duplicate values into the table SchoolYearTeachingDays.
To fix the error put a not in condition so that dates which are already inserted won't get inserted again into SchoolYearTeachingDays table.
Example,
INSERT INTO SchoolYearTeachingDays
SELECT aDate FROM TeachingSaturdaysInSchoolYear T WHERE T.aDate NOT IN (SELECT S.aDate FROM SchoolYearTeachingDays S)

SQLite check for duplicate rows

I have an SQLite database for an art exhibition. In the table "exhibits" I have columns for the artwork ID, the exhibition space ID, a begin date, and an end date. The default value for "end date" is NULL.
Of course, the same artwork cannot be displayed in two different spaces at once. So I want to ensure that a new row with an artwork ID is not created unless all existing rows with that same artwork ID have a non-null end date.
Is there some kind of constraint, trigger, etc. that I can add to the table to ensure this?
I am not an expert on writing triggers for SQLite but something like this should work,
CREATE TRIGGER check_open_ended_exhibit BEFORE INSERT ON exhibits
BEGIN
SELECT RAISE(ABORT, "Open ended exhibit exists")
WHERE EXISTS(SELECT * FROM exhibits WHERE artworkID = NEW.artworkID AND enddate IS NULL);
END
According to your information “Artwork” cannot be displayed twice in the same show which means the EndTime is a unique field when constraining it together with Artwork. So by making these two together your constrain you won’t be able to insert a record if you already have “artwork and NULL”.
So yeah you can just create a unique constrain on these two columns.
CREATE TABLE testConstrain (
id INTEGER NOT NULL,
endDate DATETIME
)
CREATE UNIQUE INDEX testConstrain
ON testConstrain(id, endDate);
INSERT INTO testConstrain VALUES('1',null)
INSERT INTO testConstrain VALUES('2','01-01-2018')
INSERT INTO testConstrain VALUES('1','01-01-2018')
INSERT INTO testConstrain VALUES('1',null)
`
And you will get:
Started executing query at Line 11
(1 row affected)
(1 row affected)
(1 row affected)
Msg 2601, Level 14, State 1, Line 4
Cannot insert duplicate key row in object 'bginsburg.testConstrain' with unique index 'testConstrain'. The duplicate key value is (1, ).
The statement has been terminated.

update and insert triggers in sqlite

I want to have riggers that set the last_modified column automatically each time a row in updated or inserted.
Lets say I have an ID that is unique to each row.
This is my query:
CREATE TRIGGER insert_trigger
AFTER INSERT ON TABLE_NAME
BEGIN
update TABLE_NAME set last_modified =strftime('%Y-%m-%d %H:%M:%S:%s','now', 'localtime') where id = old.id;
END;
After creating this trigger, when I try to insert I get the error:
no such column: old.id
I can understand why I get this error, but how can I create a proper trigger?
When inserting, there is no old row.
To get the ID of the new row, use NEW.id.

sqlite Insert statement value contains select clause : is it possible

I want to insert the records that time want to get the visitNo from other table & execute.only one fileds.
We can do it in separate separate query(that means getting visitNo from table & passing to other(insert) query).But I want to do it one query.without writting two query.
INSERT INTO wmVisitHeader (VisitNumber, ExecutiveCode, BusinessUnit, RouteCode, StartTime, UploadedBy, VisitDate, TerritoryCode)
VALUES(Select NextVisitNo from WMTransactionControl,'TEST001','MASS','VRT002','11:15' ,'TEST001','8/25/11 11:15' ,'0001')
like this ... i want...
Try this:
INSERT INTO wmVisitHeader (VisitNumber, ExecutiveCode, BusinessUnit, RouteCode, StartTime, UploadedBy, VisitDate, TerritoryCode)
VALUES((Select NextVisitNo from WMTransactionControl),'TEST001','MASS','VRT002','11:15' ,'TEST001','8/25/11 11:15' ,'0001')

Resources