Sie sind auf Seite 1von 26

Databases

1 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Basics

What is a database?
Database is an electronic administration system that has to handle
large amounts of data efficiently, without contradiction, permanently and can
represent logical relationships digitally.
Data sets can be collocated from various subsets and displayed as
required for application programs and their users.

Example: merchandise management systems


In this case, databases map the complete process flow.
This includes
 Purchase of raw materials
 Storage of intermediate products
 Sale of finished products

For example, the different user groups of the databases convert data into
 delivery notes
 customer data
 billing information
create, edit or remove.

2 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Basics

How is a database system structured?


A database system (DBS) consists of two important components:

 The Database Management System (DBMS) is responsible for


Database system
managing the database(s). It provides access to the database for an (DBS)
application.
 The database (DB) stores the information in the form of individual data
records.
Database Database
Management application
A database system enables computer-aided data processing of System (DBMS) (DBA)
information generated and processed by a database application (DBA).
The database system structures and stores the information in a database.

Database
(DB)

3 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Basics

Examples for the use of databases


Controlling & Accounting
Accounting, tax and administration programs process the information in a
database. Thus invoices, reminders, forms, etc. can be generated, filled in
and sent.

merchandise management systems (MMS)


Both large and small companies manage article data, customer data,
purchasing and sales data in a corresponding database.
For example, online shops, supermarkets, car repair shops, etc.

Enterprise Resource Planning (ERP)


Corporate tasks and resources, such as capital, personnel, resources,
material, etc., are structured centrally in databases.

Content Management Systems (CMS)


The contents of a website are often dynamic and are compiled on the basis
of users. Various editors, graphic designers, etc. create this content and
transfer it to an administration system, a database. Administrations manage
the databases and connections of the website. Texts, graphics, videos but
also layouts or user data are stored in the databases.

4 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Basics

Examples of database models


Hierarchical database model Relational database model
 A Hierarchical Database Model is the oldest database model.  The Relational Database Model is the most widely used data model,
It represents the real world through a hierarchical tree structure. Any which is used as a standard in database development. will be. The
A proposition thus has exactly one predecessor, with the foundation of the database model consists of the elements: Tables,
exception of exactly one proposition, namely the root of the attributes, relationships, and the basics of relational algebra. It represents
resulting tree structure. a mathematical description of a table and its relationship to other possible
tables. The operations on these relations are determined by relational
algebra.
network database model
 The network database model is superficially similar to the
hierarchical model, but also allows n:m relationships. Direct connections Object-oriented database model
between the nodes create a network, via which the user can access a  An object-oriented database model follows the approach of storing data
together with its functions in an object. The data model is based on the
searched data set more directly. Network databases are particularly
paradigms of object-oriented programming and in detail on the respective
suitable for mapping network-like structures, e.g. geographical locations programming language that is used.
and their interconnections, Internet and Intranet, semantic Web, personal
and company networks.

5 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

ER model
The Entity ReIationship model (ER model for short) describes data as
entities, relationships and attributes.
 An entity represents a clearly identifiable object or concept from the
real world, e.g. a product or an employee, which is described in the
database.
 An attribute represents a property that further executes the Entity 1 relation Entity 2
description of an entity, for example, the name or salary level of the
employee.
 A relationship between two or more entities represents a relationship
between the entities, for example, a working relationship between an attribute attribute
employee and a project.

6 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

attributes
One differentiates between compound or simple (atomic) attributes.

Composite attributes can be broken down into smaller parts that represent
more basic attributes with independent meanings. For example, an attribute
"Address" of the Employee entity can be divided into the attributes "Street",
"House number" and "City".
Employee

Non-divisible attributes are called atomic attributes. In the above Examples


would be "street", "house number" and "city" atomic attributes.
Address
In some cases, an attribute of a particular entity may not have a value. For
such cases, a special value is generated, which is referred to as the zero
value (NULL). A null value can also be used if the value of an attribute for a
House
particular entity is not known. Zero value therefore means nothing other Street City
number
than unknown.

7 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

relationships
A relationship describes the relationship between two entity types. For
example, an employee works in a department.

A relationship type can also have attributes. In our example, this could be
the beginning of the employment relationship as a date. Relationship types
usually have certain restrictions that limit the possible combinations of Employee relation department
entities that can participate in the corresponding set of relationships.

These limitations are determined by the 'situation' represented by the


relationships. For example, a rule may apply which states that each Date of
employee must work for exactly one department; then this restriction would recruitment
have to be described in the schema.
This restriction is also known as the cardinality relationship.

8 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

cardinality relationship
There are different forms of notation for the cardinality relationship of two
entity types. We will limit ourselves to the so-called Min-Max notation.

The min-max notation consists of two numbers, which indicate for each
entity type how many times that entity type must (first number) and may
(second number) enter at least one relationship with another entity type.

