Constraints and the test-driven database (constraints and test-driven database)

Source: Internet
Author: User

From:

Http://www.simple-talk.com/ SQL /database-administration/constraints-and-the-test-driven-database/

Constraints and the test-driven database14 December 2011by
Sam bendayan

Bad Data always seems to appear when, and where, one least expects it. sam explains the great value of a defensive approach based on constraints to any team that is developing an application in which the data has to be exactly right, and where bad data cocould
Cause consequential severe financial damage. It is perhaps better seen as creating a test-driven Database

Automatic unit testing for database development has recently been promoted via the Agile programming methodologies. Although test-driven database development is an excellent discipline to adopt, it shocould be considered into the well-known practice
Of using database constraints. by using constraints, we already have the means to create test-driven databases. database constraints are, essential, just tests. they are tests of data integrity. they also conform to agile testing practices in the sense that
They are usually designed first, before you write a single line of code, in a similar way to "test-first development ".

Database constraints offer enormous value to any development project. opting not to use them is equivalent to deciding not to test the output of your code.

Why are constraints important?

By definition, a "Database constraint" is something which restricts the possible values which the database will accept. constraints are declarative, which means that you declare them as rules that must be adhered to by the entire system you are developing.
They are also bound to the database structure and fire automatically when needed, which makes them difficult to violate. any violation of these constraints results in a "hard failure", which returns errors to the application and forces the application-developers
To deal with the issue immediately.

Database constraints are a valuable by-product of the practice of identifying and clarifying beforehand those data rules that you need to follow before you develop the rest of your application. these rules depend on the nature of the business being supported
By the application. they cocould be as trivial as checking the validity of an email address or zip code or as complex as checking for compliance with IRS Tax Rules. this process encourages the team to come to a consensus about what the business rules mean, which
Is surely one of the most demanding parts of any IT project.

If you can enshrine these rules in constraints, the database will alert you if you happen to violate any of these business rules in the heat of the development effort, and make you fix the issue before it gets out to the customer.

Large software teams in complex projects are faced with the daunting task of ensuring that everyone who is involved in the project has a clear comprehensive idea of all the rules that the system must adhere to throughout the development cycle. with Constraints
In place, this task becomes largely unnecessary because database constraints provide a safety net which gives the development team the confidence that they cocould not unwittingly violate any business rules.

So what are constraints?

There are several different types of database constraints:

  1. Primary Key & unique constraints-Enforce uniqueness of rows in a table
  2. Foreign key constraints-Enforce referential integrity ("lookup" values)
  3. Datatypes-Enforce proper data type values (date, number, String, etc .)
  4. Nullability Constraints-Determine whether a value is mandatory or not.
  5. Check Constraints-More Flexible Constraints that can be bound to a column or table and can be used to enforce a wider variety of constraints that cannot be enforced in any of the above constraints. however, the SQL that can be used in
    These is limited to smaller expressions.
  6. Triggers-The most flexible form of constraint. These can only be bound to a table but are free to use the full power of SQL, almost like a stored procedure.

What about stored procedures? Although they certainly aren't constraints, these are often used to perform data validation tasks that are impossible to implement by a constraint. they are not bound to the database design, which makes them easy to use "after
The fact "when you realize that the database design is faulty and you have to keep fit data validations into it. for the purposes of this article I will not consider these as constraints because they do not fire automatically.

So let's take a simple example. you are building a database for a retail seller and you have an orders table which stores the orders placed by your MERs. in that table you have an order date column. when thinking about the database design, you decide
That you will not allow orders that have an order-date in the future, so you write a constraint that doesn't allow future dates to be put into this column. in SQL Server, this rule will take the form of a "Check constraint" and will look something like this:

Alter table dbo. Orders add constraint chk_orders_date check (orderdate <= getdate ())

With this one simple line of code in your database, you can now stop worrying about this rule. it will not be possible to enter future-dated orders. any code trying to do so will 'errorout' and you will be forced to fix it immediately. you can actually
Declare to your MERs (or anyone else) that your application follows this rule and be completely confident in your statement!

