0% found this document useful (0 votes)
5 views

Lecture №5

The lecture provides an overview of Database Management Systems (DBMS), focusing on concepts, architecture, and SQL fundamentals. It covers various data models, normalization, integrity constraints, and the design and development of databases, including the use of SQL for data manipulation. Key topics include creating databases and tables, inserting and retrieving data, and understanding relationships between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture №5

The lecture provides an overview of Database Management Systems (DBMS), focusing on concepts, architecture, and SQL fundamentals. It covers various data models, normalization, integrity constraints, and the design and development of databases, including the use of SQL for data manipulation. Key topics include creating databases and tables, inserting and retrieving data, and understanding relationships between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture №5. Database systems.

Plan:
1.Bases of database systems: concept, characteristic, architecture. Data models. Normalization.
Integrity constraint on data. Query tuning and their processing. 2.Fundamentals of SQL. Parallel
processing of data and their restoration.
3.Design and development of databases. Technology of programming of ORM. The distributed,
parallel and heterogeneous databases.

Aim of the lecture: to provide an overview about Database Management Systems(DBMS)


and introduce you to one of DBMS: SQL

Having studied this session, you will be able to get an idea about:
What a database is
What a Database Management System is
Microsoft SQL
Creating a database using SQL
Working with SQL

1.Bases of database systems: concept, characteristic, architecture. Data models.


Normalization. Integrity constraint on data. Query tuning and their processing.

Introduction to Databases. A database is a structured collection of records or data. A


computer database is a kind of software to organize the storage of data. Databases help you
organize this related information in a logical fashion for easy access and retrieval. To develop a
database, there are several models used such as Hierarchical model, Network model, Relational
model, Object-Oriented model etc. Though discussing about these models in details is beyond
the level of this course unit, for the sake of completion, some models are briefed below.
Hierarchical model. In a hierarchical model, data is organized into an inverted tree-like
structure. This structure arranges the various data elements in a hierarchy and helps to establish
logical relationships among data elements of multiple files. Each unit in the model is a record
which is also known as a node. Each record has a single parent.

Figure 4: Hierarchical Model

Network model. The network model tends to store records with links to other records. Each
record in the database can have multiple parents, i.e., the relationships among data elements can
have a many to many relationships. So this model is an expansion to the hierarchical structure,
allowing many-to-many relationships in a tree-like structure that allows multiple parents.
The network model provides greater advantage than the hierarchical model in that it
promotes greater flexibility and data accessibility.

Figure 5: Network Model