There are the following specifications:


 no-to-one (0:1)
(1:1) Works (1:n)
 one-to-one (1:1) Employee department
in
 not-to-many (0:n)
 one-too-many (1:n)
 many-to-many (n:m)

An employee can be assigned to at least one department and may only


work in one department (1:1).

A department consists of at least one employee, but can be the workplace


for n employees (1:n).

9 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

cardinality relationship
An important restriction for the entities of an entity type is the key or
uniqueness restriction for attributes. An entity type usually has an attribute
whose values differ for each individual entity in the collection. Such an Address
attribute is called a key attribute and each entity can be uniquely identified
by its values.
Employee SvNr
For example, the Name attribute is a key for the entity type Company
because no two companies can have the same name. For the entity type
Person, the social insurance number (SvNr.) is a typical key attribute. name

Sometimes several attributes together form a key, which means that the
combination of attribute values of each entity must be different. If such a
key cannot be generated from the given attributes or if the circumstances
require it, a key can also be generated artificially. This is then often a
unique identification number (ID) in the relation. (1:n) consists (1:1)
firm department
In the ER model, each key attribute is represented as an oval with its of
underlined name.

A company consists of at least one department, but can have n many departments
include. A department belongs to exactly one company.

10 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

Relational data model


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

Informally, each relation resembles a table. If you imagine a relation as a


table with values, each row of the table represents a list of related data
values. A so-called tuple.

We presented entity types and relationship types as a concept for modeling


real-world data. In the relational model, each row of a table represents a
fact that normally corresponds to an entity or relationship of the real world. attributes
Each column corresponds to an attribute of this entity.

employees

ID first name Surname Address department salary level


1 Horst Heymann Marientalstr. 151 5 5
74523 Swabian Hall

2 Piet Klemm Ladder 55 5 4


tuples 77761 Schiltach
3 Laura Blum Erikahof 80 5 4
76344 Eggenstein-
Leopoldshafen

11 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

Relational data model


Different tables can be linked together using their key attributes. One
speaks then of primary (underlined) and/or employees
foreign keys. The foreign key of a relation represents the
primary key of another relation. ID first name Surname Address department salary level
1 Horst Heymann Marientalstr. 151 5 5
Different tables can be linked together using their key attributes. One
74523 Swabian Hall
speaks then of primary (underlined) and/or
foreign keys. The foreign key of a relation represents the 2 Piet Klemm Ladder 55 5 4
primary key of another relation. 77761 Schiltach

3 Laura Blum Erikahof 80 5 4


76344 Eggenstein-
Leopoldshafen

department

ID department name

… …
5 Safety and security
… …

12 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

department employees salary_level

ID ID LevelId
earns
Department (1:1) (0:n)
FirstName Salary
Name

LastName

Address
Works
in
(0:n) (1:1) Department

SalaryLevel

13 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

SQL
Now that a database has been designed, it is time to work with it, i.e.
create tables, enter data records into it, query or remove them from it. The
Structured Query Language (SQL) is available for these operations.
Database system
(DBS)
The SQL language is considered one of the most important reasons for the
success of relational databases in the commercial world. Because it has Database Database
become the standard for relational databases, users are less concerned Management application
about migrating their database applications from database system types,
such as network or hierarchical systems, to relational systems. Another System (DBMS) (DBA)
advantage of such a standard: Users can write statements in a database
application program that accesses data stored in two or more relational
DBMSs without having to change the database language (SQL) if both
relational DBMSs support the SQL standard. Database
(DB)

14 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Entity Relationship Model

Creating Tables CREATE TABLE departments(


Create tables, example for "employee": id INT NOT NULL,
CREATE TABLE Employees(); DepartmentName VARCHAR(20) NOT NULL,
PRIMARY KEY(id)
In addition, the necessary columns (attributes) are created in which the data is );
inserted. These are inserted within the chambers:
CREATE TABLE salary_levels(
(
LevelId INT NOT NULL,
id INT NOT NULL, Salary DECIMAL(5, 2) NOT NULL,
FirstName VARCHAR(15) NOT NULL, PRIMARY KEY(LevelId)
LastName VARCHAR(20) NOT NULL, );
Address VARCHAR(30),
Department INT NOT NULL, CREATE TABLE Employees(
SalaryLevel INT, id INT NOT NULL,
PRIMARY KEY(id), FirstName VARCHAR(15) NOT NULL,
FOREIGN KEY(Department) REFERENCES departments(ID), LastName VARCHAR(20) NOT NULL,
FOREIGN KEY(SalaryLevel) REFERENCES salary_levels(LevelId) Address VARCHAR(30),
); Department INT NOT NULL,
SalaryLevel INT,
First the column name, then the data type, and finally the other parameters are PRIMARY KEY(id),
specified. FOREIGN KEY(Department) REFERENCES departments(ID),
At the end, relations such as primary key, foreign key, references, etc. are FOREIGN KEY(SalaryLevel) REFERENCES salary_levels(LevelId)
appended. );

