0% found this document useful (0 votes)
3 views70 pages

Dbms Notes

sklnjhhjdijvgfiopdejfhbjowkqijdfbhvhokqjdefhvdhiepoqdjfhv jbhdeoqjfbhjbjrojbhjobkh hpkbokjbhijjokjbi

Uploaded by

ganeshthata3116
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)
3 views70 pages

Dbms Notes

sklnjhhjdijvgfiopdejfhbjowkqijdfbhvhokqjdefhvdhiepoqdjfhv jbhdeoqjfbhjbjrojbhjobkh hpkbokjbhijjokjbi

Uploaded by

ganeshthata3116
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/ 70

HISTORICAL PERSPECTIVE OF DBMS:

➢ From the earliest days of computer storing and manipulating data have been a
major application.

➢ Manipulating is the process of arranging data in order.

➢ 1950s and early 1960s:


• Data processing using magnetic tapes for storage
• Tapes provide only sequential access
• Punched cards for input process.
• Data storage process consists of reading data from many tapes and writing data
to a new tape.

➢ Late 1960s and 1970s:


• Hard disks allow direct access to data
• Network and hierarchical data models in widespread use

➢ The first general purpose DBMS designed by “Charles Bachman ”at general
electric in 1960’s was called “Integrated Data Store”.

➢ Charles Bachman is the first person to develop the IDS( Integrared Data
Store).which was based on network model.
➢ In the late 1960’s IBM developed the “ Integrated Management Systems “

➢ It was developed based on Hierarchical Data Model

➢ In 1970’s the “ Relational Data Base model “ was developed by “ Edgar Codd “
at IBM.

➢ Many of the data base models we use today are Relational based.

➢ In later 1980’s IBM developed a language known as “ SQL “

➢ SQL Stands for Structured Query Language.

➢ It was developed as a standard language for the queries by ISO and ANSI .

➢ ISO Stands for International Organization For Standardization.

➢ ANSI Stands for American National Standard Institute.


1980s:

➢ SQL becomes industry standard

➢ Parallel and distributed database systems

➢ Object-oriented database systems

1990s:

➢ Large decision support and data-mining applications

➢ Large multi-terabyte data warehouses

➢ Emergence of Web commerce

2000s:

➢ XML and XQuery standards

➢ Automated database administration


Purpose of Database Systems
➢ In the early days, database applications were built directly on top of file systems

➢ Drawbacks of using file systems to store data

• Multiple file formats, duplication of information in different files


Difficulty in accessing data

Need to write a new program to carry out each new task

➢ Data isolation — multiple files and formats

➢ Integrity problems

• Integrity constraints (e.g. account balance > 0) become “buried” in

• Hard to add new constraints or change existing ones


➢ Atomicity problems , Data redundancy , Inconsistency , Data isolation.

Failures may leave database in an inconsistent state with partial updates


carried out

Example: Transfer of funds from one account to another should either complete
or not happen at all

➢ Security problems.

Database systems offer solutions to all the above problems.


File Systems:
➢ If you are using a computer , you are definitely using a file system

➢ File system comes with operating system

➢ File system is used to manage the data , dbms is also used to manage the data

➢ The main difference between these two are

➢ File systems are for short amount of data as well as Dbms will consists huge data

➢ File system : A collection of files which are stored in a hard disk

➢ File : A collection of records , where records contains fields, where field contains data

➢ For example : ENO ENAME SALARY


1 XYZ 10000 --------------RECORD1
2 ABC 20000 -------------RECORD2

➢ Fields are stored in directories.


Draw Backs of File Systems:
➢ Data Redundancy : The same data is duplicated at different locations.

➢ Data Inconsistency :Because of data redundancy , data inconsistency problem will occurs.
(no accurate data)

➢ Data Isolation : when data of a file cannot be mapped with the related data of other file

➢ Dependency on application program.

➢ Atomicity Issues : If the transaction involves two operations then we have to execute those
two operations at once in order to complete the transaction..

