Relational Model -Keys
Total Page:16
File Type:pdf, Size:1020Kb
Analyze and tell the solution • In real world applications, number of tables required for storing the data is huge, and the different tables are related to each other as well. • Also, tables store a lot of data in them. Tables generally extends to thousands of records stored in them, unsorted and unorganised. • Now to fetch any particular record from such dataset, you will have to apply some conditions, but what if there is duplicate data present and every time you try to fetch some data by applying certain condition, you get the wrong data. How many trials before you get the right data? 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 1/18 Solution • To avoid all this, Keys are defined to easily identify any row of data in a table 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 2 CS8492 DATABASE MANAGEMENT SYSTEMS RELATIONAL MODEL -KEYS P.POOVIZHI AP/IT 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 3/18 Types of Keys 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 4/18 Example – Student Table student_id name phone age 1 Akon 9876723452 17 2 Akon 9991165674 19 3 Bkon 7898756543 18 4 Ckon 8987867898 19 5 Dkon 9990080080 17 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 5/18 Primary key • Primary key is a candidate key that is most appropriate to become the main key for any table. • It is a key that can uniquely identify each record in a table. 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 6/18 Unique key • Unique Key can be a field or set of fields that can be used to uniquely identify the tuple from the database. • One or more fields can be declared as a unique Key. • The unique Key column can also hold the NULL value. • Purpose of Unique key – Use of Unique Key improves the performance of data retrieval. – It makes searching for records from the database much more faster & efficient. • Example: – “cust_aadhaar_number”, “cust_pan_number” are the Unique Key as it can allow one value as a NULL in the column 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 7/18 Candidate Key • Candidate keys are defined as the minimal set of fields which can uniquely identify each record in a table. • It is an attribute or a set of attributes that can act as a Primary Key for a table to uniquely identify each record in that table. • There can be more than one candidate key. • Example – student_id and phone both are candidate keys for table Student. • A candiate key can never be NULL or empty. And its value should be unique. • A candidate key can be a combination of more than one columns(attributes). 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 8/18 Alternate key • Alternate Key is that Key which can be used as a Primary Key if required. • Alternate Key also qualifies to be a Primary Key but for the time being, It is not the Primary Key. • Example – student_id and phone both are alternate keys for table Student 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 9/18 Composite Key • Also known as Compound Key / Concatenated Key. • Key that consists of two or more attributes that uniquely identify any record in a table is called Composite key. • But the attributes which together form the Composite key are not a key independently or individually. 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 10/18 Activity • Memory game in which students are restricted to use certain words during reading comprehension. 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 11 Super Key • Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table. • Super Key is a superset of Candidate key. • Primary Key, Candidate Key & Unique Key is the Super Key • Super key of student table: – student_id, – (student_id, name), – phone etc. • student_id is unique for every row of data, hence it can be used to identity each row uniquely. • (student_id, name), now name of two students can be same, but their student_id can't be same hence this combination can also be a key. • phone number for every student will be unique, hence again, phone can also be a key. • So they all are super keys. 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 12/18 Foreign key • A foreign key is a column which is known as Primary Key in the other table i.e. A Primary Key in a table can be referred to as a Foreign Key in another table. • Foreign Key may have duplicate & NULL values if it is defined to accept NULL values. 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 13/18 Foreign key - Example 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 14/18 Assessment • Consider the example and identify the types of key. Customer Table cust_nam cust_addre cust_aadhaar_nu cust_pan_numb cust_id e ss mber er 100001 Sunil Kumar Noida 372464389211 ADSFS3456K 100002 Ankit Gupta Gr Noida 442289458453 CGHAD7583L Suresh 100003 New Delhi 878453444144 NMKRT2278O Yadav 100004 Nilam Singh Lucknow 227643441123 HFJFD3876U 100005 Amal Rawat Ghaziabad 932571156735 CBMVA9734A Harsh 100006 Kanpur 1453534363319 TRYUC2568H Saxena 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 15/18 Contd…, Order Table cust_id order_month_year order_amount 100001 2019 – Jan $100,000 100002 2019 – Jan $120,000 100003 2019 – Jan $100,000 100004 2019 – Jan $110,000 100001 2019 – Feb $105,000 100002 2019 – Feb $125,000 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 16/18 Solution 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 17/18 Reference • https://www.studytonight.com/dbms/databas e-key.php • https://www.educba.com/sql-keys/ 1/26/2020 CS8492 RELATIONAL MODEL - KEYS 18/18 .