0% found this document useful (0 votes)
13 views

DBMS UNIT-2

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

DBMS UNIT-2

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SEM :- 2-2 (R20) DBMS UNIT-2

SYLLABUS
1. Relational Model:
2. Introduction to relational model,
3. concepts of domain,
4. attribute, tuple, relation,
5. importance of null values,
6. constraints (Domain, Key constraints, integrity
constraints) and their importance BASIC SQL:
7. Simple Database schema,
8. data types,
9. table definitions (create, alter),
10. different DML operations (insert, delete,
update),
11. basic SQL querying (select and project) using
where clause,
12. arithmetic & logical operations, SQL
functions(Date and Time, Numeric, String conversion)

web-D Page 1
SEM :- 2-2 (R20) DBMS UNIT-2

Relational Model: Introduction

The relational model is a fundamental concept in database management. It was proposed


by E.F. Codd to model data in the form of relations or tables. Here are the key points
about the relational model:

1. Tables (Relations):
o The relational model uses a collection of tables to represent both data and the
relationships among that data.
o Each table has multiple columns, and each column has a unique name.
o Tables are also known as relations.
2. Record-Based Model:
o The relational model is an example of a record-based model.
o In record-based models, the database is structured into fixed-format records of
several types.
o Each table contains records of a particular type, and each record type defines a fixed
number of fields (attributes).
3. Terminologies:
o Attribute: Attributes are the properties that define an entity. For example, in a
STUDENT relation, attributes could be ROLL_NO, NAME, ADDRESS, PHONE, and AGE.
o Relation Schema: A relation schema defines the structure of the relation and
represents the name of the relation along with its attributes. For instance, the
relation schema for STUDENT could be STUDENT(ROLL_NO, NAME, ADDRESS,
PHONE, and AGE).
o Tuple: Each row in the relation is known as a tuple. The STUDENT relation contains
multiple tuples, each representing a student record.
o Relation Instance: The set of tuples of a relation at a particular instance of time is
called a relation instance. It can change due to insertions, deletions, or updates in
the database.

web-D Page 2
SEM :- 2-2 (R20) DBMS UNIT-2

o Degree: The number of attributes in the relation is known as the degree. For
example, the STUDENT relation defined above has a degree of 5.
o Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT
relation defined above has a cardinality of 4.
o Column: A column represents the set of values for a particular attribute. For
instance, the column ROLL_NO is extracted from the STUDENT relation.
o NULL Values: A NULL value represents data that is not known or unavailable. It is
represented by a blank space. For example, the PHONE attribute for the student
with ROLL_NO 4 is NULL.
o Relation Key: These keys are used to identify rows uniquely or help in identifying
tables.

Remember, the relational model is widely used, and most current database systems are
based on it. It provides a structured way to organize and manage data efficiently. 🌟🌟

Example 1: Student Management System

STUDENT Table:
StudentID Name DateOfBirth ContactNumber Email
101 Alice Lee 1998-03-15 123-456-7890 [email protected]
102 Bob Smith 1999-05-20 987-654-3210 [email protected]

COURSE Table:
CourseID CourseName Instructor Credits
201 Math 101 Prof. Johnson 3
202 History 201 Prof. Adams 4

web-D Page 3
SEM :- 2-2 (R20) DBMS UNIT-2

ENROLLMENT Table:
EnrollmentID StudentID CourseID EnrollmentDate
301 101 201 2023-01-15
302 102 202 2023-01-20

Example 2: Online Bookstore

BOOK Table:
BookID Title Genre Price
401 “Harry Potter” Fantasy 19.99
402 “Lord of the Rings” Fantasy 24.99

AUTHOR Table:
AuthorID AuthorName
501 J.K. Rowling
502 J.R.R. Tolkien

ORDER Table:
OrderID CustomerID BookID OrderDate
601 701 401 2023-02-10
602 702 402 2023-02-12

These tables represent the relationships between students, courses, books, authors, and
customer orders.

web-D Page 4
SEM :- 2-2 (R20) DBMS UNIT-2

Concepts in the Relational Model

Domain:

 A domain represents the set of allowable values for an attribute (column) in a relation
(table).
 For example:
o The domain of the “Age” attribute could be integers between 0 and 120.
o The domain of the “Gender” attribute could be {Male, Female, Other}.

