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 .

4409 questions
929
votes
10 answers

How to 'insert if not exists' in MySQL?

I started by googling, and found this article 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 the record I want to insert does not already exist without…
warren
  • 28,486
  • 19
  • 80
  • 115
461
votes
18 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
  • 72,861
  • 84
  • 184
  • 258
375
votes
10 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
  • 163,579
  • 332
  • 834
  • 1,328
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
  • 2
  • 13
  • 5
298
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,213
  • 4
  • 21
  • 19
241
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
  • 56,845
  • 75
  • 204
  • 330
174
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
  • 1,873
  • 2
  • 11
  • 8
114
votes
10 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,016
  • 19
  • 66
  • 122
108
votes
9 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,…
Nacho
  • 7,544
  • 15
  • 57
  • 74
98
votes
15 answers

Saving timestamp in mysql table using php

I have a field in a MySQL table which has a timestamp data type. I am saving data into that table. But when I pass the timestamp (1299762201428) to the record, it automatically saves the value 0000-00-00 00:00:00 into that table. How can I store the…
gautamlakum
  • 11,035
  • 22
  • 61
  • 87
94
votes
9 answers

MySQL and PHP - insert NULL rather than empty string

I have a MySQL statement that inserts some variables into the database. I recently added 2 fields which are optional ($intLat, $intLng). Right now, if these values are not entered I pass along an empty string as a value. How do I pass an explicit…
user547794
  • 12,873
  • 32
  • 97
  • 147
86
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,139
  • 1
  • 9
  • 25
73
votes
5 answers

Column count doesn't match value count at row 1

So I read the other posts but this question is unique. So this SQL dump file has this as the last entry. INSERT INTO `wp_posts` VALUES(2781, 3, '2013-01-04 17:24:19', '2013-01-05 00:24:19'. I'm trying to insert this value to the table... INSERT…
user2705462
  • 739
  • 1
  • 5
  • 3
72
votes
2 answers

PHP MYSQL UPDATE if Exist or INSERT if not?

I have no idea if this is even remotely correct. I have a class where I would like to update the database if the fields currently exist or insert if they do not. The complication is that I am doing a joining 3 tables (set_colors, school_art,…
GGcupie
  • 955
  • 1
  • 7
  • 10
72
votes
15 answers

In SQL, is UPDATE always faster than DELETE+INSERT?

Say I have a simple table that has the following fields: ID: int, autoincremental (identity), primary key Name: varchar(50), unique, has unique index Tag: int I never use the ID field for lookup, because my application is always based on working…
Roee Adler
  • 31,157
  • 31
  • 99
  • 132
1
2 3
99 100