23CS1303 Unit 2 DBMS R 2023 PDF
23CS1303 Unit 2 DBMS R 2023 PDF
1
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
CONCEPT OF RELATIONS:
❖ Relation Schema: A relation schema represents the name of the
❖ Relation key - Every row has one, two or multiple attributes, which is
2
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
● Database Instance :
● Schema diagram :
3
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
The null value is a special value that signifies that the value is unknown or does
not exist.
4
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Advantages :
● Simplicity: A relational data model is simpler than the hierarchical and
network model.
● Structural Independence: The relational database is only concerned with
data and not with a structure
● Easy to use
5
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Types of keys ,
1. Primary Key
2. Super Key
3. Candidate Key
4. Composite / Compound Key
5. Foreign Key
● { Emp_Number }
● { Emp_SSN, Emp_Number }
● { Emp_SSN, Emp_Name }
● { Emp_Number, Emp_Name }
candidate key. Each Candidate Key can work as Primary Key. In short,
minimal superkeys are called candidate keys. Candidate key is a subset of
super key.
Example 1:
The candidate keys we have selected are:
● { Emp_SSN }
● { Emp_Number }
6
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Example 2:
● {Emp_id}
● {Emp_SSN}
● {Email}
❖ Composite / Compound 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.
Example ,
7
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
❖ Foreign Key – Foreign keys are the columns of a table that points to the
Student Table
CREATE TABLE Student (Studid number PRIMARY KEY,
RollNovarchar(10) NOT NULL,Name varchar2(50) NOT NULL, EnrollNo
number UNIQUE,Addressvarchar(200) NOT NULL, DeptID number
FOREIGN KEYREFERENCES Department(DeptID))
8
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
1 Primary key uniquely identify a Foreign key is a field in the table that is
record in the table. primary key in another table.
2 Primary Key can't accept null Foreign key can accept multiple null
values. value.
3 We can have only one Primary We can have more than one foreign key
key in a table. in a table.
4 By default, Primary key is Foreign key do not automatically create
clustered index an index, clustered or non-clustered.
RELATIONAL ALGEBRA
✔ Select
✔ Project
✔ Rename
✔ Union
✔ Set difference
✔ Set intersection
✔ Cartesian product
9
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
✔ Assignment.
10
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
SET OPERATORS:
The Union Operation (∪):
This operation is used to fetch data from two relations(tables) or temporary
relation(result of another operation).
Syntax: A∪B
where A and B are relations.
Note: For this operation to work, the relations(tables) specified should
have same number of attributes(columns) and same attribute
domain(i.e.data types).
Example:
Consider the relation SUPPLIER and PARTS,
Result:
Result:
11
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Syntax: A - B
where A and B are relations.
Example:
Syntax: AX B
BINARY OPERATORS:
JOIN Operator(⋈ ) :
JOIN is used to combine related tuples from two relations.
Join operation is essentially a cartesian product followed by a selection criterion.
Natural Join( ⋈ )
Natural join can only be performed if there is a common attribute (column)
between the relations. The name and type of the attribute must be same. Such
12
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
joins result in two attributes in the resulting relation having exactly the same
value. A `natural join' will remove the duplicate attribute(s).
Example :
SELECTemp_no,emp_name,job_name,dep_name,location
FROMemp_mast
NATURALJOINdep_mast;
Output :
EMP_NO EMP_NAME JOB_NAME DEP_NAME LOCATION
---------- -------------------- ---------- ---------- ----------
1234 Alex Clerk FINANCE PARIS
2345 Jack Consultant MARKETING LONDON
3456 Paul Manager FINANCE PARIS
Example Database table:
Table Name :emp_mast
EMP_NO EMP_NAME JOB_NAME MGR_ID DEPT_NO
------------ -------------------- ----------------- ------------- ---------------
1234 Alex Clerk 4567 15
2345 Jack Consultant 3456 25
3456 Paul Manager 1234 15
4567 Jenefer Engineer 2345 45
Example :
SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mastINNER JOINdep_mastUSING(dept_no);
Output :
EMP_NO EMP_NAME JOB_NAME DEP_NAME LOCATION
--------- ---------- ---------- ---------- --------
1234 Alex Clerk FINANCE PARIS
13
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
● Non-Equi Join:
EQUIJOINS ( = ) :
An equijoin is a join with a join condition containing an equality operator.
This is represented by (=) sign. This join retrieves information by using equality
condition.
Example : Table Name : emp_mast
Example SQL :
SELECT emp_no,emp_name,job_name,dep_name FROM emp_maste, dep_mastd
WHERE e.dept_no=d.dept_no;
Output:
14
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Example SQL :
SELECT emp_no,emp_name,job_name,dep_name FROM emp_maste, dep_mastd
WHERE e.dept_no>d.dept_no;
Output :
EMP_NO EMP_NAME JOB_NAME DEP_NAME
---------- -------------------- ---------- ----------
2345 Jack Consultant FINANCE
4567 Jenefer Engineer FINANCE
4567 Jenefer Engineer MARKETING
4567 Jenefer Engineer HR
OUTER JOINScan be used when we want to keep all the tuples in R, all those in S,
or all those in both relations regardless of whether they have matching tuples in
the other relation.
Relation Loan
Relation borrower
15
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Example:
(A ∩ B) (A ∩ B) U A
(A ∩ B) UB (A ∩ B) U(A–B)U (B–
A)
16
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
A self join is such a join in which a table is joined with itself. For example,
when you require details about an employee and his manager (also an
employee).
Example SQL :
SELECT a1.emp_no,a2.emp_name,a1.job_name,a2.dept_no
FROM emp_masta1,emp_mast a2
WHERE a1.emp_no=a2.mgr_id;
CROSS JOINS:
A Cross Join or Cartesian join or Cartesian product is a join of every row of
one table to every row of another table.
Example :
SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast CROSS JOIN dep_mast;
Summary :
Inner Join Inner join, includes only those tuples that satisfy the matching
criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is
denoted by symbol θ.
17
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
EQUI Join ( = ) When a theta join uses only equivalence condition, it becomes
aequi join.
Outer Join In an outer join, along with tuples that satisfy the matching
criteria.
Left Outer Join( ) In the left outer join, operation allows keeping all tuple in the
left relation.
Right Outer join( In the right outer join, operation allows keeping all tuple in the
) right relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in
the result irrespective of the matching condition.
SQL INTRODUCTION
A query languageis a language in which a user requests information from
the database.
In general two types,
18
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
● No coding needed
● Portability
● Interactive Language
Disadvantages of SQL
● Difficult Interface
● Partial Control
● Implementation
● Cost
Applications of SQL
● Data Integration Scripts
● Analytical Queries
● Retrieve Information
1-2000 bytes
2. Number Datatypes :
✔ Number – {p=38,s=0}
19
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
4. Raw Datatype: used to store byte oriented data like binary data
and byte string.
5. Other :
sounds.
DDL COMMANDS:
● DDL stands for Data Definition Language.
● It is a language used for defining and modifying the data and its structure.
20
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
All DDL commands are auto-committed. That means it saves all the
changes permanently in the database.
The commands used are:
✔ CREATE Table – Create a new Table, database, schema
● Data type
● Size(column width).
For example ,
Table Name : Student
Column name Data type Size
Reg_no varchar2 10
Name char 30
DOB date
Address varchar2 50
Command :
CREATE TABLE Student ( Reg_no number(10),Name char(30),DOB
date,Address varchar2(50));
Data Constraint :
✔ Oracle allows constraints for attaching in the table columns via SQL
syntax that checks data for integrity.
✔ The constraints is a rule which works into the table, it disallows the that
data which are not following the rule of data constraints.
INTEGRITY CONSTRAINT
An integrity constraint is a mechanism used by oracle to prevent invalid
data entry into the table. It has enforcing the rules for the columns in a table. The
types of the integrity constraints are:
a) Domain Integrity - Not Null , Check
b) Entity Integrity - Unique , Primary Key
c) Referential Integrity - Foreign Key , References Key
General Types of Data Constraints:
21
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Input/output Constraints
1. The Primary key Constraint
2. The Foreign key Constraint
Business Rule Constraints
1. Check Constraint
2. Unique Constraint
3. NOT NULL Constraint
Primary key :
❖ A primary key is one or more column(s) in a table used to uniquely
identify each row in the table. A primary key column in a table has special
attributes :
❖ A primary key is the combination of (NOT NULL+UNIQUE) key constraints.
Unique key :
The Unique key disallows the similar value in same column of a table.
Syntax :
<col_name><data_type> (<size>) UNIQUE
For Example,
CREATE TABLE Student(Reg_no varchar2(10) PRIMARY KEY,Name
varchar2(20),
Phone number(10) UNIQUE, Address varchar2(20) default ‘Chennai’, DOB date
Not Null , city varchar2(15) CHECK (country IN(‘india’,’America’)));
NULL Value :
A NULL value is different from a blank or a zero.
NOT NULL Constraint
When a column is defined as NOT NULL , then that column becomes a
mandatory column. it must be enter a value into that column.
For Example,
CREATE TABLE Student(Reg_no varchar2(10) PRIMARY KEY, Name
varchar2(30) NOT NULL, Phone number(10) UNIQUE,Address
varchar2(50)NOT NULL , DOB date);
Creating a Database:
create database database-name;
Example,
create database Test;
database. By Using this add additional column, drop existing column and
even change the data type of columns
The general syntax :
23
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Syntax:
TRUNCATE TABLE <Table_name>
Example:
TRUNCATE TABLE Student;
Rename :
The rename command is used to rename a table or object.
Syntax,
RENAME TABLE <old_name> TO <new_name>;
For Example ,
RENAME TABLE emp TO employee;
24
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
DML COMMAND :
Data Manipulation Language (DML) statements are used for managing
data in database. DML commands are not auto-committed. It means changes
made by DML command are not permanent to database, it can be rolled back.
✔ INSERT :used for inserting a data into a table.
✔ UPDATE :used to modify the records present in existing table.
✔ DELETE : used to delete some or all records from the existing table.
✔ SELECT : used to retrieve data from the database
INSERT command
Insert command is used to insert data into a table.
INSERT into table-name values(data1,data2,..)
For example ,
// Insert all field values
INSERT into Student values (102 , 'Alex' , null);
// Insert particular Field values
INSERT into Student(id,name) values(102,'Alex');
● UPDATE Statement:
- Used to modify or update an already existing row or rows of a table.
Updating Single column:
Syntax:
UPDATE “tablename” SET “column1” = [new value] WHERE
“condition”;
Consider the following table Store_information
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
Los Angeles 1500 jan-05-1999
San Diego 250 jan-07-1999
Los Angeles 300 jan-08-1999
Boston 700 jan-08-1999
25
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
eg,
SQL> UPDATE store_information SET sales=600,txn_date= ‘jan-15
1999’ WHERE store_name= ‘San diego’;
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
Los Angeles 1500 jan-05-1999
San Diego 600 jan-15 1999
Los Angeles 500 jan-08-1999
Boston 700 jan-08-1999
● DELETE Statement:
- Delete statement is used to remove records from the table.
Syntax:
DELETE FROM “table_name” WHERE “condition”;
SQL> DELETE FROM store_information WHERE store_name= ‘los
angeles’;
Result:
San Diego 600 jan-15 1999
Boston 700 jan-08-1999
SELECT STATEMENT:
● SELECT command is used to retrieve the specific data from the database.
● It returns a result set of records from one or more tables.
SELECT Command has many optional clauses are as stated below:
Clause Description
26
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
Los Angeles 1500 jan-05-1999
San Diego 250 jan-07-1999
Los Angeles 300 jan-08-1999
Boston 700 jan-08-1999
Distinct:
SELECT keyword allows us to grab all information from a column(or columns) on
a table.
This necessarily means that there will be redundancies.
To select each distinct element, add DISTINCT after SELECT .(I,e) This keyword is
used along with select keyword to display unique values from the specified
column. It avoids duplicates during display.
Syntax:
SELECT DISTINCT “ column_name” FROM “table_name”;
SQL> SELECT DISTINCT store_name FROM store_information;
Result:
store_name
Los Angeles
San Diego
Boston
WHERE Clause:
WHERE Clause is used to filter the result based on ceratin conditions.
Syntax:
SELECT “ column_name” FROM “table_name” WHERE “ condition”;
To select all stores with sales above $1000 in table store_information,
SQL>SELECT store_name FROM store_information WHERE
sales>1000
Result:
store_name
Los Angeles
28
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
IN Operator:
Syntax for using IN keyword is as follows,
SELECT “column_name” FROM “table_name” WHERE “ column_name”
IN (value1, value2..);
BETWEEN Operator:
In order to select data that is within a range of values, the between operator is
used. (AND should be included).
Syntax:
SELECT “ column_name “ FROM “ table_name” WHERE “column_name”
BETWEEN (‘value1, value 2’);
SQL> SELECT * FROM store_information WHERE txn_date BETWEEN
‘JAN-06-1999’ and ‘JAN-10-1999’
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
San Diego 250 jan-07-1999
Los Angeles 300 jan-08-1999
Boston 700 jan-08-1999
We can also use BETWEEN operator to exclude a range of values by addingNOT
infront of BETWEEN.
SQL> SELECT * FROM store_information WHERE sales NOT BETWEEN 280
and 1000;
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
29
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
Los Angeles 1500 jan-05-1999
San Diego 250 jan-07-1999
Los Angeles 300 jan-08-1999
ARITHMETIC Operation:
The select clause may also contain arithmetic expressions involving the
operators +,-,*, and / operating on constants or attributes of tuples.
Result:
store_name sales
------------------------------
Los Angeles 1510
San Diego 260
Los Angeles 310
Boston 710
CONCATENATION Operator :
This combines information from two or more columns in a sentence according to
the format specified.
Eg,
SQL> select Store_name || ' has got very good ' || ‘sales’ ||sales|| ' on ' ||
txn_date AS "REPORT" from store_information;
Result:
REPORT
-------------------------------------------------------------------
Los angeles has got very good sales 1500 on jan-05-1999
30
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
AGGREGATE Functions:
Consider the following store_information table,
store_name sales txn_date
------------------------------ ---------- --------- -------------------- -
Los Angeles 1500 jan-05-1999
San Diego 300 jan-07-1999
Los Angeles 300 jan-08-1999
Boston 900 jan-08-1999
31
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
The order by clause arranges the contents of the table in ascending order (by
default) or in descending order (if specified explicitly) according to the specified
column.
If both ASC and DESC are not specified, then default order is ASC.
32
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
Result:
txn_date (SUM) sales
--------- -------------------- ---
jan-05-1999 1500
jan-07-1999 250
jan-08-1999 1000
HAVING CLAUSE:
The having clause can be used in conjunction with the group by clause.
Having imposes a condition on the group by clause, which further filters the
groups created by the group by clause.
Syntax:
SELECT “column_name1”, Function(“column_name2”) FROM
“table_name” [GROUP_BY “column_name1”] HAVING (arithmetic
function conditions);
Eg,
SQL> SELECT store_name, SUM(sales) FROM store_information
GROUP_BY store_name HAVING SUM(sales)>1500;
Result:
store_name SUM(sales)
------------------------------ ----------
Los Angeles 1800
SUB QUERIES:
Subqueries can be placed in a number of SQL clauses, including the WHERE
clause, the HAVING clause, and the FROM clause.
The subquery is enclosed in parentheses.
The subquery is placed on the right side of the comparison condition.
Eg,
SQL>SELECT store_name, sales, txn_date FROMstore_information
WHERE txn_date> = ( SELECT txn_date FROM Store_information
WHERE store_name = ‘Los Angeles’);
Result:
store_name sales txn_date
------------------------------ ---------- --------- -------------------- ---
Los Angeles 1500 jan-05-1999
San Diego 250 jan-07-1999
San Francisco 300 jan-08-1999
Boston 700 jan-08-1999
33
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
rollback.
COMMIT :
Commit command is used to permanently save any transaction into
database.
Syntax :
COMMIT [WORK] ;
For Example
SQL> insert into emp (empno,ename,sal) values (101,’Abid’,2300);
SQL> commit;
ROLLBACK :
To rollback the changes done in a transaction give rollback statement.
Rollback restore the state of the database to the last commit point.
Syntax :
Rollback to savepoint-name;
For example,
SQL> delete from emp;
SQL> rollback;
SAVEPOINT :
Specify a point in a transaction to which later you can roll back.
Syntax :
savepointsavepoint-name;
For Example,
34
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
1. GRANT
2. REVOKE
● It is used to grant or revoke access permissions from any database user.
GRANT Command :
● GRANT command gives user's access privileges to the database.
Syntax:
GRANT <privilege list> ON <relation name or view name> TO <user/role
list>;
Example,
GRANT ALL ON employee TO pec;
GRANT insert, update ON employee TO pec;
REVOKE command :
It is used to cancel previously granted or denied permissions.It revokes access
privileges for database objects previously granted to other users.
Syntax :
REVOKE <privilege list> ON <relation name or view name> FROM
<user name>;
Example :
REVOKEupdate ON employee from pec;
35
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
⮚ Programs written in the host language can use the embedded SQL
change at runtime.
36
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
1. Connection to DB:
The first step in using embedded SQL is establishing a connection to the
database. This is done using the CONNECT keyword, preceded by EXEC SQL to
indicate that it is a SQL statement.
Connect to the database :
EXEC SQL connect to oracle user PECIT using SIT;
EXEC SQL CONNECT db_name;
2. Declaration Section:
The declaration section is used to declare variables that will be used in SQL
queries and to capture the results of those queries.
Types of variables used in the host language are: Host Variable Declaration &
Indicator Variable Declaration.
Host Variable Declaration :
These are the variables of host language used to pass the value to the
query as well as to capture the values returned by the query. Variables of the
host language can be used within embedded SQL statements, but they must be
preceded by a colon(:) to distinguish them from SQL variables.
section.
37
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
❖ It substitutes the value of host variable and compiles when using pre-
Indicator Variable :
The indicator variable sets/gets NULL values to the column, it passes/ gets
different integer values(ie. 0-Correct value ,1- NULL Value , > 0 –
Trimmed(Original Length ) the Column , , -2 - Trimmed(Original Length is
unknown ))
Example :
EXEC SQL SELECT std_name INTO :sname INDICATOR :ind_sname
FROM student WHERE student_id =:std_id;
3. Execution Section :
This is the execution section, and it contains all the SQL queries and
statements prefixed by ‘EXEC SQL’.
Example:
EXEC SQL SELECT * FROM student WHERE student_id =:std_id;
38
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
DYNAMIC SQL
character string at runtime, submit the query, and then retrieve the
result into program variables a tuple at a time.
compilation time. This means that the compiler cannot check for errors
39
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
40
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
*****************
PART A:
41
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
✔ The values placed in primary key columns must be unique for each row.
▪INTERSECTION
▪DIFFERENCE
42
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
▪CARTESIAN PRODUCT
Unary relational operators
▪SELECT
▪PROJECT
Binary Relational Operator
▪JOIN
▪DIVIDE
5. What are the four major categories of constraints. [Nov/Dec’10] / What are
the Different Types of Keys in Database [June 2021]
SuperKey, Candidate Key, Primary Key, Foreign Key
▪You can add DISTINCT to avoid duplicates. Duplicated rows in tables can be
restricted with UNIQUE keys.
8. Define DDL,DML,TCL and DCL [May/June’15]/ Data Definition Language:
(DDL) [Nov/Dec’16] Data Definition Language: (DDL) [Nov/Dec’16]
- DDL statements are used to create, change, and remove data structures from the
database.
43
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
9. Define DML[May/June’15]
A data manipulation language(DML) used for Selecting , inserting, deleting
and updating data in a database.
Used for managing data within schema objects.
● SELECT - retrieve data from the a database
● DELETE - deletes all records from a table, the space for the records
remain.
▪Transactions allow users to make changes to data and then decide whether to
save or discard the work
- COMMIT
- ROLLBACK
44
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
- SAVEPOINT
13. What are aggregate Functions? List the aggregate functions used in
SQL.?
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
+----------------+ +----------------+
| Users | | Payments |
+----------------+ +----------------+
| UserID (PK) | | PaymentID (PK) |
45
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
soln: eg,
| borrower | bank_manager | loan_amount |
|---------- |------------- -|-------------|
Alice John 5000
Bob Mary 7000
Charlie John 8000
David Mary 6000
Natural Join:
| borrower | bank_manager | loan_amount | bank_manager | loan_amount |
--------- -------------- ----------------------------------------
Alice John 5000 John 8000
Bob Mary 7000 Mary 6000
Output:
2
PART B:
46
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
4. Explain using queries How a SQL relation can be defined using various
constraints? Also, Explain Other Data Definition Language Statements in
Detail
5. Explain about DDL (8) DML(4) EMBEDDED SQL (8) [May/Jun‘16]
[Nov/Dec’14]
6. Describe six clauses in SQL Query and show what type of constructs can be
specified in each of the six clauses. [Nov/Dec’15]
7. Explain select, project and Cartesian product operations in relational
algebra with an example. [Nov/Dec’16]/List the operations of relational
algebra and purpose of each with example. [April/May'17]
8. Explain Embedded SQL In detail./What is embedded SQL.Give Example.
[Nov/Dec'16]/Justify the need for embedded SQL. Consider the relation
student (Regno,name,mark and grade).Write embedded dynamic SQL
program in C language to retrieve all the students records whose mark is
more than 90. [April/May'17]
9. What are the several parts of SQL query language ? What are the basic built
in types used during SQL create statement? State and given example for the
basic structure of SQL queries? [June 2021]
10. Consider the following relations and write the queries using SQL and
Relational Algebra:[ Nov/Dec 2023]
Student(rollno,name,deptid,gender,advisor)
Course(courseid,cname,deptid,credits)
Enrollment(rollno,courseid,grade)
Faculty(empid,name,doj,deptid,salary)
Teaching(empid,courseid)
Department(deptid,name,hod)
1. Retrieve the student's name who have not registered for DBMS and DAA.
2. Retrieve the faculty name and course name who are teaching ECE courses
with 3 credits
3. Retrieve the students name registered for ECE and CSE courses
4. Retrieve the faculty details who are handling more than one course.
Soln:
1. SELECT DISTINCT s.name FROM Student s WHERE s.rollno NOT IN
( SELECT e.rollno FROM Enrollment e WHERE e.courseid IN ('DBMS', 'DAA')
);
2. SELECT f.name AS faculty_name, c.cname AS course_name FROM Faculty f
JOIN Teaching t ON f.empid = t.empid JOIN Course c ON t.courseid =
c.courseid WHERE c.deptid = 'ECE' AND c.credits = 3;
3. SELECT DISTINCT s.name FROM Student s JOIN Enrollment e ON s.rollno =
e.rollno JOIN Course c ON e.courseid = c.courseid WHERE c.deptid IN ('ECE',
'CSE');
4. SELECT f.* FROM Faculty f JOIN Teaching t ON f.empid = t.empid
GROUP BY f.empid HAVING COUNT(t.courseid) > 1;
47
Unit – II 23CS1303 DATABASE MANAGEMENT SYSTEMS PEC/IT
PART C
1. Write DDL, DML, DCL commands for the student database which contains
student details : name , id, DOB, branch, DOJ and Course details:
Coursename, Stud_id, Faculty_name, id, Marks [Nov/Dec’17]
2. Consider the relation schema givenbelow, design and draw an ER diagram
that capture the information of this scheme.
Employee(empno,name,office,age)
Books(isbn,title,authors,publisher)
Loan(empno,isbn,date)
Write the following queries in relational algebra and SQL.
(i) Find the names of employees who have borrowed a book published by
McGraw-Hill
(ii) Find the names of employees who have borrowed all books published
by McGraw -Hill [April/May'17]
48