Database System
Database System
SYSTEM
Database Management System (DBMS)
• 1. Increased costs:
• To maximize the efficiency of the database system, you must keep your
system current.
• Therefore, you must perform frequent updates and apply the latest patches
and security measures to all components.
• Because database technology advances rapidly, personnel training costs
tend to be significant. Vendor dependence.
• Given the heavy investment in technology and personnel training,
companies might be reluctant to change database vendors.
• 4. Frequent upgrade/replacement cycles:
24
Basic Building Blocks of Data
Models
• Entity anything about which data are to be
collected
- and stored
Attribute - a characteristic of an entity
26
1 - Hierarchical Model
• In Hierarchical Model, a hierarchical relation is formed by collection of
relations and forms a tree-like structure.
• The relationship can be defined in the form of parent child type.
• One of the first and most popular Hierarchical Model is Information
Management System (IMS), developed by IBM.
• Example
• The hierarchy shows an Employee can be an Intern, on Contract or Full-
Time. Sub-levels show that Full-Time Employee can be hired as a Writer,
Senior Writer or Editor:
27
Advantages
• The design of the hierarchical model is simple.
• Provides Data Integrity since it is based on parent/ child relationship
• Data sharing is feasible since the data is stored in a single database.
• Even for large volumes of data, this model works perfectly.
Disadvantages
• Implementation is complex.
• This model has to deal with anomalies like Insert, Update and Delete.
• Maintenance is difficult since changes done in the database may want you
to do changes in the entire database structure.
28
Network Model
• The Hierarchical Model creates hierarchical tree with parent/ child relationship,
whereas the Network Model has graph and links.
• The relationship can be defined in the form of links and it handles many-to-many
relations. This itself states that a record can have more than one parent.
29
Advantages:
• Simplicity : The network model is conceptually simple and easy to
design.
• Ability to handle more relationship types : The network model can
handle the one-to-many and many-to-many relationships.
• Ease of data access
Disadvantages:
• System Complexity : The structure of the network model is very
difficult to change. This type of system is very comple.
30
Relational Model
• In this model, data is organized in two- dimensional tables and the
relationship is maintained by storing a common field.
• This model was introduced by E.F Codd in 1970, and since then it has been
the most widely used database model, infact, we can say the only database
model used around the world.
• The basic structure of data in the relational model is tables. All the
information related to a particular type is stored in rows of that table.
• Hence, tables are also known as relations in relational model.
31
Relational Model
32
Relational
Model
• Relation (file, table) is a two-dimensional table.
• Attribute (i.e. field or data item) is a column in the
table.
• Each column in the table has a unique name within that
table.
• Each column is homogeneous. Thus the entries in any
column are all of the same type (e.g. age, name,
employee-number, etc).
• Each column has a domain, the set of possible values
that can appear in that column.
• A Tuple (i.e. record) is a row in the table.
33
Advantages
• The Relational Model does not have any issues that we saw in the previous
two models i.e. update, insert and delete anomalies have nothing to do in this
model.
• Changes in the database do not require you to affect the complete database.
• Implementation of a Relational Model is easy.
• To maintain a Relational Model is not a tiresome task
Disadvantages
• Database inefficiencies hide and arise when the model has large volumes of
data.
• The overheads of using relational data model come with the cost of using
powerful hardware and devices.
ER (Entity Relationship) Diagram in DBMS
a. Weak Entity
• Anentity that depends on another entity called a weak entity. The
weak entity doesn't contain any key attribute of its own. The weak
entity is represented by a double rectangle.
2. Attribute
• The attribute is used to describe the property of an entity. Eclipse is used
to represent an attribute.
• For example, id, age, contact number, name, etc. can be attributes of a
student.
a. Key Attribute
• The key attribute is used to represent the main characteristics of an
entity. It represents a primary key. The key attribute is represented by an
ellipse with the text underlined.
b. Composite Attribute
• An attribute that composed of many other attributes is known as a
composite attribute. The composite attribute is represented by an
ellipse, and those ellipses are connected with an ellipse.
c. Multivalued Attribute
• Anattribute can have more than one value. These attributes are
known as a multivalued attribute. The double oval is used to
represent multivalued attribute.
• For example, a student can have more than one phone number.
d. Derived Attribute
• Anattribute that can be derived from other attribute is known as a
derived attribute. It can be represented by a dashed ellipse.
• For example, A person's age changes over time and can be
derived from another attribute like Date of birth.
3. Relationship
• A relationship is used to describe the relation between entities. Diamond
or rhombus is used to represent the relationship.
c. Many-to-one relationship
• When more than one instance of the entity on the left, and only one instance
of an entity on the right associates with the relationship then it is known as a
many-to-one relationship.
• For
example, Student enrolls for only one course, but a course can have
many students.
d. Many-to-many relationship
• When more than one instance of the entity on the left, and more
than one instance of an entity on the right associates with the
relationship then it is known as a many-to-many relationship.
• Forexample, Employee can assign by many projects and project
can have many employees.
SQL Constraints
• SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.
• Constraintscan be column level or table level. Column level
constraints apply to a column, and table level constraints apply to
the whole table.
• The following constraints are commonly used in SQL:
1. NOT NULL Constraint
• By default, a column can hold NULL values.
• The NOT NULL constraint enforces a column to NOT accept NULL values.
• This enforces a field to always contain a value, which means that you cannot insert a new
record, or update a record without adding a value to this field.
• Example
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
• NOT NULL on ALTER TABLE
• ALTER TABLE Persons
ALTER COLUMN Age int NOT NULL;
2. UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness
for a column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
• CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
UNIQUE Constraint on ALTER TABLE
• ALTER TABLE Persons
ADD UNIQUE (ID);
3. PRIMARY KEY Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a table.
• Primary keys must contain UNIQUE values, and cannot contain NULL values.
•A table can have only ONE primary key; and in the table, this primary key can
consist of single or multiple columns (fields).
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
PRIMARY KEY on ALTER TABLE
• ALTERTABLE Persons
ADD PRIMARY KEY (ID);
4. FOREIGN KEY Constraint
• The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.
• The table with the foreign key is called the child table, and the table with the primary
PersonID LastName FirstName Age
key is called the referenced or parent table.
1 Hansen Ola 30
• Persons Table
2 Svendson Tove 23
3 Pettersen Kari 20
2. UPDATE
• Update existing data within a table
• UPDATE table_name SET column1 = value1, column2 = value2
3. DELETE
• Delete records from a database table
• DELETE FROM table_name WHERE condition;
DCL (Data Control Language)
• DCL includes commands such as GRANT and REVOKE which mainly
deal with the rights, permissions, and other controls of the
database system.
1. GRANT
• Assigns new privileges to a user account, allowing access to
specific database objects, actions, or functions.
• GRANT privilege_type [(column_list)] ON [object_type] object_name
TO user [WITH GRANT OPTION];
2. REVOKE
• Removes previously granted privileges from a user account, taking
away their access to certain database objects or actions.
• REVOKE [GRANT OPTION FOR] privilege_type [(column_list)]
ON [object_type] object_name FROM user [CASCADE];
SQL Aggregate Functions
• SELECT COUNT(*)
FROM Products;
• SUM() - returns the total sum of a numerical column
• SELECT SUM(column_name)
FROM table_name
WHERE condition;
• SELECT SUM(Quantity)
FROM OrderDetails;
• AVG() - returns the average value of a numerical column
• SELECT AVG(column_name)
FROM table_name
WHERE condition;
• SELECT AVG(Price)
FROM Products;