Relational model. The relational model for the database management is a database model
based on relations. The basic data structure of the relational model is a table where information
about a particular entity (say, a student) is represented in columns and rows. The columns
enumerate the various attributes (i.e. characteristics) of an entity (e.g. student name, address,
registration _number).The rows (also called records) represent instances of an entity (e.g.
specific student).
We will be following the relational database model within this course and designing and
using such databases will be covered within the next two sessions.
Object –Oriented model. In this Model we have to discuss the functionality of the object
oriented Programming. It takes more than storage of programming language objects. It provides
full-featured database programming capability, while containing native language compatibility.
It adds the database functionality to object programming languages. This approach is the
analogical of the application and database development into a constant data model and language
environment. Applications require less code, use more natural data modeling, and code bases are
easier to maintain. Object developers can write complete database applications with a decent
amount of additional effort. But object-oriented databases are more expensive to develop.
Database Management System. A Database Management System (DBMS) is computer
software designed for the purpose of managing databases based on a variety of data models. A
DBMS is a complex set of software programs that controls the organization, storage,
management, and retrieval of data in a database. DBMS are categorized according to their data
structures or types, sometime DBMS is also known as a Database Manager. Data management
tasks fall into one of four general categories as given below:
Entering data into the database.
Housekeeping tasks such as updating data, deleting obsolete records, and backing up the
database.
Sorting the data: arranging or re-arranging the database‟s records.
Obtaining subsets of data.
There are several advantages in DBMS such as reduced data redundancy and inconsistency,
enhanced data integrity, improved security etc.
Basic terminology in Databases. These terms are used often in Database Management
Systems, so you need to become familiar with them before using the database management
system, MS Access in this session. A brief and a simple description on some basic terms are
given below.
Database. A database is an organized collection of the related information.
Object. An object is a component in the database such as a table, query, form, report, or
macro, etc.
Table. A table is a group of related data organized in fields (columns) and records (rows) on
a datasheet. By using a common field in two tables, the data can be combined. Many tables can
be stored in a single database.
Field. A field is a column on a datasheet and defines a data type for a set of values in a table.
For a mailing list table might include fields for first name, last name, address, city, and telephone
number.
Record. A record is a row on a datasheet and do fields define a set of values. In a mailing
list table, each record would contain the data for one person as specified by the intersecting
fields.
Primary key. A primary key is used to uniquely identify each row in a table. It can either
be a part of the actual record itself, or it can be an artificial field (one that has nothing to do with
the actual record). A primary key can consist of one or more fields on a table. When multiple
fields are used as a primary key, they are called as a composite key.
Foreign key. A foreign key is a field (or fields) that points to the primary key of another
table. The purpose of the foreign key is to ensure referential integrity of the data.
Relationships. Two tables/entities in a database may relate to each other using one or more
common attribute. There are three types of relationships among tables namely, One-to-one, one-
to-many, many-to-many.
Consider the two entities, Faculty and the Dean in the university database. The data item
comes under the attribute Dean ID of the Science faculty is found in only one place of the Dean
table under the attribute Dean ID. In other words, the table Faculty has one and only one
corresponding entry in table Dean. In simple words, one faculty has only one Dean and one
dean is holding the deanship in only one faculty. Therefore, we say that the relationship
between the two entities Faculty and Dean is of type One-to-one. This relationship can be
illustrated by a diagram as follows.

Figure 6: One-to-one relationship


One-to-Many relationship
Table A has a relationship to table B such that many entries in B can refer to one entry in
A. Consider the above University database. Think of the relationship between the two entities
Faculty and Lecturer. Each faculty may have many lecturers but each lecturer is joined only
inone faculty. Therefore, we say that the relationship between the two entities Faculty and
Lecturer is of type One-to-many. This relationship can be illustrated by a diagram as follows.

Figure 7: One-to-Many Relationship


Many-to-Many relationship
Table A has a relationship to table B such that many entries to A can map to many entries
in B. Consider the above University database. Think of the relationship between the two entities
Lecturer and Student. Each lecturer teaches many students and each student is taught by many
lecturers. Therefore, we say that the relationship between the two entities Lecturer and Student
is of type Many-to-many. This relationship can be illustrated by a diagram as follows.

Figure 8: Many-to-Many Relationship


Different Database Management Applications
There are several different database management applications which are not only meant for
entering and retrieving information but also they facilitate simultaneous updates and queries
from multiple users.
Some of the database management applications are listed below:
Oracle
MySQL/ SQL Server
Microsoft Access
IBM DB2
Sybase

2.Fundamentals of SQL. Parallel processing of data and their restoration.


We'll use a system called SQL to manipulate databases.
SQL allows us functionality in setting up databases:
● We can use create database to create a database. There are also features for switching
between databases.
● We can use create table to create the tables that make up a database.
SQL allows us functionality in manipulating the contents of tables:
● Via insert statements, we can add records to tables.
● Via update statements, we can change the data in existing records.
● Via delete statements, we can remove entire records.
Finally, SQL allows us to retrieve data from tables:
● The select statement allows this.
● We can also do more complicated select statements with concepts such as joins and
subqueries that allow us to use tables together in extremely powerful ways.
SQL is quite different from the programming language experiences you've had thus far.
Instead of telling SQL how to get the information you want, you use SQL to say what you want.
For example:
● In C++ or Java, to retrieve all of the employees from a database whose hourly wage is at
least $10, you would likely write a for loop to iterate through all the elements of an array (or
some other structure), with a nested if statement checking the salaries.
● In SQL, you'd write a statement like so:
select * from employees where salary > 10;
Let's note a few technical details:
● SQL is case-insensitive.
● Lines of SQL code end in semicolons. (Exception: not necessary for one statement, but
it's nice to get in the habit anyway.)
● Block-style comments (/* ... */) are allowed.
● There are various flavors of SQL that have slight differences. What I'll teach you is
Apache Derby's flavor (and the majority of what I'll show you is common to MySQL and
Derby).
Introduction • Data Types • Creating Tables • Inserting Data • Retrieving Data •
Derby/Netbeans

