Considering the various implementations of the / auto -increment sequence, how can I create an SQL insert statement that will work across different rdbms?

I am trying to create a method for a class that automatically inserts a record into a table. The problem is that I need to get the code to work for mysql, mssql, postgresql, oracle and sqlite.

The only solutions I can think of are as follows:

  • Create my own primary key values, but this seems very risky, as it can be difficult to come up with a fault-tolerant solution for generating primary key values.

  • Create code that will switch the method used based on the rdbms driver used, but that seems messy, and I would like to use the SQL standard if possible.

How can I create this statement INSERTto work on all of these rdbms if they all use different methods to automatically increase the primary key field?

+5
source share
4 answers

Use specialized per-rdbms code.

Standard matching is good, but there are too many things that do not work in all databases. Starting with simple things like quoting fields and values, things like limiting results to a given number of lines and maybe hundreds of other cases.

+5
source

Net overall

; , UUID . - - , .

UUID, , INSERT ed - . SHA1 . . .

.

UUID , 2011 , , Facebook Google , , - , long.

- , , .

SQL Generic

INSERT, .

PHP , . , , , , API, JDBC PHP.

Java

boolean execute (String sql,               int autoGeneratedKeys) Statement. , , ResultSet getGeneratedKeys().

Oracle Specific

DEFAULT DDL , sequence, Oracle.

UUID GUID , .

+2

, : , ( "auto-increment" ).

, ( ). SQL .

EDITED: oook. , ( ):

, " " (mea culpa). , , ​​ (strigtforward Postgresql, Mysql, MSSQL -I think-SQLite, , - - Oracle), SQL INSERTS . ELSEWHERE, : rdbms INSERTS.

+2

Why aren't you using ORM? PHP has a ton of them: Doctrine, Redbean, and many others.

An ORM point is an abstraction of a database layer. It sounds the way you need it. Your method will process the objects in your code, and ORM has special code needed to store these objects in your database.

0
source

All Articles