0% found this document useful (0 votes)
8 views18 pages

Part 1

Uploaded by

el.fauconito
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)
8 views18 pages

Part 1

Uploaded by

el.fauconito
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/ 18

DATABASE IMPLEMENTATION

PART 1: INTRODUCTION TO DATABASES

I. DATABASES

What is a database ?
A very general definition could be:

Definition 1.1 -Database- An organized set of information with a common purpose.

Regardless of the medium used to collect and store data (paper, files, etc.), when data is collected
and stored in an organized manner for a specific purpose, it is called a database.

More precisely, a database is a structured and organized set allowing the storage of large quantities
of information in order to facilitate its use (adding, updating, searching for data). Of course, in this
course we are interested in computerized databases.

Computerized database

Definition 1.2 -Computerized database- A computerized database is a structured set of data


recorded on computer-accessible media, representing real-world information and capable of being
queried and updated by a community of users.

The result of designing a computerized database is a description of the data. By description we


mean defining the properties of sets of objects modeled in the database and not of particular
objects. Special objects are created by application programs or manipulation languages during data
insertions and updates.

This description of the data is carried out using a data model. The latter is a formal tool used to
understand the logical organization of data.

1
DATABASE IMPLEMENTATION

Management and access to a database are ensured by a set of programs which constitute the
Database Management System (DBMS).

II. CONSTRAINTS
Database constraints are a key feature of database management systems. They ensure that rules
defined at data model creation are enforced when the data is manipulated ( inserted, updated, or
deleted) in a database.

Constraints allow us to rely on the database to ensure integrity, accuracy, and reliability of the
data stored in it. They are different from validations or controls we define at application or
presentation layers; nor do they depend on the experience or knowledge of the users interacting
with the system.

we will briefly explain how to define the following types of constraint and their usage:

• DEFAULT
• CHECK
• NOT NULL
• UNIQUE KEY
• PRIMARY KEY
• FOREIGN KEY

A. Why Do We Need to Define Constraints in a Database Model?


Although it is not mandatory, defining constraints ensures at database level that the data is
accurate and reliable. Having those rules enforced at database level rather than in the application
or presentation layer ensures that the rules are consistently enforced no matter who manipulates
the data and how it is manipulated. When a customer uses a mobile or web application, when an
employee uses an internal application, or when a DBA executes data manipulation scripts, any
changes are evaluated at the database level to guarantee that no constraint is violated.

2
DATABASE IMPLEMENTATION

B. Are Database Constraints the Same in all RDBMSs?


Not all database management systems support the same types of constraints. When they
do, there may be special features or considerations for the specific system. The syntax for creating
constraints also depends on the specific RDBMS.

Constraints can be defined when we create a table or can be added later. They can be
explicitly named when created (thus allowing us to identify them easily), or they can have system-
generated names if an explicit name is omitted.

C. Constraint Types
We will start from the most basic then move on to the more complex.

1. DEFAULT

This type of constraint allows us to define a value to be used for a given column when no data
is provided at insert time. If a column with a DEFAULT constraint is omitted in
the INSERT statement, then the database will automatically use the defined value and assign it to
the column (if there is no DEFAULT defined and the column is omitted, the database will assign
a NULL value for it).

Once the DEFAULT is created for a column, we can insert a row in our table without specifying
the column:

ALTER TABLE Product ALTER CurrentStock SET DEFAULT 0;

2. CHECK

CHECK constraints allow us to define a logical condition that will generate an error if it
returns FALSE. Every time a row is inserted or updated, the condition is automatically checked,
and an error is generated if the condition is false. The condition can be an expression evaluating
one or more columns.

ALTER TABLE Product ADD CONSTRAINT CK_Product_CurrentStock


CHECK (CurrentStock >= 0);
3
ALTER TABLE Product ADD CONSTRAINT CK_Product_Price
CHECK (Price > 0);
DATABASE IMPLEMENTATION

3. UNIQUE KEY

Unique keys are defined at table level and can include one or more columns. They guarantee
that values in a row do not repeat in another. You can create as many unique keys as you need in
each table to ensure that all business rules associated with uniqueness are enforced.

ALTER TABLE Product ADD CONSTRAINT UK_Product_ProductCode UNIQUE


(ProductCode);
ALTER TABLE Product ADD CONSTRAINT UK_Product_ProductName
UNIQUE (ProductName);

4. NOT NULL

By default, all columns in a table accept NULL values. A NOT NULL constraint prevents a
column from accepting NULL values.

ALTER TABLE Product MODIFY ProductCode VARCHAR(20) NOT NULL;

5. PRIMARY KEY

A primary key is a constraint defined at table level and can be composed of one or more
columns. Each table can have only one primary key defined, which guarantees two things at row
level:

• The combination of the values of the columns that are part of the primary key is unique.
• All the columns that are part of the primary key have non-null

