简体   繁体   中英

Database data Integrity check using Python SqlAlchemy or Sql Query

I imported and will keep importing data from different sources into sql server database. Some logic check such as sum of one column should be in 5 dollars difference with one amount in another table. It is hard for me to check this logic when importing because some data will be manually inserted, some imported using Excel VBA or python. Data Accuracy is very important to me. I am thinking to check after the data inserted. I am thinking two choices

  1. Python with SqlAlchemy and write the logic in python
  2. Create a stored procedure or direct SQL to verify

What will be advantages and disadvantages of SQLAlchemy vs stored procedure for data check? or other solutions?

The Benefit in my mind for SQLAlchemy with automap:

  1. Possible combination use of Jupyter for nice user interface
  2. Logic is easier to write, such as loop one table each row should be sum of another table with some where conditions.

Benefit for SQL stored procedure

  1. Can all be managed in SQL server management studio

The answer is neither. Most RDBMS have built in mechanisms to enforce restrictions on the data that is inserted into a row or a column. As you said

It is hard for me to check this logic when importing because some data will be manually inserted, some imported using Excel VBA or python. Data Accuracy is very important to me

You can't have your code in all these places. What works is constraints .

CHECK constraints enforce domain integrity by limiting the values that are accepted by one or more columns. You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators

You can of course use stored procedures for this but constraints are more efficient, transparent and easier to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM