0% found this document useful (0 votes)
76 views20 pages

Dbms Final Notes

SQL stands for Structured Query Language. It is a programming language designed for managing data held in relational databases. Some key points about SQL: - SQL lets you access and manipulate databases. Common operations include querying data, inserting/updating/deleting records, and creating/modifying database structures. - SQL databases have a table-based structure with rows and columns, similar to a spreadsheet. Each row represents a record/tuple and each column represents a field/attribute. - The most common types of SQL statements are SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP. - SQL is an ANSI/ISO standard language, but some vendors have their own proprietary extensions in addition to

Uploaded by

dragon ball
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views20 pages

Dbms Final Notes

SQL stands for Structured Query Language. It is a programming language designed for managing data held in relational databases. Some key points about SQL: - SQL lets you access and manipulate databases. Common operations include querying data, inserting/updating/deleting records, and creating/modifying database structures. - SQL databases have a table-based structure with rows and columns, similar to a spreadsheet. Each row represents a record/tuple and each column represents a field/attribute. - The most common types of SQL statements are SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP. - SQL is an ANSI/ISO standard language, but some vendors have their own proprietary extensions in addition to

Uploaded by

dragon ball
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture 5

Database Model
Ques. What is Database Model?
Sol.
• A database model is a type of data model that determines the logical structure of
a database and fundamentally determines in which manner data can be stored,
organized, and manipulated.
• The most popular example of a database model is the relational model, which
uses a table-based format.
Ques. What is Data Model?
Sol.
• A collection of conceptual tools for describing data, data relationships, data
semantics, and consistency constraints.
• A data model provides a way to describe the design of a database at the physical,
logical, and view levels.
• A data model organizes data elements and standardizes how the data elements
relate to one another.
Ques. Categories of Data Model?
Sol.
The data models can be classified into three different categories:
1. Object based logical model: Object-based logical models describe data at the
conceptual and view levels. Various types of object based logical models are as
follows: Entity-Relationship Model, Object-Oriented Database Management
System Model.

Figure: Object Based Logical Model


2. Record based logical model: When the database is organized in some fixed
format of records of several than the model is known as Record-Based Data
Model. It has a fixed number of fields or attributes in each record type and each
field is usually of a fixed length. Three well-known data models of this type are
relational data models, network data models and hierarchical data models.

Figure: Record Based Logical Model


3. Physical model: This Data Model describes HOW the system will be implemented
using a specific DBMS system. This model is typically created by DBA and
developers. The purpose is actual implementation of the database.

Ques: Brief Object Based Database Model?


Sol.
• Object-based logical models describe data at the conceptual and view levels.
• Provide fairly flexible structuring capabilities.
• Allow one to specify data constraints explicitly.
• Object based data models use concepts such as entities, attributes, and
relationships.
• Various types of object based logical models are as follows
 Entity-relationship model.
 Object-oriented model.
 Binary model.
 Semantic data model.
 Infological model.
 Functional data model etc.

Figure: Object Based Logical Model


Ques: Entity, Attribute, Relation definition with example.
Sol.
Entity:
• An entity is an real world objects or concept about which you want to store
information
• In a database management system (DBMS), an entity is a piece of data that a
unique identifier can identify.
• Can be person, place, event, object, concept in the real world
• Can be physical object or abstraction
• Example: "John", "CSE305"
Attribute:
• In general, an attribute is a characteristic. In a database management system
(DBMS), an attribute refers to a database component, such as a table.
• Describes one aspect of an entity type;
Usually [and best when] single valued and indivisible (atomic)
• Represented by oval on E-R diagram
• Example: name, maximum_enrollment
Relation:
• Relations illustrate how two entities share information in the database structure

Ques: Explain about Cardinality of Relationships?


Sol.
• In database management, cardinality plays an important role.
• Cardinality is the number of entity instances to which another entity set can map
under the relationship.
• This does not reflect a requirement that an entity has to participate in a
relationship.
• We can say that the cardinality of a relationship is the number of tuples (rows) in
a relationship.
• Types of cardinality in between tables are:
 one-to-one
 one-to-many
 many-to-one
 many-to-many
• One-to-One: In this type of cardinality mapping, an entity in A is connected to at
most one entity in B.
Figure: One-to-One
Example: In a particular hospital, the surgeon department has one head of
department. They both serve one-to-one relationships.

• One-to-Many: In this type of cardinality mapping, an entity in A is associated with


any number of entities in B.

Example: In a particular hospital, the surgeon department has multiple doctors.


They serve one-to-many relationships.
• Many-to-One: In this type of cardinality mapping, an entity in A is connected to
at most one entity in B.

Example: In a particular hospital, multiple surgeries are done by a single surgeon.


Such a type of relationship is known as a many-to-one relationship.

• Many-to-Many: In this type of cardinality mapping, an entity in A is associated


with any number of entities in B, and an entity in B is associated with any number
of entities in A.