15 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

types of data relation


In SQL, various data types are available for storing data records. The most You can also define the primary and foreign keys of a relation. For this
important ones can be taken from the table purpose they are defined in the Create command after the declaration of
the columns as follows:

...
data type designation PRIMARY KEY(id),
FOREIGN KEY(Department) REFERENCES departments(ID),
INTEGER or INT integer
...
DECIMAL(x,y) X-digit number, with y decimal places
The first command sets the employee ID as the primary key. This must, of
CHAR(n) Character string of fixed length n course, be clear. The second command specifies the Department column of
our table as a foreign key and refers (REFERENCES) to the relation that
VARCHAR(n) variable string with up to n characters contains the values (Department table, ID column).

DATE Date (year, month, day) Of course, the relation department must also be present when we refer to
it. For this reason, the tables for departments and salary levels are
TIME Time (hour, minute, second)
created analogously.
BLOB Binary Large Object. large, binary objects such as
image or audio files

16 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Insert data
The tables can be filled with corresponding contents.
In the simplest form, the command is used to insert a single tuple into a
relation. The values should be listed in the same order in which the
corresponding attributes were specified in the Create command.

INSERT INTO Employees


VALUES ('1' , 'Horst' , 'Heymann' , 'Marientalstr. 151 67297 Marnheim' , '1' , '5' );

INSERT INTO Employees(id, FirstName, LastName, Address, Department, SalaryLevel) VALUES


(1, 'Horst', 'Heymann', 'Marientalstr. 151\r\n67297 Marnheim', 1, 5),
(2, 'Piet', 'Klemm', 'Dachsleite 55\r\n77761 Schiltach', 2, 5),
(3, 'Laura', 'Blum', 'Erikahof 80\r\n77761 Schiltach', 3, 4),
(4, 'Erich', 'Müller', 'Raesfeldstr. 180\r\n53520 Rodder', 4, 4),
(5, 'Victor', 'Fast', 'Ringelnatzstr. 6\r\n67297 Marnheim', 5, 3);

17 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

retrieve data
data records can be determined from the tables with query (select) SELECT FirstName, LastName
commands. FROM Employees
The basic form of the Select statement, also known as a mapping or WHERE Department = 5;
SELECT-FROM-WHERE block, consists of the three clauses SELECT, FirstName LastName
FROM, and WHERE. Victor Almost

SELECT <attribute list> Anastasia Hauser


FROM <table list> Sonya Popp
WHERE <condition>;

The attribute list is a list of attribute names (columns) whose values are
generated when the query is executed. The table list is a list with the names
of the relations accessed for the execution of the query and the condition
describes a conditional expression that qualifies the tuples to be processed
SELECT FirstName, LastName
with the query.
FROM Employees
Several conditions can also be queried in the WHERE clause of the query. WHERE Department = 5 AND FirstName = "Victor";
These are connected by the keywords AND, OR, NOT. FirstName LastName
Victor Almost

18 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Data query, keywords


SELECT LastName SELECT LastName
If you want your result relation to be sorted according to a certain attribute,
you can do this by using the command ORDER BY LastName Popp LastName Fast
<Attribute> DESC / ASC at the end of the request. FROM Hauser FROM Hauser
DESC stands for descending sorted and ASC for ascending sorted. Employees Fast Employees Popp
WHERE WHERE
If you want to assign a different name to attributes than the one used in the Department = 5 Department = 5
relation, you can assign them an alias using the keyword AS. ORDER BY ORDER BY
LastName DESC; LastName ASC;

SELECT LastName CareOf

LastName, Erikahof 80
Blum
77761 Schiltach
Address AS CareOf
FROM Geiststr. 90
Mitchell
94255 Böbrach
Employees
WHERE Florentine-Eichler-Str. 189
Obermüller
67297 Marnheim, Germany
Department = 3;

19 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Data query, keywords


SELECT DISTINCT SalaryLevel
If you want to get a result relation without a duplicate tuple, you must use
SalaryLevel 1
the keyword DISTINCT in the SELECT clause, which means that only
FROM 3
different tuples remain in the result.
Employees
4

The Group By clause allows tuples to be grouped according to certain 5


properties. 6

COUNT(*) Counts all the entries in a column and outputs a result relation,
which shows the department and the respective number of
of your employees.
SELECT Department amount
Department, 1 4
COUNT(*) AS amount 2 4
FROM 3 3
Employees
4 3
GROUP BY
Department; 5 3
6 3

20 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Data query: Comparison Operators FirstName LastName