➢ Data security :
File system vs DBMS
FILE SYSTEM DBMS
 File system is a software that manages  DBMS is a software for managing the data
and organizes the files in a storage with in base
a computer
 There is no redundant data
 Redundant data can be present in a file
system
 It doesn't provide backup and recovery of  It provides backup and recovery of data if
data if it is lost it is lost
 There is no efficient query processing in a  Efficient query processing is there in DBMS
file system
 There is less data consistency in file
systems  There is more data consistency because of
the process of normalization
 It is less complex when compared to DBMS
 It has more complexity
 File system provides less security in
compared to DBMS  DBMS has more security Mechanisms

 It is less expensive than DBMS  It is higher cost than a file system.


Data Base Management System
Data : Raw facts that are recorded
example text , video , images.
Data base : Data base is a collection of related data . That represents some real
world entities or real world objects,that is if there is any change in a
real world entity then that changes should be reflected on database.
Data base is used to solve data base problems.

Data base management system:


➢ DBMS is a software which is used to manage the data base
➢ In DBMS data is stored in a centralized location.
➢ DBMS is a collection of interrelated data and set of program to access those data .
➢ Database systems are designed to manage large bodies of information
➢ Manipulating the database includes such functions has quering the database to retrieve the database
➢ Update the database to reflect changes in the real world.

➢ RDBMS : Relational Data Base Management System


Data will be stored in the form of tables or relations.
In RDBMS one table is related to another, that is why it is known as relational data base
management system
Applications of DataBase System
 Banking  Social media sites

 University and colleges  Credit card transactions

➢ Telecommunications  Military

➢ Finance  Railway reservation system

➢ Manufacturing  Library management system

➢ Online retailers

➢ Airline reservation system


Network…..client server
Rdbms…….
Concept…..dbms
Rdbms….model create table row column….row means tuple…record means tuple
Hierarichal……root
Constraints means rules
No duplication that is one type of constraint
Data conditions
Uniqueness is constraints
Advantages and disadvantages of DBMS
Advantages Of DBMS Disadvantages Of DBMS
 Memory size
 Minimized redundancy
 Cost
 Eliminating inconsistency
 Complexity
 Sharing of data
 Data loss
 Data security
 High impact of failure
 Backup and recovery
 Complexity of backup and recovery
DATA MODELS:
Data model: Data model is a collection of concepts to describe structure of a data base.

Data models are :

➢ Hierarchical data model


➢ Network data model
➢ Entity relation data model [E-R MODEL]
➢ Relational data model
➢ Object oriented data model

Hierarichal data model :

➢ it is a data model in which the data is organized in the form of TREE structure
➢ The structure represents information using parent – child relation ship
➢ Here each parent can have many children and child has only single parent.
➢ That is one to many relation ship

Advantages :

➢ It supports data sharing


➢ It provides data security
➢ Data independence
Disadvantages:

➢ complex implementation
➢ Difficult to manage
➢ Structured dependency
➢ It doesn’t support many to many relationship

Network Data Model :

➢ In a network data model one parent can have several children and a child can have
many parent records.
➢ It follows GRAPH structure
➢ Network model is extension to hierarchical data model.

Advantages :

➢ The data can be accessed faster as compared to the hierarchical model.


➢ As there is a parent child relation ship so data integrity is present.
➢ Any change in parent record will reflected in the child record.

Disadvantages :

➢ As more and more relationships need to be handled the system might get complex
Entity relationship model:
It contains mainly three things
1.Entity
2.Attibutes
3.Relationships.
1.Entity :

➢ Any real world thing , any thing that has physical existence is called entity
Example : student , faculty,…

2.Attributes :

➢ Each attribute of a relation has a name


➢ The properties of entities are known as attributes.
➢ Ex: student is an entity.
Where student consists name ,roll no, marks these are known as attributes
➢ Ex : employee is an entity.
Emp no. Emp name , salary…………are the attributes