Example: In this type of cardinality mapping, an entity in A is associated with any


number of entities in B, and an entity in B is associated with any number of entities in A.
Lecture 6
Relational Algebra
Ques: Relational Algebra definition.
Sol.

• Relational algebra is a procedural query language, which consists of a set of


operations that take one or two relations as input and produce a new relation as
their result.
• Relational algebra is a formal system for manipulating relations.
• Each operation will be applied to tables of a sample database. Each table is
otherwise known as a relation and each row within the table is referred to as a
tuple.
• Operands of this algebra are relations.
• Operations of this algebra include the usual set operations and special
operations defined for relations
– selection
– projection
– join

Ques. Operations of Relational Algebra.

Sol.

The fundamental operations of relational algebra are as follows:

• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
Besides fundamental operations other relational algebra as follows:

• Namely
• Set intersection
• Natural join
• Division
• Assignment

Ques. Equation with output.


Sol.

[1] Equation: σsubject = "database"(Books)

Output: Selects tuples from books where subject is 'database'.

[2] Equation: σsubject = "database" and price = "450"(Books)

Output: Selects tuples from books where subject is 'database' and 'price' is 450.

[3] Equation: σsubject = "database" and price = "450" or year > "2010"(Books)

Output: Selects tuples from books where subject is 'database' and 'price' is 450 or
those books published after 2010.

Ques. Difference between Unary and Binary Operation.


Sol.

• The “select, project, rename” operations are called unary operations. Because
they operate on one relation.
• The “union, set difference, Cartesian product” operations are called binary
operations. Because they operate on pairs relation.

Ques. Select those tuples of the loan relation where the branch is “Perryridge”
Sol. σbranch-name= "Perryridge" (loan)

Ques. Find all tuples in which the amount is more than $1200?

Sol. σamount > 1200 (loan)


Ques. Find those tuples pertaining to loans of more than $1200 made by the
Perryridge branch?

Sol. σbranch-name= "Perryridge" ^ amount > 1200 (loan)

Ques. Find those Customers who live in Harrison.

Sol. Πcustomer − name ( σ customer − city = “Harrison ”( customer ))


Lecture 7
Ques. Schema Diagram for University Database.
Sol.

Ques. Equation with output.


Sol.

[1] Equation: ∏ author (Books) ∪ ∏ author (Articles)


Output: Projects the names of the authors who have either written a book or an
article or both.

[2] Equation: σauthor = 'tutorialspoint'(Books Χ Articles)

Output: Yields a relation, which shows all the books and articles written by
tutorialspoint.

[3] Equation: ∏ author (Books) − ∏ author (Articles)

Output: Provides the name of authors who have written books but not articles.
Ques. Find the names of all bank customers who have either an account or a loan or
both?

Sol. Πcustomer − name (borrower ) ∪ Πcustomer − name (depositor )


Ques. Set Difference definition.

Sol.

• The result of set difference operation is tuples, which are present in one relation
but are not in the second relation.
• Notation : r − s
• Finds all the tuples that are present in r but not in s.
• r and s must have the same number of attributes.
• Attribute domains must be compatible.
• Example: ∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Ques: Find the names of all customers who have a loan at the Perryridge branch.

Figure: Loan Relation


Sol.

• σ branch- name = “ Perryridge ” ( borrower × loan ) .


• σ borrower .loan- number = loan.loan- number ( σ branch- name
= “ Perryridge ”( borrower × loan ))
• Π customer - name (σ borrower .loan- number = loan.loan-
number( σ branch- name = “ Perryridge ”( borrower × loan )))
Ques. Explain about rename operation?
Sol.
• The results of relational algebra are also relations but without any name. The
rename operation allows us to rename the output relation.
• 'rename' operation is denoted with small Greek letter rho ρ.
• Notation − ρ x (E)
• Where the result of expression E is saved with name of x.
Lecture 8
Ques. SQL Definition.
Sol.
• SQL is stands for Structured Query Language.
• SQL is a special-purpose programming language designed for managing data held
in a relational database management system (RDBMS).
• The objective of SQL is to manage large amounts of data.
• The basic structure of an SQL expression consists of three clauses:
Select,
From,
Where.
Ques. How SQL works.
Sol.
• SQL is the most common language for extracting and organising data that is
stored in a relational database. A database is a table that consists of rows and
columns. SQL is the language of databases. It facilitates retrieving specific
information from databases that are further used for analysis.
• SQL manages a large amount of data, especially if there is a lot of data that is
being written simultaneously and there are too many data transactions.
• There are different versions and frameworks for SQL, the most commonly used is
MySQL
Ques. Uses of SQL.
Sol.
SQL is used for:

• Execute queries against a database


• Retrieve data from a database
• Insert records into a database
• Update records in a database
• Delete records from a database
• Create new databases, or new tables in a database
• Create stored procedures & views in a database
• Set permissions on tables, procedures, and views

Ques. Find the names of all branches in the loan relation?


