primary-key

When not to use surrogate primary keys?

大城市里の小女人 提交于 2019-11-27 04:37:58
问题 I have several database tables that just contain a single column and very few rows, often just an ID of something defined in another system. These tables are then referenced with foreign keys from other tables. For example one table contains country codes (SE, DK, US etc). All values are always unique natural keys and they are used as primary keys in other (legacy) systems. It seems really unnecessary to introduce a new surrogate key to these tables, or? In general, what are the exceptional

Hibernate, alter identifier/primary key

依然范特西╮ 提交于 2019-11-27 04:37:50
I receive the following exception when I'm trying to alter my @ID in an @Entity . identifier of an instance of com.google.search.pagerank.ItemEntity was altered from 1 to 2. I know that I'm altering the primary key in my table. I'm using JPA-annotations. I solved this by using this single HQL query: update Table set name=:newName where name=:oldName Instead of using the more OO approach: beginTransaction(); T e = session.load(...); e.setName(newName); session.saveOrUdate(e); commit(); Any idea what the diff is? I can't imagine why you'd want to do that. At all. Why would you change an entity's

How to choose my primary key?

三世轮回 提交于 2019-11-27 04:37:36
问题 I found this reading material on choosing a primary key. Is there a guide / blog post on how to choose the primary key for a given table? Should I use a auto-incremented/generated key, or should I base the primary key on the data being modeled (assuming it has a truly unique field)? Should the primary key always be long for performance's sake, or can I take an external unique id as primary key, even if it's a string? 回答1: I believe that in practice using a natural key is rarely better than a

Hibernate ID Generator

梦想的初衷 提交于 2019-11-27 04:33:55
Anyone know of some good tutorials on how to create a custom ID generator for hibernate? paxdiablo 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 whole document is very useful but not so much a tutorial as

Can a foreign key act as a primary key?

浪子不回头ぞ 提交于 2019-11-27 04:28:22
问题 I'm currently designing a database structure for our team's project. I have this very question in mind currently: Is it possible to have a foreign key act as a primary key on another table? Here are some of the tables of our system's database design: user_accounts students guidance_counselors What I wanted to happen is that the user_accounts table should contain the IDs (supposedly the login credential to the system) and passwords of both the student users and guidance counselor users. In

sqlite: multi-column primary key with an auto increment column

为君一笑 提交于 2019-11-27 03:35:25
问题 I basically want to convert a table from mysql to sqlite with the following scheme: create table items ( id integer auto_increment, version integer default 0, primary key (id, version) ); Essentially, I want ID to auto increment whenever I insert anything into the table, with VERSION starting off at 0, but still allow multiple items with the same ID as long as VERSION is different. I'm trying to replicate this behavior with Sqlite however, I can't seem to get table creation working. It seems

Problems setting a custom primary key in a Rails 4 migration

独自空忆成欢 提交于 2019-11-27 03:34:44
问题 I use postgresql 9.3, Ruby 2.0, Rails 4.0.0. After reading numerous questions on SO regarding setting the Primary key on a table, I generated and added the following migration: class CreateShareholders < ActiveRecord::Migration def change create_table :shareholders, { id: false, primary_key: :uid } do |t| t.integer :uid, limit: 8 t.string :name t.integer :shares t.timestamps end end end I also added self.primary_key = "uid" to my model. The migration runs successfully, but when I connect to

GUID vs INT IDENTITY [duplicate]

纵然是瞬间 提交于 2019-11-27 03:24:10
Possible Duplicate: How do you like your primary keys? I'm aware of the benefits of using a GUID, as well as the benefits of using and INT as a PK in a database. Considering that a GUID is in essence a 128 bit INT and a normal INT is 32 bit, the INT is a space saver (though this point is generally moot in most modern systems). In the end, in what circumstances would you see yourself using an INT as a PK versus a GUID? Kimberley Tripp (SQLSkills.com) has an article on using GUID's as primary keys. She advices against it because of the unnecessary overhead. Apart from being a poor choice when

Store and reuse value returned by INSERT … RETURNING

放肆的年华 提交于 2019-11-27 02:51:19
问题 In PostgreSQL, it is possible to put RETURNING at the end of an INSERT statement to return, say, the row's primary key value when that value is automatically set by a SERIAL type. Question: How do I store this value in a variable that can be used to insert values into other tables? Note that I want to insert the generated id into multiple tables. A WITH clause is, as far as I understand, only useful for a single insert. I take it that this will probably have to be done in PHP. This is really

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

本小妞迷上赌 提交于 2019-11-27 02:36:13
I have a table with existing data. Is there a way to add a primary key without deleting and re-creating the table? leonbloy ( 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 In old versions of PostgreSQL (prior to 8.x?) you had to do all the dirty work. The following sequence of commands