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

How get the Id of inserted row in SQL

My database table has ID column which is an auto incremental integer. While inserting data to the table its increment automatically I need to get the value of ID when a record is inserted. Eg: table employee = |ID| NAME | ADDRESS| the query is…
Azad
  • 5,144
  • 4
  • 28
  • 56
9
votes
1 answer

Unexpected INSERT ... SET query behavior

I have a table where I need to store two id's from another table. While doing some debugging I noticed some strange behavior of SQL. Example of wrong sql: INSERT INTO follower_list set `followerUserId` = '3' AND `followingUserid` = '4' The above…
ashofphoenix
  • 123
  • 1
  • 6
9
votes
3 answers

MySQL - insert if doesn't exist yet

I want to execute this MySQL query: INSERT INTO `cron-stats` (`user`) VALUES (".(int)$d['by-user'].") Whenever such user doesn't exist yet, as in: SELECT 1 FROM `cron-stats` WHERE `user` = ".(int)$d['by-user']." How can I execute this in one query?
Frantisek
  • 7,485
  • 15
  • 59
  • 102
8
votes
1 answer

How to insert json data into postgres database table

I m a beginner and trying to insert JSON values into the database using a tutorial I have created the table using the following command CREATE TABLE table_name( id character varying(50), data json NOT NULL, …
DRV
  • 676
  • 1
  • 8
  • 22
8
votes
2 answers

Why CREATE TABLE AS SELECT is more faster than INSERT with SELECT

I make a query with INNER JOIN and the result was 12 millions lines. I like to put this in a table. I did some tests and when I created the table using clause AS SELECT was more faster than, create the table first and run a INSERT with SELECT…
Fernando Delago
  • 105
  • 1
  • 2
  • 8
8
votes
4 answers

Insert values statement can contain only constant literal values or variable references in SQL Data Warehouse

Consider this table: CREATE TABLE t (i int, j int, ...); I want to insert data into a table from a set of SELECT statements. The simplified version of my query is: INSERT INTO t VALUES ((SELECT 1), (SELECT 2), ...); The real query can be much more…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
8
votes
1 answer

sql insert to table realted to another table and constant values

I have 2 SQL tables, table1 and table2. table1 has two columns and I want to insert values to these columns. one of the columns should get a static value and the other column should get a value that is a result of a query from table2. If I wanted to…
Ortal Blumenfeld Lagziel
  • 2,415
  • 3
  • 23
  • 33
8
votes
2 answers

POSTGRESQL :INSERT INTO ... SELECT with auto generated column

I have two tables A(id, col1, col2) and B(col3, col4, col5, col6) the column "id" in table A is an auto generated, not null, primary key. To insert data from table B to table A, I am trying INSERT INTO A(col1, col2) (SELECT col3, col4 from B) This…
Somil Bhandari
  • 173
  • 2
  • 3
  • 12
8
votes
3 answers

Trigger on insert and update that adds modification date

I'm making a simple table with names, emails etc, but I also have a ModifiedDate. My idea is to use a trigger after both insert and update, and insert the current date. Thus if anyone does anything (except delete) to that column, the date should…
Kalec
  • 2,681
  • 9
  • 30
  • 49
8
votes
2 answers

Postgres Insert Into View Rule with Returning Clause

I am attempting to allow insert statements with a returning clause into a view in Postgres v9.4, but am struggling with the syntax. This is how I want to call the insert statement: CREATE VIEW MyView AS SELECT a.*, b.someCol1 FROM tableA a JOIN…
Jeff G
  • 4,470
  • 2
  • 41
  • 76
8
votes
2 answers

How to get next ID upon INSERT?

I want to make sure the ID column is incremented for every insert on a table. I tried this statement: INSERT INTO Anlagenteil (ID, TaId, Subtype, Name) VALUES (MAX(ID)+1, 0, 'BdAnlageteil', 'Barcodeleser0'); Unfortunately I get…
BetaRide
  • 16,207
  • 29
  • 99
  • 177
8
votes
2 answers

How to transfer Data automaticly between MySql and Sql Server back and fourth?

I need to build a procedure to read data from SQL Server and insert it into MySql. More I also need another procedure to read data from MySql Server and insert it into SQL Server. I never done such a thing. I can't think of a way to get it done. Can…
Mike
  • 2,735
  • 11
  • 44
  • 68
8
votes
2 answers

Insert in RMySQL from data frame

Im trying to add data to MySQL table by using RMySQL. I only need to add one row at a time and it's not working. What I'm trying to do is this. dbGetQuery(con,"INSERT INTO names VALUES(data[1,1], data[1,2])") so what I'm doing is that I have values…
TheLaama
  • 307
  • 2
  • 4
  • 12
7
votes
2 answers

Postgres LEFT JOIN is creating more rows than in left table

I am running Postgres 9.1.3 32-bit on Windows 7 x64. (Have to use 32 bit because there is no Windows PostGIS release compatible with 64 bit Postgres.) (EDIT: As of PostGIS 2.0, it is compatible with Postgres 64 bit on windows.) I have a query that…
Aren Cambre
  • 6,540
  • 9
  • 30
  • 36
7
votes
1 answer

How to deal with MySQL WorkBench bit(1) bug when it puts the 'b' literal before non-bit values?

Let's say I have a table 'day_attribute_type' id bigint(20) AI PK code varchar(255) persist_date bigint(20) update_date bigint(20) active bit(1) max int(11) min int(11) name varchar(255) If I would add a new row into the Result Grid and…
Iaroslav Baranov
  • 1,100
  • 9
  • 17