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
1068
votes
11 answers

How can I do 'insert if not exists' in MySQL?

I started by googling and found the article How to write INSERT if NOT EXISTS queries in standard SQL which talks about mutex tables. I have a table with ~14 million records. If I want to add more data in the same format, is there a way to ensure…
warren
  • 32,620
  • 21
  • 85
  • 124
653
votes
22 answers

Postgres: INSERT if does not exist already

I'm using Python to write to a postgres database: sql_string = "INSERT INTO hundred (name,name_slug,status) VALUES (" sql_string += hundred + ", '" + hundred_slug + "', " + status + ");" cursor.execute(sql_string) But because some of my rows are…
AP257
  • 89,519
  • 86
  • 202
  • 261
473
votes
13 answers

How to insert a value that contains an apostrophe (single quote)?

What is the correct SQL syntax to insert a value with an apostrophe in it? Insert into Person (First, Last) Values 'Joe', 'O'Brien' I keep getting an error as I think the apostrophe after the O is the ending tag for the value.
leora
  • 188,729
  • 360
  • 878
  • 1,366
399
votes
13 answers

SQL Server Insert if not exists

I want to insert data into my table, but insert only data that doesn't already exist in my database. Here is my code: ALTER PROCEDURE [dbo].[EmailsRecebidosInsert] (@_DE nvarchar(50), @_ASSUNTO nvarchar(50), @_DATA nvarchar(30) ) AS BEGIN …
369
votes
9 answers

Best way to do multi-row insert in Oracle?

I'm looking for a good way to perform multi-row inserts into an Oracle 9 database. The following works in MySQL but doesn't seem to be supported in Oracle. INSERT INTO TMP_DIM_EXCH_RT (EXCH_WH_KEY, EXCH_NAT_KEY, EXCH_DATE, EXCH_RATE, …
jamz
  • 4,991
  • 4
  • 24
  • 19
350
votes
12 answers

What's the fastest way to do a bulk insert into Postgres?

I need to programmatically insert tens of millions of records into a Postgres database. Presently, I'm executing thousands of insert statements in a single query. Is there a better way to do this, some bulk insert statement I do not know about?
Ash
  • 24,276
  • 34
  • 107
  • 152
349
votes
5 answers

Inserting multiple rows in mysql

Is the database query faster if I insert multiple rows at once: like INSERT.... UNION INSERT.... UNION (I need to insert like 2-3000 rows)
Emma
  • 3,539
  • 3
  • 16
  • 5
285
votes
7 answers

How to speed up insertion performance in PostgreSQL

I am testing Postgres insertion performance. I have a table with one column with number as its data type. There is an index on it as well. I filled the database up using this query: insert into aNumber (id) values (564),(43536),(34560) ... I…
Luke101
  • 63,072
  • 85
  • 231
  • 359
237
votes
4 answers

"Insert if not exists" statement in SQLite

I have an SQLite database. I am trying to insert values (users_id, lessoninfo_id) in table bookmarks, only if both do not exist before in a row. INSERT INTO bookmarks(users_id,lessoninfo_id) VALUES( (SELECT _id FROM Users WHERE…
user2780638
  • 2,503
  • 2
  • 13
  • 8
159
votes
7 answers

How do I insert multiple values into a postgres table at once?

I have a table that I am trying to update multiple values at once. Here is the table schema: Column | Type | Modifiers ---------------+---------+----------- user_id | integer | subservice_id | integer | I have the user_id and…
jhamm
  • 24,124
  • 39
  • 105
  • 179
134
votes
11 answers

Avoid duplicates in INSERT INTO SELECT query in SQL Server

I have the following two tables: Table1 ---------- ID Name 1 A 2 B 3 C Table2 ---------- ID Name 1 Z I need to insert data from Table1 to Table2. I can use the following syntax: INSERT INTO Table2(Id, Name) SELECT Id, Name FROM…
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
124
votes
6 answers

Can I use return value of INSERT...RETURNING in another INSERT?

Is something like this possible? INSERT INTO Table2 (val) VALUES ((INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id)); like using the return value as value to insert a row in a second table with a reference to the first table?
Eike Cochu
  • 3,249
  • 7
  • 34
  • 57
118
votes
4 answers

Insert data in 3 tables at a time using Postgres

I want to insert data into 3 tables with a single query. My tables looks like below: CREATE TABLE sample ( id bigserial PRIMARY KEY, lastname varchar(20), firstname varchar(20) ); CREATE TABLE sample1( user_id bigserial…
Faisal
  • 1,469
  • 2
  • 13
  • 25
113
votes
11 answers

Why I am getting Cannot pass parameter 2 by reference error when I am using bindParam with a constant value?

I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname=' . DB . ';host=' . HOST, USER, PASS); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND,…
Ignacio
  • 7,947
  • 15
  • 63
  • 74
111
votes
10 answers

INSERT vs INSERT INTO

I have been working with T-SQL in MS SQL for some time now and somehow whenever I have to insert data into a table I tend to use syntax: INSERT INTO myTable I understand that keyword INTO is optional here and I do not have to use…
kristof
  • 52,923
  • 24
  • 87
  • 110
1
2 3
99 100