Sol.

Ques. Uses of Distinct.


Sol.
• The SQL DISTINCT keyword is used in conjunction with the SELECT statement to
eliminate all the duplicate records and fetching only unique records.
• The basic syntax of DISTINCT keyword to eliminate the duplicate records is as
follows –
 SELECT DISTINCT column1, column2,.....columnN
 FROM table_name
 WHERE [condition]

Ques. Find all loan numbers for loans made at the Perryridge branch with loan
amounts greater that $1200.

Sol.

• select loan_number
• from loan
• where branch_name = ’Perryridge’ and amount> 1200

Ques. Find the loan number of those loans with loan amounts between $90,000 and
$100,000.

Sol.

• select loan_number
• from loan
• where amount between 90000 and 100000

Ques. How to drop attribute from table/relation?

Sol.

• To remove a relation from an SQL database, we use the drop table command.
• The drop table command deletes all information about the dropped relation
from the database.
• alter table relation_name drop Attribute_name;

Ques. Find instructor names and course identifiers for instructors in the Computer
Science department.

Sol.

Relation Schema is given below

• Instructor( ID, name, dept_name, Salary)


• Teaches(ID, course_id , sec_id , semester ,year)
• select name, course_id
• from instructor, teaches
• where instructor. ID= teaches . ID and instructor. dept name=
’Computer Science’;

Ques. Difference between Natural join and Cartesian Product.

Sol.

Natural Join:

• The natural join operation operates on two relations and produces a relation as
the result.
• Natural join considers only those pairs of tuples with the same value on those
attributes that appear in the schemas of both relations.
• Example:

SELECT *
FROM table1
NATURAL JOIN table2;

Cartesian Product:

• A Cartesian product is the result of joining every row in one table with every row
in another table.
• Cartesian product of two relations, which concatenates each tuple of the first
relation with every tuple of the second
• Example:
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS, ORDERS;

Lecture 9

Ques. Find the names of all instructors whose salary is greater than at least one
instructor in the Biology department.

Sol.

• select distinct T . Name as instructor_name


• from instructor as T , instructor as S
• where T.salary> S.salary and S.dept_name= ’Biology’;

Ques. Find the names of all departments whose building name includes the substring
‘Watson’.

Sol.

• select dept_name
• from department
• where building like ’%Watson%’;

Ques. List in alphabetic order all instructors in the Physics department.

Sol.

• select name
• from instructor
• where dept_name= ’Physics’
• order by name;
By default, the order by clause lists items in ascending order.

Ques. Find the set of all courses taught either in Fall 2009 or in Spring 2010, or both.

Sol.

• (select course id
• from section
• where semester = ’Fall’ and year = 2009)

union

• (select course id
• from section
• where semester = ’Spring’ and year = 2010);

The union operation automatically eliminates duplicates

Ques. Find all courses taught in the Fall 2009 semester but not in the Spring 2010
semester.
Sol.

• (select course_id
• from section
• where semester = ’Fall’ and year = 2009)

intersect

• (select course id
• from section
• where semester = ’Spring’ and year = 2010);

If we want to retain all duplicates, we must write intersect all

Ques. Briefly about Aggregate Function.

Sol.

• Aggregate functions are functions that take a collection (a set or multiset) of


values as input and return a single value.
• Various types of SQL aggregate functions are:
 Count(): COUNT function is used to Count the number of rows in a
database table.
 Sum(): Sum function is used to calculate the sum of all selected columns
 Avg() : The AVG function is used to calculate the average value of the
numeric type.
 Min() : MIN function is used to find the minimum value of a certain
column.
 Max() : MIN function is used to find the minimum value of a certain
column.

Ques. Uses of Aggregate Function.

Sol.

 An aggregate function performs a calculation on a set of values, and returns a


single value. Aggregate functions are often used in databases, spreadsheets, and
statistical software packages now common in the workplace. Aggregate functions
are used extensively in economics and finance to provide key numbers that
represent economic health or market performance.
Lecture 10

Ques. Uses of Count Function.

Sol.

• COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
• Count() Syntax:

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Ques: Find all the courses taught in the both the Fall 2009 and Spring 2010 semesters.
Sol.
• select distinct course_id
• from section
• where semester = ’Fall’ and year = 2009 and
Course_id in (select course_id
from section
where semester = ’Spring’ and year
= 2010);
Ques. Find all the courses taught in the Fall 2009 semester but not in the Spring 2010
semester.
Sol.
• select distinct course_id
• from section
• where semester = ’Fall’ and year = 2009 and
course_id not in (select course_id
from section
where semester = ’Spring’ and
year = 2010);
Ques. Find the names of all instructors whose salary is greater than at least one
instructor in the Biology department.
Sol.

Ques: Delete all tuples in the instructor relation for those instructors associated with a
department located in the Watson building.
Sol.
• delete from instructor
• where dept_name in (select dept_name
from department
where building = ’Watson’);

You might also like