How to use a SQLite table's default values? - sqlite

I'm looking to insert data into a column like so:
INSERT INTO Song values (null, 'Truth of a Liar')
into a table set up like this:
CREATE TABLE Song (id integer primary key, name varchar(255), count int not null default 0)
and I get the following error: table Song has 3 columns but 2 values were supplied INSERT INTO Song2 values (null, 'Truth of a Liar')
I was expecting be able to not put in the last column because I occasionally might have to put in the count value. I'm aware that I could explicitly fill the columns like so:
INSERT INTO Song(id, name) values (null, 'msdads')
but I was hoping for an alternative.
Is there another way to INSERT data into tables, using some default values and some set values?

You could do:
INSERT INTO Song values (null, 'Truth of a Liar',0)
when you do not want to insert anything for last column. When you want to occasionally insert a value, you can use that value instead of 0.

Related

Inserting rows from table2 to end of table1 with unique ids

I'm trying to add a test user to my website that employers can look at to see my work. I want to use some of the data I have entered into my profile so that it is faster.
I have a workouts table:
CREATE TABLE workouts(
id INTEGER NOT NULL,
userID INTEGER NOT NULL,
DateAndTime smalldatetime NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (UserID) REFERENCES users(id)
);
I have taken 25 of the first results and put it into a temporary workouts2 table:
CREATE TABLE workouts2 (
userid integer,
dateandtime smalldatetime);
Now I want to take those rows from workouts2 and put them into workouts. I have tried to add them by inserting workouts2 into workouts like this:
insert into workouts (id , userID, DateandTime) values (select * from workouts2);
This gives me an Error: in prepare, near "select": syntax error (1)
I can do it one at a time like this:
insert into workouts (userid, dateandtime) values (2, "2022-01-02T06:00");
Doing it one at a time is not ideal.
What am I missing here? I know I have a syntax error but I don't know how to fix it.
I have looked at this question which inserts one at a time:
How to insert a unique ID into each SQLite row?
The problem is it only inserts one at a time.
You should use SELECT instead of VALUES and not include the column id, which is auto-incremented, in the list of columns of workouts which will receive the values (such a column does not exist in workouts2):
INSERT INTO workouts (userID, DateandTime)
SELECT *
FROM workouts2;

Multiple SQLite unique columns as one

Given the following example:
CREATE TABLE shapes(
shape_id INTEGER PRIMARY KEY,
background_color TEXT,
foreground_color TEXT,
UNIQUE(background_color,foreground_color)
);
background_color AND foreground_color need to be unique to insert but I don't want that. consider the row exists:
black|blue
and I try to insert:
white|blue
it will insert but if I try to insert another row containing:
black|blue
it will ignore the insert.
Will a primary key of the two cols accomplish this? If so, do I need to also use INSERT OR IGNORE?
First remove the UNIQUE constraint from the definition of the table.
What you can do is create a unique index not on the columns but on the min and max values of the 2 columns:
CREATE UNIQUE INDEX idx_shapes_colors ON shapes(
MIN(background_color, foreground_color),
MAX(background_color, foreground_color)
);
Then, when you try to insert the same combination of colors, like:
INSERT INTO shapes (background_color, foreground_color) VALUES ('black', 'blue');
INSERT INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
the 2nd statement will fail with an error message:
UNIQUE constraint failed: index 'idx_shapes_colors'
or, if you use INSERT OR IGNORE:
INSERT OR IGNORE INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
there will be no error but the statement will not insert the row.
See the demo.

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.

Will AutoIncrement work with Check constraints?

The question is simple:
In SQLite, if I choose to AutoIncrement a primary key of type NUMERIC which has a check constraint like CHECK(LENGTH(ID) == 10), will it work correctly inserting the first value as 0000000001 and so on?
No, that does not work. Adding a check does not magically also add a way of fullfilling the check to insert the data.
See this SQLFiddle.
If you want to restrict the value of an autoincrement column like that, you need to seed the internal sequence table. (There are other ways.)
create table foo (
foo_id integer primary key autoincrement,
other_columns char(1) default 'x',
check (length(foo_id) = 10 )
);
insert into sqlite_sequence values ('foo', 999999999);
Application code is allowed to modify the sqlite_sequence table, to
add new rows, to delete rows, or to modify existing rows.
Source
insert into foo (other_columns) values ('a');
select * from foo;
1000000000|a
Trying to insert 11 digits makes the CHECK constraint fail.
insert into foo values (12345678901, 'a');
Error: CHECK constraint failed: foo
One alternative is to insert a "fake" row with the first valid id number immediately after creating the table. Then delete it.
create table foo(...);
insert into foo values (1000000000, 'a');
delete from foo;
Now you can insert normally.
insert into foo (other_columns) values ('b');
select * from foo;
1000000001|b
In fact the ID's length is 1, so it doesn't work.

SQLite - How to define Default value of column in Create Table statement taking reference of other column value

I an using SQLite database and wanted to create a table like following:
Create Table EMPLOYEE (
[FirstName] [varchar](50),
[LastName] [varchar](50),
[FullName] [varchar](150) AS ([FirstName] + ' ' + [LastName])
);
Can it is possible to have Default value of any column (like FullName) referring to values of other columns present in same table.
UPDATED:
I tried forming CREATE TABLE statement using these IFNULL and COALESCE function but every time its failing. How should i write such CREATE TABLE statement which will have computed column. If such Computed column in CREATE statement is not supported then how should i achieve such requirement. Can anybody please give me an example
The DEFAULT value expression must be a constant expression and referring to other column values makes it non-constant.
You can use IFNULL() or COALESCE() when selecting the data to concatenate other column values in case value is null. For example:
SELECT IFNULL(fullname, firstname || ' ' || lastname) ...

Resources