0% found this document useful (0 votes)
9 views75 pages

Unit 2 Dbms Syed

Unit 2 of the Database Management System course focuses on the relational model and SQL, explaining the structure and concepts of relational databases, including domains, attributes, tuples, and relationships. It covers relational model constraints, integrity constraints, and SQL commands for data definition, manipulation, and control. The unit provides a comprehensive overview of how data is organized, accessed, and managed in relational databases.

Uploaded by

cheetah69757
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)
9 views75 pages

Unit 2 Dbms Syed

Unit 2 of the Database Management System course focuses on the relational model and SQL, explaining the structure and concepts of relational databases, including domains, attributes, tuples, and relationships. It covers relational model constraints, integrity constraints, and SQL commands for data definition, manipulation, and control. The unit provides a comprehensive overview of how data is organized, accessed, and managed in relational databases.

Uploaded by

cheetah69757
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/ 75

22CS2403

Database Management System


Unit – 2

1
Unit 2 Relational Model and SQL
Syllabus

2
Relational Database and Relational Models
• Relational databases are based on the relational model, an intuitive, straightforward way of
representing data in tables. In a relational database, each row in the table is a record with a unique
ID called the key.

• A relational database is a collection of information that organizes data in predefined relationships


where data is stored in one or more tables (or "relations") of columns and rows, making it easy to
see and understand how different data structures relate to each other.

3
4
5
Relationship Model Concepts

• The relational model represents the database as a collection of relations

• Each relation resembles a table of values or, to some extent, a flat file of
records. It is called a flat file

• When a relation is thought of as a table of values, each row in the table


represents a collection of related data values

• Domains, Attributes, Tuples, and Relations – Major concepts on RDBMS

6
Domain
• A domain D is a set of atomic values. i.e. domain is indivisible as far as the formal relational model is concerned. A
common method of specifying a domain is to specify a data type from which the data values forming the domain are
drawn
• A domain has a logical definition:
• Example: “Mobile numbers” are the set of 10 digit phone numbers .
• “Aadhar Card Numbers” are set of 12 digit numbers valid in India

• A domain also has a data-type or a format defined for it.