3.Reltionships:
➢ Relationship is manly use in order to connect two entities
1.one to one relationship 2. one to many relationship
3.many to one relationship 4.many to many relatioship
E-R Diagram for the Banking Enterprise
Features of ER Model:
➢ Graphical design for better understanding : It is very easy to understand so it can be
used by the developers
➢ Database design: This model helps the database designers to build the database
➢ ER Diagram :ER Diagram is used as a visual tool for representing the model.

Advantages of ER Model:

➢ ER Model is very easy to build.


➢ If we know the relationship between the attributes and entities we can easily build ER diagram
➢ Easy conversion to any model.
➢ This model can also be converted to any other model like network model, hierarchical model

Disadvantages of ER Model :

➢ Hidden Information: some information might be lost or hidden in ER Model.


Relational data model :

➢ Relational data model is the most widely used model.


➢ Relational model represents data as a collection of tables.
➢ All the information is stored in the form of rows and columns.
➢ So, the tables are also called relations in the relational model.
➢ Each row in the table is called “TUPLE”.
➢ Note that Attribute/Field/Column.., Tuple/Record/Row
➢ The features of relation models are TUPLES and ATTRIBUTES.

➢ STUDENT TABLE:----------RELATION NAME

ATTRIBUTES

ROLL NO NAME AGE PHONE NO.


10 MAHESH 20 1234567890
TUPLES
20 NARESH 19 1234567890
Advantages of Relational Model :

➢ Simple : This model is more simple as compared to the network and hierarchical model.

➢ Scalable : This model can be easily scaled as we can add as many rows and columns we want.

➢ Structural independence : we can make changes in database structure without changing


the way to access data.

Disadvantages of Relational model:

➢ For hiding the complexities and making things easier for the use this model requires more
powerful hardware computers and storage devices.
➢ As the relational model is very easy to design and use.

Object Oriented Data Model:

➢ In this model both the data and relationship are present in a single structure known as object.
➢ We can store audio ,video ,images ,etc in the database which is not possible in the Relational
model
➢ Although you can store audio and video in relational database , it is advised not to store in the
relational database.
➢ In this model, the attributes describe the properties of an object.
➢ Object that share similar characteristics are grouped in classes.
➢ Therefore, a class is a collection of similar objects model, two or more objects are
connected with the help of links.
➢ We use the link to relate objects.

For example:

EMPLOYEE DEPARTMENT
ATTRIBUTES ATTRIBUTES
NAME DEPT_ID
PHONE NO. DEPT_NAME
SALARY METHODS
DEPT_ID Change department
METHODS
Change number
➢ There are two data objects in the above example :

1.Employee
2.Department

➢ Each object data and relationship are contained in a single unit.


➢ The attributes are NAME,JOB.
➢ METHODS are used to perform the operation with the help of attributes.
➢ The two objects are connected through a common attribute DEPT_ID and communication
between these two will be done with the help of id.

Advantages:

➢ Semantic content is added


➢ Support for complex objects
➢ Inheritance promotes data integrity

Disadvantages :
➢ it is complex navigation system
➢ Slow development of standards
➢ High system overheads
➢ Slow transactions.
LEVELS OF ABSTRACTON :

➢ Data abstraction is the process of hiding unwanted or irrelevant details from the end user
➢ The database system consists of complicated data structures and relation.
➢ For users to access the data easily, these complications are kept hidden, and only the
relevant part of the database is made accessible to the users through the data abstraction.

➢ Mainly there are three level of abstraction of DBMS.

1.Physical (or) Internal level.


2.Logical (or) Conceptual level
3.View (or) External level

VIEW LEVEL
VIEW 1 ….
VIEW2 VIEW N

LOGICAL
LEVEL

PHYSICAL
LEVEL
Physical level:

➢ It is an lowest level of abstraction , that is for confidential and security purpose.


➢ Physical level tells us that how the data is stored
➢ This level is hidden by compiler from programmer.
➢ At this level the records can be described as block of storage (bytes,TB,GB).

