dbms
dbms
(23E05402T)
This unit describes the basic differences between the traditional way of processing, also called as file processing,
and the database method of processing the data. Every operating system provides users to open, save, and close
a file. The users can store appropriate information in these files. Take a look at the Figure 1.1 which shows the
traditional file processing system that stores the program and data description in a file. The related information
of a particular application is stored in various files named as File1, File2, etc., and these files are manipulated
using Program1, Program2, etc. This is the method that was used in early days.
It means that without a DBMS, the data will simply be dumped into one or more files. For any updation, the
files need to be opened and manually search for the line or record, update and then save the file. Now you can
understand the difficulties involved in marinating this type of information storage.
With the advent of database systems, the file processing approach is no longer used. Now you can observe with
the Figure 1.2 that the database is in the disk which in turn is controlled by the DBMS. In this approach the
Application Program-1 along with its data semantics, Application Program-2 along with its data semantics, etc.,
interact with the database where the actual data and constraints are stored through the DBMS. The DBMS
provides the necessary control and manipulation software modules for these application programs to access the
data stored in the database.
Drawbacks of File Processing System
1.Catalog: In DBMS, the database structure is stored in a catalog and it also contains the storage details with
constraints.
The DBMS software must equally work with any number of database applications provided the catalog contains
the structure and other details of that application. In file processing the data definition is part of the application
program.
Example: Record declaration in Pascal. Class or structure declaration in C++.
2. Program-data independence: In file processing, if changes are done in the structure of the file, then we may
require changing the program design that accesses it. In DBMS the access programs are written independent of
any specific files. This is called as program-data independence.
The DBMS stores the data in such a way that the user need not be aware of these details. This concept is called
as data abstraction and it may also be called as conceptual representation
3. Views: A database may have many users and each one may be interested on a particular view of the
application. A view is conceptually a table, but the records of this table are not stored in the database.
Example: Consider the Student database in which we can think of two views:
View 1: Students Grade in various courses. To obtain this information the tables Course and Grade_Report are
to be joined and created as a view.
View 2: If we want to know the Prerequisite Courses that a student needs to study, three tables are to be joined.
These tables are nothing but Student, Section and Prerequisite.
4. Sharing and Transaction processing: A DBMS must provide control for various users trying to access the
database.
The importance of database systems can be judged in another way—today, database system vendors like
Oracle are among the largest software companies in the world, and database systems form an important part of
the product line of Microsoft and IBM.
1.6 Brief introduction of different Data Models
Underlying the structure of a database is the data model: 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.
There are a number of different data models that we shall cover in the text. The data models can be classified
into four different categories:
• Relational Model. The relational model uses a collection of tables to represent both data and the relationships
among those data. Each table has multiple columns, and each column has a unique name. Tables are also known
as relations. The relational model is an example of a record-based model. Record-based models are so named
because the database is structured in fixed-format records of several types. Each table contains records of a
particular type. Each record type defines a fixed number of fields, or attributes. The columns of the table
correspond to the attributes of the record type. The relational data model is the most widely used data model,
and a vast majority of current database systems are based on the relational model.
The entity-relationship (E-R) data model uses a collection of basic objects, called entities, and relationships
among these objects. An entity is a “thing” or “object” in the real world that is distinguishable from other
objects.
Object-Based Data Model. Object-oriented programming (especiallyinJava, C++, or C#) has become the
dominant software-development methodology. This led to the development of an object-oriented data model
that can be seen as extending the E-R model with notions of encapsulation, methods (functions), and object
identity. The object-relational data model combines features of the object-oriented data model and relational
data model.
• Semistructured Data Model. The semi structured data model permits the specification of data where
individual data items of the same type may have different sets of attributes. This is in contrast to the data models
mentioned earlier, where every data item of a particular type must have the same set of attributes. The Extensible
Markup Language (XML) is widely used to represent semi structured data.
1.7 Concept of Schema
When we talk about a database, we must differentiate between the database schema, which is the logical design
of the database, and the database instance, which is a snapshot of the data in the database at a given instant in
time. The concept of a relation corresponds to the programming-language notion of a variable, while the concept
of a relation schema corresponds to the programming-language notion of type definition. In general, a relation
schema consists of a list of attributes and their corresponding domains.
In a two-tier architecture, the application resides at the client machine, where it invokes database system
functionality at the server machine through query language statements. Application program interface standards like
ODBC and JDBC are used for interaction between the client and the server.
In contrast, in a three-tier architecture, the client machine acts as merely a front end and does not contain any
direct database calls. Instead, the client end communicates with an application server, usually through a forms
interface. The application server in turn communicates with a database system to access data. The business logic
of the application, which says what actions to carry out under what conditions, is embedded in the application
server, instead of being distributed across multiple clients. Three-tier applications are more appropriate for large
applications, and for applications that run on the World Wide Web.
Entity Relationship Model
1.13 Introduction
The Entity-Relationship (ER) Model is a conceptual framework used to describe the structure of a
database in terms of entities, their attributes, and the relationships between them. It was introduced by
Peter Chen in 1976 and is commonly used in the design phase of database systems to visually represent
the data requirements and relationships before the actual database is created.
Advantages of the ER Model:
Easy to Understand: The ER model provides an intuitive and easy-to-understand view of data
relationships.
Comprehensive: It captures all essential aspects of the data model, including entities,
attributes, and relationships.
Flexible: The model can be adapted to describe complex relationships, including hierarchies and
weak entities.
Limitations:
Static Representation: The ER model captures data at a specific point in time and doesn't
represent changes or dynamics over time.
Not Suitable for Some Database Operations: Some database operations, like performance
optimization or indexing, are not covered in the ER model.
1.14 Representation of Entities
Entity: An entity is an object and it is represented by rectangle symbol.
Student College
For example: In the following ER diagram we have two entities Student and College and these two entities
have many to one relationship as many student’s study in a single college.
Student College
study
Types of Entities: An entity is classified into two types. 1. Weak Entity and 2. Strong Entity
1. Weak Entity: An entity that cannot be uniquely identified by its own attributes and relies on
the relationship with other entity is called weak entity. The weak entity is represented by a
double rectangle.
For example – a bank account cannot be uniquely identified without knowing the bank to
which the account belongs, so bank account is a weak entity.
1. Strong Entity: A strong entity is an entity that can exist independently and is uniquely
identifiable by its own attributes. It has a primary key that uniquely identifies each entity
instance.
Example: A "Student" entity in a college database, with StudentID as its primary key, is
a strong entity because it doesn’t rely on other entities for identification.
Student College
study
1.15 Attributes:
Attributes represent the properties or characteristics of an entity.
For example, for a "Student" entity, attributes might be Name, Student ID, Date of Birth, etc.
Each attribute is represented by an oval in an ER diagram, connected to its corresponding entity.
Types of attributes:
Simple Attribute (or) Key Attribute : Cannot be divided further (e.g., Age, ID).
For example, student roll number can uniquely identify
a student from a set of students. Key attribute is
represented by oval same as other attributes however
the text of key attribute is underlined.
Multi-valued Attribute: An attribute that can hold multiple values is known as multivalued
attribute. It is represented with double ovals in an ER Diagram. For example – A person can have
more than one phone numbers so the phone number attribute is multivalued. (e.g., Phone
Numbers for a person).
1.16 Entity Sets: An entity set is a group of entities that share the same properties and are used to
organize data in a database.
For example, a customer entity might have attributes like name, address, and phone number. A customer
entity set would be a group of customers with those same attributes.
Customer
name
Customer_id Customer phone
no
Customer
Relationship set:
In a Relational database, relationship sets are built
up by utilizing keys, such as primary and foreign
keys, to interface related records over distinctive
tables.
In the above ER Diagram, ‘Payment’ is the weak entity, ‘Loan Payment’ is the non-identifying
relationship and ‘Payment Number’ is the partial key. Primary Key of the Loan along with the partial key
would be used to identify the records.
Binary Relationship: A relationship among 2 entity sets. Example: A professor teaches a course
and a courseis taught by a professor.
Ternary Relationship: A relationship among 3 entity sets. Example: A professor teaches a course in so
and sosemester.
2. Cardinality: Cardinality defines the number of instances of one entity that can or must be associated
with instances of another entity in a relationship.
The main types of cardinality are:
One-to-One (1:1): Each instance of one entity is related to exactly one instance of another entity.
For example, a student has only
one identification card and an identification
card is given to one person.
One-to-Many (1:M): An instance of one entity can be related to multiple instances of
another entity. For example, a customer
can place many orders, but an order
cannot be placed by many customers.
Many-to-One (M:1): Many instances of one entity can be related to one instance of another entity.
For example, many students can involve only on one project.
For example, students have to opt for a single
course, but a course can have many students.
Many-to-Many (M:M): Instances of one entity can be related to many instances of another entity,
and vice versa. For example, students can enroll in multiple courses, and courses can have multiple
students.
For example, you can assign an employee to
many projects and a project can have many
employees.
1.16. Constraints: In Database Management Systems (DBMS), constraints are rules enforced on
the data in tables to ensure accuracy, integrity, and consistency. They define certain conditions that data
must meet when being inserted, updated, or deleted from a database. Constraints play a critical role in
maintaining data integrity across various operations in a database. Types of Constraints: There are various
types of constraints in DBMS. They are
1. Domain Integrity Constraints
2. Entity Integrity Constraints
3. Referential Integrity Constraints
4. Key Constraints
1. Domain Integrity Constraints: Domain constraints in DBMS are the set of rules which defines
what kind of data can be stored in an attribute. Domain Constraints help us to enter the data into
the table according to the particular data type. The domain constraints are
a) NOT NULL 2) CHECK.
a) NOT NULL: It is domain constraint that a column cannot have a NULL value.
Syntax: CREATE TABLE <table name> (Column1 data type NOT NULL,
Column2 data type NOT NULL,
...
Column n data type NOT NULL); Example: CREATE
TABLE customers ( ID number(6) NOT NULL,
NAME varchar2(20), NOT NULL,
AGE number(3) NOT NULL, ADDRESS
char(25),
SALARY number(10,2),
PRIMARY KEY (ID));
b) CHECK: Check is a domain constraint, it allows the data into the table only after checking the
condition.
Syntax: CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical expression), ….);
Example: CREATE TABLE student(Rollno number(6) NOT NULL, Name
varchar2(20) NOT NULL, Age number(3) CHECK (Age>=18));
2. Entity Integrity Constraint: Entity integrity ensures that every table has a primary key, and that the
primary key values are unique and non-null. This ensures that every entity (record) in a table can be
uniquely identified.
Example: A table of students must have a not-null, unique StudentID for each record.
create table student(StudentID number(5) NOT NULL, name varchar2(20), marks number(3),
grade char(1), primary key(StudentID));
3. Referential Integrity Constraint: Referential integrity (Foreign Key Constraints) is maintained
using foreign key constraints, which enforce a link between two tables. A foreign key in one table
refers to a primary key in another table. This constraint ensures that records in a table cannot
reference non-existent records in another table, preventing invalid data entries.
Example: Table-1 Department and Table-2 Employee
CREATE TABLE Department (DeptID NUMBER(6) PRIMARY KEY, DeptName VARCHAR2(50) );
CREATE TABLE Employee (EmployeeID NUMBER(6) PRIMARY KEY, Name VARCHAR2(50), DeptID
NUMBER(2), FOREIGN KEY (DeptID) REFERENCES
Department(DeptID) );
4. Key Constraints: Key constraints in Database Management Systems (DBMS) ensure that data
within a table is uniquely identifiable and that the integrity of the data is maintained. Key constraints
are rules that are applied to keys, which are attributes (columns) used to identify rows (records) in a
table.
There are several types of key constraints in DBMS, and each has a specific role in maintaining data
integrity and preventing redundancy. The most common key constraints are Primary Key, Unique
Key, Foreign Key, and Composite Key.
a. Primary Key: A primary key is used in table that uniquely identifies each row/record. This
is also one type of Integrity Constraint. Primary keys must have distinct values. Null values
are not allowed in a primary key column. A table can
only have one primary key, which can be made up of one or more fields. It creates a
composite key when several fields are used as a primary key.
Syntax: Create table table_name (Column_name1 datatype NOT NULL, Column_name2 datatype,
..... Column_name n datatype, PRIMARY KEY (column_name1) ) .
Example: create table student(id number(6) not null, name varchar2(20), marks number(3),
grade varchar2(5), primary key(id));
b. Foreign key: A foreign key is used to define a column or group of columns in a relational
database table that provides a link between data in two tables. It acts as a cross-reference
between tables because it references the primary key of another table, thereby establishing a
link between them. It means, the foreign key constraint ensures that values in the foreign key
column(s) match values in the referenced primary key column(s).
Syntax:CREATE TABLE Table_Name ( col_name type(size) FOREIGN KEY(col_name)
REFERENCES table_name);
customers table
CustomerID FirstName LastName Email
orders table
c. Unique Constraint:
A unique constraint ensures that all values in a column or a set of columns are unique.
Unlike the primary key, a unique constraint allows NULL values.
Syntax
CREATE TABLE Table_name (
Column_Name1 DataType NOT NULL UNIQUE,
Column_Name2 DataType NOT NULL, Column_Name3
DataType,
.......
Column_Name N DataType);
Example:
CREATE TABLE Student_DB (S_ID number NOT NULL UNIQUE, L_Name
varchar2(255) NOT NULL, F_Name varchar2(255), Age number);
1.18. Inheritance: It refers to the ability of a subclass (or specialized entity) to inherit attributes and
relationships from a superclass (or general entity). This allows for more efficient and logical modeling of
hierarchical relationships within a database.
Superclass and Subclass:
A superclass is a general entity that holds attributes and relationships common to multiple
entities.
A subclass is a specialized entity that inherits all the attributes and relationships of the
superclass but may have additional attributes or relationships specific to itself.
Attributes Inheritance:
A subclass inherits all the attributes of the superclass, meaning the subclass automatically
includes the attributes of the superclass without redefining them.
Relationships Inheritance:
A subclass also inherits the relationships that the superclass participates in. For example, if the
superclass has a relationship with another entity, the subclass will also have the same relationship.
Types of Inheritance in DBMS: 1. Generalization and 2. Specialization.
1. Generalization: Generalization is a bottom-up approach in which two lower level entities
combine to form a higher-level entity. In generalization, the higher-level entity can also
combine with other lower level entity to make further higher-level entity.
For example, if both Car and Truck share common attributes like VehicleID, Make, and Model,
these attributes can be abstracted into a Vehicle superclass, from which both Car and Truck will
inherit.
Specialization: Specialization is the opposite of generalization. It is the process of creating a more specific
entity (subclass) from a more general one (superclass). The specialized subclass inherits the common attributes
and relationships of the superclass but can also have its own unique attributes. For example, the entity Person
can be specialized into Student and Teacher. In the diagram, the higher-level entity called Product, which
represents all products. Then, there are specialized entities for specific product categories, such as Electronics,
Clothing, and Books, with each having their own specialized attributes.
UNIT -2
DBMS
(R23 Syllabus)
UNIT II:
Relational Model: Introduction to relational model, concepts of domain, attribute, tuple, relation,
importance of null values, constraints (Domain, Key constraints, integrity constraints) and their
importance, Relational Algebra, Relational Calculus. BASIC SQL: Simple Database schema, data
types, table definitions (create, alter), different DML operations (insert, delete, update).
2.1. Introduction to relational model: In 1970, E.F. Codd proposed the relational Model
to model data in the form of relations or tables. After designing the conceptual model of the
Database using ER diagram, we need to convert the conceptual model into a relational model
which can be implemented using any RDBMS language like Oracle SQL, MySQL, etc. So, we
will see what the Relational Model is.
Key Concepts of the Relational Model:
1. Relation (Table):
o A relation is a table with rows and columns. Each relation represents a specific entity or
concept.
o The rows (also called tuples) represent individual records.
o The columns represent attributes of the entity.
2. Tuple (Row):
o A tuple is a single row in a table, representing a single record with specific attribute values.
3. Attribute (Column):
o An attribute is a named column of a relation. It holds values from a specific domain (e.g., an
attribute "Age" may hold integers).
4. Domain:
o A domain is the set of possible values that an attribute can take. For instance, a "Gender"
attribute might have a domain containing the values {Male, Female, Other}.
5. Schema:
o The schema defines the structure of a relation. It specifies the relation name, attributes, and
their types.
o Example: Student (StudentID, Name, Age, Gender)
6. Primary Key:
o A primary key is a unique identifier for each tuple in a relation. No two rows in a table can
have the same value for the primary key attribute(s).
o Example: In a "Student" table, "StudentID" might be the primary key.
7. Foreign Key:
o A foreign key is an attribute (or a set of attributes) in one table that refers to the primary key
in another table. This is used to establish relationships between tables.
8. Relation Operations: The relational model supports a set of operations (called relational
algebra) for manipulating relations. Some of the core operations are:
o Selection: Extracts rows from a table that meet a specific condition.
o Projection: Extracts specific columns from a table.
o Join: Combines related rows from two or more tables.
o Union, Intersection, and Difference: Used for set operations on relations.
Advantages of the Relational Model:
1. Simplicity: Data is organized into simple tables, which are easy to understand and manipulate.
2. Data Integrity: Constraints (like primary and foreign keys) ensure data consistency and integrity.
3. Flexibility: Tables can be easily joined or modified, and complex queries can be written to retrieve
data.
4. Normalization: The relational model supports techniques like normalization to reduce redundancy
and improve efficiency.
Example: Consider two tables, Students and Courses.
StudentID Name Age Gender
101 Alice 20 Female
102 Bob 22 Male
103 Carol 19 Female
CourseID CourseName StudentID
C01 Mathematics 101
C02 Physics 102
C03 Chemistry 103
Primary Key: "StudentID" in the "Students" table.
Foreign Key: "StudentID" in the "Courses" table references the "Students" table.
In this case, the Students table represents the main data about students, while the Courses table
shows the courses each student is enrolled in, linking back to the primary key in the Students table.
The relational model provides a structured and highly efficient way of managing large amounts of
interrelated data in modern databases.
2.2. Concepts of domain
In a Database Management System (DBMS), a domain refers to the set of permissible values that
an attribute (column) can take. It is essentially a constraint on the attribute, defining the kind of
data that can be stored in that column. Domains are crucial because they help ensure data integrity
by restricting the values that can be entered into a database.
Key Aspects of a Domain:
1. Data Type: The domain defines the data type of the attribute, such as integers, floating-
point numbers, strings, dates, or Boolean values. For example:
An attribute "Age" might have the domain of integers.
An attribute "Date of Birth" might have the domain of dates.
2. Range or Set of Values:
o Domains can specify a range or specific set of values that an attribute can take. For
example:
The "Gender" attribute might have the domain of {Male, Female,
Other}.
The "Age" attribute might have a domain specifying valid ages between 0
and 120.
3. Null Values:
o The domain may allow or disallow NULL values, depending on whether the
attribute can contain missing or undefined data.
4. User-Defined Domains:
o In addition to built-in data types, some DBMSs allow users to define custom
domains with specific constraints, such as creating a domain for "Country Codes"
where only valid country abbreviations are allowed.
Example of Domain: Consider a table Students with the following schema:
Attribute Data Type Domain
StudentID Integer Positive integers
Name String Non-empty string
Age Integer Values between 0 and 120
Gender String {Male, Female, Other}
DateOfBirth Date Valid date values
In this example:
The "StudentID" domain restricts the values to positive integers.
The "Age" attribute has a domain that limits values between 0 and 120.
The "Gender" attribute can only have values from the set {Male, Female, Other}.
Importance of Domains:
1. Data Integrity: By restricting attributes to specific domains, the database ensures that only
valid and meaningful data can be stored, preventing errors.
2. Consistency: Domains enforce consistency in the data across the database, especially when
multiple tables use the same domain (e.g., different tables storing gender data will always
use the same set of valid values).
3. Validation: Domains help validate data at the time of entry, ensuring that any invalid or
inappropriate values are rejected.
Domain Constraints:
Domains can have additional constraints, such as uniqueness, non-null constraints, or even
foreign key references to enforce referential integrity.
In short, a domain in DBMS is a critical concept that defines the valid set of values for each attribute,
ensuring that the data stored in the database is accurate, consistent, and follows a predefined
structure.
2.3. Attribute:
In a Database Management System (DBMS), an attribute refers to a column in a table, representing
a specific property or characteristic of the entity that the table models. Each attribute contains data
of a particular type, and its values describe or define various aspects of the records (or rows) within
the table.
Key Points about Attributes:
1. Columns in a Table:
o An attribute is equivalent to a column in a relational database table.
o Each table represents an entity (e.g., Student, Employee), and each attribute
represents a specific detail or property of that entity.
2. Data Type:
o Each attribute has an associated data type, which defines the kind of data the
attribute can store, such as:
Integer: Whole numbers (e.g., Age, EmployeeID)
String: Text or characters (e.g., Name, Address)
Date: Date values (e.g., DateOfBirth)
Boolean: True/False values
3. Attribute Values:
o Each record (or tuple) in a table has a value for each attribute.
o The value for an attribute may be mandatory (required) or optional (null) based on
the design of the database and any constraints applied.
4. Domains:
o Each attribute belongs to a specific domain, which defines the set of permissible
values that the attribute can take. For example, an attribute like "Age" may have a
domain that only allows integers between 0 and 120.
5. Key Attributes:
o Primary Key: An attribute (or set of attributes) that uniquely identifies each tuple
(row) in the table. For example, "StudentID" in a "Students" table.
o Foreign Key: An attribute that references the primary key of another table, creating
relationships between tables. For example, "StudentID" in an "Enrollments" table,
which refers to the "Students" table.
6. Composite Attributes:
o A composite attribute consists of multiple components or sub-attributes. For
example, an attribute "FullName" might be split into "FirstName" and "LastName."
7. Derived Attributes:
o These are attributes that can be calculated from other attributes. For instance, "Age"
could be derived from the "DateOfBirth" attribute.
2.4. Tuple: In DBMS (Database Management System), a tuple refers to a single row or record
in a table. A tuple represents a set of values corresponding to the attributes (or columns) of a table,
where each attribute has a specific data type. Essentially, a tuple is a collection of related data points
that describe a specific entity or object.
Example: Consider a table named Students:
Student_ID Name Age Grade
101 Alice 20 A
102 Bob 21 B
103 Charlie 22 A
Each row in this table (e.g., (101, Alice, 20, A)) is a tuple. In this context:
The table represents a relation.
Each column represents an attribute.
Each row (or tuple) represents an instance of the relation.
2.5. Relation: In DBMS (Database Management System), a relation refers to a table that stores
data in a structured format. A relation is a collection of tuples (rows) that share a common set of
attributes (columns). Each relation represents an entity or a concept, and its structure is defined by
its attributes.
Key Components of a Relation:
1. Attributes (Columns): These are the characteristics or properties of the entity represented
by the relation. Each attribute has a data type and defines what kind of data can be stored.
2. Tuples (Rows): A tuple is a single record in the relation, representing one instance of the
entity. Each tuple contains a set of attribute values.
3. Domain: The domain refers to the possible values that an attribute can take. For instance, if
an attribute is "Age", its domain might be restricted to positive integers.
4. Degree: The degree of a relation is the number of attributes or columns it has.
5. Cardinality: The cardinality of a relation is the number of tuples or rows it contains.
Example: Consider a Students relation:
Student_ID Name Age Grade
101 Alice 20 A
102 Bob 21 B
103 Charlie 22 A
Relation: The table "Students" is the relation.
Attributes: Student_ID, Name, Age, Grade.
Tuples: Each row like (101, Alice, 20, A) is a tuple.
2.6. Importance of null values:
In DBMS (Database Management System), the use of NULL values is important for representing
missing, unknown, or inapplicable data. Here are the key uses of NULL values in a database:
1. For example, if the birthdate of a customer is not available, it can be stored as NULL.
2. For instance, in an employee database, the "Passport Number" field can be NULL
for employees who do not have passports.
3. For example, in a user registration form, fields like "Alternate Phone Number" or
"Middle Name" might be optional and can be left as NULL if not filled out.
4. NULL allows you to clearly express that the data is missing, rather than incorrect.
5. For example, if an employee is not assigned to any project, the foreign key for the
project ID in the employee table can be NULL.
6. NULL helps maintain the flexibility and integrity of the database by allowing
incomplete records to be stored without introducing incorrect or default data.
7. NULL can be used in SQL queries to search for records where specific fields have
missing data. Functions like IS NULL or IS NOT NULL allow users to filter or identify
records with missing information.
Example: SELECT * FROM Employees WHERE Department_ID IS NULL;
8.
9. When performing operations on Aggregate functions like SUM(), AVG(), COUNT(),
etc., NULL values are typically ignored. This behavior allows the database to avoid
incorrect calculations when data is missing.
For example: SELECT AVG(Salary) FROM Employees;
In this query, employees with a NULL salary are excluded from the average
calculation.
Syntax: CREATE TABLE <table name> (Column1 data type NOT NULL,
Column2 data type NOT NULL,
...
Column n data type NOT NULL);
Example: CREATE TABLE customers( ID number(6) NOT NULL,
NAME varchar2(20), NOT NULL,
AGE number(3) NOT NULL,
ADDRESS char(25),
SALARY number(10,2),
PRIMARY KEY (ID));
2.7. Constraints and their importance: In DBMS (Database Management System),
constraints are rules or restrictions applied to the data in a table to ensure accuracy, consistency,
and integrity. These constraints help to maintain the quality of the data by restricting the types of
values that can be inserted into a table.
Types of Constraints: There are various types of constraints in DBMS. They are
1. Domain Integrity Constraints
2. Entity Integrity Constraints
3. Referential Integrity Constraints
4. Key Constraints
1. Domain Integrity Constraints: Domain constraints in DBMS are the set of rules which
defines what kind of data can be stored in an attribute. Domain Constraints help us to enter
the data into the table according to the particular data type. The domain constraints are
a) NOT NULL 2) CHECK.
a) NOT NULL: It is domain constraint that a column cannot have a NULL value.
Syntax: CREATE TABLE <table name> (Column1 data type NOT NULL,
Column2 data type NOT NULL,
...
Column n data type NOT NULL);
Example: CREATE TABLE customers ( ID number(6) NOT NULL,
NAME varchar2(20), NOT NULL,
AGE number(3) NOT NULL,
ADDRESS char(25),
SALARY number(10,2),
PRIMARY KEY (ID));
b) CHECK: Check is a domain constraint, it allows the data into the table only after checking
the condition.
Syntax: CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical
expression), ….);
Example: CREATE TABLE student(Rollno number(6) NOT NULL, Name
varchar2(20) NOT NULL, Age number(3) CHECK (Age>=18));
2. Entity Integrity Constraint: Entity integrity ensures that every table has a primary key, and
that the primary key values are unique and non-null. This ensures that every entity (record)
in a table can be uniquely identified.
Example: A table of students must have a not-null, unique StudentID for each record.
create table student(StudentID number(5) NOT NULL, name varchar2(20), marks
number(3), grade char(1), primary key(StudentID));
3. Referential Integrity Constraint: Referential integrity (Foreign Key Constraints) is
maintained using foreign key constraints, which enforce a link between two tables. A foreign
key in one table refers to a primary key in another table. This constraint ensures that records
in a table cannot reference non-existent records in another table, preventing invalid data
entries.
Example: Table-1 Department and Table-2 Employee
CREATE TABLE Department (DeptID NUMBER(6) PRIMARY KEY, DeptName
VARCHAR2(50) );
CREATE TABLE Employee (EmployeeID NUMBER(6) PRIMARY KEY, Name
VARCHAR2(50), DeptID NUMBER(2), FOREIGN KEY (DeptID) REFERENCES
Department(DeptID) );
4. Key Constraints: Key constraints in Database Management Systems (DBMS) ensure
that data within a table is uniquely identifiable and that the integrity of the data is maintained.
Key constraints are rules that are applied to keys, which are attributes (columns) used to
identify rows (records) in a table.
There are several types of key constraints in DBMS, and each has a specific role in
maintaining data integrity and preventing redundancy. The most common key constraints
are Primary Key, Unique Key, Foreign Key, and Composite Key.
a. Primary Key: A primary key is used in table that uniquely identifies each
row/record. This is also one type of Integrity Constraint. Primary keys must have
distinct values. Null values are not allowed in a primary key column. A table can
only have one primary key, which can be made up of one or more fields. It creates a
composite key when several fields are used as a primary key.
Syntax: Create table table_name (Column_name1 datatype NOT NULL, Column_name2
datatype, ..... Column_name n datatype, PRIMARY KEY (column_name1) ) .
Example: create table student(id number(6) not null, name varchar2(20), marks
number(3), grade varchar2(5), primary key(id));
b. Foreign key: A foreign key is used to define a column or group of columns in a
relational database table that provides a link between data in two tables. It acts as a
cross-reference between tables because it references the primary key of another table,
thereby establishing a link between them. It means, the foreign key constraint ensures
that values in the foreign key column(s) match values in the referenced primary key
column(s).
Syntax:CREATE TABLE Table_Name ( col_name type(size) FOREIGN
KEY(col_name) REFERENCES table_name);
Example: Providing relation between Customer table and Order table.
CREATE TABLE customers (customerID number(6) PRIMARY KEY, FirstName
VARCHAR2(30), LastName VARCHAR2(30), Email VARCHAR2(50) UIQUE);
CREATE TABLE orders (orderID NUMBER(6) PRIMARY KEY, customerID
NUMBER(6), orderdate DATE, TotalAmount NUMBER(10, 2), FOREIGN KEY
(customerID REFERENCES customers(customerID));
customers table
CustomerID FirstName LastName Email
orders table
OrderID CustomerID orderdate TotalAmount
c. Unique Constraint:
A unique constraint ensures that all values in a column or a set of columns are
unique. Unlike the primary key, a unique constraint allows NULL values.
Syntax
CREATE TABLE Table_name (
Column_Name1 DataType NOT NULL UNIQUE,
Column_Name2 DataType NOT NULL,
Column_Name3 DataType,
.......
Column_Name N DataType);
Example: CREATE TABLE Student_DB (S_ID number NOT NULL UNIQUE, L_Name varchar2(255)
NOT NULL, F_Name varchar2(255), Age number);
2.8. Relational Algebra: Relational Algebra is a procedural query language used in Database
Management Systems (DBMS) to manipulate and query relational data. It forms the theoretical
foundation of SQL and is used to retrieve data from relations (tables). In relational algebra,
operations are performed on one or more relations to produce a new relation.
Basic Operations in Relational Algebra
1. Selection (σ) (sigma):
The selection operation selects rows (tuples) from a relation that satisfy a given
condition.
Syntax: σ condition (Relation)
Example-1: SQL QUERY: SELECT * FROM STUDENT;
ALGEBRA QUERY: σ (STUDENT);
Example-2: SQL-QUERY: SELECT * FROM STUDENT WHERE GRADE =
‘A’;
ALGEBRA-QUERY : σ grade = ‘A’ (student)
It will display all rows from the ‘student’ table where the grade = ‘A’.
1. Projection (π):
o The projection operation selects specific columns (attributes) from a relation,
removing duplicates.
o Notation: π<sub>attribute1, attribute2, ..., attributeN</sub>(R)
o Example: π Name, Age (Person) selects only the Name and Age columns from the
Person table.
2. Union (∪):
o The union operation combines tuples from two relations, removing duplicates (both
relations must have the same set of attributes).
o Notation: R1 ∪ R2
o Example: Student ∪ Teacher gives a relation containing all tuples present in either
the Student or Teacher relation.
3. Set Difference (-):
o The set difference operation finds tuples that are in one relation but not in the other.
o Notation: R1 - R2
o Example: Employee - Manager gives tuples that are in the Employee relation but not
in the Manager relation.
4. Cartesian Product (×):
o The Cartesian product operation combines every tuple of one relation with every
tuple of another relation, producing all possible combinations.
o Notation: R1 × R2
o Example: Student × Teacher returns all possible combinations of tuples from Student
and Teacher.
5. Rename (ρ):
o The rename operation allows you to rename a relation or its attributes.
o Notation: ρ<sub>new_relation_name</sub>(R) or ρ<sub>new_attr1,
new_attr2</sub>(R)
o Example: ρ S(Student) renames the Student relation to S.
Additional Operations
1. Intersection (∩):
o The intersection operation retrieves tuples that are present in both relations.
o Notation: R1 ∩ R2
o Example: Student ∩ Teacher gives a relation containing tuples present in both
Student and Teacher relations.
Example Query Using Relational Algebra
1. Find the names of all employees who work in the ‘HR’ department:
π Name (σ Dept = 'HR' (Employee))
2. Find all departments that have no employees:
π Dept (Department) - π Dept (Employee)
2.9. Relational Calculus: Relational Calculus is a non-procedural query language used in
Database Management Systems (DBMS), focusing on what to retrieve rather than how to retrieve
it. It is different from relational algebra, which is a procedural language. In relational calculus, you
define the properties of the data you want without specifying a step-by-step procedure to obtain it.
There are two types of relational calculus:
1. Tuple Relational Calculus (TRC):
In tuple relational calculus, queries are expressed in terms of tuples. It uses tuple variables that
range over a relation, and conditions are specified on the attributes of the tuple.
A TRC query has the form:
{ t | P(t) }
t is a tuple variable.
P(t) is a condition or formula in the predicate logic.
The result of the query includes all the tuples t that satisfy the condition P(t).
Example: This retrieves all tuples t from the Students relation where the age attribute is greater than 18.
2. Domain Relational Calculus (DRC):
In domain relational calculus, queries are expressed using domain variables that take values
from the attributes of the relations, rather than the tuples themselves.
A DRC query has the form:
{ <x1, x2, ..., xn> | P(x1, x2, ..., xn) }
o x1, x2, ..., xn are domain variables.
o P(x1, x2, ..., xn) is a condition or formula in the predicate logic.
2.10. BASIC SQL: Simple Database schema, data types, table definitions (create,
alter), different DML operations (insert, delete, update).
2.10.1. DDL: Data Definition Language (DDL) statements are used to define the database
structure orschema.
DDL Commands: Create, Alter, Drop, Rename, TruncateCREATE - to create objects in the database
ALTER - alters the structure of the databaseDROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated for the records are
removedRENAME - rename an object
1. The CREATE TABLE statement is used to create a new table in a database.
Syntax-1
CREATE TABLE <table name> (column name1 data type1, column name2 data type2, … column
name n data type n);
Example: CREATE TABLE date_1(dd number(2), mm number(2), year number(4));
Table is created.
Syntax-2
CREATE TABLE <table name> (column name1 data type1 constrain, column name2 data type2,
… column name n data type n);
Example: CREATE TABLE Student( rollno varchar2(10) primary key, name varchar2(10), dob
date, course varchar2(20), course_fees number(10,2), street varchar2(10), city varchar2(10), state
varchar2(10), pincode number(6));
Table is created.
Syntax-3
CREATE TABLE <table name> AS SELECT column1, column2, - - - FROM <existing table
name> WHERE <condition>
This command is used to new_table with existing table data.
Example: CREATE TABLE library_table AS SELECT rollno, ename, course FROM student
WHERE course = “CSE-DS”;
1. ALTER Command: Alter command is used to add a new column, rename the column, change
the size of column or drop the column from given table.
a. ADD command: This command is used to add new column. It is used in ALTER command.
Syntax: ALTER TABLE <table name> ADD <column name> <data type>;
Example: ALTER TABLE student ADD email varchar2(35);
b. RENAME command: This command is used to rename the column. It is using in ALTER
command.
Syntax: ALTER TABLE <table name> RENAME COLUMN old name TO new name;
Example: ALTER TABLE student RENAME COLUMN name TO student_name;
c. ALTER Command: This is used to change the column size. It is used in ALTER command.
Syntax: ALTER TABLE <table name> ALTER COLUMN <column name> <data type>;
Example: ALTER TABLE student ALTER name varchar2(25);
d. DROP Command: This is used to drop the column from table. It is used in ALTER command.
Syntax: ALTER TABLE <table name> DROP <column name>;
Example: ALTER TABLE student DROP email;
e. DROP/TRUNCATE: This command is used to drop or remove the table from sql.
Syntax: DROP TABLE <table name>; (or) Syntax: TRUNCATE TABLE <table name>;
Example: DROP TABLE student; (or) TRUNCATE TABLE student;
INSERT command: This command is used to insert data into Tables. After creation of table, the
insert command is used to insert the records (data) into table.
Syntax:
a. INSERT INTO <table name> (<&col1>,<&col2>) VALUES(<exp>,<exp>); (or)
b. INSERT INTO <table name> VALUES (<exp/data>,<exp/data>);
Example-1: INSERT INTO student (&rollno, ‘&name’,’&course’) VALUES (32101, VASU,
CSE);
Example-2: INSERT INTO student VALUES (32101, VASU, CSE);
Example-3: INSERT INTO student (&rollno, ‘&name’,’&course’);
UPDATE command: This command is used to update or modify the existing records or data in the
table
Syntax-1: Update <tablename> set <col1>=<exp1>, <col2>=<exp2> . . . ;
Syntax-2: Update <tablename> set <col1>=<exp1>, <col2>=<exp2> . . . where <condition>;
Example: UPDATE student SET name = 'sreenivas' WHERE rollno = 32101;
DELETE command: This command is used to delete or remove the existing records or data from
the table
Syntax: DELETE FROM <tablename>;
Syntax: DELETE FROM <tablename> WHERE <condition>;
Example-1: DELETE FROM student;
Example-2: DELETE FROM student WHERE rollno = 32105;