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

unit -4 dbms

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

unit -4 dbms

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

Unit -4: Relational data model

The relational model is a way of organizing and managing data in a structured


format

Imagine it as a collection of tables, like in a spreadsheet, where each table stores


specific types of information.

For example:

 One table might hold information about students (with columns like ID,
name, age).
 Another table might store courses (with columns like course ID, course
name, credits).

Properties of relational model:

 Data is stored in structured tables.


 Organized into rows (records) and columns (fields).
 Unique identifier for each row (primary key).
 Links tables by connecting related data (foreign key).
 Keeps data accurate and consistent.
 Works with SQL for easy data handling.
 Reduces duplicates, saving space (normalization).

Advantages
 Easy to understand and manage structured data.
 Ensures data accuracy and reduces redundancy.
 Supports complex queries with SQL.

Disadvantages
 Slows down with very large datasets.
 Requires detailed setup and planning.
 Less suitable for unstructured data.

I
Relational Constraints are rules applied to a relational database to maintain
data accuracy, consistency, and integrity.

These constraints ensure that the data stored in the database adheres to certain
standards, preventing errors and invalid data.

Types of Relational Constraints:


Domain Constraint: Ensures that all data values in a column belong to a specific
set or data type.

For example, a column for "age" must contain only integer values within a valid
range.

Entity Integrity Constraint: Ensures each row in a table is unique and


identifiable

This is achieved through Primary Keys, which must have unique values and
cannot be null.

Referential Integrity Constraint: Ensures relationships between tables remain


consistent.

Foreign Keys enforce this by referencing a primary key from another table. If a
foreign key is used, the referenced value must exist in the primary table.

Key Constraints: Ensures that certain attributes (or combinations) in a table


uniquely identify rows. Primary Key and Unique Key are examples, preventing
duplicate rows based on specified columns

Relational Algebra
In DBMS is a procedural query language that defines operations to retrieve data
from relational databases.

Main Operations in Relational Algebra:


Selection(σ): It is used to select required tuples of the relations.
Example: σ(condition)(Table), selects rows in "Table" that meet the "condition."

A B C

1 2 4

2 2 3

3 2 3

4 3 4
For the above relation, σ(c>3) R will select the tuples which have c more than 3.
A B C

1 2 4

4 3 4

Projection (π): Extracts specific columns from a table.

Example: π (column1, column2)(Table), retrieves only column1 and column2


from "Table."

Consider Table 1. Suppose we want columns B and C from Relation R.


A B C

1 2 4

2 2 3

3 2 3

4 3 4
B C

2 4

2 3

3 4

Union (∪): Combines rows from two tables with the same structure, removing
duplicates.

Example: Table1 ∪ Table2, returns rows in both "Table1" and "Table2."


FRENCH

Student Name Roll Number

Ram 01

Mohan 02

Vivek 13

Geeta 17

GERMAN
student Name Roll Number

Vivek 13

Geeta 17

Shyam 21
student Name Roll Number

Rohan 25
 Consider the following table of Students having different optional subjects
in their course.
 π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name

Ram

Mohan

Vivek

Geeta

Shyam

Rohan

Set Difference (-): Returns rows present in one table but not in another.
Example: From the above table of FRENCH and GERMAN, Set Difference is
used as follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name

Ram

Mohan

Cartesian Product (×): Combines each row of one table with each row of another
table.
Example: Table1 × Table2, pairs every row in "Table1" with every row in
"Table2."

Name Age Sex

Ram 14 M

Sona 15 F

Kim 20 M
B
ID Course

1 DS

2 DBMS
Ax
B
Name Age Sex ID Course

Ram 14 M 1 DS

Ram 14 M 2 DBMS

Sona 15 F 1 DS

Sona 15 F 2 DBMS

Kim 20 M 1 DS
Name Age Sex ID Course

Kim 20 M 2 DBMS

Derived Operators:
Natural Join (⨝): Automatically joins tables on columns with the same name and
values.

