Generate Surrogate Key In Teradata

Posted on  by

Jun 12, 2009 Putting together a few steps to generate surrogate key: Most of you might've dealt with it already. But, sending it as it might be a quick reference incase of future use. Generating it as such isn’t a big deal, it might get a little tricky when you are trying to insert new values in continuation of already existing surrogate key. Generating a sequential Surrogate Key in Teradata This has to the commonest problem of all in the Data Warehousing World. And people suggest different ways to tackle this based on their comfort level. Here I show an approach to create a sequentially increasing surrogate key without the use of variables or CSUM. Surrogate Key Question Is there a way to generate a surrogate key dynamically using set processing rather than record level processing? We have a large amount of records in the source which we need to generate a unique identifier in the EDW for and cursor level processing seems to be quite slow in generating surrogate keys for this volume of data. You can generate surrogate keys in Teradata Database using the identity column feature (see Identity Columns). To use an identity column to generate surrogate key values, specify the GENERATED ALWAYS and NO CYCLE options and ensure that the data type for the. In these situations, and only in these situations, surrogate keys are an ideal solution. As previously defined (see “Surrogate key” under “Definitions” on page 78 ), a surrogate key is an artificial, simple key used to identify individual entities when there is no natural key or when the situation demands a simple key, but no natural simple key exists.

Mar 06, 2014  Surrogate key generation in Teradata Surrogate key generation. Putting together a few steps to generate surrogate key: Most of you might've dealt with it already. But, sending it as it might be a quick reference incase of future use Generating it as such isn’t a big deal, it might get a little tricky when you are trying to insert new values. Nov 19, 2019  Teradata Identity Columns The advantage of an IDENTITY column is that a new surrogate key is generated by the system as soon as a row is inserted into the key table. However, it is important to define the IDENTITY column so that no duplicates are created. For this, the IDENTITY column must be defined with GENERATED.

This article demonstrates how to “roll your own” surrogate keys and sequences in a platform-independent way, using standard SQL.

Surrogate keys

Relational theory talks about something called a “candidate key.” In SQL terms, a candidate key is any combination of columns that uniquely identifies a row (SQL and the relational model aren’t the same thing, but I’ll put that aside for this article). The data’s primary key is the minimal candidate key. Many people think a primary key is something the DBA defines, but that’s not true. The primary key is a property of the data, not the table that holds the data.

Unfortunately, the minimal candidate key is sometimes not a good primary key in the real world. For example, if the primary key is 6 columns wide and I need to refer to a row from another table, it’s impractical to make a 6-column wide foreign key. For this reason, database designers sometimes introduce a surrogate key, which uniquely identifies every row in the table and is “more minimal” than the inherently unique aspect of the data. The usual choice is a monotonically increasing integer, which is small and easy to use in foreign keys.

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 AUTO_INCREMENT. It’s possible to generate the value in SQL, but it’s easier and generally safer to let the RDBMS do it instead. This does lead to some issues itself, such as the need to find out the value that was generated by the last insertion, but those are usually not hard to solve (LAST_INSERT_ID() and similar functions, for example).

It’s sometimes desirable not to use the provided feature. For instance, I might want to be sure I always use the next available number. In that case, I can’t use the built-in features, because they don’t generate the next available number under some circumstances. For example, SQL Server doesn’t decrement the internal counter when transactions are rolled back, leaving holes in the data (see my article on finding missing numbers in a sequence). Neither MySQL nor SQL Server decrements the counter when rows are deleted.

In these cases, it’s possible to generate the next value in the insert statement. Suppose my table looks like this:

The next value for c1 is simply the maximum value + 1. If there is no maximum value, it is 1, which is the same as 0 + 1.

There are platform-dependent ways to write that statement as well, such as using SQL Server’s ISNULL function or MySQL’s IFNULL. This code can be combined into an INSERT statement, such as the following statement to insert 3 into the second column:

The code above is a single atomic statement and will prevent any two concurrent inserts from getting the same value for c1. It is not safe to find the next value in one statement and use it in another, unless both statements are in a transaction. I would consider that a bad idea, though. There’s no need for a transaction in the statement above.

Downsides to this approach are inability to find the value of c1 immediately after inserting, and inability to insert multiple rows at once. The first problem is inherently caused by inserting meaningless data, and is always a problem, even with the built-in surrogate keys where the RDBMS provides a mechanism to retrieve the value.

Sequences: a better surrogate key

Surrogate keys are often considered very bad practice, for a variety of good reasons I won’t discuss here. Sometimes, though, there is just nothing for it but to artificially unique-ify the data. In these cases, a sequence number can often be a less evil approach. A sequence is just a surrogate key that restarts at 1 for each group of related records. For example, consider a table of log entries related to records in my t1 table:

At this point I might want to enter some more records (0, 11) into t1:

Now suppose I want the following three log entries for the first row in t1:

There’s no good primary key in this data. I will have to add a surrogate key. It might seem I could add a date-time column instead, but that’s a dangerous design. It breaks as soon as two records are inserted within a timespan less than the maximum resolution of the data type. It also breaks if two records are inserted in a single transaction where the time is consistent from the first to the last statement. I’m much happier with a sequence column. The following statement will insert the log records as desired:

If I want to enter a log record on another record in t1, the sequence will start at 1 for it:

MySQL actually allows an AUTO_INCREMENT value to serve as a sequence for certain table types (MyISAM and BDB). To do tihs, just make the column the last column in a multi-column primary key. I’m not aware of any other RDBMS that does this.

Generate Surrogate Key In Teradata Data

Teradata: Surrogate Key Concept

Surrogate Key is a unique, database supplied or generated identifier generally used as the primary key/index of a table.


➠ When to use a surrogate key

  • Surrogate key should be used if each row of table cannot be uniquely identified using 1 or more columns.
  • Surrogate key can also be used when a unique key is too long and non-numeric.

➠ Advantages of surrogate key

  • Each row can be uniquely identified within a table using surrogate key value.
  • Surrogate key can be used as primary index for a table to distribute data evenly on all the AMPs.

➠ Disadvantages of surrogate key

  • We cannot derive any meaning or relationship between the surrogate key and the rest of the data columns in a row, therefore surrogate keys have no meaning to the users.
  • There will be cases when data will be shared among different databases. In this case, same rows(from different database) may have different surrogate key and different rows(from different database) may have same surrogate key.

Generate Surrogate Key In Teradata Update


➠ How to generate surrogate key in Teradata

Due to having many powerful features, Windows 7 is used by millions of people around the world especially students, firms, offices and various organizations. When you want to repair or reinstall definitely then you will require Windows 7 Ultimate Product Key or Serial Key. Windows 7 Ultimate Activation Keygen + PatchThe app includes an interface that is attractive and intuitive. Especially, a good thing regarding Windows SEVEN is the start menu and features list. Windows 7 ultimate activation key generator free download. So, you can get these from this web without any extra fee.

  • By using analytical functions
    1. By using CSUM analytical function, Syntax/Example 1:Single AMP (usually vproc 0) generally processes all the data when using CSUM(1,1)
    2. By using SUM analytical function, Syntax/Example 2:
    3. By using ROW_NUMBER analytical function, Syntax/Example 3:Note: '(SELECT ZEROIFNULL(MAX(emp_no)) FROM employee)' is used in above examples to generate new sequence which must be greater than the current max value present in the table.

  • By using Identity column, check Identity Columns page for more detail on Identity Column(Sequence) in Teradata.
    Syntax/Example 4:Note: There will always be gaps in generated number when using identity column because in Teradata it is not one sequence but multiple parallel sequences(one on each AMP).