Questions tagged [sql-insert]

The SQL INSERT statement allows you to insert a single or multiple rows into a table.

SQL INSERT statement adds one or more rows to a single table in a relational database.

The basic INSERT values syntax is:

INSERT INTO table [ (column1 [, column2, column3 ... ]) ]
VALUES (value1 [, value2, value3 ... ]);

The INSERT SELECT syntax is:

INSERT INTO table [ (column1 [, column2, column3 ... ]) ]
SELECT c1 [, c2, c3 ... ] FROM table

Reference

For questions regarding SQL INSERT statement use this tag instead of .

5071 questions
17
votes
1 answer

Is there a non-commercial alternative to Z.EntityFramework.Extensions?

Entity Framework can be very slow on mass insert/update/delete operations. Even the often suggested tweaks to turn off AutoDetectChanges and/or ValidateOnSaveEnabled does not always help. I have come across the Z.EntityFramework.Extensions on NuGet,…
Michael
  • 938
  • 1
  • 10
  • 34
17
votes
2 answers

SQL INSERT INTO with subquery and value

Is there a way I can use a combination of hard values and a subquery to insert into a table with one command? For example: INSERT INTO suppliers (supplier_id, supplier_name, supplier_type) SELECT account_no, name FROM customers WHERE city = 'San…
Jaiesh_bhai
  • 1,778
  • 8
  • 26
  • 41
16
votes
8 answers

MySQL, how to insert null dates

I am having trouble inserting null values into date fields into a MySQL table. Here is the insert query: $query = 'INSERT INTO table (column_s1, column_s2, column_d1, column_d2) VALUES ("'.$string1.'", "'.$string2.'", '.$date1.',…
ajor
  • 1,592
  • 7
  • 22
  • 40
15
votes
2 answers

android.database.sqlite.SQLiteConstraintException: error code 19: constraint failedexception

I created a table named resources but when I insert values in it, this exception is thrown: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failedexception Here is my create table statement: public static final String…
ekjyot
  • 2,247
  • 7
  • 39
  • 63
15
votes
1 answer

PostgreSQL ON CONFLICT with a WHERE clause

Postgres documentation makes it seem like a WHERE clause is possible as an ON CONFLICT condition: https://www.postgresql.org/docs/9.5/static/sql-insert.html I have not been able to get this working (if it's possible). Here is one of the many…
smeckydev
  • 433
  • 2
  • 4
  • 13
15
votes
1 answer

Can INSERT [...] ON CONFLICT be used for foreign key violations?

Given => select * from referenced; referenced_id | name ---------------+------- 1 | one 2 | two 3 | three and => select * from entries; entry_id | referenced_id | name …
peterwimsey
  • 532
  • 2
  • 6
  • 17
15
votes
4 answers

SQLiteDatabase: Insert only if the value does not exist (not via raw SQL command)

I know there's an SQL command that goes like this: IF NOT EXISTS, but since Android's SQLiteDatabase class has some fine methods, I was wondering if it's possible to insert a value if it doesn't exist via a method. Currently I'm using this to insert…
gosr
  • 4,593
  • 9
  • 46
  • 82
15
votes
1 answer

How can I insert multiple row into oracle database using one insert statement?

Possible Duplicate: Best way to do multi-row insert in Oracle? I have this insert statement INSERT INTO mytable VALUES ('val1', 'val2'), ('aa', 'cc'), ('ww', 'dd'); and I got this error ORA-00933: SQL command not properly ended
Moataz Aahmed Mohammed
  • 1,307
  • 5
  • 22
  • 33
15
votes
2 answers

REPLACE INTO, does it re-use the PRIMARY KEY?

The REPLACE INTO function in MySQL works in such a way that it deletes and inserts the row. In my table, the primary key (id) is auto-incremented, so I was expecting it to delete and then insert a table with id at the tail of the database. However,…
matt
  • 2,857
  • 7
  • 33
  • 58
14
votes
2 answers

how to have postgres ignore inserts with a duplicate key but keep going

I am inserting record data in a collection in memory into postgres and want the database to ignore any record that already exists in the database (by virtue of having the same primary key) but keep going with the rest of my inserts. I'm using…
THX1137
  • 903
  • 6
  • 15
14
votes
3 answers

INSERT INTO ... FROM SELECT ... RETURNING id mappings

I'm using PostgreSQL 9.3. I want to duplicate some of the db records. Since I'm using an auto-increment pk id for the table, I want to get back the id mappings from the generated ids of duplicated records to the original ones. For example, say I…
fengye87
  • 2,433
  • 4
  • 24
  • 41
14
votes
2 answers

Reference value of serial column in another column during same INSERT

I have a table with a SERIAL primary key, and also an ltree column, whose value I want to be the concatenation of those primary keys. e.g. id | path ---------- 1 1 2 1.2 3 1.2.3 4 1.4 5 1.5 I'm curious if there's a way to do such an…
Aaron Fi
  • 10,116
  • 13
  • 66
  • 91
13
votes
5 answers

Efficiently duplicate some rows in PostgreSQL table

I have PostgreSQL 9 database that uses auto-incrementing integers as primary keys. I want to duplicate some of the rows in a table (based on some filter criteria), while changing one or two values, i.e. copy all column values, except for the ID…
EMP
  • 59,148
  • 53
  • 164
  • 220
13
votes
1 answer

SQL (postgres) RETURNING data from multiple tables after an INSERT INTO / UPDATE

Is it possible in postgres to return data from multiple tables after 1) An INSERT INTO, and 2) an UPDATE? For example, supposing the following contrived example: review fit ------ ---- id id fit_id …
craigmichaelmartin
  • 6,091
  • 1
  • 21
  • 25
13
votes
2 answers

How to insert a new line ("\n") character in SQLite?

While trying to insert something like: "Hello\nWorld" SQLite throws error something like: Message: unrecognized token: "'Hello";" (also few other errors) Even though I convert above string to "Hello''\nWorld" or "Hello\"\n\"World", these escape…
iammilind
  • 68,093
  • 33
  • 169
  • 336