Let the dbms do it for you. The specific manner is, alas, db-specific, since the sql standard does not include anything on this subject, but every real dbms provides such a function. With mysql, it's simply a matter of adding the 'auto_increment' attribute to the column definition. With postgresql, you can use a column type of 'serial'. And so forth.
There is never a problem of duplicate id's using this capability, though of course you can have gaps in the sequence if an INSERT fails or a row is subsequently deleted (but that would be true of any method of generating ID's).