Primary Keys are one of the most important aspects of good database design.
Primary Key Properties
- May never be null
- One primary key per table
- Cannot be modified to a new value
Most of what you will find about Primary Keys will pertain to most or all database platforms, but there may be some minor differences in how they are implemented and how they behave on disk.
There is a lot of information on the internet about Primary Keys. A quick search will turn up thousands of articles, examples, blog posts, forums and question/answer threads.
Here are a few we like, but feel free to continue the search:
The Wikipedia page for Primary Keys actually redirects to a larger page on Unique Keys.
A primary key guarantees data is unique within a column. 0:00 Just like unique key, but there are several impulse and differences. 0:03 Like unique keys the primary keys are unique values. 0:08 A unique key can allow aid no value. 0:12 However a primary key will never allow a no value, 0:15 each table can only have one primary key. 0:19 But can have many unique keys. 0:22 Lastly you cannot modify the value of a primary key. 0:25 But you can modify the value of a unique key 0:28 as long as it doesn't violate the uniqueness criteria of that column. 0:31 A primary key doesn't necessarily need to be a numeric or integer data type. 0:35 But in a lot of instances, 0:41 they are implemented as auto incrementing number fields. 0:42 Because they're primarily used for 0:46 relating tables of data within the database itself, primary keys don't 0:48 really have any useful values that have any meaning to business or application users. 0:53 The use of number values as opposed to text data reduces disk storage space and 0:59 speeds up query results. 1:05 In this example the ID field is a primary key. 1:07 Let's assume it isn't an auto incrementing field. 1:11 So we're on the hook for inserting that value. 1:15 If we make a mistake or 1:18 a duplicate value the database will not allow the record to be written. 1:19 If we accidentally try to write a null value the system would return 1:24 an error as well. 1:29 Only a non null numeric value can be written to this primary key field. 1:30 Remember that database systems can handle the generation of the ID field for us. 1:36 Normally it's already set up for us to do this by the database designer. 1:41 And then we don't have to worry about doing this ourselves. 1:45 In the next video, we'll see how a database creates and 1:49 enforces relationships between tables using primary keys and foreign keys. 1:53
You need to sign up for Treehouse in order to download course files.Sign up