Logical level:

➢ middle level describes what data is stored.


➢ Programmer works at this level.
➢ Data administrator use this level of abstraction to decide what information to keep
in a database.

View level :

➢ Highest level of abstraction.


➢ Describes only specific part of database
➢ At this level we have application programs which are accessed by user according to
data required.
Instances and Schemas :

Instances :

➢ The collection of information is stored in the database at a particular


movement is called an instance of the database.

(OR)
➢ The content of the database at a given point of time
➢ It is a dynamic value which keeps on changing.
ROLL NO NAME MARKS
➢ For ex: student table.
100 RAJU 90
101 RAMESH 85
102 MAHESH 95

This table contains 3 row i.e. instance of the table or instance of database
Schema :

➢ The overall design of the database is called the database schema.


➢ It is the logical structure of database.
➢ Schema will not be changed frequently.

➢ For ex: student table


in student table roll no, name , college is a structure and here it is known as schema.

➢ It is divided into three categories:

1.physical schema (Internal schema)


2. logical schema (conceptual schema)
3. view schema (External schema).

Database systems have several schemas, according to the level of abstraction.


➢ The physical schema describes the database design at the physical level
➢ The logical schema describes the database design at the logical level.
➢ A database may also have several schemas at the view level. some times
called subschemas, that describes different views of the database.
Data Independence:

➢ If we change data or data structure in the database it will not modify the user
level instructions.(or) the ability to change the schema at one level without
changing the schema at another level is known as data independence.

Types of data independence:


1.physical data independence
2. logical data independence.

Physical data independence :

➢ If we change the data in the database these changes will not reflect above
levels of logical and view level

Logical data independence :

➢ If we changes the description of data , data semantics, constraints and these


changes will not propagated to above and the below levels.
Structure of Database management system
➢ Database management system is a software that allows to access the data stored in a
Database and provides an easy and effective methods of :

❖ Defining the information


❖ Storing the information
❖ Manipulating the information
❖ Protecting the information from system crashes and thefts.
❖ Differentiating access permissions for different users.

❖ Note That: structure of Database management system is also referred as overall system structure
or Database Architecture but is different from the Tier Architecture of Database.

➢ The database system is divided into three components:

1.Query Processer
2. Storage Manager
3.Disk Storage
ER DIAGRAMS
➢ The entity relationship diagram explains the relationship among the entities present in the
Database.
➢ ER models are used to model real world objects like a person, a car, a company, and the
relation between these real world objects.
➢ ER diagrams are used to represent the E-R model in a database, which make them easy to
Convert into relations(Tables).
➢ ER diagrams require no technical knowledge and no hardware support
➢ These diagrams are very easy to understand and easy to create even for a naïve user.

Symbols used in ER Model:


➢ ER Model is used to , model the logical view of the system from a data perspective which
consists of these symbols.

❖ Rectangles : rectangles represent Entities in ER Model


❖ Ellipses : Ellipses represent Attributes in ER model
❖ Diamond : Diamonds represent Relationships among Entities.
❖ Lines : Lines represent Attributes to entities and Entity sets with other relationship types.
❖ Double Ellipse : Double Ellipses represent Multi-Valued Attributes.
❖ Double Rectangle : Double Rectangle represents a WEAK Entity.
FIGURES SYMBOLS REPRESENTS

RECTANGLE Entities in ER Model

Attributes in ER Model
ELLIPSE
Relationship Among
DIAMOND Attributes

LINES Attributes to Entities


and Entity sets with
other relationship types
DOUBLE ELLIPSE Multi-Valued Attributes

DOUBLE RECTANGLE Weak Entity

Symbols used in ER Model


Components of ER Diagram :

➢ ER Model consists of Entities , Attributes and Relationships among Entities in a


Data base System.

ER MODEL:

1.Entity.

❑ Strong Entity
❑ Weak Entity

2. Attributes.

