Embed Size (px)
Transcript of Teradata Basic
What is a Relational Data Model?A Relational Data Model is a defined number of tables, made up of columns and rows, which represent a situation. Heres an example:Teradata stores its information inside Tables. A table consists of rows and columns. A row is one instance of all columns. According to relational concepts column positions are arbitrary and a column always contains like data. Teradata does not care what order you define the columns and Teradata does not care about the order of rows in a table. Rows are arbitrary also, but once a row format is established then Teradata will use that format because a Teradata table can have only one row format. There are many benefits of not requiring rows to be stored in order. Unordered data does not have to be maintained to preserve the order. Unordered data is independent of the query.
Primary Keys are Different than Primary IndexesThe Primary Key of a table is the column or group of columns whose values will identify uniquely each row of that table.
Every table has to have a primary key and only oneTables are very flexible when it comes to defining how a tables data can be laid out. However, every table must have a primary key. Each row within that table must always be uniquely identifiable. If the table happens to have several possible combinations that could work as a primary key, only one can be chosen. You cannot have more than one primary key on a table. The smallest group of columns, often just one, is usually the best.
Foreign KeysA foreign key is a normal column in one table that happens to be a primary key in another table. Foreign keys help to relate tables together. This is where the term relational database comes from.
Primary Key Foreign Key QuizBelow you see the Department Table and the Employee Table. They have a relation. How many Primary Key Foreign Key relationships do these two tables have together? Remember that a Foreign Key is a normal column in one table that is the Primary Key of another table (Hint Hint)
Primary Key Foreign Key Quiz AnswersThere are two Primary Key Foreign Key relationships between the tables below. The first relationship is the Primary Key of the Department Table which is Dept_No. Dept_No is a normal column in the Employee_Table. Notice that they have the same exact names. The second Primary Key Foreign Key relationship is the Employee_No of the Employee Table and the Mgr_No of the Department Table. Notice that they have different names. They are though said to be part of the same domain. That means that both columns have the same data type, the same range of values and represent the same thing. Both represent Employee_Nos. In the Employee Table all Employee_Nos are listed. In the Department Table only the Employee_Nos for Managers are listed.
The Primary Index
"Alone we can do so little; together we can do so much."
Helen KellerHelen Keller may have been blind, but she saw so much more then the rest of us. Can you imagine living in a world of such darkness, yet becoming such a shining light? Helen Keller was the ultimate leader and she helped millions realize that they should continue to always learn, and that the journey of life is the ultimate destination.
Teradata uses the Primary Index of each table to provide a row its destination to the proper AMP. This is why each table in Teradata is required to have a Primary Index. The biggest key to a great Teradata Database Design begins with choosing the correct Primary Index. The Primary Index will determine on which AMP a row will reside. Because this concept is extremely important, let me state again that the Primary Index value for a row is the only thing that will determine on which AMP a row will reside. Many people new to Teradata assume that the most important concept concerning the Primary Index is data distribution. INCORRECT! The Primary Index does determine data distribution, but even more importantly, the Primary Index provides the fastest physical path to retrieving data. The Primary Index also plays an incredibly important role in how joins are performed. Remember these three important concepts of the Primary Index and you are well on your way to a great Physical Database Design.
The Primary Index plays 3 roles: Data Distribution Fastest Way to Retrieve Data Incredibly important for JoinsWhat needs to be known prior to selecting the Primary Index to ensure excellent distribution? Columns that define the index. If they are unique or nearly unique then Teradata will spread the data evenly.
Two Types of Primary Indexes (UPI or NUPI)"A man who chases two rabbits catches none."
Roman ProverbEvery table must have at least one column as the Primary Index. The Primary Index is defined when the table is created. There are only two types of Primary Indexes, which are a Unique Primary Index (UPI) or a Non-Unique Primary Index (NUPI). "A man who chases two rabbits misses both by a HARE! A person who chases two Primary Indexes misses both by an ERR!"
Tera-Tom ProverbEvery table must have one and only one Primary Index. Because Teradata distributes the data based on the Primary Index columns value it is quite obvious that you must have a primary index and that there can be only one primary index per table.
The Primary index is the Physical Mechanism used to retrieve and distribute data. The primary index is limited to the number of columns in the primary index. This means that the primary index is comprised totally of all the columns in the primary index. You can have up to 16 multi-column keys comprising your primary index or as little as one column as your primary index.. Most databases use the Primary Key as the physical mechanism. Teradata uses the Primary Index. There are two reasons you might pick a different Primary Index then your Primary Key. They are (1) for Performance reasons and (2) known access paths.
A Table can only have one primary index, but that Primary Index can consist of a single column or a combination of columns. With V2R5 and V2R6 up to 64 columns.Unique Primary Index (UPI)
"Always remember that you are unique just like everyone else."
AnonymousA Unique Primary Index (UPI) is unique and cant have any duplicates. It is as unique as you are. Nobody is like you and you are extremely beautiful and amazing. Not one other person in the history of mankind has ever been exactly like you. You are the creation of your beautiful parents and must realize how important you are to the world. A Unique Primary Index is not as amazing as you are, but it is also special.
A Unique Primary Index means that the values for the selected column must be unique. If you try and insert a row with a Primary Index value that is already in the table, the row will be rejected. A Unique Primary Index will always spread the table rows evenly amongst the AMPs. Please dont assume this is always the best thing to do. Below is a table that has a Unique Primary Index. We have selected EMP to be our Primary Index. Because we have designated EMP to be a Unique Primary Index, there can be no duplicate employee numbers in the table.
Employee TableEMP DEPT LNAME FNAME SAL
UPI1 2 3 4 40 20 20 ? BROWN JONES NGUYEN BROWN CHRIS JEFF XING SHERRY 95000.00 70000.00 55000.00 34000.00
A Unique Primary Index (UPI) will always spread the rows of the table evenly amongst the AMPs. UPI access is always a one-AMP operation. It also requires no duplicate row checking.
Non-Unique Primary Index (NUPI) "You miss 100 percent of the shots you never take."
Wayne GretzkyTake a shot at using a Non-Unique Primary Index in your Teradata tables. A Non-Unique Primary Index (NUPI) means that the values for the selected column can be non-unique. You can have many rows with the same value in the Primary Index. A Non-Unique Primary Index will almost never spread the table rows evenly. Please dont assume this is always a bad thing. Below is a table that has a Non-Unique Primary Index. We have selected LNAME to be our Primary Index. Because we have designated LNAME to be a Non-Unique Primary Index we are anticipating that there will be individuals in the table with the same last name.
NUPI1 2 3 4 40 20 20 ? BROWN JONES NGUYEN BROWN CHRIS JEFF XING SHERRY 95000.00 70000.00 55000.00 34000.00
A Non-Unique Primary Index (UPI) will almost NEVER spread the rows of the table evenly amongst the AMPs.A Non-Unique Primary Index (NUPI) will contain like data. There can be more than one row with the same Primary Index value because it is non-unique. An All-AMP operation will take longer if the data is unevenly distributed. You might pick a NUPI over an UPI because the NUPI column may be more effective for query access and joins.
Primary Index Explained in Simple TermsAll Teradata tables must have one and only one Primary Index. The Primary Index will be used to distribute a tables rows to the proper AMP. The Primary Index is also utilized when retrieving the data.What needs to be known prior to selecting the Primary Index to ensure excellent distribution? Columns that define the index. If they are unique or nearly unique then Teradata will spread the data evenly.
Primary Index (PI) Data Distribution in Theory"Acting is all about honesty. If you can fake that, youve got it made"
- George Burns To store the data, the value(s) in the PI are hashed though a calculation to determine which AMP will possess the row. The same data values always hash the same row hash and therefore are always associated with the same AMP. The PI is what makes or breaks the system. The PI is responsible for all of the systems data distribution. Our example below is designed to only show in theory how Teradata places a row on an AMP. We are going to divide the Primary Ind