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
24
votes
5 answers

How can I insert the return of DELETE into INSERT in postgresql?

I am trying to delete a row from one table and insert it with some additional data into another. I know this can be done in two separate commands, one to delete and another to insert into the new table. However I am trying to combine them and it is…
lanrat
  • 4,344
  • 10
  • 35
  • 41
24
votes
3 answers

How to improve SQLite insert performance in Python 3.6?

Background I would like to insert 1-million records to SQLite using Python. I tried a number of ways to improve it but it is still not so satisfied. The database load file to memory using 0.23 second (search pass below) but SQLite 1.77 second to…
etoricky
  • 631
  • 1
  • 7
  • 10
23
votes
2 answers

Seed data with old dates in Temporal Table - SQL Server

I need to seed data for my local development purpose in the following Temporal Table, the start date should be old. The given Table Schema is CREATE TABLE [dbo].[Contact]( [ContactID] [uniqueidentifier] NOT NULL, [ContactNumber]…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
23
votes
3 answers

INSERT INTO fails with node-mysql

I am trying to insert some data with node.js. I have written the following code and installed MySQL support via npm, but I failed to INSERT INTO the table. Here is my code: var mysql = require('mysql'); function BD() { var connection =…
rokirakan78
  • 251
  • 1
  • 2
  • 9
23
votes
3 answers

mysql: insert record if not exists else return the id of record

i need to insert unique values in a table, and need the ids of records, need to insert those id's in relationship table, need the query to insert the record if not exists return the inset id, if exists return the record primary key(id). and i want…
Abuzer Firdousi
  • 1,552
  • 1
  • 10
  • 25
23
votes
5 answers

Insert query check if record exists - If not, Insert it

I have a mysql table ip_list... +----+---------------+ | id | ip_addr | +----+---------------+ | 1 | 192.168.100.1 | | 2 | 192.168.100.2 | | 3 | 192.168.100.3 | | 4 | 192.168.100.4 | | 5 | 192.168.100.5 | +----+---------------+ I want to…
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
22
votes
2 answers

Postgres INSERT ON CONFLICT DO UPDATE vs INSERT or UPDATE

I have stock_price_alert table with 3 columns. stock_price_id is PRIMARY KEY & also FOREIGN KEY to other table. Table definition as below: create table stock_price_alert ( stock_price_id integer references stock_price (id) on delete cascade not…
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
22
votes
2 answers

MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields

From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE statement to specify behavior when values inserted (with INSERT or SET or VALUES) are already in destination table w.r.t. PRIMARY KEY or some UNIQUE field. If value for PRIMARY…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
21
votes
2 answers

postgresql ON CONFLICT ON CONSTRAINT for 2 constraints

In am currently working with PostgreSQL 9.5 and was wondering if the is a possibility to include names of 2 constraints in the ON CONFLICT ON CONSTRAINT statement. My sql is below INSERT INTO LIVE.TABLE (column1, column2, column3) SELECT DISTINCT ON…
Sky21.86
  • 627
  • 2
  • 9
  • 26
21
votes
2 answers

How to ignore duplicate keys when extracting data using OPENQUERY while joining two tables?

I am trying to insert records into MySQL database from a MS SQL Server using the "OPENQUERY" but what I am trying to do is ignore the duplicate keys messages. so when the query run into a duplicate then ignore it and keep going. What ideas can I do…
Jaylen
  • 39,043
  • 40
  • 128
  • 221
21
votes
3 answers

Insert Null into SQLite3 in Python

I am trying to insert a None value into a row entry of my db. The table present exists db.execute("INSERT INTO present VALUES('test', ?, 9)", "This is a test!") db.execute("INSERT INTO present VALUES('test2', ?, 10)", None) but I get an…
moesef
  • 4,641
  • 16
  • 51
  • 68
20
votes
1 answer

Insert into table from temporary table

I have the following table: Example: create table test ( col1 varchar(10), col2 varchar(20), col3 varchar(30) ); Now I want to insert two values by variables and last one by #temp table. #Temp: create table #temp ( col3 varchar(30) ); #Temp:…
MAK
  • 6,824
  • 25
  • 74
  • 131
19
votes
4 answers

What is the best way to insert multiple rows in PHP PDO MYSQL?

Say, we have multiple rows to be inserted in a table: $rows = [(1,2,3), (4,5,6), (7,8,9) ... ] //[ array of values ]; Using PDO: $sql = "insert into `table_name` (col1, col2, col3) values (?, ?, ?)" ; Now, how should you proceed in inserting the…
user3330840
  • 6,143
  • 7
  • 26
  • 39
18
votes
2 answers

Passing a record as function argument PL/pgSQL

First I am really new to pl/pgsql. Need it for a project. I am stuck with this (simplified) problem. My db schema has a n to m relationship (author, books, author_books) Now I want to have a pl/psgsql function insert_book. (I do know that all…
taranaki
  • 778
  • 3
  • 9
  • 28
17
votes
2 answers

Postgres Insert without ANY VALUES FOR COLUMNS. ALL ARE DEFAULT

I have a table in Postgres that only has default column values (id, created_at). The only way I can insert into this table is INSERT INTO pages(id) VALUES(DEFAULT) RETURNING id; Why can't I do this: INSERT INTO pages RETURNING id; Just curious.
b.lyte
  • 6,518
  • 4
  • 40
  • 51