❑ Key Attributes
❑ Composite Attributes
❑ Multivalued Attributes
❑ Derived Attributes
❑ Simple Attribute.
❑ Single Valued Attributes.
Relationship:

❑ One to One
❑ One to Many
❑ Many to One
❑ Many to Many

Entity :
An Entity may be an object with a physical existence – a particular person, car, house, or employee – or it may
be an object with a conceptual existence – a company, a job, or a university course.
Entity Set:
An Entity is an object of Entity Type and a set of all entities is called an entity set.
For Example, E1 is an entity having Entity Type Student and the set of all students is called Entity Set.
In ER diagram.
Entity Type is represented as:
1.Strong Entity:

➢ A Strong Entity is a type of entity that has a key Attribute.


➢ Strong Entity does not depend on other Entity in the Schema.
➢ It has a primary key, that helps in identifying it uniquely, and it is represented by a rectangle.
➢ These are called Strong Entity Types.

2. Weak Entity :

➢ An Entity type has a key attribute that uniquely identifies each entity in the entity set.
➢ But some entity type exists for which key attributes can’t be defined.
➢ These are called Weak Entity types.

For Example:

▪ A company may store the information of dependents (Parents, Children, Spouse) of an Employee.
But the dependents don’t have existed without the employee.

▪ So Dependent will be a Weak Entity Type and Employee will be Identifying Entity type for Dependent,
which means it is Strong Entity Type.
➢ A weak entity type is represented by a Double Rectangle.
➢ The participation of weak entity types is always total.
➢ The relationship between the weak entity type and its identifying strong entity type is called
identifying relationship and it is represented by a double diamond.

Strong Entity and Weak Entity

Attributes:
➢ Attributes are the properties that define the entity type.
For example, RollNo, Name, DOB, Age, Address, and MobileNo are the attributes that define entity type Student.
➢ In ER diagram, the attribute is represented by an oval.
1. Key Attribute :

➢ The attribute which uniquely identifies each entity in the entity set is called the key attribute.
➢ For example, Roll_No will be unique for each student.
➢ In ER diagram, the key attribute is represented by an oval with underlying lines.

2. Composite Attribute :

➢ An attribute composed of many other attributes is called a composite attribute.


➢ For example, the Address attribute of the student Entity type consists of Street, City, State, and Country.
➢ In ER diagram, the composite attribute is represented by an oval comprising of ovals.
3. Multivalued Attribute:

➢ An attribute consisting of more than one value for a given entity.


➢ For example, PhoneNo (can be more than one for a given student).
➢ In ER diagram, a multivalued attribute is represented by a double oval.
4. Derived Attribute :

➢ An attribute that can be derived from other attributes of the entity type is known
as a derived attribute. e.g.; Age (can be derived from DOB).
➢ In ER diagram, the derived attribute is represented by a dashed oval.

The Complete Entity Type Student with its Attributes can be represented as:
Relationship Type and Relationship Set :

➢ A Relationship Type represents the association between entity types.


➢ For example, ‘Enrolled in’ is a relationship type that exists between entity type Student and Course.
➢ In ER diagram, the relationship type is represented by a diamond and connecting the entities with lines.

➢ A set of relationships of the same type is known as a relationship set.


➢ The following relationship set depicts S1 as enrolled in C2, S2 as enrolled in C1, and S3 as
registered in C3.

Relationship Set
Degree of a Relationship Set :
The number of different entity sets participating in a relationship set is called the degree of a relationship set.

1. Unary Relationship:
➢ When there is only ONE entity set participating in a relation,
the relationship is called a unary relationship.
➢ For example, one person is married to only one person.

2. Binary Relationship:
➢ When there are TWO entities set participating in a relationship, the relationship is called a
binary relationship.
➢ For example, a Student is enrolled in a Course.
1. One-to-One:
➢ When each entity in each entity set can take part only once in the relationship, the cardinality is one-to-one.
➢ Let us assume that a male can marry one female and a female can marry one male.
➢ So the relationship will be one-to-one.
➢ the total number of tables that can be used in this is 2.