• The USA_phone_numbers may have a format: (ddd)ddd-dddd where each d is a decimal digit.
• Dates have various formats such as year, month, date formatted as yyyy-mm-dd, or as dd- mm-yyyy etc.
Some more Eg’s:
• Names: The set of names of persons (First Name, Last Name or Last Name, Middle Name, First Name.

• Grade_point_averages: Possible values of computed grade point averages; each must be a real (floating point)
number between 0 and 4.

• Employee_ages: Possible ages of employees of a company; each must be a value between 15 and 80 years old.

• Academic_department_names: The set of academic department names, such as Computer Science, Economics,
and Physics, in a university.

• Academic_department_codes: The set of academic department codes, such as CS, ECON, and PHYS, in a
university.
Attribute

• The attribute name designates the role played by a domain in a


relation.

• Used to interpret the meaning of the data elements


corresponding to that attribute
• Example: The domain Date may be used to define two attributes named “Invoice-
date” and “Payment-date” with different meanings
Tuples
• A tuple is an ordered set of values enclosed in angled brackets ‘< … >’
• Each value is derived from an appropriate domain.
• A row in the CUSTOMER relation is a 4-tuple and would consist of
four values, for example:
<632895, "John Smith", "101 Main St. Atlanta, GA 30332",
"(404) 894-2000">
• This is called a 4-tuple as it has 4 values
• A tuple is a row in the CUSTOMER relation.
• A relation is a set of such tuples (rows)
Relationships
• Formally,
• Given R(A₁, A₂, .........., An)
• r(R) ⊆ dom (A1) . dom (A2) . .... . dom(An)
• R(A₁, A₂, …, An) is the schema of the relation
• Where, R is the name of the relation
• A₁, A₂, .........., An are the attributes of the relation
• r(R): a specific state (or "value" or “population”) of relation R – this is a set
of tuples (rows)
• r(R) = {t₁, t₂, .........., tn} where each ti is an n-tuple
• ti = < v₁, v₂, .........., vn >where each vi element of domain(Aj)
Informal Terms Formal Terms

Table Values Relation

Column Header Attribute

All possible Column Values Domain

Row Tuple

Table Definition Schema of a Relation


Characteristics of Relation
• Ordering of tuples in a relation r(R):
• The tuples are not considered to be ordered, even though they
appear to be in the tabular form.
• In other words, a relation is not sensitive to the ordering of tuples.
Eg: tuples in the STUDENT relation could be ordered by values of
Name, Ssn, Age, or some other attribute.
Both relations are same ,but with different ordering of tuples
• t1 = < (Name, Dick Davidson),(Ssn, 422-11-2320),(Home_phone, NULL),(Address, 3452
Elgin Road), (Office_phone, (817)749-1253),(Age, 25),(Gpa, 3.53)>

• t2 = < (Address, 3452 Elgin Road),(Name, Dick Davidson),(Ssn, 422-11-2320),(Age, 25),


(Office_phone, (817)749-1253),(Gpa, 3.53),(Home_phone, NULL)>

• At an abstract level, the order inside the tuples doesn’t matter.

• When the attribute name and value are included together in a tuple, it is known as self-
describing data, because the description of each value (attribute name) is included in the
tuple.
Values in the Tuples:
• Each value in a tuple is an atomic value; that is, it is not divisible into
components within the framework of the basic relational model.

• Each value in a tuple must be from the domain of the attribute for that column

• If tuple t = <v1, v2, …, vn> is a tuple (row) in the relation state r of R(A1, A2,
…, An) , Then each vi must be a value from dom(Ai)

• We refer to component values of a tuple t by t[Ai] or t.Ai

• This is the value vi of attribute Ai for tuple t


• Null values: It is used to represent the values of attributes that may be
unknown or may not apply to a tuple.
Several Meanings of NULL values
1)Value unknown
2) Value exists but is not available
3) Attribute does not apply
4) value undefined

NB: During database design, it is best to avoid NULL values as much as possible
Relational Model Constraints
• In a relational database, there will typically be many relations, and the tuples in
those relations are usually related in various ways.

• . There are generally many restrictions or constraints on the actual values in a


database state

• Constraints on databases can generally be divided into three main categories:

• Inherent model-based constraints or implicit constraints:

• Schema-based constraints or explicit constraint:

• Application-based or semantic constraints or business rules:


Inherent model-based constraints or
implicit constraints
• Constraints that are inherent in the data model. Eg: the constraint that a relation
cannot have duplicate tuples is an inherent constraint.
• These are the restrictions or sets of rules imposed on the database contents.
• It validates the quality of the database. It validates the various operations like data insertion,
updation, and other processes that have to be performed without affecting the integrity of the
data.
• It protects us against threats/damages to the database.

In this relation, Name is a


composite attribute and Phone
is a multi-values attribute, so it
is violating domain constraint.
Schema-based constraints or Explicit constraint

• Constraints that can be directly expressed in the schemas of the data model,
typically by specifying them in the Data Definition Language (DDL).

• The schema-based constraints include domain constraints, key constraints,


constraints on NULLs, entity integrity constraints, and referential integrity
constraints.

• e.g., films have only one director


Application-based or semantic constraints

• Also called business rules

• Not directly expressed in schemas

• Expressed and enforced by Application program

e.g., this year’s salary increase can be no more than last year’s
Domain constraints
• Declared by specifying the data type for each attribute:
• Numeric data types for integers and real numbers
• Characters
• Booleans
• Fixed-length strings
• Variable-length strings
• Date, time, timestamp
• Money
• Other special data types
Key constraints
• Uniqueness constraints on tuples.
• If K is a key, then it satisfies two properties :
1. No two distinct tuples have the same values across all attributes in K (i.e., it
is a superkey)
2. It is a minimal superkey (i.e., no subset of K has this uniqueness constraint)
• Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the same value for SK .
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK] ≠ t2[SK], This condition must hold in any valid state
r(R)
• Key of R:
• A "minimal" superkey
• That is, a key is a superkey (K) such that removal of any attribute from K results in a set
of attributes that is not a superkey (does not possess the superkey uniqueness property)
• A Key is a Superkey but not vice versa
24
25
Primary key of the relation
• Relation schema may have more than one key.
• Declare one chosen key among candidates as primary
• Its values will be used to refer to specific tuples
• Cannot have the value NULL for any tuple
• Other candidate keys are designated as unique
• Non-null values cannot repeat, but values may be NULL
Integrity constraints
• Entity integrity constraint
• No primary key value can be NULL