Attribute:

 An attribute corresponds to a column in a relation (table).


 It represents a specific property or characteristic of an entity.
 For instance, in a “Student” relation, attributes could include “StudentID,” “Name,” and
“GPA.”

web-D Page 5
SEM :- 2-2 (R20) DBMS UNIT-2

Tuple:

 A tuple is a row in a relation (table).


 Each tuple represents a specific instance or record.
 For example, a tuple in the “Employee” relation could represent an individual employee.

Relation:

 A relation is a table in the relational model.


 It contains multiple tuples (rows) with the same attributes (columns).
 Each relation represents a specific entity or concept (e.g., “Customers,” “Orders”).

Examples:
1. Domain Example:
o Attribute: “Age”
o Domain: Integers between 0 and 120
2. Attribute Example:
o Relation: “Student”
o Attributes: “StudentID,” “Name,” “GPA”
3. Tuple Example:
o Relation: “Employee”
o Tuple: EmployeeID = 101, Name = “Alice,” Salary = $60,000
4. Relation Example:
o Relation: “Customers”
o Attributes: “CustomerID,” “Name,” “Email”
o Sample Tuple: CustomerID = 201, Name = “Bob,” Email = “[email protected]

Remember, understanding these concepts is essential for effective database design and
management. 🌟🌟

-------------------------------------------------------------------------------------------------------------------------------

web-D Page 6
SEM :- 2-2 (R20) DBMS UNIT-2

Importance of Null Values

 Null values serve a specific purpose in relational databases:


o Denote the absence of information or the inapplicability of a data element.
o Interpretations of null values:
 Value unknown (exists but not known).
 Value not available (exists but purposely withheld).
 Attribute not applicable (undefined for this tuple).
o SQL uses a three-valued logic with values True, False, and Unknown when
dealing with nulls.
o NULL values are different from zero values or fields containing spaces.
o They allow flexibility in representing missing or unknown data.

Remember, understanding null values is crucial for managing data integrity and
semantics within a database. 🌟🌟

------------------------------------------------------------------------------------------------------------

Relational Model Constraints: Keeping Your Data in Check

The relational model enforces data integrity and consistency through constraints. These

act like rules that govern the type and organization of data stored within the database.

Let's explore the different types of constraints and their importance:

 Domain Constraints: As discussed earlier, domain constraints define the valid set of

values that an attribute (column) can hold. This ensures data consistency by restricting

the type of information stored (e.g., numbers, text) and preventing invalid entries.

web-D Page 7
SEM :- 2-2 (R20) DBMS UNIT-2

 Key Constraints: These constraints uniquely identify rows within a table and play a

crucial role in maintaining data integrity. There are two main types:

o Primary Key: A table can have only one primary key, which is a minimal set of

attributes that uniquely identifies each row. This eliminates duplicate records and

ensures data accuracy.

o Candidate Key: Any set of attributes that can uniquely identify rows in a table is

considered a candidate key. A table can have multiple candidate keys, but only one is

chosen as the primary key.

 Referential Integrity Constraints: These constraints ensure consistency between

related tables. They enforce a relationship by referencing the primary key of one table

(parent) in another table (child). A foreign key in the child table must hold a value that

