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. Clear database contents

Clear database contents

Scheduled Pinned Locked Moved Database
csharpdatabasehelpquestion
10 Posts 3 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.
  • Z Offline
    Z Offline
    Zoltan Balazs
    wrote on last edited by
    #1

    I'm curios what's the best option to clear a database that contains a large number of tables? Do we need to pick every table and issue delete statements (except those that have cascade rule set)? Does anybody had any similar issue?

    company, work and everything else @ netis

    D M 2 Replies Last reply
    0
    • Z Zoltan Balazs

      I'm curios what's the best option to clear a database that contains a large number of tables? Do we need to pick every table and issue delete statements (except those that have cascade rule set)? Does anybody had any similar issue?

      company, work and everything else @ netis

      D Offline
      D Offline
      Dr_X
      wrote on last edited by
      #2

      Another way is simply to extract the schema and create a new database from it.

      I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

      1 Reply Last reply
      0
      • Z Zoltan Balazs

        I'm curios what's the best option to clear a database that contains a large number of tables? Do we need to pick every table and issue delete statements (except those that have cascade rule set)? Does anybody had any similar issue?

        company, work and everything else @ netis

        M Offline
        M Offline
        Member 96
        wrote on last edited by
        #3

        There are three ways I would approach this: The cleanest and fastest way if it's a database that contains a lot of data is to issue a drop statement against the entire database. I.E. remove the database completely and then recreate it. The advantage of this is that it's clean and fast if you have to empty a database that already has a lot of data in it. The disadvantage is that you have to recreate the database from code, but it's usually a good idea to have code do that in any case which makes installations etc easier. The second way depends on the database server itself many SQL servers have some kind of syntax for quickly emptying tables. In MS SQL server you can use something like "truncate table" (oracle too I think) it will delete much faster because it won't log each row deletion, however it will still respect referential integrity so if you have foreign keys etc you will have to either a) Be careful what order you remove the tables in always removing the ones at the outermost "branches" of the relationships first or b) drop the relationship then recreate it after you've truncated. The third way is to issue delete statements against tables with the same caveats about referential integrity as the second way, but it's much slower than using truncate table. In my own software I use the second *and* third way because for the areas that need to clean out the datbase they need to keep some of the data in some of the tables. And I have to support Firebird as well as mssql which doesn't have a truncate table command. If this weren't the case I'd go with option 1 every time, nothing is faster or cleaner than that.

        Z 1 Reply Last reply
        0
        • M Member 96

          There are three ways I would approach this: The cleanest and fastest way if it's a database that contains a lot of data is to issue a drop statement against the entire database. I.E. remove the database completely and then recreate it. The advantage of this is that it's clean and fast if you have to empty a database that already has a lot of data in it. The disadvantage is that you have to recreate the database from code, but it's usually a good idea to have code do that in any case which makes installations etc easier. The second way depends on the database server itself many SQL servers have some kind of syntax for quickly emptying tables. In MS SQL server you can use something like "truncate table" (oracle too I think) it will delete much faster because it won't log each row deletion, however it will still respect referential integrity so if you have foreign keys etc you will have to either a) Be careful what order you remove the tables in always removing the ones at the outermost "branches" of the relationships first or b) drop the relationship then recreate it after you've truncated. The third way is to issue delete statements against tables with the same caveats about referential integrity as the second way, but it's much slower than using truncate table. In my own software I use the second *and* third way because for the areas that need to clean out the datbase they need to keep some of the data in some of the tables. And I have to support Firebird as well as mssql which doesn't have a truncate table command. If this weren't the case I'd go with option 1 every time, nothing is faster or cleaner than that.

          Z Offline
          Z Offline
          Zoltan Balazs
          wrote on last edited by
          #4

          I'm using Firebird and I also need to keep (in some cases) data in some of the tables. I did go with the third option, however it is just getting more and more complicated :( (and I don't know if it's a good idea to reset the generators also)

          company, work and everything else @ netis

          M 1 Reply Last reply
          0
          • Z Zoltan Balazs

            I'm using Firebird and I also need to keep (in some cases) data in some of the tables. I did go with the third option, however it is just getting more and more complicated :( (and I don't know if it's a good idea to reset the generators also)

            company, work and everything else @ netis

            M Offline
            M Offline
            Member 96
            wrote on last edited by
            #5

            Resetting the generators depends on a lot of things: Let's say you are using them for a number that is visible to the user of the program and may print on reports etc. If they erase the data and start again it's possible that they might have old documents with the old number on them, in which case it's a bad idea to reset it as it won't be unique. If they are only used internally and not to any of the unerased data then it's not a problem I don't use generators for unique record ID's in Firebird because I support more than one type of database and my app generates it's own unique GUID values for records so in Firebird I just use the GUID's instead. This stuff is always complicated, I have hundreds of tables in my app with some linked 4 deep. If it was easy they wouldn't need anyone to write software. ;)

            Z 1 Reply Last reply
            0
            • M Member 96

              Resetting the generators depends on a lot of things: Let's say you are using them for a number that is visible to the user of the program and may print on reports etc. If they erase the data and start again it's possible that they might have old documents with the old number on them, in which case it's a bad idea to reset it as it won't be unique. If they are only used internally and not to any of the unerased data then it's not a problem I don't use generators for unique record ID's in Firebird because I support more than one type of database and my app generates it's own unique GUID values for records so in Firebird I just use the GUID's instead. This stuff is always complicated, I have hundreds of tables in my app with some linked 4 deep. If it was easy they wouldn't need anyone to write software. ;)

              Z Offline
              Z Offline
              Zoltan Balazs
              wrote on last edited by
              #6

              John Cardinal wrote:

              This stuff is always complicated, I have hundreds of tables in my app with some linked 4 deep. If it was easy they wouldn't need anyone to write software.

              Exactly :) I guess I'm going on with the 3rd option ;)

              company, work and everything else @ netis

              M 1 Reply Last reply
              0
              • Z Zoltan Balazs

                John Cardinal wrote:

                This stuff is always complicated, I have hundreds of tables in my app with some linked 4 deep. If it was easy they wouldn't need anyone to write software.

                Exactly :) I guess I'm going on with the 3rd option ;)

                company, work and everything else @ netis

                M Offline
                M Offline
                Member 96
                wrote on last edited by
                #7

                For my code for the 3rd option I just made a helper method to delete the tables with the option of passing a generic List of ID's, then I can just pass that method a table name, it takes care of emptying it and optionally a list with one or more id's which it then constructs into a different format delete to keep the records id'd. I know it's messy and not really programming to have to keep track of and work with the database schema, but there's no getting around it. You really do have to take the time to determine the correct order to delete those tables and always keep it in mind when adding new tables etc. It's easy to add a new table or feature to a program and then find out later from the end user that the erase database function is now broken. It helps to put it all in one place, the code that creates the schema updates and the code that does the table deletion and also make sure you have a schema level version number in your database itself so that you can handle properly database schema versionsing between your code and the database. This means you can then put delete and table creation blocks in regions flagged with the db schema version number. It means nothing at first release, but it's critical down the road for updates.

                Z 1 Reply Last reply
                0
                • M Member 96

                  For my code for the 3rd option I just made a helper method to delete the tables with the option of passing a generic List of ID's, then I can just pass that method a table name, it takes care of emptying it and optionally a list with one or more id's which it then constructs into a different format delete to keep the records id'd. I know it's messy and not really programming to have to keep track of and work with the database schema, but there's no getting around it. You really do have to take the time to determine the correct order to delete those tables and always keep it in mind when adding new tables etc. It's easy to add a new table or feature to a program and then find out later from the end user that the erase database function is now broken. It helps to put it all in one place, the code that creates the schema updates and the code that does the table deletion and also make sure you have a schema level version number in your database itself so that you can handle properly database schema versionsing between your code and the database. This means you can then put delete and table creation blocks in regions flagged with the db schema version number. It means nothing at first release, but it's critical down the road for updates.

                  Z Offline
                  Z Offline
                  Zoltan Balazs
                  wrote on last edited by
                  #8

                  John Cardinal wrote:

                  a schema level version number

                  I have, but I store only the last version of the DB.

                  John Cardinal wrote:

                  It means nothing at first release, but it's critical down the road for updates.

                  story of my life :laugh: ...

                  company, work and everything else @ netis

                  M 1 Reply Last reply
                  0
                  • Z Zoltan Balazs

                    John Cardinal wrote:

                    a schema level version number

                    I have, but I store only the last version of the DB.

                    John Cardinal wrote:

                    It means nothing at first release, but it's critical down the road for updates.

                    story of my life :laugh: ...

                    company, work and everything else @ netis

                    M Offline
                    M Offline
                    Member 96
                    wrote on last edited by
                    #9

                    Zoltan Balazs wrote:

                    I have, but I store only the last version of the DB.

                    That's fine, that's what I do. When my user logs in, the required db schema set in my app is checked against the db's stored schema number. If the db has a higher number my app exits with a warning that it's out of date. If the db has a lower number my app triggers a schema update that goes through a big switch statement and executes each newer schema level update until it matches the program's required version.

                    Z 1 Reply Last reply
                    0
                    • M Member 96

                      Zoltan Balazs wrote:

                      I have, but I store only the last version of the DB.

                      That's fine, that's what I do. When my user logs in, the required db schema set in my app is checked against the db's stored schema number. If the db has a higher number my app exits with a warning that it's out of date. If the db has a lower number my app triggers a schema update that goes through a big switch statement and executes each newer schema level update until it matches the program's required version.

                      Z Offline
                      Z Offline
                      Zoltan Balazs
                      wrote on last edited by
                      #10

                      Thanks, you've given a couple of ideas :)

                      company, work and everything else @ netis

                      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