MySQL InnoDB: autoincrement non-primary key

Yes you can. You just need to make that column be an index. CREATE TABLE `test` ( `testID` int(11) NOT NULL, `string` varchar(45) DEFAULT NULL, `testInc` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`testID`), KEY `testInc` (`testInc`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; insert into test( testID, string ) values ( 1, ‘Hello’ ); insert into test( … Read more

Entity Framework 4: How to find the primary key?

So I was finally able to find out how to get this to work. I wish I hadn’t lost the link to the blog I read last night as I didn’t write the code. public T GetByPrimaryKey<T>(int id) where T : class { return (T)_db.GetObjectByKey(new EntityKey(_db.DefaultContainerName + “.” + this.GetEntityName<T>(), GetPrimaryKeyInfo<T>().Name, id)); } string GetEntityName<T>() … Read more

Hibernate ID Generator

A cursory search of Google for ‘hibernate custom id generator tutorial’ turned up the following possibilities. I’ve excluded those that don’t look useful and summarized the content of each. http://www.devx.com/Java/Article/30396 – covers the issues of generating an ID before the data is persisted (and hence does not yet have a business key). http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#mapping-declaration – the … Read more

How to add an auto-incrementing primary key to an existing table, in PostgreSQL?

(Updated – Thanks to the people who commented) Modern Versions of PostgreSQL Suppose you have a table named test1, to which you want to add an auto-incrementing, primary-key id (surrogate) column. The following command should be sufficient in recent versions of PostgreSQL: ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY; Older Versions of PostgreSQL … Read more