➢ Using Sets, it can be represented as :


2. One-to-Many:

➢ In one-to-many mapping as well where each entity can be related to more than
one relationship and the total number of tables that can be used in this is 2.

Using sets, one-to-many cardinality can be represented as:


3. Many-to-One:

➢ When entities in one entity set can take part only once in the relationship set and entities in other
entity sets can take part more than once in the relationship set, cardinality is many to one.
➢ Let us assume that a student can take only one course but one course can be taken by many
students.
➢ It means that for one course there can be n students but for one student, there will be only one
course.

Using Sets, it can be represented as:


➢ In this case, each student is taking only 1 course but 1 course has been taken by many students
4. Many-to-Many:
➢ When entities in all entity sets can take part more than
once in the relationship cardinality is many to many.
➢ Let us assume that a student can take more than one course and one course can be taken
by many students.
➢ So the relationship will be many to many.

Using Sets, it can be represented as:


In this example, student S1 is enrolled in C1 and C3 and Course C3 is enrolled by S1, S3, and S4.
So it is many-to-many relationships.
Generalization, Specialization and Aggregation in ER Model:

➢ Generalization, Specialization, and Aggregation in ER model are used for data abstraction.
➢ in which an abstraction mechanism is used to hide details of a set of objects.
➢ Some of the terms were added to the Enhanced ER Model,
➢ where some new concepts were added.

These new concepts are:

1.Generalization:

➢ Generalization is the process of extracting common properties from a set of entities and
creating a generalized entity from it.

➢ It is a bottom-up approach in which two or more entities can be generalized to a higher-level entity
if they have some attributes in common.

➢ For Example, STUDENT and FACULTY can be generalized to a higher-level entity called PERSON as shown in
Figure

➢ In this case, common attributes like P_NAME, and P_ADD become part of a higher ENTITY (PERSON),
and specialized ATTRIBUTES like S_FEE become part of a specialized entity (STUDENT).
Generalization
Specialization:
➢ In specialization, an entity is divided into sub-entities based on its characteristics.
➢ It is a top-down approach where the higher-level entity is specialized into two or more lower-
level entities.
➢ For Example, an EMPLOYEE entity in an Employee management system can be specialized into
DEVELOPER, TESTER, etc. as shown in Figure 2.
➢ In this case, common attributes like E_NAME, E_SAL, etc. become part of a higher entity
(EMPLOYEE), and specialized attributes like TES_TYPE become part of a specialized entity
(TESTER).
Aggregation:
➢ An ER diagram is not capable of representing the relationship between an
entity and a relationship which may be required in some scenarios.

➢ In those cases, a relationship with its corresponding entities is aggregated into


a higher-level entity.

➢ Aggregation is an abstraction through which we can represent relationships as


higher-level entity sets.

➢ For Example, an Employee working on a project may require some machinery.

➢ So, REQUIRE relationship is needed between the relationship WORKS_FOR and


entity MACHINERY.

➢ Using aggregation, WORKS_FOR relationship with its entities EMPLOYEE and


PROJECT is aggregated into a single entity and relationship REQUIRE is created
between the aggregated entity and MACHINERY.
What are Keys in DBMS?

➢ KEYS in DBMS is an attribute or set of attributes which helps you to identify a


row(tuple) in a relation(table).

➢ They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.

➢ Key is also helpful for finding unique record or row from the table. Database
key is also helpful for finding unique record or row from the table.

Employee ID First Name Last Name


11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Example:
➢ In the above-given example, employee ID is a primary key because it uniquely identifies an employee record.

➢ In this table, no other employee can have the same employee ID.
Why we need a Key?

➢ Here are some reasons for using sql key in the DBMS system.
➢ Keys help you to identify any row of data in a table. In a real-world application, a table
could contain thousands of records. Moreover, the records could be duplicated.
➢ Keys in RDBMS ensure that you can uniquely identify a table record despite these
challenges.
➢ Allows you to establish a relationship between and identify the relation between tables.

