12
In-Sem Paper Solution Subject: Database Management System (2015 Patter) August, 2018 Answer: [1]

Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

In-Sem Paper Solution

Subject: Database Management System (2015 Patter) August, 2018

Answer: [1]

Page 2: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

Ans: Weak entity set always appears in association with identifying relationship with total

participation constraint. [2]

An entity set which does not contain sufficient attributes for the unique identification of its entities is known as a weak entity set.

In other words, a primary key does not exist for a weak entity set which is completely formed by itself.

However, it contains a partial key called as a discriminator which can identify a group of entities from the entity set and is represented by underlining with a dashed line.

The discriminator of weak entity set and primary key of the strong entity set can together identify any entity of the weak entity set uniquely and therefore this combination is used as a primary key for the weak entity set.

ER Diagram symbols-

A double rectangle is used for representing a weak entity set.

A double diamond symbol is used for representing the relationship that exists between the strong and weak entity sets and this relationship is known as identifying relationship.

A double line is used for representing the connection of the weak entity set with the relationship set.

Total participation always exists in the identifying relationship.

Eg.of Weak Entity Set & its conversion into table.

Here, two tables will be required-

1. A ( a1 , a2 )

2. BR ( a1 , b1 , b2 )

Page 3: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

Q.2 b.

Query Processor Components are [3]:

• DML Pre-compiler : It translates DML statements in a query language into low level instructions that query evaluation engine understands. It also attempts to transform user's request into an equivalent but more efficient form.

• Embedded DML Pre-compiler : It converts DML statements embedded in an application program to normal procedure calls in the host language. The Pre-compiler must interact with the DML compiler to generate the appropriate code.

• DDL Interpreter : It interprets the DDL statements and records them in a set of tables containing meta data or data dictionary.

• Query Evaluation Engine : It executes low-level instructions generated by the DML compiler.

i) Select Cust_name, loan_no, Amount

From Loan A, Borrower B Where A.loan_no = B.loan_no

ii) Select Cust_name From Loan A, Borrower B Where A.loan_no = B.loan_no and Branch_name = ‘Pune’ Order By Cust_name

iii) Select loan_no, Amount From Loan A, Borrower B Where A.loan_no = B.loan_no and Branch_name = ‘Pune’ And Amount > 20000

Ans: [4] DECLARE

Page 4: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

-- Variables to store area area NUMBER(6, 2) ; --Assigning the value of radius radius NUMBER(1) := 3; --Constant value of PI pi CONSTANT NUMBER(3, 2) := 3.14; BEGIN --Formula for area and perimeter of a circle area := pi * radius * radius; dbms_output.Put_line('Area = ' || area); END;

Ans:

View can be described as virtual table which derived its data from one or more than one table columns. It is stored in the database. View can be created using tables of same database or different database. It is used to implement the security mechanism in the SQL Server [5].

For example:

Create table Emp_Details(EmpId int, EmpName nvarchar(200), EmpLogin nvarchar(20), EmpPassword nvarchar(20) , EmploymentDate datetime )

And for example, table has the following data of employees:

Hide Copy Code

EmpId EmpName EmpLogin Emppassword EmploymentDate 1 EmployeeA EmpA EmpAPwd 29/01/2006 2 EmployeeB EmpB EmpBPwd 06/02/2007 3 EmployeeC EmpC EmpCPwd 14/05/2007 4 EmployeeD EmpD Empd 30/03/2008 5 EmployeeE EmpE EmpEPwd 30/06/2007 6 EmployeeF EmpF EmpFPwd 12/09/2012

Use of a View

Views are used for security purposes because they provide encapsulation of the name of the table. Data is in the virtual table, not stored permanently. Views display only selected data.

When can a view be updated?

1. The view is defined based on one and only one table.

2. The view must include the PRIMARY KEY of the table based upon which the view has been created.

Page 5: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

3. The view should not have any field made out of aggregate functions.

4. The view must not have any DISTINCT clause in its definition.

5. The view must not have any GROUP BY or HAVING clause in its definition.

6. The view must not have any SUBQUERIES in its definitions.

7. If the view you want to update is based upon another view, the later should be updatable.

8. Any of the selected output fields (of the view) must not use constants, strings or value expressions.

Ans:

get e:/P1.sql;

1 declare

2 eno employee.emp_no%type;

3 ename employee.employee_name%type;

4 begin

5 eno:=&eno;

6 select emp_no,employee_name into eno,ename from employee where emp_no=eno;

7 dbms_output.put_line('---------output----------');

8 dbms_output.put_line('employee no :'||eno);

9 dbms_output.put_line('employee name :'||ename);

10* end;

Ans:

Student_Details is not 3 NF as Stud_id and Stud_name both are candidate keys and also the City is also functionally depends on Zip and Stud_id. So decomposition is essential.

Stud1 (Stud_id, Stud_Name, Zip)

Stud(Zip,City)

Both can be converted in third normal form.

Ans:

Referential integrity [7] refers to the accuracy and consistency of data within a relationship.

Page 6: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