Data Types
Various data types are available, similar to what you know in Java. Here are some you're
likely to use Table 4:

Data Type Notes


char(n) Fixed-length string of n characters.
varchar(n) Same as above, but variable-length. Recommended in general.
smallint 16 bit, i.e. accepts values ranging from -215 to 215-1
32 bit, i.e. accepts values ranging from -231 to 231-1 (int in
integer
MySQL)
float
Floating point numbers
double
date Format YYYY-MM-DD
time Format HH:MM:SS
Table 4: Data types
In MySQL, float and double require two arguments in parentheses: p and s. with precision p
(i.e. p total allowable digits) and scale s (i.e. s digits to the right of the decimal point). float will
be fine most of the time, with values in the range of approximately± 3.4e38 (while double goes
to ± 1.9e308.)
Creating Tables
We can create a table with the SQL command create table. We'll essentially list off our
variables and their types.
The general syntax is:
create tabletableName
(name type,
/* more ... */
name type
);
Note that, unlike Java, types come after names, commas follow all but the last declaration,
and we use parentheses instead of braces around the entire table definition.
Consider this example:
/* Table to hold information about various PSU courses */
create table courses
(
course_id smallint, /* id, primary key */
dept varchar(5), /* department abbrev, .g. CMPSC */
number smallint, /* catalog number,e.g.221 */
name varchar(30), /* course title */
credits smallint, /* number of credits */
num_sections smallint, /* how many sections are offered now*/
major_notes varchar(30) /* who requires it? a service course?*/
);

Primary Keys and Required Fields


Some other restrictions can be specified in our declarations.
One is that we specify some fields to require values. We do this by adding not null to the
end of their declarations.
We can also specify fields to be primary keys. We do this by adding a constraint to the
table. Here's a general form of how to define a primary key constraint:
constraint pk_tableName primary key ( primary key field name )

Here's an improved version of the above definition:


/* Table to hold information about various PSU courses */
create table courses
(
course_id smallint not null, /* id, primary key */
dept varchar(5), /* department abbrev, e.g. CMPSC*/
number smallint, /* catalog number, e.g. 221 */
name varchar(30), /* course title */
credits smallint, /* number of credits */
num_sections smallint, /* how many sections are offered now */
major_notes varchar(30), /* who requires it? a service course? */
constraint pk_courses primary key (course_id)
);
Inserting Data into Tables
Great, so we've got a table. How do we put data into it with SQL? The insert statement
comes into play here.
Here's the general form of an insert statement:
insert intotable_name
(field_name_1,field_name_2,etc.)
values (value_for_field_name_1,value_for_field_name_2,etc.);
Note: Strings in SQL are given inside single quotes, e.g. 'This string will work' NOT "This
one will make you wonder why the computer's mad at you."
Consider these examples:
insert into courses
(course_id, dept, number, name, credits, num_sections)
values (0, 'CMPSC', 111, 'Logic of CS Seminar', 1, 1);

insert into courses


(course_id, dept, number, name, credits, num_sections, major_notes)
values (1, 'CMPSC', 121, 'Intro to Programming Tech', 3, 3, 'CMPSC, CMPEN Required');

3.Design and development of databases. Technology of programming of ORM. The