So, we can consider the primary key as a combination of the NOT


NULL and UNIQUE constraints.

4
DATABASE IMPLEMENTATION

ALTER TABLE Product ADD CONSTRAINT PK_Product PRIMARY KEY (ProductID);

6. FOREIGN KEY

Foreign keys are vital to maintaining referential integrity in a database. Foreign keys are
created in child tables, and they “reference” a parent table. To be able to reference a table, a
constraint that ensures uniqueness (either a UNIQUE or PRIMARY KEY) must exist for the
referenced columns of the parent table.

When a foreign key is defined, the two tables become related, and the database engine will ensure
that:

• Every value or combination of values entered at INSERT or UPDATE in the columns that
are part of a foreign key exist exactly once in the parent table. This means that we cannot
insert or update a row in the Order table with a reference to a product that does not exist in
the Product
• Every time we try to DELETE a row in the parent table, the database will verify that it does
not have child rows associated; the DELETE will fail if it does. This means that we would
not be able to remove a row in Product if it has one or more related rows in the Order

ALTER TABLE OrderDetail ADD CONSTRAINT FK_OrderDetail_ProductID


FOREIGN KEY (ProductID) REFERENCES Product (ProductID);

ALTER TABLE PurchaseDetail ADD CONSTRAINT


FK_PurchaseDetail_ProductCode FOREIGN KEY (ProductCode) REFERENCES
Product (ProductCode);

5
DATABASE IMPLEMENTATION

D. How Database Constraints Are Classified


Constraints are usually either column-level or table-level, and the classification depends
on the sections of the CREATE TABLE statement in which they can be defined. Reviewing
the CREATE TABLE syntax, we can easily identify those places:

CREATE TABLE table_name (


column1 datatype column_level_constraint1
column_level_constraint2,
column2 datatype column_level_constraint3,
table_level_constraint1,
table_level_constraint2
);

All constraint types we have reviewed can be defined at column level as long as they involve only
a single column (the column that is being defined). All constraint types except NOT NULL can
also be defined at table level, and this is mandatory when a constraint involves more than one
column (like complex CHECK conditions and multiple-column unique, primary, or foreign
keys). DEFAULT constraints can involve only one column, but they can be defined at either level.

Constraint Type Table Level Column Level

DEFAULT Yes (only one column) Yes (only one column)

CHECK Yes (multiple columns) Yes (only one column)

NOT NULL No Yes (only one column)

UNIQUE Yes (multiple columns) Yes (only one column)

FOREIGN KEY Yes (multiple columns) Yes (only one column)

PRIMARY KEY Yes (multiple columns) Yes (only one column)

6
DATABASE IMPLEMENTATION

III. JOINs

MySQL databases usually store large amounts of data. To analyze that data efficiently, analysts
and DBAs have a constant need to extract records from two or more tables based on certain
conditions. That’s where JOINs come to the aid. JOINS are used to retrieve data from multiple
tables in a single query. For JOINs to work, the tables need to be related to each other with a
common key value. JOIN clauses are used in the SELECT, UPDATE, and DELETE statements.

a. Different types of JOINs in MySQL


MySQL JOIN type defines the way two tables are related in a query. MySQL supports the
following types of JOIN clauses: INNER JOIN, OUTER JOIN, and CROSS JOIN. OUTER
JOINs can further be divided into LEFT JOINs and RIGHT JOINs.

CREATE TABLE `users` (


`auid` int(10) UNSIGNED NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(150) NOT NULL,
`createdate` datetime NOT NULL,
`isActive` tinyint(1) NOT NULL
);

CREATE TABLE `userprofile` (


`apid` int(10) UNSIGNED NOT NULL,
`auid` int(10) UNSIGNED NOT NULL,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`phone` varchar(45) NOT NULL
);

ALTER TABLE userprofile ADD CONSTRAINT FK_user FOREIGN KEY


(auid) REFERENCES users (auid);

7
DATABASE IMPLEMENTATION

1. MySQL INNER JOIN clause

INNER JOINs are used to fetch only common matching records. The INNER JOIN clause
allows retrieving only those records from Table A and Table B, that meet the join condition. It is
the most widely used type of JOIN.

Here is the syntax for MySQL INNER JOIN:

SELECT columns FROM tableA INNER JOIN tableB ON


tableA.column = tableB.column;

2. MySQL OUTER JOINs

In contrast to INNER JOINs, OUTER JOINs return not only matching rows but non-
matching ones as well. In case there are non-matching rows in a joined table, the NULL values
will be shown for them.

There are the following two types of OUTER JOIN in MySQL: MySQL LEFT JOIN and MySQL
RIGHT JOIN.

2.1. MySQL LEFT JOIN clause

8
DATABASE IMPLEMENTATION

