1. What Are The Two Main Ways To Generate Surrogate Keys 2017
  2. What Are The Two Main Ways To Generate Surrogate Keys Free
  3. What Are The Two Main Ways To Generate Surrogate Keys In India
  4. What Are The Two Main Ways To Generate Surrogate Keys In Windows 10
  5. What Are The Two Main Ways To Generate Surrogate Keys Free
  6. What Are The Two Main Ways To Generate Surrogate Keys In Excel
  7. What Are The Two Main Ways To Generate Surrogate Keys Florida
  • Jan 31, 2011 A surrogate key is typically a numeric value. Within SQL Server, Microsoft allows you to define a column with an identity property to help generate surrogate key values. Before I talk about the pros and cons of natural and surrogate keys, let me first expand a little more on each type of key.
  • Generate surrogate key Goal Fill in a data warehouse dimension table with data which comes from different source systems and assign a unique record identifier (surrogate key) to each record.

Posted Jan 31, 2011

Apr 20, 2006 Every RDBMS of which I’m aware offers a feature to make surrogate keys easier by automatically generating the next larger value upon insert. In SQL Server, it’s called an IDENTITY column. In MySQL, it’s called AUTOINCREMENT. It’s possible to generate the value in SQL, but it’s easier and generally safer to let the RDBMS do it instead.

By Gregory A. Larsen

When designing a database to support applications you need to consider how you are going to handle primary keys. This article explores natural and surrogate keys, and discusses the pros and cons of each, allowing you to determine what makes the best sense in your environment when you are designing your databases.

When designing a database to support applications you needto consider how you are going to handle primary keys. There are two schoolsof thought, or maybe three. There are those that say primary keys shouldalways be a made up key, or what is commonly called a surrogate key. Otherssay there are good reasons to use real data as a key value; this type of key isknown as natural key. The third group is those that design their databases sotheir primary keys are a combination of natural and surrogate keys. In thisarticle, I’m going explore natural and surrogate key, and discuss the pros andcons of each. This will allow you to determine what makes best sense in yourenvironment when you are designing your databases.

Natural Key verses Surrogate Key

When you design tables with SQL Server, a table typically hasa column or a number of columns that are known as the primary key. The primarykey is a unique value that identifies each record. Sometimes the primary key ismade up of real data and these are normally referred to as natural keys, whileother times the key is generated when a new record is inserted into a table. When a primary key is generated at runtime, it is called a surrogate key. A surrogatekey is typically a numeric value. Within SQL Server, Microsoft allows you todefine a column with an identity property to help generate surrogate key values.

Before I talk about the pros and cons of natural andsurrogate keys, let me first expand a little more on each type of key. By doingthis you will have a better understanding of each of these two types of keys,and will have a more solid foundation to determine which type of key you shoulduse in your database design.

A natural key is a single column or set of columns thatuniquely identifies a single record in a table, where the key columns are madeup of real data. When I say “real data” I mean data that has meaning andoccurs naturally in the world of data. A natural key is a column value thathas a relationship with the rest of the column values in a given data record. Here are some examples of natural keys values: Social Security Number, ISBN, andTaxId.

What Are The Two Main Ways To Generate Surrogate Keys 2017

A surrogate key like a natural key is a column that uniquelyidentifies a single record in a table. But this is where the similaritystops. Surrogate keys are similar to surrogate mothers. They are keys thatdon’t have a natural relationship with the rest of the columns in a table. Thesurrogate key is just a value that is generated and then stored with the restof the columns in a record. The key value is typically generated at run timeright before the record is inserted into a table. It is sometimes alsoreferred to as a dumb key, because there is no meaning associated with thevalue. Surrogate keys are commonly a numeric number.

Now that you have an understanding of the difference betweenthese two types of keys I will explore why you might use one key over theother. In the world of data architects, there is much debate over when it isappropriate to use a natural key and when a better solution would be to use asurrogate key. As already stated there are mainly just twodifferent camps. Some say you should always use a natural key and the otherssay a surrogate key is best. I suppose there is also a third camp that uses acombination of both natural keys and surrogate keys in their database design. Rather than state my opinion on which is best I’ll give you the pros and consof uses each and then you can decide with is best for your design.

Surrogate Key Pros and Cons

A definite design and programming aspect of working with databases is built on the concept that all keys will be supported by the use surrogate keys. To understand these programming aspects better, review these pros and cons of using surrogate keys.

Pros:

  • The primary key has no business intelligence built into it.Meaning you cannot derive any meaning, or relationship between the surrogatekey and the rest of the data columns in a row.
  • If your business rules change, which would require you to updateyour natural key this can be done easily without causing a cascade effectacross all foreign key relationships. By using a surrogate key instead of anatural key the surrogate key is used in all foreign key relationships. Surrogatekeys will not be updated over time.
  • Surrogate keys are typically integers, which only require 4 bytesto store, so the primary key index structure will be smaller in size than theirnatural key counter parts. Having a small index structure means betterperformance for JOIN operations.