Are automatic unit tests sufficient?

Why not just use automatic unit tests? Well, you shoshould be using automatic unit tests, but they will not replace database constraints. Database constraints have a special contribution to the effort to reduce defects in software.

  1. Constraints Fire Automatically, all the time whenever the application is used, so you can't get around them. automatic tests fire on demand only, so if you don't run the test suites they can't find any bugs.
  2. Constraints are shipped out to mers MERs and run on production environments, as part of the product, whereas automatic tests are usually only run on development and test environments.
  3. Constraints focus solely on the data, whereas automatic tests will more likely focus on an entire unit of code (hence the name automatic "unit" tests ). therefore, unit tests have a much wider area to cover and will not be able to focus on data as much
    Constraints will. This is why it's a good idea to use both constraints and automated unit tests. They have different areas of focus.
  4. Constraints have "positive" logic; they define what "good data" is and everything else that falls outside of this is automatically considered to be "bad" data. because of this it is not necessary to find all the different scenarios which might cause bad
    Data. all you need to define is the one (or few) scenarios that constitute "good" data. automated tests have opposite type of logic; they try to break the application by defining all the scenarios that may be considered "bad ". in my experience there are unknown,
    Faster more such scenarios, and deletests are initially left out of these test suites initially because it is very difficult to come up with all the "bad" scenarios that your MERs are going to think.
  5. Constraints don't need any additional hardware or separate suites of code to function. constraints typically have only a few lines of code as opposed to automated unit test suites which can be huge, and require you to install new technologies and be trained
    In their programming ages. finally, you have to dedicate environments to run these test suites and wait until they are finished, which can be anywhere from minutes to hours. database constraints don't have any of these problems; they run within your existing
    Code base so no additional hardware or technologies are necessary.

You can get around database constraints by dropping them, but in the production environment your MERs wocould not drop DLLs or web pages, and likewise shouldn't drop constraints. if any type of test fails then either the test was wrong or there is a bug
In the application. to drop a failing test is a way of "shooting the messenger" and ignoring the true cause of the error. A well-worded SLA will keep MERs from dropping constraints if you declare up front that dropping database constraints will invalidate
The support agreement.

Constraints don't necessarily slow the database down. while it is true that constraints have to fire as part of the application, they are designed to do this and are very efficient at it. constraints can actually speed up the database because the SQL query
Optimizer has access to these constraints and can use them when it decides how to optimize queries. let's take the example from above, where we were not going to allow any orders with ure dates. if someone then runs a query looking for future dated orders,
The database knows that it is not possible to have that data in the database and immediately returns an empty result set, without even trying to run the query.

The impact of database constraints on the development process 'beating all the points to death'

As with any rigorous test regime, implementing database constraints has a profound effect on a software project's lifecycle. testing an application is impossible without a sound definition of how an application shocould function. to implement constraints,
The software project has to start with a painstaking database design process, during which the customer sees no obvious progress. no screens or web pages are produced during this phase, and the more inexperienced team members can become impatient. considerable
Pressure can be put on development teams during this phase to start 'cutting Code '.

All those design debates are necessary because the team will spot issues that customers will bring up later on in development if you don't fix them now. this is the perfect time to argue, since the design only exists as a drawing; if the design is inadequate,
All you have to do is erase it from the board! No code needs to be refactored, and no MERs are impacted. thus it makes sense to fully argue and "beat all of the points to death" during this phase. in addition to this, it is extremely important to choose
The right people for the design phase, supported by a project manager that understands software design. it is the quality of the people involved that will ensure you don't end up running around in circles, which is a common pitfall of such design phases.

The constraints shocould be preserved