LEFT JOINs allow retrieving all records from Table A, along with those records from
Table B for which the join condition is met. For the records from Table A that do not match the
condition, the NULL values are displayed.

Here is the syntax for MySQL LEFT JOIN:

SELECT columns FROM tableA LEFT [OUTER] JOIN tableB


ON tableA.column = tableB.column;

2.2. MySQL RIGHT JOIN clause

Accordingly, RIGHT JOINs allow retrieving all records from Table B, along with those
records from Table A for which the join condition is met. For the records from Table B that do not
match the condition, the NULL values are displayed.

9
DATABASE IMPLEMENTATION

Here is the syntax for MySQL RIGHT JOIN:

SELECT columns FROM tableA RIGHT [OUTER] JOIN tableB


ON tableA.column = tableB.column;

3. MySQL CROSS JOIN clause

MySQL CROSS JOIN, also known as a cartesian join, retrieves all combinations of rows from
each table. In this type of JOIN, the result set is returned by multiplying each row of table A with
all rows in table B if no additional condition is introduced.

10
DATABASE IMPLEMENTATION

When you might need that type of JOIN? Envision that you have to find all combinations of a
product and a color. In that case, a CROSS JOIN would be highly advantageous.

Here is the syntax for MySQL CROSS JOIN:

SELECT columns FROM tableA CROSS JOIN tableB;

b. MySQL JOINs guidelines


JOINs in MySQL allow you to use a single JOIN query instead of running multiple simple
queries. Thus, you can achieve better performance, reduce server overhead, and decrease the
number of data transfers between MySQL and your application.

Unlike SQL Server, MySQL does not support FULL OUTER JOIN as a separate JOIN
type. However, to get the results same to FULL OUTER JOIN, you can combine LEFT OUTER
JOIN and RIGHT OUTER JOIN.

SELECT * FROM tableA LEFT JOIN tableB


ON tableA.id = tableB.id
UNION
SELECT * FROM tableA RIGHT JOIN tableB
ON tableA.id = tableB.id

11
DATABASE IMPLEMENTATION

Using MySQL JOINs, you can also join more than two tables.

SELECT * FROM tableA

LEFT JOIN tableB

ON tableA.id = tableB.id

LEFT JOIN tableC

ON tableC.id = tableA.id;

12
DATABASE IMPLEMENTATION

IV. Database management system (DBMS)

a. Operating principles
Management and access to a database are ensured by a set of programs which constitute the
Database Management System (DBMS). A DBMS must allow the addition, modification and
search of data. A database management system generally hosts several databases, which are
intended for different software or purposes.

Currently, most DBMS operate in a client/server mode. The server (meaning the machine that
stores the data) receives requests from several clients concurrently. The server analyzes the
request, processes it and returns the result to the client. The client/server model is quite often
implemented using the sockets interface, the network being the Internet.

b. Types of Data Models in DBMS


A typical database management system can support the following types of data models:

1. Hierarchical model

In a database organized in a hierarchical structure, the data is collected in a tree-like form. This
model represents some of the links in the actual world, such as recipes for food, sitemaps for
websites, etc. A hierarchical model has the following characteristics:
• One-to-many relationship: The one-to-many relationship between the datatypes is
present in the data organization, which resembles a tree.
• Parent-child relationship: Although a parent node might have more than one child
node, every child node has a parent node.
• Deletion problem: When a parent node is erased, all child nodes follow suit.
• Pointers: Pointers navigate between the stored data and connect the parent and child
nodes.

13
DATABASE IMPLEMENTATION

2. Relational model

One of the most frequently used data models is the relational model. The data in this model is
kept as a two-dimensional table. The data storage takes the shape of rows and columns. Tables are
a relational model’s fundamental building block. In the relational paradigm, the tables are also
referred to as relations. The key traits of the relational model are as follows:
• Tuples: The table’s rows are referred to as tuples. All the information about any
object instance is contained in a row.
• Attribute or field: The property that defines a table or relation is called an attribute.
The attribute’s values ought to come from the same domain.
3. Object-oriented model

According to this paradigm, a database is a group of objects, or reusable software components,


with related features and procedures. Various types of object-oriented databases exist, including:
• Images and other media types that one cannot keep in a relational database are
included in multimedia databases.
• Any object can link to any other object using a hypertext database.
Although it helps organize vast volumes of different data, it’s not the best option for numerical
analysis. The object-oriented database model, the most popular post-relational database model,
incorporates tables without being restricted to them. These designs are also referred to as hybrid
database designs.
4. Network model

The network model expands on the hierarchical model by enabling many-to-many


relationships among linked records, which implies multiple parent records. The model is built
using sets of related records on the basis of mathematical set theory. A network model has to offer
the following features:
• Increased capacity for relationship fusion: The more relationships in this model,
the more connected the data is.
• Several paths: There may be more than one path to the same record due to the
increased number of relationships. This facilitates quick and easy data access.