• Referential integrity constraint


• Specified between two relations
• Allows tuples in one relation to refer to tuples in another
• Maintains consistency among tuples in two relations
Relational Database Schema

• Relational Database Schema:


• A set S of relation schemas that belong to the same database.

• S is the name of the whole database schema

• S = {R1, R2, ..., Rn} and a set IC of integrity constraints.

• R1, R2, …, Rn are the names of the individual relation schemas within
the database S
Schema diagram for the COMPANY relational
database schema
Update Operations on Relations

• INSERT a tuple

• DELETE a tuple

• MODIFY a tuple
Update Operations on Relations
• Integrity constraints should not be violated by the update operations. Several
update operations may have to be grouped together.
• Updates may propagate to cause other updates automatically.
• In case of integrity violation, several actions can be taken:
• Cancel the operation that causes the violation (RESTRICT or REJECT option)
• Perform the operation but inform the user of the violation
• Trigger additional updates so the violation is corrected (CASCADE option, SET NULL
option)
• Execute a user-specified error-correction routine
Possible violations for each operation
INSERT may violate any of the constraints:
• Domain constraint:
if one of the attribute values provided for the new tuple is not of the specified
attribute domain
• Key constraint:
• if the value of a key attribute in the new tuple already exists in another tuple in the relation

• Referential integrity:
• if a foreign key value in the new tuple references a primary key value that does not
exist in the referenced relation
• Entity integrity:
• if the primary key value is null in the new tuple
Possible violations for each operation
DELETE may violate only referential integrity:

• If the primary key value of the tuple being deleted is referenced from other tuples in the database

• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL

❑RESTRICT option: reject the deletion

❑CASCADE option: propagate the new primary key value into the foreign keys of the referencing
tuples

❑SET NULL option: set the foreign keys of the referencing tuples to NULL

One of the above options must be specified during database design for each foreign key constraint
Possible violations for each operation
• UPDATE may violate domain constraint and NOT NULL constraint on an attribute being modified

• Any of the other constraints may also be violated, depending on the attribute being updated:

• Updating the primary key (PK):


• Similar to a DELETE followed by an INSERT

• Need to specify similar options to DELETE

• Updating a foreign key (FK):


• May violate referential integrity

• Updating an ordinary attribute (neither PK nor FK):


• Can only violate domain constraints
36
PART II
SQL
• SQL Data Definition and Data Types

• Specifying Constraints in SQL

• Basic Retrieval Queries in SQL

• INSERT, DELETE, and UPDATE Statements in SQL

• Additional Features of SQL

37
SQL Introduction
❖ SQL stands for Structured Query Language.

❖ SQL is a database computer language designed for the retrieval and


management of data in a relational database.

❖ SQL is the standard language for Relational Database System.

❖ All the Relational Database Management Systems (RDMS) like MySQL, MS


Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their
standard database language.

38
Schema and Catalog Concepts in SQL
◼ SQL schema

◼ Identified by a schema name

◼ Includes an authorization identifier to indicate the user or account who owns the schema,

◼ and descriptors for each element

◼ Schema elements include

◼ Tables, constraints, views, domains, and other constructs

◼ Each statement in SQL ends with a semicolon

39
◼ A schema is created via the CREATE SCHEMA statement, which can include all
the schema elements’ definitions
◼ CREATE SCHEMA statement
▪ The schema can be assigned a name and authorization identifier
◼ CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
◼ Catalog
◼ Named collection of schemas in an SQL environment
◼ SQL also has the concept of a cluster of catalogs.

