P24CDMCA4 Unit3
P24CDMCA4 Unit3
1
RDBMS and SQL
• Relational Database Management System (RDBMS): A database
management system based on the relational model.
• Relational Model: Organizes data into tables with rows and
columns.
• SQL (Structured Query Language): Standard language for
managing and manipulating relational databases.
• Key concepts: Entities, attributes, relationships, tables, records,
fields.
• Advantages: Data independence, flexibility, scalability, integrity.
• Challenges: Complexity, performance optimization.
• Real-world applications: Banking, e-commerce, inventory
management.
2
The SQL Query Language
• Purpose: Retrieve, manipulate, and manage data in relational
databases.
• Data Definition Language (DDL): Creating, modifying, and
deleting database structures.
• Data Manipulation Language (DML): Inserting, updating, and
deleting data.
• Data Control Language (DCL): Granting and revoking user
privileges.
• Data Query Language (DQL): Retrieving data from the database.
• Basic SQL syntax: SELECT, FROM, WHERE clauses.
• Examples: Simple SELECT queries, using arithmetic operators,
comparison operators.
3
Querying Multiple Relations
• Joins: Combining data from multiple tables.
• Types of joins: Inner join, outer join (left, right, full),
self-join.
• Syntax: JOIN keyword, ON clause, WHERE clause.
• Examples: Joining tables based on common columns.
• Complex queries: Using multiple joins, subqueries.
• Performance optimization: Indexing, query hints.
• Real-world applications: Generating reports, data
analysis.
4
Creating Relations in SQL
• CREATE TABLE statement: Syntax and components.
• Data types: Number, character, date, time, etc.
• Constraints: PRIMARY KEY, FOREIGN KEY, NOT NULL,
UNIQUE, CHECK.
• Example: Creating a table with columns and constraints.
• Altering table structure: Adding, modifying, or dropping
columns.
• Best practices: Normalization, indexing for performance.
• DDL commands: CREATE, ALTER, DROP.
5
Destroying and Altering Relations
• DROP TABLE statement: Removing a table and its data.
• TRUNCATE TABLE statement: Removing all data from a
table without dropping the structure.
• ALTER TABLE statement: Modifying table structure (add,
drop, modify columns).
• Constraints: Adding, modifying, or dropping constraints.
• Impact on data: Considerations before altering tables.
• Best practices: Backup data before making changes.
• Examples: Dropping a table, altering a column data
type.
6
Adding and Deleting Tuples
• INSERT statement: Adding rows to a table.
• VALUES clause: Specifying data for new rows.
• DEFAULT values: Using default values for
columns.
• DELETE statement: Removing rows from a table.
• WHERE clause: Specifying conditions for deletion.
• Impact on related tables: Referential integrity
considerations.
• Examples: Inserting and deleting records.
7
Integrity Constraints (ICs)
• Definition: Rules that ensure data accuracy and consistency.
• Entity integrity: Primary key must have unique, non-null
values.
• Referential integrity: Foreign key values must match primary
key values in another table.
• Domain integrity: Data values must conform to specified
domains.
• User-defined integrity: Custom constraints based on business
rules.
• Enforcing ICs: DBMS checks constraints during data
manipulation.
• Importance: Maintaining data quality and preventing errors.
8
Primary and Candidate Keys in SQL
• Primary key: Uniquely identifies each row in a table.
• Candidate key: Any column or combination of columns that
uniquely identifies a row.
• Choosing a primary key: Considerations for performance
and data integrity.
• SQL syntax: PRIMARY KEY constraint.
• Examples: Defining primary keys for tables.
• Impact on data manipulation: Updating and deleting
records with primary keys.
• Importance: Ensuring data uniqueness and referential
integrity.
9
Foreign Keys, Referential Integrity in SQL
• Foreign key: Column or set of columns referencing the primary
key of another table.
• Referential integrity: Maintaining consistency between related
tables.
• ON DELETE and ON UPDATE actions: Specifying behavior when
parent records are modified or deleted.
• CASCADE, SET NULL, RESTRICT: Options for handling referential
integrity.
• Examples: Creating foreign keys, enforcing referential integrity.
• Importance: Preventing data inconsistencies and anomalies.
• Challenges: Maintaining referential integrity in complex database
designs.
10
Enforcing Referential Integrity
• DBMS role: Checking and enforcing referential integrity
constraints.
• Triggers: Database objects that automatically execute
actions in response to data changes.
• Deferred constraints: Checking constraints at commit time.
• Performance impact: Consider trade-offs between
consistency and performance.
• Best practices: Careful design of foreign key relationships.
• Examples: Using triggers to enforce referential integrity.
• Tools and techniques: Database management system
features.
11
References
• https://www.tutorialspoint.com/dbms/
• https://www.w3schools.com/sql/
• https://www.geeksforgeeks.org/sql-tutorial/
• https://dev.mysql.com/doc/
12