Search code examples
sqldatabasedatabase-designidentifier

How can one check an identifier for unescaped validity in most RDBMSs?


I'd like my application to work on several databases. While I'm not going to explicitly support them right now, I'd like to be able to define my schemata such that I'm not using reserved identifiers. The reason? Identifier delimiters are not standard between databases. For instance, MySQL uses backticks (`) to quote identifiers, while MSSQL uses brackets ([]).

However, I'm not sure what subset of names are "safe". Is there an easy way to check this?


Solution

  • Overall I think this is not a good strategy, but to answer your specific question about identifiers, I expect you will need to determine them on a per-case basis if you want to avoid escaping completely.

    You could also consider using ANSI_QUOTES mode in MySQL - then all your identifiers for SQL Server, Oracle and MySQL could be quoted with double-quotes - the ANSI standard.

    For instance, SQL Server 2000 "Regular Indentifiers" are undelimited: http://msdn.microsoft.com/en-us/library/aa223962%28v=sql.80%29.aspx

    Same docs for SQL Server 2005: http://msdn.microsoft.com/en-us/library/ms175874%28v=sql.90%29.aspx

    SQL Server 2005 Reserved Word List: http://msdn.microsoft.com/en-us/library/ms189822%28v=SQL.90%29.aspx

    Equivalents for Oracle 10g: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements008.htm

    Reserved words in MySQL: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html