Reputation: 1391
I’m currently feeding data from an API to my PostgreSQL database via SQLalchemy.
I run my script every night and append the new data to the relevant tables in the database.
Now, how do I ensure that I’m not adding duplicate rows to my database?
I’m currently using pandas and the pd.to_sql() method to create the tables and append the new data using the if_exists=“append” keyword in the pd.to_sql() method.
Upvotes: 1
Views: 311
Reputation: 2003
You can use unique constraint in your table as below, the “Unique” constraint is a very powerful table-level constraint that you can apply to your table against a chosen table column, which can greatly assist to prevent duplicates in your data.
While creating a new table
USE TestDB;
GO
CREATE TABLE Users
(
UserID int NOT NULL IDENTITY(1,1),
Name varchar(100) NOT NULL,
Surname varchar(100) NOT NULL,
EmailAddress varchar(100) NOT NULL,
CONSTRAINT AK_UnqueEmail UNIQUE(EmailAddress)
);
GO
For existing Table
USE TestDB;
GO
ALTER TABLE Users
ADD CONSTRAINT AK_UniqueEmail UNIQUE (EmailAddress);
GO
Upvotes: 1