8
votes
Fortnightly Programming Q&A Thread
General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.
Don't forget to format your code using the triple backticks or tildes:
Here is my schema:
```sql
CREATE TABLE article_to_warehouse (
article_id INTEGER
, warehouse_id INTEGER
)
;
```
How do I add a `UNIQUE` constraint?
I use Python a lot for math and analytics, but I have 2 major gaps in my skill set
1)I don't really know much about creating my own custom visualizations and graphics. Like, I'm perfectly happy using matplotlib or bokeh or whatever else to use prebaked visualizations. But I wanted to make a completely custom animation or graphic to demonstrate a concept I'm basically in the dark. Does anyone know of a good primer on that subject?
2)The same goes for UI. I tend to leave my parameters as raw variables, or something similar to an .ini file. I would love to move toward something more user friendly, but don't really know where to start.
I primarily use Jupyter Notebook in Anaconda and would love any tips anyone has to offer
Is there a way in SQL (specifically PostgreSQL, if it matters) to insert a bunch of rows but skip certain ones if a specific column's value from that row already exists in the database? Essentially, I have a bunch of feeds (with
title
andurl
values) I want to import, but I want to skip certain ones when the URL already exists.I can think of some inefficient ways to do this in the code itself (basically just a loop and see if each URL exists one by one) but I'm curious if there's a simpler/more efficient way through SQL, since I imagine this isn't too rare of a scenario.
You could add an unique constraint on the
url
column and use theON CONFLICT
[1] modifier whenINSERT
ing(You can also perform an
UPSERT
operation using theON CONFLICT
modifier, i.e. update the row if a row exists already)For example:
Create a dummy table
Insert some initial dummy data
Trying to reinsert rows with same URL will fail and error out on the first error.
Using
ON CONFLICT
:That's exactly what I'm looking for, thanks!