exists in the primary key of the parent table. This prevents orphaned records (entries in

the child table that don't correspond to any valid record in the parent table).

Importance of Constraints:

Constraints are essential for maintaining a clean and reliable database. They:

 Improve Data Accuracy: By restricting invalid entries and ensuring unique

identification, constraints prevent data inconsistencies.

 Enforce Data Relationships: Referential integrity constraints guarantee the validity of

relationships between tables.

 Optimize Data Retrieval: Unique identifiers like primary keys facilitate efficient data

retrieval using queries.

web-D Page 8
SEM :- 2-2 (R20) DBMS UNIT-2

Basic SQL: A Peek into the Language of Relational

Databases

SQL (Structured Query Language) is the standard language for interacting with

relational databases. It allows you to:

 Create, modify, and delete tables: You can define the structure of your tables with

specific attributes and data types using SQL commands.

 Insert and update data: SQL provides commands to populate your tables with data

and modify existing entries.

 Retrieve data: The powerful SELECT statement allows you to fetch specific data from

tables based on various criteria.

 Manage database objects: You can use SQL to control user access, create backups,

and perform other administrative tasks.

Here's a Simple Database Schema Example:

Imagine a database for a music store. We could have two tables:

1. Artists

Attribute Data Type Primary Key

Artist_ID Integer Yes (auto-incrementing)

Name Varchar(255) No

Genre Varchar(50) No

web-D Page 9
SEM :- 2-2 (R20) DBMS UNIT-2

2. Albums

Attribute Data Type Primary Key Foreign Key

Album_ID Integer Yes (auto-incrementing)

Title Varchar(255) No

Artist_ID Integer No References Artists(Artist_ID)

Constraints in Action:
 The Artist_ID in the Artists table would be a primary key, ensuring each artist has a

unique identifier.

 The Artist_ID in the Albums table would be a foreign key referencing the Artists table's

primary key. This enforces referential integrity, guaranteeing that albums are linked to

existing artists.

Schema Design and Table Creation:

1. A schema defines the structure of a database, including tables, columns,


relationships, and constraints.
o To create a table, we use the CREATE TABLE command. Here’s an example
for a table called “Persons” with columns: PersonID, LastName,
FirstName, Address, and City:
o CREATE TABLE Persons (
o PersonID int,
o LastName varchar(255),
o FirstName varchar(255),
o Address varchar(255),
o City varchar(255)
o );

web-D Page 10
SEM :- 2-2 (R20) DBMS UNIT-2

o You can also create a new table based on an existing one using CREATE

TABLE AS SELECT.

2. Altering Tables:

o The ALTER TABLE command allows you to modify existing tables.

o To add a new column (e.g., “Email”) to the “Customers” table:

o ALTER TABLE Customers ADD Email varchar(255);

o To delete a column (e.g., “Email”) from the “Customers” table:

o ALTER TABLE Customers DROP COLUMN Email;

3. DML Operations:

o Insert: Adds new records to a table.

o INSERT INTO Persons (PersonID, LastName, FirstName,

Address, City)

o VALUES (1, 'Smith', 'John', '123 Main St', 'New York');

o Delete: Removes records from a table.

o DELETE FROM Persons WHERE PersonID = 1;

o Update: Modifies existing records.

o UPDATE Persons SET City = 'Los Angeles' WHERE LastName =

'Smith';

------------------------------------------------------------------------------------------------------------

web-D Page 11
SEM :- 2-2 (R20) DBMS UNIT-2

Basic SQL Querying (SELECT and Project)

1. SELECT Statement:
o The SELECT statement retrieves data from one or more tables.
o Syntax:
o SELECT column1, column2, ...
o FROM table_name;

o Example:
o SELECT FirstName, LastName
o FROM Persons;

2. Projecting Columns:
o Projecting means selecting specific columns to display in the result set.
o Example:
o SELECT FirstName, LastName, City
o FROM Customers;

Using WHERE Clause

1. The WHERE clause filters rows based on specified conditions.


o Example:
o SELECT ProductName, Price
o FROM Products
o WHERE CategoryID = 1;

Arithmetic and Logical Operations

1. Arithmetic Operators:
o +, -, *, /, % (addition, subtraction, multiplication, division, modulo)

o Example:
o SELECT Quantity * Price AS TotalCost
o FROM OrderDetails;

web-D Page 12
SEM :- 2-2 (R20) DBMS UNIT-2

2. Logical Operators:
o AND, OR, NOT (combine conditions)

o Example:
o SELECT ProductName
o FROM Products
o WHERE CategoryID = 1 AND Price > 50;

SQL Functions

1. Date and Time Functions:


o GETDATE(), DATEADD(), DATEDIFF(), YEAR(), MONTH(), DAY()

o Example:
o SELECT OrderID, OrderDate
o FROM Orders
o WHERE YEAR(OrderDate) = 2023;
2. Numeric Functions:
o SUM(), AVG(), COUNT(), MAX(), MIN()

o Example:
o SELECT CategoryName, COUNT(ProductID) AS ProductCount
o FROM Categories
o GROUP BY CategoryName;
3. String Conversion Functions:
o CONCAT(), SUBSTRING(), UPPER(), LOWER()

o Example:
o SELECT CONCAT(FirstName, ' ', LastName) AS FullName
o FROM Employees;

web-D Page 13

You might also like