distributed, parallel and heterogeneous databases.
Arithmetic Operators and Functions
The basic arithmetic operators +, -, *, / are known in SQL. There are also several built-in
functions.
Arithmetic expressions can be built from these operators and functions as we know. They
can be used both computing columns of results and in filtering results.
When you have a computed column of results in Derby, you'll notice that the column header
is just a number. Not so descriptive! We can set a name for it by following the expression with
the desired heading:
select arithmeticExpression name
Suppose we'd added a field to our courses table called enrollment with the total number of
students in each course. Then we might have request a calculated column as in the following:
select dept, number, enrollment/num_sections avg_class_size
from courses;
Basic Boolean Logic
Basic Boolean expressions in SQL are built up like they are in Java: from relational
operators, logical operators, and parentheses.
Relational Operators in SQL are:
● =
● >
● <
● <=
● >=
● !=
Logical Operators in SQL are:
● and
● or
● not
So, we could write logical expressions like some of these:
● credits = 3
● number < 200
● number >= 200 and number < 300
● not(number < 200 or number >= 300)
● credits = 3 and dept = 'CMPSC'
Where Clauses
We can use Boolean expressions to filter results, choosing only those that meet some
criteria. This is where a where clause comes into play. It is followed by a Boolean expression
with the criteria for being included in the result set.
Here's an improved general form of a select statement:
select [* | list_of_columns_and_constants]
fromtableName
where expression;
Here are some examples:
select *
from courses
where credits = 3 and dept = 'CMPSC';
select number
from courses
where credits = 3 and dept = 'CMPSC';
select dept, number, name
from courses
where number >= 200 and number < 300;
More Operators for Boolean Expressions
We have three additional operators that give us more power with Boolean expressions.
Like
The like operator can be used for text comparisons where we're looking to specify part of
our desired output, but not all.
The general form is this:
expression like 'patternString'
The pattern string is built from these wildcards:
● _ to represent exactly one character. We could use __ for two characters, etc.
● % to represent any number of characters (including zero).
Consider these examples:
select dept, number, name
from courses
where name like '%Engineers';
select dept, number, name
from courses
where name like '%Prog%';
select dept, number, name
from courses
where dept like 'CMP__';
In
The in clause lets us specify a list of values to check. It returns true when the expression
matches anything in the list.
The general form is this:
expression in (value1,value2, ... )
The values listed could be text (given inside quotes) or numeric. We could also say not in.
Consider these examples:
select *
from courses
where number in (121, 122, 221, 311);
select *
from courses
where number not in (121, 122, 221, 311);
select *
from courses
where dept in ('CMPSC', 'CMPEN');

We could alternatively use something called a subquery in place of the list. We'll learn about
subqueries next time.
Between
The between operator is merely a replacement for building an expression in terms of >= and
=< and and. Its general form is as follows:
expression betweenlowandhigh
Consider this example:
select name, length(name)
from courses
where length(name) between 10 and 20;
The above produces the same result as this query:
select name, length(name)
from courses
where length(name) >= 10 and length(name) <= 20;

String Functions
Several functions are provided for working with strings (generally string fields, but also
string constants).
First, though, || is the concatenation operator for strings in Derby.
Here are some functions:
● upper(str) returns str converted to all uppercase
● lower(str) returns str converted to all lowercase
● length(str) returns how many characters are in str
● char(ascii_val) returns the character with ASCII code ascii_val
● locate(str1, str_field) returns the index, starting from 1, where str1 is found in str_field or
0 if it's not. A third argument may be supplied to tell the index where the search will start.
● substr(str, start_pos, length) returns the substring starting at start_pos and of length
characters in str1.

Questions:
1.What makes databases such an essential component of modern life?
2. Can databases be used to predict consumer behavior?
3. What are the basic components of a database?
4. Why are relationships a significant aspect of databases?
5. What’s the difference between flat files and other database models?
6. What is the best software for creating and managing databases?
7. Is it possible to access databases using the Web?
References
1. June J. Parsons and Dan Oja, New Perspectives on Computer Concepts 16th Edition -
Comprehensive, Thomson Course Technology, a division of Thomson Learning, Inc Cambridge,
MA, COPYRIGHT © 2014.
2. Lorenzo Cantoni (University of Lugano, Switzerland) James A. Danowski (University of
Illinois at Chicago, IL, USA) Communication and Technology, 576 pages.
3. Craig Van Slyke Information Communication Technologies: Concepts, Methodologies,
Tools, and Applications (6 Volumes). ISBN13: 9781599049496, 2008, Pages: 4288
4. Utelbaeva A.K.,Utelbaeva A.K. Study guide for lectures on discipline “Computer
science”, Shimkent 2008, 84 pages.

You might also like