Application developers get frustrated with database constraints very quickly because the constraints constantly get in their way. this is understandable; developers want to get their job done as fast as possible, and since limit of them are typically not
Involved in the design of the constraints they don't understand why the constraints have to be there in the first place. management needs to facilitate at this point, just as the construction foreman serves as the final arbiter between different parts of
Construction project. A describe to drop the constraints of a database is difficult to reverse later on, and the longer you wait the harder it is to justify going back and redoing everything.

Once the application is in production, and data errors do happen, the team may need to make sure that the constraint wasn't too restrictive, needing to be adjusted to make it more flexible. however, if the constraint is correct and the application has
Bug that wasn't caught in testing then that bug needs to be fixed. Whatever is decided, no data needs fixing because the bad data never got into the database in the first place!

However, if a constraint is missing and bad data does get in, it is considered a failure on the part of the requirements team and then the application bug and the data have to be fixed. however, even in such a case this data fix is much more unlikely
Be harmful because there are so secure other constraints in the database which will prevent the data fix from damaging any surrounding data.

What if you don't use constraints?

So what happens if you decide not to use database constraints? At first, management is happy with your progress as they see screens or web pages appear quickly in your demos and they even see them working. All is well, and when the product is considered
"Done" it is tested and shipped out to the customer. Then the Troubles start.

Customers will find unexpected and creative ways to use your software and start putting bad data in. customers might also import data into your application by going straight to the database, thereby bypassing all of your front end and middle tier data validations,
And even the ones in stored procedures, because they don't fire automatically. this is why stored procedures aren't able to implement real constraints. so the company starts to hire support extensionals to fix these issues as they come up but, over time,
The data issues increase; you add more MERs, the product gets more complex, new developers are hired that don't have all the business rules inside their head, and usually there is not enough documentation to give them. so developers violate the business
Rules, as do support people when they have to fix production data in a high-pressure environment. the database, of course, says nothing about all these violations, since it doesn' t have the constraints. and what do companies do at this point? In my experience
They accept this situation as the normal cost of doing business! Everyone complains about how bad the database is, but usually no one does anything about it because at this point it is too late.

In addition to the above, bugs have to be dealt with wrong times instead of just once, and the "fixes" to the data will likely end up damaging the surrounding data, causing more bugs. I have seen cases where entire departments of support people are hired
To deal with these data issues. they end up developing entire libraries of data fix scripts to be run in production, and they run those fixes on a daily or hourly manner. usually they have someone that manages these script libraries as well, resulting in
Type of "parallel development effort", just to fix the data bugs that the Development Department allowed in the product because they didn't use database constraints!

Conclusion

Good software design of database applications is very difficult to achieve without using database constraints. development projects are very complex and one must have separation of concerns in order to make the project manageable. one of the best ways
Have this is to research the business rules first and put them in the database in a way that they cannot be subverted. later on in the project there are too things to worry about; the development of the UI and middle tiers, any frameworks that have to be
Developed, and, yes, there are even more data validation rules that take place only at the middle tier and UI. you don't need to add the issue of database validation rules to this long list of problems that the development team has to deal with during
Development phase.

I have observed first-hand the cost of deciding not to use database constraints in database design; it results in frustration and confusion all the way from management to the MERs. costs explode as the product scales and the customer base grows. while
Automatic unit tests are another weapon in the fight for Application quality, they are truly valid tive only when combined with proper database design with constraints. although the process of getting the design right and resilient against incorrect data
The early phases of design and development is painful and its benefits are difficult to measure, everyone will be happier with the result in the long run.

This article has beenviewed 847 times.

Author profile:
Sam bendayan

Sam is a database professional with 15 years 'experience in database development and architecture. he currently works at ultimate software as a database named ECT for ultipro, an industry-leading payroll/hr (HCM) application. he is a big fan of Data Modelling
And likes to get involved in the entire spectrum of database development activity, from the requirements gathering phase all the way through development, testing and deployment. outside of work he has Ds most of my time with his wife and 3 children and tries
To head to the outdoors whenever he gets a chance. He is also a big fan of traveling.

Search for other articles by Sam bendayan

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.