40
Table

▪ The data in an RDBMS is stored in database objects which are called as tables.
▪ This table is basically a collection of related data entries and it consists of
numerous columns and rows.
◼ Specifying a new relation
◼ Provide name of table

◼ Specify attributes, their types and initial constraints

◼ Schema:
◼ CREATE TABLE COMPANY.EMPLOYEE ...

• or
◼ CREATE TABLE EMPLOYEE …

41
SQL Types

• DDL (Data Definition Language)


• DML (Data Manipulation Language)
• DCL (Data Control Language)
• TCL (Transaction Control Language)

42
DDL (Data Definition Language)
➢ Purpose: DDL is used to define and manage the structure of the database.

• Commands:
• CREATE: Creates a new database object (table, index, view, etc.).
• ALTER: Modifies the structure of an existing database object.
• DROP: Deletes a database object.
• TRUNCATE: Removes all records from a table.
• RENAME: Renames an existing database object.
• COMMENT: Adds comments or descriptions to a database object.
• CREATE INDEX: Creates an index on one or more columns.
• DROP INDEX: Removes an existing index.

43
DDL

• DDL (Data Definition Language) commands are a subset of SQL


(Structured Query Language) used to
➢define and manage the structure of a database.

• DDL commands are responsible for defining and managing database


objects, such as tables, indexes, and constraints.

44
DML (Data Manipulation Language):
• DML (Data Manipulation Language):
• Purpose: DML is used to manipulate and interact with the data stored in the
database.

• Commands:
• SELECT: Retrieves data from one or more tables.
• INSERT: Adds new records into a table.
• UPDATE: Modifies existing records in a table.
• DELETE: Removes records from a table.

45
DCL (Data Control Language)

• DCL (Data Control Language):


• Purpose: DCL is used to control access and permissions to the database.

• Commands:
• GRANT: Provides specific privileges to users or roles.

• REVOKE: Removes specific privileges from users or roles.

46
TCL (Transaction Control Language)

• TCL (Transaction Control Language):


• Purpose: TCL is used to manage transactions within the database.

• Commands:
• COMMIT: Saves all changes made during the current transaction.
• ROLLBACK: Undoes changes made during the current transaction.
• SAVEPOINT: Sets a point within a transaction to which you can later roll back.
• ROLLBACK TO: Rolls back a transaction to a specified savepoint.
• SET TRANSACTION: Modifies properties of the current transaction.

47
Data Integrity Controls

• Referential integrity–constraint that ensures that foreign key


values of a table must match primary key values of a related
table in 1:M relationships
• Restricting:
• Deletions of primary records
• Updates of primary records
• Inserts of dependent records

48
The SELECT-FROM-WHERE
Structure of Basic SQL Queries

49
SQL “DISTINCT” keyword

51
SQL “GROUP BY”

52
Having clause
• The HAVING Clause enables you to specify conditions that filter which group results appear in the results.

• The WHERE clause places conditions on the selected columns, whereas the HAVING clause places conditions on groups
created by the GROUP BY clause

53
SQL Join’s

54
SQL CARTESIAN Join or CROSS Join

55
56
SQL INNER JOIN
▪ The most important and frequently used of the joins is the INNER
JOIN. They are also referred to as an EQUIJOIN.

▪ returns rows when there is a match in both tables.

▪ The INNER JOIN creates a new result table by combining column


values of two tables (table1 and table2) based upon the join-
predicate.
▪ The query compares each row of table1 with each row of table2 to find all pairs of
rows which satisfy the join-predicate.
▪ When the join-predicate is satisfied, column values for each matched pair of rows
of A and B are combined into a result row.
▪ Inner Joins do not have to use equality to join the fields • Can use <, >, <>

57
• Inner join produces only the set of records that match in both Table A
and Table B

• Most commonly used, best understood join

58
59
Outer Joins →LEFT Joins
▪ The SQL LEFT JOIN returns all rows from the left table, even if there are no
matches in the right table.

▪ This means that if the ON clause matches 0 (zero) records in the right table;
the join will still return a row in the result, but with NULL in each column
from the right table.