Types of Keys in DBMS (Database Management System):

➢ There are mainly Eight different types of Keys in DBMS and each key has it’s different
functionality:

1.Super Key
2.Primary Key
3.Candidate Key
4.Alternate Key
5.Foreign Key
6.Compound Key
7.Composite Key
8.Surrogate Key
Super Key – A super key is a group of single or multiple keys which identifies rows in a table.

Primary Key – is a column or group of columns in a table that uniquely identify every row in that table.

Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with
no repeated attributes.
Alternate Key – is a column or group of columns in a table that uniquely identify every row in that table.

Foreign Key – is a column that creates a relationship between two tables. The purpose of Foreign keys is to
maintain data integrity and allow navigation between two different instances of an entity.

Compound Key – has two or more attributes that allow you to uniquely recognize a specific record. It is possible
that each column may not be unique by itself within the database.

Composite Key – is a combination of two or more columns that uniquely identify rows in a table. The
combination of columns guarantees uniqueness, though individual uniqueness is not
guaranteed.

Surrogate Key – An artificial key which aims to uniquely identify each record is called a surrogate key. These
kind of key are unique because they are created when you don’t have any natural primary key.
What is the Super key?

➢ A super key is a group of single or multiple keys which identifies rows in a table.

➢ A Super key may have additional attributes that are not needed for unique
identification.

Example:

In the above-given example, EmpSSN and EmpNum name are superkeys.

EmpSSN EmpNum Empname


9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
What is a Primary Key?
➢ PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely identify every row in that table.
➢ The Primary Key can’t be a duplicate meaning the same value can’t appear more than once in the table.
➢ A table cannot have more than one primary key.

Rules for defining Primary key:


➢ Two rows can’t have the same primary key value
➢ It must for every row to have a primary key value.
➢ The primary key field cannot be null.
➢ The value in a primary key column can never be modified or updated if any foreign key refers to that
primary key.
Example:
➢ In the following example, <code>StudID</code> is a Primary Key.

StudID Roll No First Name Last Name Email


1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
What is the Alternate key?

➢ ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that
table.

➢ A table can have multiple choices for a primary key but only one can be set as the primary key.

➢ All the keys which are not primary key are called an Alternate Key.

Example:

➢ In this table, StudID, Roll No, Email are qualified to become a primary key.

➢ But since StudID is the primary key, Roll No, Email becomes the alternative key.

StudID Roll No First Name Last Name Email

1 11 Tom Price [email protected]


2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]
What is a Candidate Key?

➢ CANDIDATE KEY in SQL is a set of attributes that uniquely identify tuples in a table.

➢ Candidate Key is a super key with no repeated attributes.

➢ The Primary key should be selected from the candidate keys.

➢ Every table must have at least a single candidate key.

➢ A table can have multiple candidate keys but only a single primary key.

Properties of Candidate key:

➢ It must contain unique values


➢ Candidate key in SQL may have multiple attributes
➢ Must not contain null values
➢ It should contain minimum fields to ensure uniqueness
➢ Uniquely identify each record in a table
➢ Candidate key Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.
StudID Roll No First Name Last Name Email
1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]

Candidate Key in DBMS:


What is the Foreign key?

➢ FOREIGN KEY is a column that creates a relationship between two tables.


➢ The purpose of Foreign keys is to maintain data integrity and allow navigation between two different
instances of an entity.
➢ It acts as a cross-reference between two tables as it references the primary key of another table.

Example:

DeptCode DeptName
001 Science
002 English
005 Computer

Teacher ID Fname Lname


B002 David Warner
B017 Sara Joseph
B009 Mike Brunton
➢ In this key in dbms example, we have two table, teach and department in a school. However, there
is no way to see which search work in which department.

➢ In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship
between the two tables.

Teacher ID DeptCode Fname Lname

B002 002 David Warner


B017 002 Sara Joseph
B009 001 Mike Brunton

