SQL Database Keys
Ho ho ho , another Sunday morning and I am feeling much better than yesterday. Been a bit busy and stressed out with work , school and a visit to the Dentist. I couldn’t come up with anything that is more than a few lines last night but after reading on Databases , for my degree course , I came up with something interesting and related to Databases.
I be posting on database keys. Thats right , keys. I am sure you remember we talked about two such keys when I posted on the sample database. If you don’t remember or you have not seen it before , one such tutorial is here.They are “Primary” and “Foreign” keys.
A primary key is an unique key which is used to locate a particular row in the database. For example , finding an employee’s information using his employee id. You might wonder , why don’t we use his name? Well there might be two people with the same name! Thats also true for the birthday , address and phone number. On the other hand , the employee id is an internal numbering which is internally controlled by the company. It only makes sense that it should be the only way we are going to be able to find the employee information , unaffected by the outside influence.
A foreign key , on the other hand , is a primary key of a table on the another table. If you looked at our sample database page , you will see that employee id is also present in the department table. Therefore , employee id is a primary key in one table and a foreign key in another.
So what other types of keys have we not talked about? Plenty it seems. Below , I will be putting up a summary of the keys in the database , for easy reference for you and me. I am sure I will be looking back to this article every now and then.
- Primary Key – A unique key that is used to quickly locate records by the database
- Foreign Key – A primary key on one table that is used as a constraint in another table
- Compound Key or composite Key – A key that includes more than one field
- Superkey – A set of columns for which no two rows can have the exact same value
- Candidate Key – A minimal superkey
- Unique Key – A superkey used to require uniqueness by the database
- Alternate Key – A candidate key that is not the primary key
- Secondary Key – A key used to look up records but that may not guarantee uniqueness
Keys Conclusion
Phew. Thats all for keys. Glad I got something to write on. Cya!