Cons:/microsoft-office-2013-key-generator-free-download-no-survey.html.

  • If foreign key tables use surrogate keys then you will berequired to have a join to retrieve the real foreign key value. Whereas if theforeign key table used a natural key then the natural key would be already beincluded in your table and no join would be required. Of course this I onlytrue if you only needed the natural key column returned in your query
  • Surrogate keys are typically not useful when searching for datasince they have no meaning.

Natural Key Pros and Cons

Having natural keys as indexes on your tables mean you willhave different programming considerations when building your applications. Delta force xtreme 2 cd key generator. Youwill find that pros and cons for natural keys to be just the opposite as thepros and cons for surrogate keys.

Pros:

  • Will require less joins when you only need to return the keyvalue of a foreign key table. This is because the natural key will already beimbedded in your table.
  • Easier to search because natural keys have meaning and will bestored in your table. Without the natural key in your table, a search for recordsbased on a natural key would require a join to the foreign key table to get thenatural key.

Cons:

  • Requires much more work to change a natural key, especially whenforeign relationship have been built off the natural key.
  • Your primary key index will be larger because natural keys aretypically larger in size then surrogate keys.
  • Since natural keys are typically larger in size then surrogatekeys and are strings instead of integers joins between two tables on a naturalkey will take more time.

What Kind of Database Designer Are You?

There is much debate in the world of data modeling over whatkind of data should be used to support primary keys. There are some puristthat say all primary key should be surrogate keys, no matter how small thenatural key, or the fact that the natural key will never be updated. Other sayyou need to use natural keys because they make coding your application just somuch easier. When you design your databases, you need to decide what works bestin your environment. What kind of database designer are you and into which design camp do you fall?

» See All Articles by ColumnistGregory A. Larsen





Latest Forum Threads
MS SQL Forum
TopicByRepliesUpdated
SQL 2005: SSIS: Error using SQL Server credentialspoverty3August 17th, 07:43 AM
Need help changing table contentsnkawtg1August 17th, 03:02 AM
SQL Server Memory confifurationbhosalenarayan2August 14th, 05:33 AM
SQL Server – Primary Key and a Unique Keykatty.jonh2July 25th, 10:36 AM


As those of you who watched my recent webinar Data Modeling Fundamentals With Sisense ElastiCube might recall, a primary key is a unique identifier given to a record in our database, which we can use when querying the database or in order to join multiple sources. This article will discuss the concept of surrogate keys and show some examples of when and how to apply them using simple SQL.

General Guidelines for Selecting Primary Keys

Before we dive into natural vs. surrogate keys, let’s recall four important rules to follow when selecting a primary key for your data model:

  1. The primary key must be unique for each record. A primary key with duplicates will lead to inaccurate queries with duplicated counts and totals. If two customers are assigned the same primary key, their sales activity will be unintentionally blended together. If the customer is accidentally duplicated, their sales activity will also be duplicated. Database architects refer to this as a loss of referential integrity.
  2. The primary key must apply uniform rules for all records. Whether your key is strictly numeric, alphanumeric, or a random system-generated value, each record must be programmed in a consistent format. This format must exist despite whatever complexities there are in the business requirements. An inconsistent format can lead to difficult data analysis, especially in parent/child data relationships.
  3. The primary key must stand the test of time. A key based off of contextual data at the present time, may not have the same contextual meaning later. For example, if a customer ID key is based on customer name, what happens when a customer is acquired or reorganized? Changing key formats should be avoided at all costs. Changing keys will require changing all stored procedures referencing the new key in any JOINs or WHERE clauses, as well as UPDATEs to all existing references to the old key in all of your database tables.
  4. The primary key must be read-only. In order to stand the test of time, primary keys should never be edited. Edited primary keys can have typos (123123 vs 132123), varying formats based on the user’s preference (1 vs 000001), and allow for overwriting a previously deleted record. Never allow anyone to edit the value of primary keys.

Selecting a Primary Key: Surrogate vs. Natural Keys

First, let’s go over the difference between these two forms of primary keys:

A natural key is a key that has contextual or business meaning (for example, in a table containing STORE, SALES, and DATE, we might use the DATE field as a natural key when joining with another table detailing inventory).

A natural key can be system-generated, but natural keys are at least partially determined by a manual process. Some natural keys are totally manually generated. One of the most widely recognized uses of a natural key is a stock ticker symbol – i.e. MSFT, APPL, and GOOGL. Natural keys serve as a great primary key when contextual meaning is important.

What Are The Two Main Ways To Generate Surrogate Keys Free

