Types of keys Key plays an important role in relational database; it is used for identifying unique rows from table. It also establishes relationship among tables. There are various types of keys used in RDBMS. • Primary Key • Super Key • Candidate Key • Alternate Key • Composite Key • Foreign Key Primary Key A primary is a column or set of columns in a table that uniquely identifies tuples (rows) in that table. Points to think • The value of primary key should be unique for each row of the table. Primary key column cannot contain duplicate values. • Primary key column should not contain nulls. • Primary keys are not necessarily to be a single column; more than one column can also be a primary key for a table. For e.g. {Stu_Id, Stu_Name}collectively can play a role of primary key properties • Stable: The value of a primary key must not change or should not become null Through out the life of an entity. A stable primary key helps to keep the model stable. For example: if we consider a patient record, the value for the primary key (Patientnumber) must not change with time as would happen with the age field. • Minimal: The primary key should be composed of the minimum number of fields that ensures the occurrences are unique. • Definitive: A value must exist for every record at creation time. Because an entity occurrence cannot be substantiated unless the primary key value also exists. • Accessible: Anyone who wants to create, read or delete a record must be able to see the Primary key value. example CustomerNo FirstName LastName 1 Sally Thompson 2 Sally Henderson 3 Harry Henderson 4 Sandra Wellington
CustomerNo is the primary key.
Foreign key • Foreign keys are the columns of a table that points to the primary key of another table. They act as a cross-reference between tables. • A foreign key means that values in one table must also appear in another table. • The referenced table is called the parent table while the table with the foreign key is called the child table. The foreign key in the child table will generally reference a primary key in the parent table. Candidate key The minimal set of attribute which can uniquely identify a tuple is known as candidate key. For Example, STUD_NO in STUDENT relation. • The value of Candidate Key is unique and non-null for every tuple. • There can be more than one candidate key in a relation. For Example, STUD_NO as well as STUD_PHONE both are candidate keys for relation STUDENT. • The candidate key can be simple (having only one attribute) or composite as well. For Example, {STUD_NO, COURSE_NO} is a composite candidate key for relation STUDENT_COURSE. SSecurityNo EmployeeNo FirstName LastName DateOfBirth DateEmployed
AF-23432334 1 Manny Tomanny 12 Apr 1966 01 May 1999
DQ-65444444 2 Rosanne Kolumns 21 Mar 1977 01 Jan 2000
Super key • The set of attributes which can uniquely identify a tuple is known as Super Key. For Example, STUD_NO, (STUD_NO, STUD_NAME ) etc. • Adding zero or more attributes to candidate key generates super key. • A candidate key is a super key but vice versa is not true. Alternate key • The candidate key other than primary key is called as alternate key. For Example, STUD_NO as well as STUD_PHONE both are candidate keys for relation STUDENT but STUD_PHONE will be alternate key (only one out of many candidate keys).
Candidate key PK Alternate keys Super Key
• A superkey is either a single or a combination of
attributes that can be used to uniquely identify a database record. A table might have many combinations that create superkeys. Rules • Every CK is a super key. • Every SK can’t be a CK. • Minimal SK is the CK. • Super key=CK +Zero or More Attributes example Eid Name Salary 1 Alex 40,000 2 Carel 50,000 3 Alex 40,000 4 Null 50,000 Composite key • A key that consists of more than one attribute to uniquely identify rows (also known as records & tuples) in a table is called composite key. It is also known as compound key. cust_Id order_Id product_code product_count
C01 O001 P007 23
C02 O123 P007 19
C02 O123 P230 82
C01 O001 P890 42
views View is the simply subset of table which are stored logically in a database means a view is a virtual table in the database whose contents are defined by a query.
Views are used for security purpose in databases,
views restricts the user from viewing certain column and rows means by using view we can apply the restriction on accessing the particular rows and columns for specific user.
A view can be read only view- insertion and updation
is not allowed. Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables or another view.