11.4.1.3 explain the difference between primary composite and foreign key Primary key & Composite key. Foreign key A primary key is a field in a table that contains unique data. A primary is a single-column value used to identify a database record uniquely. It has the following attributes
Let's look at an example in the Criminal Table:
Which of these attributes can store unique data?
There is a shortcut to writing out database table designs, where the underlined attribute is the primary key. Note that primary keys are normally written first. TableName(PrimaryKey, Attribute, Attribute, Attribute, Attribute) Criminal(IDnumber, Name, Date_of_Birth, Number_of_scars, Home_town) Composite key - сollection of attributes uniquely identifies a tuple rather than just one.
Which of these would be the primary key?
So what do we do? Before we make our database tables it's best to go through this process and we come to realise that we need to introduce another attribute that is unique for each player. We might even invent one, a playerID: Player(PlayerID, Name, Position, Number, Injured, Team) Another way of solving these problems is to try and find a combination of attributes that together are unique. This is called a composite key. Take a look at this example for houses:
We could make a primary key up, but if you look carefully, we can use a combination of attributes. The house number and the road name combined seem to be unique. You can't have two 45 Belmont Close, can you? House(Number, Road, Color, Post code) Does that sound ok? What about if we were storing data on all the towns in the country and there was a 5 London Road in Manchester and a 5 London Road in Winchester. This would mean that the combination was not unique. We might try using the house number and postcode instead, and this combination is always unique, this is our composite key: House(Number, Road, Color, Postcode) Foreign key is a field in one table that is linked to the primary key in another table. Questions: Exercises: Exam questions:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Просмотров: 5227 | | |
Всего комментариев: 0 | |