Example:
EMP
Name ID Dept_Name

A 120 IT

B 125 HR

C 110 Sales

D 111 IT
DEPT
Dept_Name Manager

Sales Y

Production Z

IT A
Natural join between EMP and DEPT with condition:

EMP ⋈ DEPT
EMP.Dept_Name = DEPT.Dept_Name
Name ID Dept_Name Manager

A 120 IT A

C 110 Sales Y

D 111 IT A

SQL (Structured Query Language


SQL (Structured Query Language) is the standard language used to interact with
relational databases in DBMS (Database Management Systems).

SQL enables users to create, manage, and manipulate databases by defining and
accessing data efficiently.

Overview of SQL:

IBM developed the original version of SQL, originally called Sequel (Structured
English Query Language), as part of the System R project in the early 1970s.

The Sequel language has evolved since then, and its name has changed to SQL
(Structure Query Language) (some other company has trademark on the word
sequel). SQL has clearly as the standard relational database language.

In 1986, the American National Standards Institute (ANSI) and the International
Organization Standardization (ISO) published an SQL standard, called SQL-86.

The next version of the standard was SQL-89, SQL-92, SQL: 1999, SQL:2003,
SQL:200 SQL:2011, SQL: 2016, SQL: 2019and most recently SQL:2023.

Characteristics of SQL

 SQL is easy to learn.


 SQL is used to access data from relational database management systems.
 SQL can execute queries against the database.
 SQL is used to describe the data.

Main Components of SQL:


DDL (Data Definition Language): Commands to define the structure of a
database.

Examples:

 CREATE: Creates new tables, databases, views.


 ALTER: Modifies existing structures (like adding a column to a table).
 DROP: Deletes tables or databases.

DML (Data Manipulation Language): Commands for modifying data


within tables.

Examples:

 INSERT: Adds new rows to a table.


 UPDATE: Modifies existing data in a table.
 DELETE: Removes rows from a table.

DQL (Data Query Language): Focuses on querying and retrieving data.


Example:

SELECT: Retrieves data from tables, allowing filtering, sorting, and grouping.

DCL (Data Control Language): Commands for managing access


permissions.

Examples:

 GRANT: Gives users access rights.


 REVOKE: Removes user access rights.

TCL (Transaction Control Language): Manages transactions to ensure


data consistency.
Examples:

 COMMIT: Saves all changes made during a transaction.


 ROLLBACK: Reverts changes if there’s an error or need for undo.

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.

Data Definition Language (DDL): Used to define or modify database


structures.

CREATE: Create a new table, database, index, or view.

CREATE TABLE table_name (


column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
...
);

ALTER: Modify an existing table, like adding or dropping a column.

ALTER TABLE table_name


ADD column_name datatype;

DROP: Delete tables, databases, indexes, or views.

DROP TABLE table_name;

Data Manipulation Language (DML): Used to insert, update, delete, and


retrieve data in tables.

INSERT INTO: Add new data to a table.

INSERT INTO table_name (column1, column2, ...)


VALUES (value1, value2, ...);
UPDATE: Modify existing data in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE: Remove data from a table.

DELETE FROM table_name WHERE condition;

Data Query Language (DQL): Used to query data from tables.

SELECT: Retrieve data from one or more tables.

SELECT column1, column2, ...


FROM table_name
WHERE condition
ORDER BY column;

Data Control Language (DCL): Used to control access to data.


GRANT: Give access privileges to users.

GRANT SELECT, INSERT ON table_name TO user;

REVOKE: Remove access privileges.

REVOKE SELECT, INSERT ON table_name FROM user;

Transaction Control Language (TCL): Used to manage transactions


within a database.

COMMIT: Save all changes made in the current transaction.

COMMIT;

ROLLBACK: Revert changes made in the current transaction.


ROLLBACK;

SAVEPOINT: Set a point in a transaction to which you can later roll back.

SAVEPOINT savepoint_name;

You might also like