Database design for database-agnostic applications

后端 未结 17 1713
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 14:15

What do I have to consider in database design for a new application which should be able to support the most common relational database systems (SQL Server, MyS

相关标签:
17条回答
  • 2021-02-02 14:47

    The short answer is to stick to features that are standardly, or close to standardly implemented. What this means in more detail is:

    • Avoid anything that uses the database's procedural language (stored procedures or triggers) since this is where the huge differences between the systems come in. You may need to use them to emulate some features, but don't use them to create your own functionality.

    • Separate auto-increment fields' sequences from the fields themselves. This will look a bit forced for MSSQL but will implement cleanly in Oracle, DB/2 etc without needing any emulation fixes.

    • Keep char and varchar fields below the smallest maximum size for the set of engines you're aiming at.

    • When you're writing queries use full JOIN syntax, and bracket the JOINs so that each join is between a single table and bracketed expression.

    • Keep date handling logic in the code, not the queries, since a lot of the date functions are outside the standard. (For example: if you want to get stuff for the past two weeks calculate the date two weeks ago in code and use that in the query.)

    Beyond that the effort involved shouldn't be too intimidating, so it may well be worth it.

    0 讨论(0)
  • 2021-02-02 14:49

    In 2001, I worked on a product that had to support Oracle 8, MS SQL Server 2000 and MS Jet 3.51 (a.k.a. Access97). In theory we could have employed specialists for each of these products and a testing process that ensured all produced the same results. In practice, there was a tendency towards the lowest common denominator.

    One approach was to create linked tables in Access/Jet for Oracle and SQL Server then exclusively write Jet SQL. The problem here is that Jet SQL syntax is very limited.

    Another approach (commonly employed even on systems which have only ever used one DBMS product!) is to try to do more of the work that one really ought to in the front end, things which should be the domain of the DBMS. The problem here is it is often disastrous as regards data integrity. I'm sure you know the situation: the application should refrain from writing illegal data but without constraints in the DBMS itself it is wide open to application bugs. And then there are the user who know how to connect to the data via Excel, SQL Management Studio, etc, and thereby totally bypassing the application that is supposed to ensure data integrity...

    Personally, I found myself increasingly writing code on a sliding scale of what I only later discovered was called 'portability'. Ideally, in the first instance is 'vanilla' code understood by all DBMSs we supported and in doing so I discovered the SQL-89 and SQL-92 Standards. Next was SQL code that could easily be translated (perhaps using code) for each DBMS e.g. Oracle used that horrid infixed outer join syntax but the concept of an outer join was there; Oracle and SQL Server used SUBSTRING but Jet required the keyword to be MID$; etc.. Finally, there are things that simply have to be implementation specific, obviously avoided if at all possible while still paying due regard to data integrity, functionality and performance.

    Happily, in the intervening years the products have been moving closer to the ANSI SQL Standards (apart from Jet, which was deprecated by MS is now only kept alive by the MS Access team seemingly by cutting major features such as security and replication). So I've kept the habit of writing Standard SQL where possible.

    0 讨论(0)
  • 2021-02-02 14:54
    1. Don't use stored procedures
    2. Don't use vendor specific SQL

    Or, use a persistence technology such as hibernate / nHibernate that abstracts away the differences between different DBs.

    0 讨论(0)
  • 2021-02-02 14:58

    Research up front the lowest common denominator for data types. For example, SQL Server has an integer but Oracle uses a number.

    0 讨论(0)
  • 2021-02-02 15:01

    If I were you, I'd think hard about the return on your investment here.

    It always sounds like a great idea to be able to hook up to any back end or to change back ends whenever you like, but this very rarely happens in The Real World in my experience.

    It might turn out that you may cover 95% of your potential customers by supporting just Oracle & SQL Server (or MySQL & SQL Server, or... etc.).

    Do your research before going any further, and good luck!

    0 讨论(0)
提交回复
热议问题