First, comparison conditions for certain parts of a character string are SELECT aerie Heymann
described using the comparison operator LIKE. Substrings are specified by FirstName, Erich millers
using two reserved characters: LastName
Victor Almost
FROM
The percent sign (%) replaces any number of characters and the Sasha Guzman
Employees
underscore (_) replaces a single character. WHERE Felix mitchell
Address LIKE anastasia houses
...displays the first and last names of all employees who are registered in '%str.%';
Jonah gall
Springfield live in. It doesn't matter which character in front of the word you
are looking for "Springfield" are written here. axel head miller
Sonya poppy

...displays the first and last names of all employees whose first name is Helen brack

exactly 5 characters long and has an "n" as the third letter. Liam bluebottle

SELECT FirstName LastName


FirstName, aerie Heymann
LastName Sonya Becker
FROM Sonya poppy
Employees
WHERE FIRST NAME LIKE
'_o___ ';

21 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Data query: arithmetic operators SELECT Salary * 1.1


Salary * 1.1 1760.000
SQL also allows the use of arithmetic operations in queries. The standard
FROM
arithmetic operations Addition (+), Subtraction (-), Multiplication (*) and salary_levels
Division (/) can be applied to numeric values or attributes with a numeric WHERE
value range. LevelId = 3;

SELECT first name salary bracket


Another comparison operator is BETWEEN, which outputs the tuples
FirstName, Victor 3
between two values.
SalaryLevel dads 3
FROM Erich 1
SQL supports queries that can check whether a value is NULL, i.e. is Employees
missing, undefined or not available. Aileen 3
WHERE
(SalaryLevel BETWEEN 1 AND 3); axel 1
Henry 3
Sonya 3
SELECT
FirstName,
LastName first name Surname
FROM
Employees
WHERE
SalaryLevel IS NULL;

22 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Data query: arithmetic operators


SELECT AmountEmployees
Since grouping and aggregation are required in many database
COUNT(*) AS AmountEmployees, 20
applications, SQL provides concepts as integrated functions. FROM
These functions include COUNT, SUM, MAX, MIN and AVG. The SUM, Employees
MAX, MIN and AVG functions are applied to a set or multiset of numeric
values and return the sum, maximum value, minimum value or average
(mean) of these values. The COUNT command counts the entries stored
for a value.

23 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

Delete data
The DELETE command removes tuples from a relation. It contains a
WHERE clause, similar to the one in the SQL query, to select the tuples to
be deleted. Tuples are explicitly deleted from only one table at a time.

DELETE FROM Employees


WHERE last name = 'Grimes';

This removes all tuples from the relation, which are used as surnames.
"Grimes" have.

A missing WHERE clause indicates that all tuples in the relation are to be
deleted. However, the table remains in the database as an empty table.
The DROP TABLE command is available to finally delete a table. So in
our example this would be:

DROP TABLE Employees;

ATTENTION: The DROP TABLE command deletes the relation


permanently and cannot restore it.

24 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

nested queries SELECT FirstName Surname

FirstName, Victor Almost


Some queries require that existing values are first read from the database
LastName anastasia houses
and then used in a comparison condition. Such requests can be formulated
as nested requests. These are SELECT...FROM...WHERE requests within FROM Sonya poppy
the WHERE clause of another request called an external request. Employees.
WHERE
The "back" nested query selects the ID of all departments that have to Employees.Department IN(
do with security. The "front" query returns the first names of all SELECT
employees whose department is located in (IN) the set of queried id
departments. FROM
Departments
WHERE
DepartmentName = 'Security‘);

SELECT
FirstName Surname department
Employees.FirstName,
name
Employees.LastName,
Victor Almost Security
Departments.DepartmentName
FROM anastasia houses Security
Employees, Sonya poppy Security
Departments
WHERE
DepartmentName = 'Security'
AND Employees.Department = Departments.id;

25 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.
Databases | Structured Query Language (SQL)

nested queries: JOIN SELECT


Employees.LastName,
The values from several tables can be combined using the JOIN
Employees.FirstName,
keyword. Instead of transferring several individual queries to the
database, a single query with several tables is executed at the same Departments.DepartmentName
time. FROM
Employees
In this query, the tables are linked in the FROM clause at the position (ON) INNER JOIN Departments ON Employees.Department =
where the table meets the condition. Departments.id
WHERE
In the SELECT clause, you can now also define attributes of the other Departments.DepartmentName = 'Security';
Display Tables

The JOIN operator is very extensive. Besides the INNER JOIN there is
also the OUTER JOIN, which can be divided into FULL OUTER JOIN, FirstName LastName DepartmentName
LEFT JOIN and RIGHT JOIN.
Victor Almost Security
anastasia houses Security
Sonya poppy Security

26 Rexroth Academy | 2019-11-20


© Bosch Rexroth AG 2019. All rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of applications for industrial property rights.

Das könnte Ihnen auch gefallen