A surrogate key is a key which does not have any contextual or business meaning. It is manufactured “artificially” and only for the purposes of data analysis. The most frequently used version of a surrogate key is an increasing sequential integer or “counter” value (i.e. 1, 2, 3). Surrogate keys can also include the current system date/time stamp, or a random alphanumeric string.

See Sisense in action:

When should you stick to natural keys in your data model?

The main advantage of natural keys is in their simplicity and in the fact that the data maintains its original context. They will often be (relatively) easy to recognize to people viewing the data, and relying on natural keys reduces the need to enrich the data using custom SQL. Additionally:

  • Natural keys are great for multiple data types in the database. Natural keys allow the user to easily identify the data type from the key, even when multiple data types use similar key formats. Financial databases frequently format their keys using a natural and sequential key together.

    Even though all three records contain a sequential ID of 123, the natural key prefix allows the user to immediately identify different data types.

  • Natural keys work well when connecting two systems with two different primary key formats. Thus for example, we can use

    To create

  • Natural keys make for a more easy-to-understand GUI. A customer ID such as GOOGL is easy for a user to recognize (for instance, you likely knew this stock ticker symbol is for Google). Easier recognition also allows for easier search.

Drawbacks of using natural keys

While it might be tempting and initially easier to rely on existing natural keys, this could prove problematic when scaling the data model, or in a more complex environment, which we will demonstrate using an example of stock tickers:

  • Natural keys do not apply uniform rules for each record. Designators or variables in the natural key make the key difficult to query and understand after the fact. For example, stock ticker symbols of preferred shares have a multitude of designators, including P, PR, and /PR. Trying to query for the designator P (SELECT * FROM stock_quotes WHERE stock_ticker_symbol like %P) would return all results where the stock ticker symbol ends in P, regardless if the symbol is actually preferred stock or not.
  • Natural keys do not stand the test of time. Symbols which might have been business meaning could become meaningless, or bear a different meaning in the future. Thus, for example, the symbols GOOG and GOOGL do not accurately represent the reorganization of the company from Google to Alphabet.
  • Natural keys can be easily confused with each other. Sticking with the previous example – when Twitter was ready to launch their IPO under the ticker TWTR, many investors bought from a defunct electronics company named Tweeter, trading under the ticker TWTRQ. Because TWTR and TWTRQ contain the same first four letters, many investors unintentionally invested in the wrong stock. Tweeter later changed their ticker symbol to THEGQ, which could also be misconstrued with GQ Magazine (a privately held company under Conde Nast).

Advantages of using surrogate keys

What Are The Two Main Ways To Generate Surrogate Keys In India

As mentioned, a surrogate key sacrifices some of the original context of the data. However, it can be extremely useful for analytical purposes for the following reasons:

  • Surrogate keys are unique. Because surrogate keys are system-generated, it is impossible for the system to create and store a duplicate value.
  • Surrogate keys apply uniform rules to all records. The surrogate key value is the result of a program, which creates the system-generated value. Any key created as a result of a program will apply uniform rules for each record.
  • Surrogate keys stand the test of time. Because surrogate keys lack any context or business meaning, there will be no need to change the key in the future.
  • Surrogate keys allow for unlimited values. Sequential, timestamp, and random keys have no practical limits to unique combinations.

Combining Natural and Surrogate Keys

What Are The Two Main Ways To Generate Surrogate Keys In Windows 10

Certain business scenarios might require keeping the natural key intact as a means for users to interact with the database. In these cases …

  • If a natural key is recommended, use a surrogate key field as the primary key, and a natural key as a foreign key. While users may interact with the natural key, the database can still have surrogate keys outside of the users’ view, with no interruption to user experience.
  • If a natural key must be used without an additional surrogate key, be sure to combine it with a surrogate key element. In our financial database example, Expense Reports (ER-123) have a natural key is used in conjunction with a surrogate sequential key. This format prevents many of the natural key side effects listed above.

What Are The Two Main Ways To Generate Surrogate Keys Free

An Example of Adding a Surrogate Key Using Custom SQL

What Are The Two Main Ways To Generate Surrogate Keys In Excel

In the following example, we will look at a table containing historical data about product prices. By using a custom SQL expression in the Sisense Elasticube Manager, we create the surrogate key ProdDate_Key, which in this case is created by combining the other fields into a single, unique identifier that can easily be queried later.

Original:

SQL used to add surrogate key:

SSELECT DISTINCT
tostring(ProductID)+'_'+tostring(getyear(Date))+'-'+tostring(getmonth(Date))+'-'+tostring(Getday(Date)) AS Prod_Date_Key,
Date,
PH.ProductID,
PH.ListPrice
FROM [ProductListPriceHistory] PH JOIN [AllDates] ON Date between PH.StartDate AND PH.EndDate

What Are The Two Main Ways To Generate Surrogate Keys Florida

Result:

Want to master data modeling? Watch our on demand webinar and learn the fundamental skills every analyst should have.

Coments are closed
Scroll to top