bulk insert with default constraint - constraints

I have two table tblPerson and tblGender
the coulmn 'GenderID'in tblPerson is a foriegn key for the coulmn 'ID'(primary) in the tblGender
here is a default contarint
Alter Table tblPerson
Add constraint DF_tblPerson_GenderID
Default 4 for GENDERID
now the insert query is
insert into tblPerson (ID,[Name],EmailID,GenderID)values
(4,'Riya','riya#.com',2),
(5,'Raj','raj#.com',3),
(6,'Tisha','tisha#.com',2),
(7,'Stephen','stephen#.com'); // here im getting the error
now as you can see in the 7 I'm trying to add nothing in the genderid column and i thought it will be considered as a default constraint
but it is giving me error 'The number of columns for each row in a table value constructor must be the same.'

This is a T-SQL (SQL Server) specific answer. You should tag your question with the actual RDBMS. Your code looks like T-SQL with the [ and ] but who knows....
Since you can't change the column list, you need to use the default keyword to trigger a default.
INSERT INTO tblPerson (ID, [Name], EmailID, GenderID) VALUES
(4, 'Riya', 'riya#.com', 2),
(5, 'Raj', 'raj#.com', 3),
(6, 'Tisha', 'tisha#.com', 2),
(7, 'Stephen', 'Stephen#.com',default)
See here for an example. This is a T-SQL answer for a SQLLite question.
How to insert default values in SQL table?
I can't find a clear example of this in the T-SQL docs. This has a very vague description
https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-ver16

Related

Cost of inserting into a table in an arbitrary position using the INT primary key in SQLite3

I have a question on the mechanism used when we insert some item into table in arbitrary position with an INT primary key. I know it will use binary search to execute SELECT and WHERE with rowid or INTEGER primary key. What I want to know is is the query plan use same binary search when it insert with primary key.
The following is the example.
CREATE TABLE carBrand
(
brandID INTEGER PRIMARY KEY,
branName TEXT
);
INSERT INTO carBrand VALUES (2, 'BMW');
INSERT INTO carBrand VALUES (3, 'KIA');
INSERT INTO carBrand VALUES (5, 'FORD');
INSERT INTO carBrand VALUES (8, 'AUDI');
After these commands, what method will the query plan use if I do like
INSERT INTO carBrand VALUES (4, 'AUDI');
Thanks a lot.
Primary key columns in SQLite are backed by a B tree index. So the cost of inserting (4, 'AUDI') into your table carBrand is the cost of restructuring the primary key index. In theory, a B tree insert will generally be logarithmic. In practice, in a database context, a given insert might be slower for a number of reasons. For example, if a block has no more room, the database might have to search around nearby for other space to write the record, or, in a worse case, maybe even move around entire blocks.
SQLite would just insert it the same way it did the prior inserts. It doesn't do out of order inserts any different than it does in order ones.

SQLite Insert into a table with one column that is auto incrament

This should be an easy one. I need the SQL to insert into a table that has only one column and it is and autoincrement field.
Similar to this post but SQLite (I am new to SQLite).
Inserting rows into a table with one IDENTITY column only
create table ConnectorIDs
(
ID integer primary key AUTOINCREMENT
);
--none of the following work
INSERT INTO ConnectorIDs VALUES(DEFAULT);
INSERT ConnectorIDs DEFAULT VALUES;
Yes this is strange and if you care here is the reason, if you want to tell me a better way. I have several different item tables that all can have many-to-many links between them but sparse. Instead of having n! bridge tables, or one bridge table with a "Type" that I can't guarantee truly maps to the correct table. I will have one ConnectorID table and each item with have a connectorID key. Then I can have one bridge table.
Insert a null value:
INSERT INTO ConnectorIDs VALUES(NULL);
From the docs:
If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically.

Can Sqlite's AUTOINCREMENT/Primary Key be started at a number other than 1?

