surrogate-key

Retrofit surrogate key in table with natural key in MySql?

Assume a table that uses a natural key and has a number of existing rows. What would be the easiest way to retrofit a surrogate key column and populate it with unique values using MySql? I.e. transform table employees ( social_security_no varchar(20), ... constraint emp_pk primary key (social_security_no) ); to table emp...

Many-to-many relationship with surrogate key in Entity Framework

Entity Framework magically interprets the following table structure as a many-to-many relationship. table foo (int id) table foo_bar (int foo_id, int bar_id) table bar (int id) But if the join table has any additional fields it will instead be interpreted as two one-to-many relationships. I am using a database in which the join table...

Guaranteeing Unique Surrogate Key Assignment - Maximum Matching for Non-Bipartite Graph

I am maintaining a data warehouse with multiple sources of data about a class of entities that have to be merged. Each source has a natural key, and what is supposed to happen is that one and only one surrogate key is created for each natural key for all time. If one record from one source system with a particular natural key represent...

Should I use "id" or "unique username"?

I am using PHP, AS3 and mysql. I have a website. A flash(as3) website. The flash website store the members' information in mysql database through php. In "members" table, i have "id" as the primary key and "username" as a unique field. Now my situation is: When flash want to display a member's profile. My questions: Should Flash pas...

Must Django ManyToManyField association tables have a surrogate key?

I'm mapping an existing database structure into Django models. I have a many-to-many structure where the association table is natural-keyed: CREATE TABLE foo (id INTEGER PRIMARY KEY); CREATE TABLE bar (id INTEGER PRIMARY KEY); CREATE TABLE foo2bar (foo_id INTEGER REFERENCES foo(id), bar_id INTEGER REFERENCES bar(id...

Surrogate key as a foreign key over composite keys

I realise there might be similar questions but I couldn't find one that was close enough for guidance. Given this spec, Site --------------------------- SiteID int identity Name varchar(50) Series --------------------- SiteID int SeriesCode varchar(6) ... --SeriesCode will be unique for every unique SiteID Episod...

Generating surrogate keys remotely

Hi Guys, Sorry in advance as this question is similar (but not the same!) to others. Anyway, I need to be able to generate surrogate keys in more than one location to be synchronized at a later time. I was considering using GUIDs, however these keys may have to appear in the parameters of a URL and GUIDs would be really complicated and...

Recommend datatype Number vs Varchar for a surrogate key

Can anyone of you please tell me which is the best data type for a surrogate key (number or varchar2)? I have read that surrogate key works best with the number type. But is the performance seriously have major impact with the data types. And is it database dependent too? reply would be much appreciated! ...

Separate table in case of surrogate key?

I have been reading the heated debates on composite vs surrogate keys on Stack Overflow and other websites, and even though puzzled about some of the arguments given, I feel I am aware of the pros and cons of each. I feel tempted to go for surrogate keys, but now I have another question. Let me explain my situation. I have a table consi...

Preferred way to add surrogate key to existing Oracle DB table

I have to modify an existing table in a Oracle 10g DB with a few thousand records to add a surrogate autonumber key. One way that comes to my mind is to Create a new sequence Create the id column, allowing null values Updating the id column with the sequence Alter table to add "not null" and "primary key" for the new id column Is th...

How should I define a composite foreign key for domain constraints in the presence of surrogate keys?

I am writing a new app with Rails so I have an id column on every table. What is the best practice for enforcing domain constraints using foreign keys? I'll outline my thoughts and frustration. Here's what I would imagine as "The Rails Way". It's what I started with. Companies: id: integer, serial company_code: char, unique, no...

Loading Fact Table + Lookup / UnionAll for SK lookups.

I got to populate FactTable with 12 lookups to dimension table to get SK's, of which 6 are to different Dim Tables and rest 6 are lookup to same DimTable (type II) doing lookup to same natural key. Ex: PrimeObjectID => lookup to DimObject.ObjectID => get ObjectSK and got other columns which does same OtherObjectID1 => lookup to DimOb...

Mixed surrogate composite key insert in JPA 2.0, PostgreSQL and Hibernate 3.5

First off, we are using JPA 2.0 and Hibernate 3.5 as persistence provider on a PostgreSQL database. We successfully use the sequence of the database via the JPA 2.0 annotations as an auto-generated value for single-field-surrogate-keys and all works fine. Now we are implementing a bi-temporal database-scheme that requires a mixed key i...

Can I use an assigned, natural-key identifier while still allowing NHibernate to identify transient instances?

Object A has a one-to-many association: many object B. When I look in the database -- TableB -- I'd like to see the unique, readable string A.Name instead of having always to join or subselect on a surrogate integer identifier to see the name. I can map Name as the identifier for A, but this causes lots of extra SELECT queries because ...

Relational database design question - Surrogate-key or Natural-key?

Which one is the best practice and Why? a) Type Table, Surrogate/Artificial Key Foreign key is from user.type to type.id: b) Type Table, Natural Key Foreign key is from user.type to type.typeName: ...

Database Design - How to enforce a foreign key constraint on a table with a Surrogate Key

My table schema is something like this 1. Master table : Clause Columns ClauseID = surrogate pk (identity) ClauseCode = nvarchar user specified value Class = nvarchar FK to a master class table etc... ClauseCode + Class = candidate key for this table 2. Master table : GroupClause Columns GroupClauseID = surrogate pk (identi...