14
DATABASE IMPLEMENTATION

• Linked circular list: The circular linked list performs operations on the network
model.

C. DBMS Examples
You can use multiple database management systems or DBMS software for information
storage, organization, and data analysis. Some of the top options include:

1. Microsoft Access

Microsoft’s DBMS, known as Access, combines a graphical user interface, software


development tools, and the relational Microsoft Jet Database Engine. Due to the graphical user
interface, it is simple. The professional and higher editions of Microsoft Office include it as part
of their suite of programs.
The advantages of MS Access include its quick and easy creation of a fully effective
relational database management system, ease of data import from many sources, and simplicity of
customization to suit individual and business requirements. The drawback of MS Access is that it
works well only for small and medium-sized enterprises.

2. MySQL

This is an open-source relational database management system (RDBMS) with a client-


server structure (open source, in a nutshell, is software ready for alterations and free usage).
Let’s briefly discuss what client-server is. Clients are the devices that install and use RDBMS
software. They establish a connection to the RDBMS server whenever they require data access.
The “client-server” component is simply that. Uncomplicated syntax and moderate feature
complexity characterize MySQL. Developers even regard MySQL as a database with a human-
like language. One drawback of MySQL is scalability, which is ingrained in its code, and was not
considered when it was being developed.

3. Oracle Database

15
DATABASE IMPLEMENTATION

It is the company’s fourth attempt at developing a relational database management system.


Large enterprises, especially, can store larger volumes of data on Oracle databases. Additionally,
it is adaptable and helpful for shared structured query language (SQL) and locking. The system’s
relational database framework allows users immediate access to data items.
It builds a powerful engine for synchronous data processing combined with the ability to
process data in memory. If you plan to use Oracle DB, you should think about hiring specialized
professionals.

4. MongoDB

MongoDB is a general-purpose data store. Compared to other forms of databases, this


offers several benefits. MongoDB data is mapped to a configurable schema. You can quickly
modify how your data is kept if the requirements of your application change. This enables you to
refine fresh ideas much more rapidly.
You can lock down your schema as much or as little as you’d want because MongoDB also
offers schema validation. This indicates that it can manage any data structuring needs you may
have now or in the future. MongoDB allows users to combine documents via references and
procedures that cater to different needs, including $lookup.

5. IBM Db2 DBMS

The world’s top database specialists created IBM Db2, which gives developers, DBAs, and
enterprise architects the tools they need to execute real-time analytics and low-latency transactions
for even the most demanding workloads. Db2 is the tried-and-true hybrid database that offers
extreme availability, sophisticated integrated security, seamless scalability, and intelligent
automation for systems that run the world from microservices to AI workloads.
Virtually all of your data is now accessible across hybrid cloud or multi-cloud settings to
power your AI applications, thanks to the majority of the Db2 family being made available on the
IBM Cloud Pak for Data platform, either as an add-on or an included data source service.

6. Amazon RDS

16
DATABASE IMPLEMENTATION

The managed SQL database service known as Amazon Relational Database Service (RDS)
is offered by Amazon Web Services (AWS). Amazon RDS supports a variety of database engines
for data storage and organization. Additionally, it supports activities related to relational database
maintenance, including data migration, backup, recovery, and patching. Amazon RDS makes it
easy to set up, run, and scale a relational database in the cloud.
While automating time-consuming administrative activities like hardware provisioning,
database setup, patching, and backups, it offers affordable and expandable capacity. It gives you
more time to concentrate on your applications, giving them the quick response, high availability,
security, and compatibility required.

7. PostgreSQL

Open-source database management system PostgreSQL is designed for businesses. For


flexibility and SQL compliance, it allows relational and non-relational queries in both SQL and
JSON. PostgreSQL is an effective open source object-relational database system. It has been
actively developed for more than 15 years, and because of its tried-and-true architecture, it enjoys
a solid reputation for dependability, data integrity, and correctness.
All popular operating systems, including Linux, UNIX, and Windows, support
PostgreSQL. Advanced data types and performance-enhancing tools are only supported by pricey
commercial databases like Oracle and SQL Server; however, PostgreSQL has these features built-
in. It also goes by the name Postgres.

8. Apache Cassandra

With no single point of failure and the ability to handle massive volumes of data across
numerous commodity servers, Apache Cassandra is a distributed database that is highly scalable
and highly functional. It belongs to the NoSQL database family. The capacity to manage
organized, semi-structured and unstructured data is a strength of Apache Cassandra.
Initially created by Facebook, Cassandra was made available to the public in 2008 before
becoming one of the top-level Apache projects in 2010. Major corporations particularly benefit
from their capacity to handle enormous volumes. Because of this, many significant corporations,
like Apple, Facebook, and Instagram, are currently using it.

17
DATABASE IMPLEMENTATION

18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy