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
108
votes
10 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
  • 14,263
  • 36
  • 103
  • 152
106
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,815
  • 23
  • 67
  • 90
106
votes
4 answers

SQL Error: ORA-01861: literal does not match format string 01861

I am trying to insert data into an existing table and keep receiving an error. INSERT INTO Patient ( PatientNo, PatientFirstName, PatientLastName, PatientStreetAddress, PatientTown, PatientCounty, PatientPostcode, DOB, Gender, …
LizzyPooh
  • 1,155
  • 2
  • 9
  • 12
103
votes
6 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
  • 1,039
  • 2
  • 7
  • 3
100
votes
2 answers

PostgreSql INSERT FROM SELECT RETURNING ID

In PostgreSql 9.2.4 I have two tables: user (id, login, password, name) and dealer (id, user_id). And I want to insert into both tables returning id of created dealer. Currently I'm doing it with two queries: WITH rows AS ( INSERT INTO "user" …
Nailgun
  • 3,999
  • 4
  • 31
  • 46
89
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
  • 33,434
  • 32
  • 105
  • 133
77
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
  • 1,003
  • 1
  • 8
  • 11
76
votes
3 answers

Conditional INSERT INTO statement in postgres

I'm writing a booking procedure for a mock airline booking database and what I really want to do is something like this: IF EXISTS (SELECT * FROM LeadCustomer WHERE FirstName = 'John' AND Surname = 'Smith') THEN INSERT INTO LeadCustomer…
The General
  • 1,239
  • 2
  • 17
  • 28
73
votes
3 answers

SQL Insert into table only if record doesn't exist

I want to run a set of queries to insert some data into an SQL table but only if the record satisfying certain criteria are met. The table has 4 fields: id (primary), fund_id, date and price I have 3 fields in the query: fund_id, date and price. So…
harryg
  • 23,311
  • 45
  • 125
  • 198
64
votes
6 answers

No results returned by the Query error in PostgreSQL

I am trying to insert a data into a table. After executing the query i am getting an exception stating org.postgresql.util.PSQLException: No results were returned by the…
Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46
60
votes
5 answers

ROWID INTEGER PRIMARY KEY AUTOINCREMENT - How to insert values?

I created an SQLite table in Java: create table participants (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, col1,col2); I tried to add rows : insert into participants values ("bla","blub"); Error: java.sql.SQLException: table participants has 3…
Anthea
  • 3,741
  • 5
  • 40
  • 64
56
votes
7 answers

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this? I've tried INSERT INTO table…
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
56
votes
3 answers

How does COPY work and why is it so much faster than INSERT?

Today I spent my day improving the performance of my Python script which pushes data into my Postgres database. I was previously inserting records as such: query = "INSERT INTO my_table (a,b,c ... ) VALUES (%s, %s, %s ...)"; for d in data: …
turnip
  • 2,246
  • 5
  • 30
  • 58
52
votes
2 answers

Is there any way to show progress on a `gunzip < database.sql.gz | mysql ...` process?

Once a week I need to run a giant database update into my local development environment like so: $ gunzip < /path/to/database1.sql.gz | mysql -uUSER -p database1 & $ gunzip < /path/to/database2.sql.gz | mysql -uUSER -p database2 & $ gunzip <…
Ryan
  • 14,682
  • 32
  • 106
  • 179
44
votes
1 answer

Cannot INSERT: ERROR: array value must start with "{" or dimension information

INSERT INTO user_data.user_data (username,randomint) VALUES ('mahman',1); ERROR: array value must start with "{" or dimension information LINE 1: ... user_data.user_data (username,randomint) VALUES ('mahman... INSERT INTO user_data.user_data…
Noob Doob
  • 1,757
  • 3
  • 19
  • 27