This concept is also known as Referential Integrity.


What is the Compound key?

➢ COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record.
➢ It is possible that each column may not be unique by itself within the database.
➢ However, when combined with the other column or columns the combination of composite keys become
unique.
➢ The purpose of the compound key in database is to uniquely identify each record in the table.

Example:

OrderNo ProductID Product Name Quantity


B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 OMG446789 Laser Printer 3

➢ In this example, OrderNo and ProductID can’t be a primary key as it does not uniquely identify a record.

➢ However, a compound key of Order ID and Product ID could be used as it uniquely identified each record.

.
What is the Composite key?

➢ COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table.
➢ The combination of columns guarantees uniqueness, though individually uniqueness is not guaranteed.
➢ Hence, they are combined to uniquely identify records in a table.

➢ The difference between compound and the composite key is that any part of the compound key can be a foreign
key, but the composite key may or maybe not a part of the foreign key.

What is a Surrogate key?

➢ SURROGATE KEYS is an artificial key which aims to uniquely identify each record is called a surrogate key.
➢ This kind of partial key in dbms is unique because it is created when you don’t have any natural primary key.
➢ They do not lend any meaning to the data in the table.
➢ Surrogate key in DBMS is usually an integer.
➢ A surrogate key is a value generated right before the record is inserted into a table.

Fname Last name Start Time End Time


Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
➢ above, given example, shown shift timings of the different employee.

➢ In this example, a surrogate key is needed to uniquely identify each employee.

Surrogate keys in sql are allowed when:

➢ No property has the parameter of the primary key.

➢ In the table when the primary key is too big or complicated..


Difference Between Primary key & Foreign key

Primary Key Foreign Key


 Helps you to uniquely identify a  It is a field in the table that is the
record in the table. primary key of another table.

 Primary Key never accept null  A foreign key may accept multiple
null values.
values.

 A foreign key cannot automatically


 Primary key is a clustered index
create an index, clustered or non-
and data in the DBMS table are
clustered. However, you can
physically organized in the
manually create an index on the
sequence of the clustered index.
foreign key.

 You can have the single Primary


 You can have multiple foreign keys
key in a table.
in a table.
Frequently Asked Questions :

1. What are Generalization, Specialization, and Aggregation in ER Model?

Generalization is the higher level of understanding of data from lower levels of data whereas
Specialization is the process of defining one or more entities from present entities and Aggregation is the
process of combining two or more entities.

2. What is the purpose of the Generalization?

Generalization is simply gathering the common properties from entities and creating a generalized
concept from those extracted data. Generalization helps in improving the flexibility, and reusability of
the database.

3. Why is generalization important in the database?

Generalization is important in the database because it helps to gather important information so that it
becomes easier and faster for the user the analysis of data and it also helps in making decisions faster.
UNIT -II
Integrity Constraints:
➢ Integrity constraints are a set of rules. It is used to maintain the quality of information.
➢ Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
➢ Thus, integrity constraint is used to guard against accidental damage to the database.

Types of Integrity Constraint :


1. Domain constraints
➢ Domain constraints can be defined as the definition of a valid set of values for an attribute.

➢ The data type of domain includes string, character, integer, time, date, currency, etc.

➢ The value of the attribute must be available in the corresponding domain.

Example:
2. Entity integrity constraints:

➢ The entity integrity constraint states that primary key value can't be null.

➢ This is because the primary key value is used to identify individual rows in relation and if the primary
key has a null value, then we can't identify those rows.

➢ A table can contain a null value other than the primary key field.

Example:
3. Referential Integrity Constraints :

➢ A referential integrity constraint is specified between two tables.

➢ In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2, then
every value of the Foreign Key in Table 1 must be null or be available in Table 2.

Example:
4. Key constraints :
➢ Keys are the entity set that is used to identify an entity within its entity set uniquely.

➢ An entity set can have multiple keys, but out of which one key will be the primary key. A primary key can
contain a unique and null value in the relational table.

Example:

You might also like