▪ This means that a left join returns all the values from the left table, plus
matched values from the right table or NULL in case of no matching join
predicate.

60
SQL LEFT JOIN
SQL RIGHT JOIN
➢ The SQL RIGHT JOIN returns all rows from the right
table, even if there are no matches in the left table.

➢ This means that if the ON clause matches 0 (zero)


records in the left table; the join will still return a row
in the result, but with NULL in each column from the
left table.

➢ This means that a right join returns all the values from
the right table, plus matched values from the left table
or NULL in case of no matching join predicate.
SQL RIGHT JOIN
SQL FULL JOIN

➢ The SQL FULL JOIN combines the results


of both left and right outer joins.
➢ The joined table will contain all records
from both the tables and fill in NULLs for
missing matches on either side.
SQL FULL JOIN
Ambiguous Attribute Names
◼ Same name can be used for two (or more)
attributes in different relations
◼ As long as the attributes are in different relations
◼ Must qualify the attribute name with the relation
name to prevent ambiguity
Aliasing, and Renaming
◼ Aliases or tuple variables
◼ Declare alternative relation names E and S to refer
to the EMPLOYEE relation twice in a query:

Query 8. For each employee, retrieve the employee’s first and last name
and the first and last name of his or her immediate supervisor.
◼ SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
◼ Recommended practice to abbreviate names and
to prefix same or similar attribute from multiple
tables.
Triggers
• Triggers are special types of stored procedures that are automatically
executed (or "triggered") in response to certain events on a particular table
or view. Triggers are used to enforce business rules, perform data validation,
or take specific actions when certain conditions are met. The events that can
activate triggers typically include:

• INSERT: Triggered after a new row is inserted into a table.

• UPDATE: Triggered after an existing row is updated.

• DELETE: Triggered after a row is deleted from a table.


Security
• Database security is a critical aspect of managing and maintaining a relational database
management system (RDBMS). Ensuring the security of your database involves protecting
sensitive data, preventing unauthorized access, and maintaining the integrity and
confidentiality of the information stored in the database.

• Authentication and Authorization

• Encryption

• Backup and Recovery

• Security Policies and Procedures

• Database Firewall
Advanced SQL features

• Stored Procedures and Functions

• Triggers

• Views

• Indexes

• Recursive Queries

• Dynamic SQL
Embedded SQL

• Embedded SQL refers to the practice of embedding SQL statements


directly within a programming language code, typically in a high-level
programming language like C, COBOL, Java, or Python.

• This allows developers to seamlessly integrate database operations


into their application code.

• Embedded SQL provides a way to combine the strengths of both the


programming language and SQL for database interaction.
Dynamic SQL
• Dynamic SQL is a programming technique that involves constructing and
executing SQL statements dynamically at runtime.

• Unlike static SQL, where the SQL statements are hard-coded into the source
code, dynamic SQL allows for flexibility in building and modifying SQL statements
based on runtime conditions, user input, or other variables.

• This technique is often used in situations where the structure of the SQL query or
the number of parameters is not known until the program is running.
Introduction to Distributed Databases

• A distributed database is a type of database that spans multiple physical or


logical locations, and the data is distributed across different nodes or sites.

• The goal of distributed databases is to provide improved performance,


reliability, and scalability compared to a centralized database system.

• In a distributed database, data is stored and processed across multiple


servers or nodes that are interconnected, often over a network.
Client/Server Databases

• Client/server databases refer to a type of database architecture in


which the responsibilities for processing database requests are
divided between two types of software components: clients and
servers.

• This architecture is designed to improve performance, scalability,


and maintainability by distributing tasks between the client-side
application and a dedicated database server.
Outline
• Relational Model Concepts
• Relational Model Constraints and Relational Database Schemas
• Update Operations and Dealing with Constraint Violations
• SQL Data Definition and Data Types
• Specifying Constraints in SQL
• Basic Retrieval Queries in SQL
• INSERT, DELETE, and UPDATE Statements in SQL
• Additional Features of SQL

You might also like