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
41
votes
2 answers

How to insert data in postgresql using for loop?

I would like to know how to insert data in postgresql using for loop? I want to insert 1 to 1000 in row of id..
Pablo Job
  • 485
  • 1
  • 5
  • 8
40
votes
13 answers

"column not allowed here" error in INSERT statement

I created this table called LOCATION by doing this: CREATE TABLE LOCATION( POSTCODE VARCHAR(10) PRIMARY KEY, STREET_NAME VARCHAR(20), CITY VARCHAR(20)); and when I try to add some date within the table it doesn’t work saying there is an…
john
  • 419
  • 2
  • 6
  • 7
39
votes
2 answers

Equivalent of ON CONFLICT DO NOTHING for UPDATE postgres

I want to update rows in my postgres database if the updated version wouldn't violate the primary key constraint. If it would, I want to leave the row as it is. Assuming the table has primary keys on col1, col2, col3, if I run a query like…
BHC
  • 889
  • 1
  • 7
  • 18
39
votes
6 answers

Insert into multiple tables in one query

Assuming that I have two tables, names and phones, and I want to insert data from some input to the tables, in one query. How can it be done?
yossi
  • 3,090
  • 7
  • 45
  • 65
38
votes
7 answers

Postgres error: null value in column "id" - during insert operation

I use postgresql and yii2 framework. Well I got a very interesting error message: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, null, null, null, null, 1,…
Dabagab
  • 2,497
  • 4
  • 26
  • 31
37
votes
6 answers

What are differences between INSERT and UPDATE in MySQL?

It seems INSERT and UPDATE do the same things to me. Is there any occasions where I should use INSERT instead of UPDATE and vice versa?
shin
  • 31,901
  • 69
  • 184
  • 271
35
votes
9 answers

C# with MySQL INSERT parameters

Good day to all, I'm using Visual C# 2010 and MySQL Version 5.1.48-community. I hope you can help me with this code. I don't find it working on me. What am I missing? string connString =…
Boy Karton
  • 565
  • 3
  • 9
  • 13
34
votes
3 answers

ERROR: column of relation does not exist PostgreSQL ,Unable to run insert query

Hi I am trying to insert into a table tester3 it fails when i use the syntax insert into tester3 (UN0, UN1) values ( 1, 'jishnu1'); but insert into tester3 values ( 1, 'jishnu1'); is working fine. mydb=# CREATE TABLE tester3 mydb-# ( mydb(# …
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
33
votes
1 answer

select into a table with different column names

In SQL, Select into ... copies rows into a different (backup) table. Is this possible if the backup table has different structure (or different column names)? If not, what is the best way to achieve this? Here is what I want to do: TableA has…
zolio
  • 2,439
  • 4
  • 22
  • 34
30
votes
1 answer

MySQL - How to insert into table that has many-to-many relationship

I have a table of persons. Each person has a property and many persons may have a certain property. So this is a many-to-many relationship. This is the schema: CREATE TABLE persons ( person_id int(11) NOT NULL AUTO_INCREMENT, firstname…
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80
30
votes
1 answer

INSERT-OUTPUT including column from other table

I have a stored procedure that needs to insert into three different tables, but I need to get the ID generated from the one input and use that to insert into the next table. I'm familiar with the INSERT-OUTPUT construct, but I'm not sure how to go…
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
29
votes
1 answer

How to create and insert a JSON object using MySQL queries?

I'm trying to create a JSON object and then read the values from it into MySQL table. But I'm facing errors and I'm new to both JSON and MySQL. SET @j = '{"key1": "value1", "key2": "value2"}'; CREATE TABLE Person (name int,id int); INSERT INTO…
rj_23495
  • 293
  • 1
  • 3
  • 6
28
votes
4 answers

PostgreSQL INSERT into an array of enums

How can I insert an array of enums? Here is my enum: CREATE TYPE equipment AS ENUM ('projector','PAsystem','safe','PC','phone'); Then my table has an array of equipment: CREATE TABLE lecture_room ( id INTEGER DEFAULT NEXTVAL('lecture_id_seq') ,…
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
27
votes
1 answer

Generating sql insert into for DataGrip

When you run a query (a SELECT statement) in the console, the data retrieved from the database are shown in table format in the Result pane of the Database Console tool window. I've searched through datagrip Help and I'm just wondering if there…
Oxobo
  • 513
  • 1
  • 5
  • 13
27
votes
2 answers

In Oracle SQL: How do you insert the current date + time into a table?

I've written below code, but it only seems to insert the current date and not the current time. Anyone knows how to do that? insert into errortable (dateupdated,table1id) values (TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),1083);
Tikkaty
  • 772
  • 1
  • 8
  • 24