DBMS Important Questions and Answers
DBMS Important Questions and Answers
DBMS Important Questions and Answers
Minimizing redundancy: Before entering data, we are creating definitions in which we are
imposing restrictions. Due to these restrictions, dbms will not allow duplicate data to be
entered. Restrictions are constraints like primary key, unique key, not null etc..
if we want only few columns data then we need not create another table. We can give access to
only those few columns to the users using views and data control language (DCL) commands. In
this way, the data is stored in only one location.
3. Sharing of data: We can share the data to authorized user using DCL commands
4. Data Security: We can create users and give them username and passwords. We can grant
and revoke permissions to create session, create table etc.. We can also give access to row or
column only by using views.
5. Flexibility of the system is improved: Due to data independence, changes in data does not
affect the application programs.
6. Data integrity: Constraints ensures data in database to be complete, correct, and consistent.
For example: By using check constraint and data types, we can ensure that numbers are not
entered in name field. By using commit command, data once stored in database will be
consistent.
7. Provides back up and recovery: due to which data once stored in database will never be lost.
1. Data
2. Procedures
3. Hardware
4. Software
5. People
refer notes
Hierachical data model, network data model, relational data model etc…refer notes
A data dictionary contains metadata i.e data about the data.The users of the database normally
don't interact with the data dictionary, it is only handled by the database administrators.
Physical information about the tables such as where they are stored and how.
Table constraints such as primary key attributes, foreign key information etc.
5. What is Specialization?
Relationship: Any association between two entity types is called a relationship. Entities take
part in the relationship.
Cardinality: Number of instances of one entity is associated with how many number of entities
of another entity
Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives
∧ (and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
A field with a NULL value is a field with no value. If a field in a table is optional, it is
possible to insert a new record or update a record without adding a value to this field. Then,
the field will be saved with a NULL value. A NULL value is different from a zero value or a
field that contains spaces. A field with a NULL value is one that has been left blank during
record creation!
To Test NULL value, we will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
9. Explain about roll back and commit?
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made by these
commands are not permanent, until the current session is closed, the changes made by these
commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as permanent.
COMMIT;
ROLLBACK command restores the database to last commited state. It is also used with
SAVEPOINT command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database, and realise
that those changes were not required, then we can use the ROLLBACK command to rollback
those changes, if they were not commited using the COMMIT command.
ROLLBACK TO savepoint_name;
Types of Triggers
1) Level Triggers
a) ROW LEVEL TRIGGERS: It fires for every record that got affected. It always use a FOR EACH
ROW clause
b) STATEMENT LEVEL TRIGGERS: It fires once for each statement that is executed.
2) Event Triggers
a) DDL EVENT TRIGGER: It fires with the execution of every DDL statement(CREATE, ALTER,
DROP, TRUNCATE).
b) DML EVENT TRIGGER: It fires with the execution of every DML statement(INSERT, UPDATE,
DELETE).
c) DATABASE EVENT TRIGGER: It fires with the execution of every database operation which
can be LOGON, LOGOFF, SHUTDOWN, SERVERERROR etc.
3) Timing Triggers
a) BEFORE TRIGGER: It fires before executing DML statement.
b) AFTER TRIGGER: It fires after executing DML statement.
Example : Create a trigger that fires after insert of every new row in Stu table. The trigger
automatically updates total.
create or replace trigger stud_marks
after INSERT
on
Stu
for each row
begin
update Stu set total = subj1 + subj2 + subj3;
end;
/
11. a. Define file and explain about draw backs of file based system?
(Or)
b. Describe about ANSI/SPARC database architecture?
12. a. Describe generalization and specialization with suitable examples?
(Or)
b. Define ER and Explain about components of ER diagrams?
13. a. What is constraint? Explain about Key constraint with suitable examples?
(Or)
b. Describe about relational algebra with suitable examples?
Explain about DDL, DML and DCL commands with suitable Examples?
SQL is a nonprocedural query language in which the expected results is given without the
specific details about how to accomplish the task.
SQL COMMANDS
SQL contains different types of commands
DDL (Data Definition Language) Commands
DML(Data Manipulation Language) Commands
TCL(Transaction Control Language) Commands
DCL(Data Control Language) Commands
I DDL Commands: The Data Definition Languages (DDL) Commands are used to create a table
structure, modify the table structure and delete the table structure.
CREATE TABLE : The CREATE TABLE command is used to create a new table
Syntax: Create tabletablename (column_name1 data_ type (size) constraint, …);
Example:
create table student (sname varchar2(20) , rollno number(10) ,dob date );
c. ALTER TABLE DROP To delete a column in a table, use the following syntax
ALTER TABLE table_name DROP COLUMN column_name1,… ;
Example: alter table student drop phone_no;
DROP: This command is used to delete the structure of the table as well as records in the
relation.
Syntax :drop tabletable_name;
Ex: drop table student;
Create AS select: This is used to create a new relation or table from an existing relation or
table.
Syntax:
create tablenew_table_name (column_1, …..column_n) as select column_1, ….column_n
from old_table_name;
Ex: create table student2(sno,sname,saddress) AS select sno, name, address from student;
TRUNCATING TABLE: Truncate command will delete all the records permanently in a
specified table but structure of the table will not be deleted.
• Syntax
– TRUNCATE TABLE TableName;
Eg: TRUNCATE TABLE student;
II Commands in DML:
The SQL INSERT INTO Statement: The INSERT INTO statement is used to insert new
records in a table. There are different forms of insert commands are used to insert a
records into a table.
Syntax
The first form of insert is used to insert values for all columns in a table.
INSERT INTO table_name VALUES (value1,value2,value3,...);
The second form of insert is used to insert values for some columns of a record in a table.
INSERT INTO table_name (column1,...) VALUES (value1,...);
Select: The select command is used to retrieve records from the tables.
This type is used to display the set of fields or all the fields of the selected or all records in a
table.
a. Syntax: Select * from <table_name> [WHERE condition];
Ex: select * from student;
b. Syntax: select set of fields from table_name [where condition];
Ex: selectsno,sname,addressfrom student where address=’BVRM’;
Update: The update command is used to update the content of a table.
Syntax
– UPDATE TableName SET ColumnName1=Expression1,
Ex1: update student SET total = sub1+sub2+sub3;
Ex2: UPDATE student SET total = sub1+sub2+sub3 wheresno = 10;
Delete: The delete command is used to delete all the records or selected records in a table.
But the structure of the table remains.
Syntax: DELETE FROM TableName;
Example 1: Delete from student;
Example 2:Delete from student where address=’BVRM’;
DCL Commands:
Two types of DCL commands are
1. Grant
2. Revoke
Grant: SQL Grant command is used to grant a privileges on the database objects to the
users.
The syntax for the GRANT command is:
GRANT privilege_name ON object_name TO user_name ;
Example: GRANT SELECT ON employee TO user1 ;
This command grants a SELECT permission on employee table to user1.
REVOKE : The revoke command removes user privileges to the database objects.
The syntax for the REVOKE command is:
REVOKE privilege_name ON object_name FROM User_name ;
For Example:
REVOKE SELECT ON employee FROM user1;
This command will revoke a SELECT privilege on employee table from user1.
INNER JOIN:
returns rows when there is a match in both tables.
LEFT JOIN:
returns all rows from the left table, even if there are no matches in the
right table.
RIGHT JOIN:
returns all rows from the right table, even if there are no matches in the
left table.
FULL JOIN:
returns rows when there is a match in one of the tables.
SELF JOIN:
is used to join a table to itself as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN:
returns the Cartesian product of the sets of records from the two or
more joined tables.
Consider two tables student and course as shown below:
cross join or cartesian join
natural join
innerjoin
outer joins
Explain about control structures of PL/SQL?
CONDITIONAL CONTROL STATEMNETS In PL/SQL there are three types of conditional
control : IF - THEN, IF-THEN-ELSE and CASE
IF – THEN Statement
This is the most basic kind of a conditional control and has the following structure
If Condition Then
Statement 1;
….
Statement 2;
End If;
The reserved word IF marks the beginning of the IF statement.
IF – THEN – ELSE
This statement enables you to specify two groups of statements One group of statements
is executed when the condition evaluates to TRUE and the other group of statements is executed
when the condition evaluates to FALSE.
If Condition Then
Statement 1;
ELSE
Statement 2;
End If;
Statement 3;
CASE:
A case statement has the following structure:
CASE SELECTOR
WHEN EXPRESSION 1 STATEMENT 1;
WHEN EXPRESSION 1 STATEMENT 1;
…..
WHEN EXPRESSION 1 STATEMENT 1;
ELSE STATEMENT N+1;
END CASE;
The reserved word CASE marks the beginning of the case statement. A selector is a value that
determines which WHEN clause should be executed.
ITERATIVE CONTROL
In PL/SQL there are three types of loops : Simple LOOP, WHILE loops and Numeric FOR
loop
A simple loop, as you can see from its name, is the most basic kind of loop and has the following
structure:
LOOP
STATEMENT 1;
STATEMENT 2;
…….
STATEMENT N;
END LOOP;
The reserved word LOOP marks the beginning of the simple loop. Statement 1 through N are a
sequence of statements that is executed repeatedly.
EXIT statement causes a loop to terminate when exit condition evaluates to TRUE.
LOOP
STATEMENT 1;
STATEMENT 2;
IF CONDITION THEN
EXITL;
END IF;
…….
STATEMENT N;
END LOOP;
WHILE LOOPS:
A while loop has the following structure
Use OUT parameter to return the Use RETURN to return the value
value
RETURN will simply exit the control RETURN will exit the control from
from subprogram. subprogram and also returns the value
Return datatype will not be specified Return datatype is mandatory at the time of
at the time of creation creation
To use Function:
DECLARE
c NUMBER(2);
BEGIN
c := totalcustomers();
dbms_output.put_line('Total number of customers:'||c);
END;
O/P:
Total number of customers:4
Statement processed.