In relationships, data is linked between two or more tables. This is achieved by having the foreign key (in the associated table) reference a primary key value (in the primary – or parent – table). Because of this, we need to ensure that data on both sides of the relationship remain intact.

So, referential integrity requires that, whenever a foreign key value is used it must reference a valid, existing primary key in the parent table.

Example

For example, if we delete record number 15 in a primary table, we need to be sure that there’s no foreign key in any related table with the value of 15. We should only be able to delete a primary key if there are no associated records. Otherwise, we would end up with an orphaned record.

Screenshot of a diagram depicting an orphaned record.

Here, the related table contains a foreign key value that doesn’t exist in the primary key field of the primary table (i.e. the “CompanyId” field). This has resulted in an “orphaned record”.

Here, the related table contains a foreign key value that doesn’t exist in the primary key field of the primary table (i.e. the “CompanyId” field). This has resulted in an “orphaned record”.

So referential integrity will prevent users from:

Adding records to a related table if there is no associated record in the primary table.

Changing values in a primary table that result in orphaned records in a related table.

Deleting records from a primary table if there are matching related records.

Consequences of a Lack of Referential Integrity

A lack of referential integrity in a database can lead to incomplete data being returned, usually with no indication of an error. This could result in records being “lost” in the database, because they’re never returned in queries or reports.

It could also result in strange results appearing in reports (such as products without an associated company).

Or worse yet, it could result in customers not receiving products they paid for.

Worse still, it could affect life and death situations, such as a hospital patient not receiving the correct treatment, or a disaster relief team not receiving the correct supplies or information.

Eg in SQL

Page 7: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

CREATE TABLE employee ( id number(5) Primary Key, name char(20), dept char(10), age number(2), salary number(10), location char(10), ALTER TABLE employee ADD CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY (id) );

CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), mgr_id number(5) REFERENCES employee(id), salary number(10), location char(10) );

Update & Delete Cascade of Referential Integrity:

ADD CONSTRAINT fk_SalesHistoryCustomerID FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE SET NULL ON UPDATE SET NULL

ALTER TABLE SalesHistory

ADD CONSTRAINT fk_SalesHistoryProductID FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ON DELETE CASCADE ON UPDATE CASCASE

If a delete statement affects one or more rows in a foreign key table, those rows will be deleted when the primary key record is deleted. If an update statement affects rows in the foreign key table, those rows will be updated with the value from the primary key record after it has been updated.

Ans:

Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Let’s discuss about anomalies first then we will discuss normal forms with examples.

Anomalies in DBMS

There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this.

Example: Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this:

Page 8: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

emp_id emp_name emp_address emp_dept

101 Rick Delhi D001

101 Rick Delhi D002

123 Maggie Agra D890

166 Glenn Chennai D900

166 Glenn Chennai D004

The above table is not normalized. We will see the problems that we face when a table is not normalized.

Update anomaly: In the above table we have two rows for employee Rick as he belongs to two departments of the company. If we want to update the address of Rick then we have to update the same in two rows or the data will become inconsistent. If somehow, the correct address gets updated in one department but not in other then as per the database, Rick would be having two different addresses, which is not correct and would lead to inconsistent data.

Insert anomaly: Suppose a new employee joins the company, who is under training and currently not assigned to any department then we would not be able to insert the data into the table if emp_dept field doesn’t allow nulls.

Delete anomaly: Suppose, if at a point of time the company closes the department D890 then deleting the rows that are having emp_dept as D890 would also delete the information of employee Maggie since she is assigned only to this department.

To overcome these anomalies we need to normalize the data. In the next section we will discuss about normalization.

Normalization

Here are the most commonly used normal forms:

First normal form(1NF)

Second normal form(2NF)

Third normal form(3NF)

Boyce & Codd normal form (BCNF)

First normal form (1NF)

As per the rule of first normal form, an attribute (column) of a table cannot hold multiple values. It should hold only atomic values.

Example: Suppose a company wants to store the names and contact details of its employees. It creates a table that looks like this:

emp_id emp_name emp_address emp_mobile

101 Herschel New Delhi 8912312390

102 Jon Kanpur 8812121212

9900012222

103 Ron Chennai 7778881212

104 Lester Bangalore 9990000123

8123450987

Two employees (Jon & Lester) are having two mobile numbers so the company stored them in the same field as you can see in the table above.

Page 9: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

This table is not in 1NF as the rule says “each attribute of a table must have atomic (single) values”, the emp_mobile values for employees Jon & Lester violates that rule.

To make the table complies with 1NF we should have the data like this:

emp_id emp_name emp_address emp_mobile

101 Herschel New Delhi 8912312390

102 Jon Kanpur 8812121212

102 Jon Kanpur 9900012222

103 Ron Chennai 7778881212

104 Lester Bangalore 9990000123

104 Lester Bangalore 8123450987

Second normal form (2NF)

A table is said to be in 2NF if both the following conditions hold:

Table is in 1NF (First normal form)

No non-prime attribute is dependent on the proper subset of any candidate key of table.

An attribute that is not part of any candidate key is known as non-prime attribute.

