Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Validation Rule In SQL Server 2005

Validation Rule In SQL Server 2005

Scheduled Pinned Locked Moved Database
databasesql-serverdesignsysadmintutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hussain attiya
    wrote on last edited by
    #1

    Hi, is there any way to write a validation rule for the field in the datbase design of sql server 2005. Like ms access. i tried to search the net but no luck. so i decided to write triggers on insert and update to validate my data. please, advice me if this is good approach. or if there is anything else we can do in the datbase design. (what i mean by validation rule is to for example for my numeric field i want the data to be in a specific range.)

    Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

    D M P N 4 Replies Last reply
    0
    • H hussain attiya

      Hi, is there any way to write a validation rule for the field in the datbase design of sql server 2005. Like ms access. i tried to search the net but no luck. so i decided to write triggers on insert and update to validate my data. please, advice me if this is good approach. or if there is anything else we can do in the datbase design. (what i mean by validation rule is to for example for my numeric field i want the data to be in a specific range.)

      Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      You can make use of check constraint on your table for this. Something like:

      Alter YourTableName
      check (intCol > 0 and intCol < 100)

      Although, you should try and filter out faulty data at the UI only. Why to hit database to know that it is a faulty data? It will make your application slow.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      H 1 Reply Last reply
      0
      • D dan sh

        You can make use of check constraint on your table for this. Something like:

        Alter YourTableName
        check (intCol > 0 and intCol < 100)

        Although, you should try and filter out faulty data at the UI only. Why to hit database to know that it is a faulty data? It will make your application slow.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        H Offline
        H Offline
        hussain attiya
        wrote on last edited by
        #3

        Thanks, oh yeah.. i forgot about the check :sigh: and you are right to validate the data in the UI. which i am aleady doing it. the reason why i want to do the validation in the database design level, is to make sure that we are adding the right data and also, to prevent the other developers from playing with the database. our database is shared one and we are using it many applications. thanks for your reply

        Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

        1 Reply Last reply
        0
        • H hussain attiya

          Hi, is there any way to write a validation rule for the field in the datbase design of sql server 2005. Like ms access. i tried to search the net but no luck. so i decided to write triggers on insert and update to validate my data. please, advice me if this is good approach. or if there is anything else we can do in the datbase design. (what i mean by validation rule is to for example for my numeric field i want the data to be in a specific range.)

          Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You need to think about what you are doing, what are you going to do when you get a validation fail, raise an error. Then what are you going to do, trap the error in your DAL/BusinessObject and pass it back to the UI. This is called PROGRAMMING BY ERROR and is the absolute worst validation model you could build. Put you validation is either the UI or the BusinessObject. Validation does not belong in the database.

          Never underestimate the power of human stupidity RAH

          H 1 Reply Last reply
          0
          • M Mycroft Holmes

            You need to think about what you are doing, what are you going to do when you get a validation fail, raise an error. Then what are you going to do, trap the error in your DAL/BusinessObject and pass it back to the UI. This is called PROGRAMMING BY ERROR and is the absolute worst validation model you could build. Put you validation is either the UI or the BusinessObject. Validation does not belong in the database.

            Never underestimate the power of human stupidity RAH

            H Offline
            H Offline
            hussain attiya
            wrote on last edited by
            #5

            Thanks for your reply. i agree with you. but for your information, i am not waiting to get the error from that database in order to catch it in my DAL/BusinessObject. i am already doing my validation also in the UI. what i really want to make sure is, the data are inserted right. i dont want to keep any chance for developers to play with the application UI to update the database. now you make me confused. because i am doing the database design. here is another question: if i consider the following in my database design: - primary keys - Forien Keys - Default Values - Check Constraint - indexes - Database Diagrams is it bad database design?

            Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

            M 1 Reply Last reply
            0
            • H hussain attiya

              Thanks for your reply. i agree with you. but for your information, i am not waiting to get the error from that database in order to catch it in my DAL/BusinessObject. i am already doing my validation also in the UI. what i really want to make sure is, the data are inserted right. i dont want to keep any chance for developers to play with the application UI to update the database. now you make me confused. because i am doing the database design. here is another question: if i consider the following in my database design: - primary keys - Forien Keys - Default Values - Check Constraint - indexes - Database Diagrams is it bad database design?

              Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Yah, I opened my (big mouth) reply before you had replied, got delayed and posted after you. What you are doing is correct and is the reason constraints are in the database, if you were doing this as the primary validation my argument would apply. The additional elements you raise are all valid and should be used for a well designed database, what you cannot do is expect and error. With a FK if you decided to try and write the record and trap the error for a FK violation as the primary validation it would be wrong. Having the FK to enforce data integrity is absolutely valid.

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • H hussain attiya

                Hi, is there any way to write a validation rule for the field in the datbase design of sql server 2005. Like ms access. i tried to search the net but no luck. so i decided to write triggers on insert and update to validate my data. please, advice me if this is good approach. or if there is anything else we can do in the datbase design. (what i mean by validation rule is to for example for my numeric field i want the data to be in a specific range.)

                Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

                P Offline
                P Offline
                puri keemti
                wrote on last edited by
                #7

                Many ways to do validation in database (1) Constraints (2) SP (3) Triggers

                1 Reply Last reply
                0
                • H hussain attiya

                  Hi, is there any way to write a validation rule for the field in the datbase design of sql server 2005. Like ms access. i tried to search the net but no luck. so i decided to write triggers on insert and update to validate my data. please, advice me if this is good approach. or if there is anything else we can do in the datbase design. (what i mean by validation rule is to for example for my numeric field i want the data to be in a specific range.)

                  Hussain Mohammed Saleh Attiya ISP Technical Manager Atyaf Telcom - Bahrain

                  N Offline
                  N Offline
                  Niladri_Biswas
                  wrote on last edited by
                  #8

                  Check Constaint [^] to help. Also you can use Rules that enforce data integrity. Check this out Creating Rules and Defaults[^] Just scroll down a bit in the page to get a cleaner picture about the Rules :)

                  Niladri Biswas

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups