0% found this document useful (0 votes)
11 views

Unit1 Dbms

Uploaded by

Shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit1 Dbms

Uploaded by

Shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
Keys Key plays an important role in relational database; itis used for ide also establishes relationship among tables. ig unique rows from table. It ‘Types of keys in DBMS Primary Key ~ A primary isa column or set of columns in a table that uniquely identifies tuples (rows) in that table, Super Key —A super key isa set of one of more columns (attributes) to uniquely identify rows in a table. Candidate Key — A super key with no redundant attribute is known as candidate key Alternate Key — Out of all candidate keys, only one gets selected as primary key, remaining keys are i known as alternate or secondary keys 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. 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. 1) Primary key in DBMS Definition: A primary is a columin or set of colum: hi ntifies tuples k set of columns ina tabl ‘omni le that uniquely identifies tuples Example: Student Table Stuld | stuName | —stuage 101 Steve 23 402 John an 103 | Robert 28 4104 Carl 22 In the above Student table, the Stu_Id column uniquely identifies each row of the table. Note: We denote the primary key by underlining the column name. The value of primary key should be unique for each row of the table. Primary key colurnn 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 fora table. For e.g. {Stu_Id, Stu_Name).collectively can play a role of primary key in the above table, but that does not make sense because Stu_Id alone is enough to uniquely identifies rows in a table then why to make things complex. Having that said, we should choose more than ofié columns as primary key only when there is no single column that can play the role of primary key. How to choose a primary key? ‘There are two ways: Either to create a column and let database automatically have numbers in increasing order for each row or choose a column yourself'making sure that it does not contain duplicates and nulls. For e.g in the above Student table, The Stu_Name column cannot be a primary Key as more than one people can have same name, similarly the Stu_Age column cannot play a primary key tole as more than one persons can have same age. 2) Super key in DBMS Definition: A super key is a set or one of more columns (attributes) to uniquely identify rows in a table. Often péople get confused between super key and candidate key, so we will also discuss alittle about candidate key here, - How candidate key is different from super key? Answer is simple ~ Candidate keys are selected from the 'set of super keys, the only thing we take care while selecting candidate key is: It should not have any redundant attribute. That's the reason they are also termed as imal super key. Employee table Emp_SSN Emp_Number | Emp_Name 123456789 226 Steve 999999321 227 Ajeet 888997212 228 Chaitanya 777778888 229 Robert {Emp_SSN} {Emp_Number} {Emp_SSN, Emp_Number} {Emp_SSN, Emp_Name} q {Emp_SSN, Emp_Number, Emp_Name} {Emp_Number, Emp_Name} All of the above sets are able to uniquely identify rows of the employee table. 3) Candidate Keys: As stated above, they are the minimal super keys with no redundant attributes. {Emp_SsN) cs {Emp_Number) Only these two sets are candidate keys as all other sets are having redundant attributes that are not necessary for unique identification. Super key vs Candidate Key I have been getting lot of comments regarding the confusion between super key and candidate key. Let me give you a clear explanation. 1. First you have to understand that all the candidate keys are super keys. This is because the candidate keys are chosen out of the super keys. 2. How we choose candidate keys from the set of super keys? We look for those keys from which we cannot remove any fields. In the above example, we have not chosen (Emp_SSN, jp_Name)} as candidate key because (Emp_SSN) alone can identify a Unique row in the table ‘and Emp_Name is redundant, Primary key: Primary key is being selected from the sets of candidate keys by database designer. So Either {Emp_SSN} or {Emp_Number} can be the primary key. : 4) Foreign key in DBMS Definition: 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, For example: In the below example the Stu_Id column in Course_enrollment table isa foreign key ast points to the primary key of the Student table. Course_enrollment table: Course_Id Stud COL 101 coz 302 cos 101 € cos 302 06 103, co7 [102 ali Student table: Stu_Id ‘Stu_Name Stu_Age no Chaitanya 22 102 ‘Arya 26 103) Bran 25 104 Jon 21 Note: Practically, the foreign key has nothing to do with the primary key tag of another table, if it points to a unique column (not necessarily a primary key) of another table then too, it would be a foreign key. So, a correct definition of foreign key would be: Foreign keys are the columns of a table that points to the candidate key of another table. 5) Candidate Key in DBMS A super key with no redundant attribute is known as candidate key: Candidate keys are selected from the set of super keys, the only thing we take care while selecting candidate key is: It should not have any redundant attributes. That's the reason they are also termed as minimal super key. For example: Emp 1d | Emp_Number | Emp_Name E01 2264 Steve E22 2278 Ajeet E23 2288 Chaitanya 2290 Robert ae There are two candidate keys in above table: {Emp_id} {Emp_Number} Note: A primary key is being selected from the group of candidate keys. That means we can either have Emp_Id or Emp_Number as primary key. 6) Composite key in DBMS 5 Akey that consists of more than one atiribute to uniquely identify rows (also known as records & tuples) ina table is called composite key. It is also known as compound key. ‘ . Example: Table - Sales qust1d | ofder_td | praduct_code | product_count cot | 0007 POT rn pt coz | 07123 | POO7 19 , é Coz | 0123 | F230 82 Sor | 0007 Pa90 @ Key in above table: (cust_id, order_id} “Thisis a composite key as it consists of more than one attribute. 7) Alternate key in DBMS Out of all candidate keys, onl » Only one gets selected skerhcoreaeaieh ay aS primary key, remaining keys are known as For example: Consider the below table Emp Id] Emp.Number Emp=Name E01 2264 Steve E22 2278 Ajeet S E23 2288 Chaitanya E45 2290 Robert ‘There are two candidate keys in above table: {Emp_id} & {Emp_Number} Since we have selected Emp_ld as primary key, the remaining key Emp_Number would be called alternative or secondary key. What is Relational Calculus? c , oney ‘0 Relational Algebra which is a procedural query language to fetch data and which also “plains how it is done, Relational Calculus in non-procedural query language and has no description oe ra the query will work or the data will be fetched. It only focuses on what to do, and not on how lit, Relational Calculus exists in two forms: 1) Tuple Relational Calculus (TRC) 2) Domain Relational Calculus (DRC) 1) Tuple Relational Calculus (TRC) | tuple relational calculus, we work on filtering tuples based on the given condition. Syntax: {t | Condition} OR {¢] P(t)) In this form of relational calculus, we define a tuple variable, spe ‘the tuple is to be searched for, along with a condition. the table(relation) name in which We can also specify column name using a . dot operator, with the tuple variable to only get a certain attribute(column) in result. A lot of information, right! Give it some time to sink A tuple variable is nothing but a name, can be anythi let's say t is a tuple variable. , generally we use a single alphabet for this, so To specify the name of the relation (table) in which we want to look for data, we do the following: Relation (t), where tis our tuple variable. for example four tables Student, we would pu it as Student() .n comes the conditi i it e oe ae par, to specify a condition applicable fora particluar attribute(column), we 7 +do le with the tuple variable to specify it, like in table Student, if we want to get data for students with age greater than 17, then, we can write it 2s, tage > 17, where t is our tuple variable, Putting it all together, if we want to use Tuple Relational Calculus to fetch names of students, from table ‘Student, with age greater than 17, then, for t being our tuple variable, Example 1: t.name | Student (t) AND tage > 17 Example2: {t | EMPLOYEE (t) and t SALARY>10000) - implies that it selects the tuples from EMPLOYEE relation such that resulting employee tuples will have salary greater than 10000. Itis example of selecting a range of values. Example3: {t | EMPLOYEE (t) AND t.DEPT_1D = 10}~this select all the tuples of employee name who ‘work for Department 10. ‘The variable which is used in the condition is called tuple variable. In above example tSALARY and {.DEPT_ID are tuple variables. In the Second example above, we have specified the condition t.SALARY 10000. What is the meaning of it? For all the SALARY>10000, display the emptoyees. Here the SALARY is called as bound variable. Any tuple variable with ‘For All (2) or ‘there exists’ (2) condition is called bound variable. Here, for any range of values of SALARY greater than 10000, the meaning of the condition remains the same. Bound variables are those ranges of tuple variables whose meaning will not change if the tuple variable is replaced by another tuple variable. In the third example, we have used DEPT_II 10. That means only for DEPT_ID = 10 display employee details. Such variable is called free variable. Any tuple variable without any ‘For All’ or ‘there exists’ condition is called Free Variable. If we change DEPT_ID in this condition to some other variable, say EMP_ID, the meaning of the query changes. For example, if we change EMP_ID = 10, then above it will result in different result set. Free variables are those ranges of tuple variables whose meaning will change if the tuple variable is replaced by another tuple variable. All the conditions used in the tuple expression are called as well formed formula — WEF. All the conditions in the expression are combined by using logical operators like AND, OR and NOT, and qualifiers ike ‘For Alt’ (?) of there evists’ (2), If the tuple variables are all bound varlables in a WEF is called closed WFF. In an open WEF, we will have at least one free variable. 2) Domain Relational Calculus (DRC) In domain relational calculus, filtering is done based on the domain of the attributes and not based on the tuple values. Syntax: {c1,€2,€3, en | F (C1, €2.€3, onl} Where, c1, €2...ete represents domain of attributes(columns) and F detines the formula including the condition for fetching the data. For example, {e name, age > | € Student A age > 17) ‘Again, the above query will return the names and ages of the students in the tab ble Student who are older than 17.

You might also like