Example: Suppose a school wants to store the data of teachers and the subjects they teach. They create a table that looks like this: Since a teacher can teach more than one subjects, the table can have multiple rows for a same teacher.

teacher_id subject teacher_age

111 Maths 38

111 Physics 38

222 Biology 38

333 Physics 40

333 Chemistry 40

Candidate Keys: {teacher_id, subject}

Non prime attribute: teacher_age

he table is in 1 NF because each attribute has atomic values. However, it is not in 2NF because non prime attribute teacher_age is dependent on teacher_id alone which is a proper subset of candidate key. This violates the rule for 2NF as the rule says “no non-prime attribute is dependent on the proper subset of any candidate key of the table”.

To make the table complies with 2NF we can break it in two tables like this:

teacher_details table:

teacher_id teacher_age

111 38

222 38

333 40

Teacher_subject table:

teacher_id subject

Page 10: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

111 Maths

111 Physics

222 Biology Now the tables comply with Second normal form (2NF).

Third Normal form (3NF)

A table design is said to be in 3NF if both the following conditions hold:

Table must be in 2NF

Transitive functional dependency of non-prime attribute on any super key should be

removed.

An attribute that is not part of any candidate key is known as non-prime attribute.

In other words 3NF can be explained like this: A table is in 3NF if it is in 2NF and for each

functional dependency X-> Y at least one of the following conditions hold:

X is a super key of table

Y is a prime attribute of table

An attribute that is a part of one of the candidate keys is known as prime attribute.

Example: Suppose a company wants to store the complete address of each employee, they

create a table named employee_details that looks like this:

emp_id emp_name emp_zip emp_state emp_city emp_district

1001 John 282005 UP Agra Dayal Bagh

1002 Ajeet 222008 TN Chennai M-City

1006 Lora 282007 TN Chennai Urrapakkam

1101 Lilly 292008 UK Pauri Bhagwan

1201 Steve 222999 MP Gwalior Ratan

Super keys: {emp_id}, {emp_id, emp_name}, {emp_id, emp_name, emp_zip}…so on

Candidate Keys: {emp_id}

Non-prime attributes: all attributes except emp_id are non-prime as they are not part of

any candidate keys.

Here, emp_state, emp_city & emp_district dependent on emp_zip. And, emp_zip is

dependent on emp_id that makes non-prime attributes (emp_state, emp_city &

emp_district) transitively dependent on super key (emp_id). This violates the rule of 3NF.

To make this table complies with 3NF we have to break the table into two tables to remove

the transitive dependency:

employee table:

emp_id emp_name emp_zip

Page 11: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

1001 John 282005

1002 Ajeet 222008

1006 Lora 282007

1101 Lilly 292008

1201 Steve 222999

employee_zip table:

emp_zip emp_state emp_city emp_district

282005 UP Agra Dayal Bagh

222008 TN Chennai M-City

282007 TN Chennai Urrapakkam

292008 UK Pauri Bhagwan

222999 MP Gwalior Ratan

Boyce Codd normal form (BCNF)

It is an advance version of 3NF that’s why it is also referred as 3.5NF. BCNF is stricter than

3NF. A table complies with BCNF if it is in 3NF and for every functional dependency X->Y, X

should be the super key of the table.

Example: Suppose there is a company wherein employees work in more than one

department. They store the data like this:

emp_id emp_nationality emp_dept dept_type dept_no_of_emp

1001 Austrian Production and planning D001 200

1001 Austrian stores D001 250

1002 American design and technical support D134 100

1002 American Purchasing department D134 600

Functional dependencies in the table above:

emp_id -> emp_nationality

emp_dept -> {dept_type, dept_no_of_emp}

Candidate key: {emp_id, emp_dept}

The table is not in BCNF as neither emp_id nor emp_dept alone are keys.

To make the table comply with BCNF we can break the table in three tables like this:

emp_nationality table:

emp_id emp_nationality

Page 12: Answer: [1] · Normalization [6] is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Lets discuss about

1001 Austrian

1002 American

emp_dept table:

emp_dept dept_type dept_no_of_emp

Production and planning D001 200

stores D001 250

design and technical support D134 100

Purchasing department D134 600

emp_dept_mapping table:

emp_id emp_dept

1001 Production and planning

1001 stores

1002 design and technical support

1002 Purchasing department

Functional dependencies:

emp_id -> emp_nationality

emp_dept -> {dept_type, dept_no_of_emp}

Candidate keys:

For first table: emp_id

For second table: emp_dept

For third table: {emp_id, emp_dept}

This is now in BCNF as in both the functional dependencies left side part is a key.

Ref:

[1] https://slideplayer.com/slide/4559159/

[2] https://www.gatevidyalay.com/entity-sets-in-dbms/

[3] http://www.imfrosty.com/2014/09/components-overall-structure-dbms.html

[4] https://www.geeksforgeeks.org/area-and-perimeter-of-a-circle-in-pl-sql/

[5] https://www.codeproject.com/Tips/639239/Creating-and-Usage-of-View-in-SQL

[6] https://beginnersbook.com/2015/05/normalization-in-dbms/

[7] https://database.guide/what-is-referential-integrity/