I would like to start different tables off at different values for their primary keys during testing to verify I don't have any bugs in my code. Is this possible in Sqlite?
As documented, the last value of an AUTOINCREMENT column is stored in the internal sqlite_sequence table, where it can be changed.
Yes, you can do that.
The simplest way is probably just to insert a row, and specify the the number. If I wanted to start with 1000, I might do something like this.
sqlite> create table test (test_id integer primary key, s char(1));
sqlite> insert into test values (999, 'a');
sqlite> insert into test (s) values ('b');
sqlite> select * from test;
999|a
1000|b
After you've inserted the "first" row (test_id is 1000), you can delete the "seed" row (test_id is 999).

SQLITE - INSERT or UPDATE without changing ROWID value

I need to update a table row IF EXISTS, otherwise INSERT a new row.
I tried:
INSERT OR REPLACE INTO table VALUES ...
but if the row row exist this statement changes the row's ROWID, and that's what I'm trying to avoid (I need the rowid :D)
I also tried to find a way to get some sort of return value from the update, in the case where an update has taken place, but I still don't understand how... If I could get the return value from the update statement, I could choose wether to proceed with an insert or not.
Do you have any suggestion or solution to this problem? Or do I need to make a copy of the ROWID and use that instead of the "pure" table ROWID?
Thanks in advance, best regards
ps: I was looking HERE and I was wondering if sqlite has the OUTPUT special word too, but google didn't help me..
---- EDIT after reading comments:
table schema example
CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id)
)
INSERT or REPLACE INTO test (table_id, some_field) VALUES ("foo","bar")
I tested Chris suggestion but the rowid still gets changed. I think the best alternative is to do a SELECT to see if a row with that key already exist. If so, UPDATE, otherwise, INSERT... good old fashion but guaranteed to work.
Combine it with select, like this
INSERT or REPLACE INTO test (ROWID, table_id, some_field)
VALUES ((SELECT ROWID from test WHERE table_id = 'foo' UNION SELECT max(ROWID) + 1 from test limit 1), 'foo','bar')
You need to specify that your table_id is unique in addition to being the primary key:
sqlite> CREATE TABLE test (
table_id TEXT NOT NULL,
some_field TEXT NOT NULL,
PRIMARY KEY(table_id),
UNIQUE(table_id)
);
sqlite> insert or replace into test values("xyz", "other");
sqlite> select * FROM test;
xyz|other
sqlite> insert or replace into test values("abc", "something");
sqlite> insert or replace into test values("xyz", "whatever");
sqlite> select * FROM test;
abc|something
xyz|whatever
From version 3.24.0 (2018-06-04), SQLite now supports an UPSERT clause that will do exactly what the OP needed: https://www.sqlite.org/lang_UPSERT.html
The insert would now look like this:
INSERT INTO test (table_id, some_field) VALUES ("foo","baz")
ON CONFLICT(table_id) DO UPDATE SET some_field=excluded.some_field;

SQLite - How to insert to table with preliminary scan of the fields?

I use a database in my project and when i insert values ​​into a table i need to check if the field already has a value that does not produce an insert.
for exemple:
INSERT INTO myTable (column1) values ('some_value1')
if some_value1 alredy exists in column1 do not insert the value.
Put a unique constraint on myTable.column1. Then, whenever you try to insert a duplicate value, it won't let you as it violates the constraint. You can either catch and handle this error, or just let the DB engine do it's thing automatically.
EDIT: Note that SQLite doesn't allow you to do many alterations to your table, once it's created; so you may have to recreate your table with the constraint in place.
I believe this can be handled using the conflict resolution IGNORE method on SQLite. The code below should do the trick. The column1 here should be set to unique for this.
INSERT OR IGNORE INTO myTable (column1) values ('some_value1')
I'm using the following links for reference;
http://www.sqlite.org/lang_insert.html
http://www.sqlite.org/lang_conflict.html

Resources