Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sqlite insert if not exists

#id column is assumed to be primary key

INSERT INTO destination_table(id,name) 
SELECT id, name
FROM source_table s
WHERE NOT EXISTS (
  SELECT 1
  FROM destination_table d
  WHERE d.id = s.id
);
Comment

sqlite insert if not exists

--- If you never want to have duplicates, you should declare this as a table constraint: ---

CREATE TABLE bookmarks(
    users_id INTEGER,
    lessoninfo_id INTEGER,
    UNIQUE(users_id, lessoninfo_id)
);

---(a primary key over both columns would have the same effect)

	It is then possible to tell the database that you want to ignore if the insert didn't work---
    
    INSERT OR IGNORE INTO bookmarks(users_id,lessoninfo_id) VALUES(123,456)
Comment

PREVIOUS NEXT
Code Example
Sql :: select last n rows mysql 
Sql :: delete from inner join sql 
Sql :: postgres how to add field created at 
Sql :: sqlalchemy get schema from database 
Sql :: mysql select latest entry by time 
Sql :: sql last time database was accessed 
Sql :: SQLITE_BUSY: database is locked 
Sql :: uninstall mysql ubuntu 18.04 stackoverflow 
Sql :: json_value oracle 
Sql :: mysql if else 
Sql :: oracle sql average 
Sql :: sub query postgres 
Sql :: add week ending date sql server 
Sql :: len sql 
Sql :: oracle show errors compilation 
Sql :: python sqlite3 search 
Sql :: postgres delete by id 
Sql :: postgres find missing indexes 
Sql :: SQL get last 5 minutes data 
Sql :: mysql grant user permissions 
Sql :: copy column from one table to another without column duplicate postgres 
Sql :: connecting to postgresql on windows amd ubuntu 20.04 
Sql :: BigQuery Remove Duplicate Keys From Table 
Sql :: how to get column name in db from an sqlalchemy attribute model 
Sql :: copy from one table to another postgres using matching column 
Sql :: how to move a column to different spot mysql 
Sql :: count sql 
Sql :: sql server delete records with specific date 
Sql :: if sql 
Sql :: unique sql 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =