Mid Term Exam DBMS, May 2023-1
Mid Term Exam DBMS, May 2023-1
Answer:
The three-schema architecture can be used to explain the concept of data independence,
which can be defined as the capacity to change the schema at one level of a database
system without having to change the schema at the next higher level.
1. Logical data independence :Itis the capacity to change the conceptual schema
without having to change external schemas or application programs. We may
change the conceptual schema to expand the database (by adding a record type or
data item), or to reduce the database (by removing a record type or data item). In
the latter case, external schemas that refer only to the remaining data should not be
affected. Only the view definition and the mappings need be changed in a DBMS
that supports logical data independence. Application programs that reference the
external schema constructs must work as before, after the conceptual schema
undergoes a logical reorganization. Changes to constraints can be applied also to
the conceptual schema without affecting the external schemas or application
programs.
2. Physical data independence : It is the capacity to change the internal schema
without having to change the conceptual (or external) schemas. Changes to the
internal schema may be needed because some physical files had to be reorganized
- for example, by creating additional access structures - to improve the performance
of retrieval or update. If the same data as before remains in the database, we should
not have to change the conceptual schema.
`
(c) Compare and Contrast between relational algebra and relational calculus.
Answer:
Answer:
Join in DBMS is a binary operation which allows you to combine join product and selection in
one single statement. The goal of creating a join condition is that it helps you to combine the
data from two or more DBMS tables. The tables in DBMS are associated using the primary key
and foreign keys.
Types of Join
There are mainly two types of joins in DBMS:
I. Inner Join
Inner Join is used to return rows from both tables which satisfy the given condition. It is the
most widely used join operation and can be considered as a default join-type.An Inner join or
equijoin is a comparator-based join which uses equality comparisons in the join-predicate.
However, if you use other comparison operators like “>” it can’t be called equijoin.
1. Theta join
2. Natural join
3. EQUI join
1. Theta Join
Theta Join allows you to merge two tables based on the condition represented by theta. Theta
joins work for all comparison operators. It is denoted by symbol θ. The general case of JOIN
operation is called a Theta join.
Syntax:
A ⋈θ B
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
For example:
2. EQUI Join
EQUI Join is done when a Theta join uses only the equivalence condition. EQUI join is the
most difficult operation to implement efficiently in an RDBMS, and one reason why RDBMS
have essential performance problems.
For example:
Natural Join does not utilize any of the comparison operators. In this type of join, the attributes
should have the same name and domain. In Natural Join, there should be at least one common
attribute between two relations.It performs selection forming equality on those attributes which
appear in both relations and eliminates the duplicate attributes.
Example:
C
Num Square
2 4
3 9
D
Num Cube
2 8
3 18
C⋈D
An Outer Join doesn’t require each record in the two join tables to have a matching record. In this
type of join, the table retains each record even if no other matching record exists.
A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
(A B)
Right Outer Join returns all the columns from the table on the right even if no matching rows
have been found in the table on the left. Where no matches have been found in the table on the
left, NULL is returned. RIGHT outer JOIN is the opposite of LEFT JOIN.
For e.g., let’s assume that you need to get the names of members and movies rented by them.
Now we have a new member who has not rented any movie yet.
A B
In a Full Outer Join , all tuples from both relations are included in the result, irrespective of the
matching condition.
Example:
(A B)
Answer:
SQL Commands
SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
All the command of DDL are auto-committed that means it permanently save
all the changes in the database.
CREATE
ALTER
DROP
TRUNCATE
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE table_name;
Example
DROP TABLE EMPLOYEE;
c. ALTER: It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new
attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
To modify existing column in the table:
ALTER TABLE table_name MODIFY(column_definitions....);
EXAMPLE
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20)); ALTER TAB
LE STU_DETAILS MODIFY (NAME VARCHAR2(20));
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
DML commands are used to modify the database. It is responsible for all form of
changes in the database.
The command of DML is not auto-committed that means it can't permanently save
all the changes in the database. They can be rollback.
INSERT
UPDATE
DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2
, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [W
HERE CONDITION]
For example:
UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3'
Q2 (a) Consider a university which has several schools. Each school is headed by a
teacher and offers one or more courses. A course can be offered by only one school. A
course can be offered by only one school. A course may be full time course or part time
course. Each course consists of a set of subjects. Each school may have one or more
teachers. A teacher can teach one or more subjects. The student can get admission in
only one course. You may assume appropriate attributes of each entity type and
relationship type. State assumptions clearly, if any.
Relational Algebra
Relational Algebra
c) List all booking for hotel ITC Maurya by the guest 'G010' using subquery.
SQL Command
Relational Algebra
Answer:
1. Physical Level:
2. Conceptual Level:
Conceptual level describes the structure of the whole database for a group of users.It is
also called as the data model.
Conceptual schema is a representation of the entire content of the database.
These schema contains all the information to build relevant external records.
It hides the internal details of physical storage.
3. External Level:
External level is related to the data which is viewed by individual end users.
This level includes a no. of user views or external schemas. This level is closest to the
user.
External view describes the segment of the database that is required for a particular user
group and hides the rest of the database from that user group.
(4) (b) What are integrity constraints? Explain different integrity constraints with the
help of examples.
Answer:
In DBMS, Integrity constraints can be defined as a set of rules that are used to maintain
the information’s quality. This ensures that the data integration is not affected at all by
data insertion, updation or other processes. Hence integrity constraints are like
insurance to guard the database if there is any accidental damage.
1. Domain constraint
2. Entity integrity constraint
3. Referential integrity constraint
4. Key constraint
1. Domain constraint
In DBMS, Domain constraints can be defined as a set of values that are valid for an
attribute. The domain’s data type includes string, character, integer, time etc. The value
must be in the corresponding domain of the attribute.
Example
Now in the table above, we can observe in the column Age, the data type of the domainis
an integer but the attributes data type is a character.
This constraint states that in DBMS we can not make the primary key with the value
NULL. Now, this is because if the primary key is NULL, then we won’t be able to
determine or identify the tuple in the relation.
But in a relation, there can be NULL values but they must be not the primary key.
Example
In the above example, S_No column and last tuple, the attribute is null, so it can not be
assigned as the primary key.
3. Referential integrity constraint
This constraint is defined between two tables. Let us consider tables A and B, so in this
constraint, if a foreign key is referred to as a primary key of another table then the
contents of the foreign key of table A must be null or available to table B.
Example
TABLE 1
TABLE 2
Code City
4178 Patna
2451 Delhi
9654 Mumbai
1258 Pune
Now in the example above, we can see that in table 1, Code 5758 is not valid, as this
attribute is not defined in table 2 and also the attribute is assigned as the primary key,
and also Code in table 1 is assigned the foreign key.
4. Key constraint
In DBMS, a key is used to uniquely identify an entity in an entity set. An entity set can
have multiple keys but out only one can be a primary key. This primary key should not
be null and must be unique. Although it can contain a null and a non-null unique value.
Example
In the example above, S_Nocan not be defined as a primary key because it contains a
duplicate value. All the rows must be unique.