You are on page 1of 73

CSC2243: Databases, Visual

Basic and Software Engineering


By NGIRUWONSANGA Albert
E-mail: ngiruwonsanga.rw@gmail.com

Phone contact: 0788 471 881


University of Rwanda
College of Education-School of Education

Department of Mathematics, Science and Physical Education


Year 2 MCsE DEGREE-Academic Year: 2019/2020
Module description
• Module Title: Databases, Visual Basic and Software Engineering

• Module Code: CSC2243

• Credit Units: 20

• Contact time: 96 hours Room: 1STD3

• Timetable: Tuesday & Wednesday (8 hours)-From 8:00am to


11:55am

• Year: II MCsE DEGREE

2
Assessment Pattern
Assessment Type Weighting (%)
In-course Group Assignment 10%
Assessment (written assignments
and projects)
Individual 20%
Assignment (Online
Quizzes, written
assignments)
CAT 20%
Summative Final Exam 50%
Assessment
3
Content Outline

1. Terms related to DBMS

2. Database models

3. RDBMS

4. MySQL and MS Access to manage databases

5. Normalization

6. Relational Algebra
4
Content Outline
7. Overview of PHP

8. Connecting dynamic PHP web pages to MySQL


databases

9. HTML and CSS

10.Overview of Visual Basic

11.Connecting VB forms to MS Access Database

5
Content Outline
12. Development of a software project using Database and PHP by
including HTML tags and CSS codes to decorate pages (setting
foreground and background colors, size, margins, applying
borders, etc.).

13. Development of a software project using Database and Visual


Basic. Different tools like label, textbox, combo box, command
button, etc. will be used.

6
Table
• Table: It is a collection of related data held in a structured format
within a database.

• It consists of columns, and rows.

• In relational databases, a table is a set of data elements (values)


using model of vertical columns (identifiable by name) and
horizontal rows, the cell being the unit where a row and column
intersect.

7
Columns

• Each column in the table has a unique name and


contains different data.

• Each column has an associated data type.

• For instance, in the Students table, you can see that


Student_id is an integer and other four columns are strings.

• Columns are sometimes called fields or attributes.


8
Rows

• Each row in the table represents a different student.


Because of the tabular format, they all have the same
attributes.

• Rows are also called records or tuples.

9
Table…
Student_id name Regno Branch Address
1 Zawadi CSE-18 CSE Kicukiro
2 Yvonne IT-18 IT Nyarugenge
3 Dan CSE-18 CSE Kayonza
4 Ange CSE-18 CSE Nyagatare
10 Claire IT-18 IT Kicukiro

Table 1: Students Table

10
Table…
• As the student_id in students table will be unique, it can be used
easily to fetch any data.

• A primary key can be used to fetch data from any column in the
table.

• For example:

1) Branch name of student with student_id=10

2) Name of student with student_id=10

• All what I need is the student_id and other columns depend on it.
11
Exercises related to Table 1

• Provide the relation schema for students table.

• How many tuples does table 1 has?

• How many attributes does it have?

• What is its cardinality?

• What is its grade?

12
Table…..
The relation schema for
Empnum name Age Deptnum
employee relation is:
3 Jones 27 1
7 Smith 34 2
11 Bob 18 1 employee(Empnum,
15 Jane 23 3 name, Age, Deptnum)
18 Mary 31 1

employees relation

13
Table…
• employee relation consists of four attributes: Empnum, name,
Age, and Deptnum. And the relation has five tuples;

• for example (3, jones, 27,1) is a tuple of the relation.

• The grade of employee relation is 4 and the cardinality is 5.

• The relation name and the names of attributes appearing in it


are called the relation schema of the relation.

14
Table…
• The relation schema is employee(Empnum, name, Age,
Deptnum)

• The set of possible values for a given attribute is called its domain.

• Thus for instance, Empnum takes its values from the domain of
employee numbers, constituted by the positive integers,

• And Age takes its values from the domain of ages, constituted by
integers between 0 and 100.
15
Table…
• Though these two domains have some values in common,
they are considered/regarded as different domains; since
the meaning given to common values is different in the
two contexts.

16
Table…

• A table has a specified number of columns, but can have


any number of rows. Each row is identified by one or more
values appearing in a particular column subset.

• A specific choice of columns which uniquely identify rows


is called the primary key.

17
Table…
• In relational databases, data are stored in tables, called
relations.

• Each relation has a fixed number of columns, called attributes.

• And a (dynamic, time-varying) number of rows, called tuples.

• The number of attributes of a relation is called its grade.

• The number of tuples is called its cardinality.

18
Defining some terms
1. Tuple: A single row of a table, which contains a single record
for that relation.

2. Relation instance: It is a finite set of tuples in the relational


database system. Relational instances do not have duplicate
tuples.

3. Relation schema: a relation schema describes the relation


name (table name), attributes, and their names.

19
Defining some terms…

4. Relation key: Each row has one or more attributes,


known as relation key, which can identify the row in the
relation (table) uniquely.

5. Constraints: Every relation has some conditions that must


hold for it to be a valid relation. These conditions are
called relational integrity constraints.

20
5. Constraints

There are three main integrity constraints:

1. Key constraints

2. Domain constraints

3. Referential integrity constraints

21
5.1. Key Constraints

• There must be at least one minimal subset of attributes in


the relation, which can identify a tuple uniquely.

• This minimal subset of attributes is called key for that


relation.

• If there are more than one such minimal subsets, these are
called candidate keys.

22
5.1. Key Constraints

Key constraints force that:

 In a relation with a key attribute, no two tuples can have


identical values for key attributes.

 A key attribute cannot have NULL values.

Key constraints are also referred to as entity constraints.

23
5.2. Domain Constraints
• Attributes have specific values in real-world scenario.
• For example age can only be a positive integer.
• The same constraints have been tried to employ on the
attributes of a relation.
• Every attributes is bound to have a specific range of values.
• For example, age cannot be less than zero and telephone
numbers cannot contain a digit outside 0-9.

24
5.3. Referential integrity Constraints
• They work on the Companies Table
concept of foreign keys. Company_id Company_name Company_address
1 CIMERWA Bugarama
• A foreign key is a column 2 URWIBUTSO Nyirangarama

that references the


primary key of another
table in order to maintain
relationship.

25
5.3. Referential integrity Constraints
Contact_id Company_id Contact_name Contact_title Phone Email
1 1 Jeff Spireman Vice President 2505130 jspi@yahoo.com
2 1 Bradley Cocc President 2505112 Bradley@gmail.com
3 2 Rugwiro DAF 2505511 ernru@Hotmail.com

Contacts table

The Contacts table contains a company_id column, which


references the primary key of the Companies table. This
column is a foreign key to the Companies table.

26
5.3. Referential integrity Constraints

• A foreign key is a key attribute of a relation that can be


referred in other relation.

• Referential integrity constraints states that if a relation


refers to a key attribute of a different or same relation,
then that key element must exist.

27
Database

• A database is a collection of information that is organized


so that it can easily be accessed, managed and
updated.

• A database is an application that manages data and


allows fast storage and retrieval of that data.

28
Database models

1. Hierarchical database model

2. Network database model

3. Relational database model

29
What is a model?

• Databases types might be complex for some people to


understand them.

• To understand them we reduce them to their essential


parts.

• Models are simplified representations of reality.

 the way we understand it


30
Database models
1. Hierarchical database model
• A hierarchical database model is a data model in which the
data are organized into a tree-like structure.
• The data are stored as records which are connected to one
another through links.
• A record is a collection of fields, with each field containing
only one value.
• The type of a record defines which fields the record contains.

31
Database models
1. Hierarchical database model

• In order to retrieve data from a hierarchical database the


whole tree needs to be traversed starting from the root node.

• This model is recognized as the first database model created


by IBM in the 1960s.

• An example of the use of hierarchical databases is Windows


Registry in the Microsoft Windows operating system.

32
Database models
1. Hierarchical database model Regedit.exe (Registry Editor)

33
Database models

2. Network database model

• The network model is a database model conceived as a


flexible way of representing objects and their relationships.

• Its distinguishing feature is that the schema, viewed as a


graph in which object types are nodes and relationship
types are arcs.

34
Database models
• The network model's original
inventor was Charles
Bachman, and it was
developed into a standard
specification published in 1969.

• This was followed by a second


publication in 1971, which
became the basis for most
implementations.

35
Database models
3. Relational database model

• A relational database management system (RDBMS) is a


database management system (DBMS) based on the
relational model invented by Edgar F. Codd at IBM's San Jose
Research Laboratory.

• Most databases in widespread use today are based on his


relational database model.

36
Database models

3. Relational database model

• According to DB-Engines, in June 2018, the most widely


used systems were Oracle, MySQL (Free software),
Microsoft SQL Server, PostgreSQL (Free software), IBM DB2,
Microsoft Access, and SQLite (Free software).

37
Database models
3. Relational database Relationships in a RDBMS
model

38
Database Types
1. Text database

• The simplest form of databases is a text database. When


data is organized in a text file in rows and columns, it can
be used to store, organize, protect, and retrieve data.

• Saving a list of names in a file, starting with first name and


followed by last name, would be a simple database.

39
Database Types
1. Text database

• Each row of the file represents a record.

• You can update records by changing specific names, you


can remove rows by deleting lines, and you can add new
rows by adding new lines.

40
Database Types
2. Desktop database programs

• They are more complex than a text database but


intended for a single user.

• A Microsoft Excel spreadsheet or Microsoft Access are


good examples of desktop database programs.

• They allow users to enter data, store it, protect it, and
retrieve it when needed.

41
Database Types
2. Desktop database programs

• The benefit of desktop database programs over text


databases is the speed of changing data, and the ability
to store large amounts of data while keeping
performance of the system manageable.

42
Database Types
3. Relational databases

• Relational databases are the most common database


systems.

• They include databases like SQL Server, Oracle Database,


Sybase, Informix, and MySQL.

• The RDMS feature much better performance for


managing data over desktop database programs.

43
Database Types
3. Relational databases

• For example, they allow multiple users (even thousands!)


to work with the data at the same time, creating
advanced security for access to the data.

• RDBMS systems store data in columns and rows, which in


turn make up tables. A table in RDBMS is like a
spreadsheet.

44
Database Types
3. Relational databases

• A set of tables makes up a schema.

• A number of schemas create a database.

• Many databases can be created on a single server.

45
ODBC
• Open Database Connectivity (ODBC) is a common framework
for accessing and altering the contents of databases.

• ODBC is a standard database access method developed by


the SQL Access group in 1992.

• The goal of ODBC is to make it possible to access any data


from any application, regardless of which database
management system (DBMS) is handling the data.

46
ODBC
• ODBC manages this by inserting a middle layer, called a
database driver, between an application and the DBMS.
• The purpose of this layer is to translate the application's data
queries into commands that the DBMS understands.
• For this to work, both the application and the DBMS must be
ODBC-compliant--that is, the application must be capable of
issuing ODBC commands and the DBMS must be capable of
responding to them.

47
DBMS

• A database management system (DBMS) is a set of


computer programs that controls the creation,
maintenance, and the use of the database of an
organization and its users.

• DBMSes may use any of a variety of database models,


such as the network model or relational model.

48
RDBMS
• RDBMS is a type of database management system (DBMS)
that stores data in the form of related tables.

• Relational databases are powerful because they require


few assumptions about how data is related or how it will
be extracted from the database.

• As a result, the same database can be viewed in many


different ways.

49
SQL

• Abbreviation of structured query language, SQL is


standardized query language for requesting information
from a database.

• The original version called SEQUEL (Structured English


query Language) was designed by an IBM research
center in 1974 and 1975.

50
SQL

• SQL was first introduced as a commercial database


system in 1979 by Oracle Corporation.

• It is a simplistic programming language that is used in


relational database.

51
MySQL
• MySQL is an open source relational database
management system (RDBMS).

• Its name is a combination of "My", the name of co-founder


Michael Widenius's daughter, and "SQL", the abbreviation
for Structured Query Language.

• MySQL is a central component of the LAMP open-source


web application software stack (and other "AMP" stacks).

52
MySQL
• LAMP is an acronym for "Linux, Apache, MySQL,
Perl/PHP/Python".

• Applications that use the MySQL database include:


TYPO3, Joomla, and WordPress.

• MySQL is also used in many high-profile, large-scale


websites, including Google (though not for searches),
Facebook, Twitter, Flickr, and YouTube.

53
Using MySQL to create database and
tables
• C:\Users\HP>cd c:\

• c:\>cd "Program Files (x86)"

• c:\Program Files (x86)>cd EasyPHP

• c:\Program Files (x86)\EasyPHP>cd mysql

• c:\Program Files (x86)\EasyPHP\mysql>cd bin

54
Using MySQL …
• c:\Program Files (x86)\EasyPHP\mysql\bin>mysql -u root -
p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or
\g.
• mysql> show databases;
• mysql> use test\g
• mysql> show tables;
• mysql> use mysql\g
55
Using MySQL …

• mysql> show tables\g

• mysql> describe host;

• mysql> select *from host;

• mysql> create database URCE\g

• mysql> show databases;

56
Using MySQL …

• mysql> use URCE\g

• mysql> show tables;

• mysql> create table students(reg_num bigint,fname


char(40),lname char(58),dateofbirth date);

• mysql> show tables;

• mysql> describe students;


57
Using MySQL …

• mysql> insert into students values ('20071100', 'Aline',


'Umutoni','1991-10-01');

• mysql> insert into students values ('20071002', 'Kevin',


'Ganza','1984-08-23');

• mysql> insert into students set reg_num='20071002',


fname='Kevin',lname='Ganza',dateofbirth='1984-08-23';

58
Using MySQL …

• mysql> select *from students\g

• mysql> alter table students change reg_num regnum


bigint;

• mysql> describe students;

• mysql> update students set lname="Uwase", fname="Ella",


dateofbirth="1998-05-17" where regnum="20071002";
59
Using MySQL …

• mysql> select *from students;

• mysql> delete from students where regnum='20071002';

• mysql> select *from students;

• mysql> insert into students set regnum='20071002',


fname='Kevin', lname='Ganza',dateofbirth='1984-08-23';

60
Using MySQL …

• mysql> insert into students set regnum='20071017',


fname='Diendra', lname='Ngabo',dateofbirth='2000-11-11';

• mysql> insert into students


values('20071230','Ellen','Mutesi','1998-05-17');

• mysql> select *from students;

61
SQL ALTER TABLE statement

• The ALTER TABLE statement is used to add, delete or


modify columns in an existing table.

• The ALTER TABLE statement is also used to add and drop


various constraints on an existing table.

62
SQL ALTER TABLE statement

• Adding a column in MySQL


1. ALTER TABLE table ADD [COLUMN] column_name_1
column_1_definition [FIRST|AFTER existing_column];
2. Example:

alter table E add item varchar(30) after SN;

63
SQL ALTER TABLE statement

• Adding a column in MySQL

• To add a column at a specific position within a table row,


use FIRST or AFTER col_name. The default is to add the
column last.

64
SQL ALTER TABLE statement

• Adding a column in MySQL


1. ALTER TABLE table ADD [COLUMN] column_name_1
column_1_definition [FIRST|AFTER existing_column],
ADD [COLUMN] column_name_2 column_2_definition
[FIRST|AFTER existing_column], ...;

65
SQL ALTER TABLE statement

• Renaming a column in MySQL


1. ALTER TABLE “table_name” change “column 1” “column 2”
[“Data Type”];

2. ALTER TABLE “Table_name” RENAME COLUMN “column 1” TO


“column 2”;

3. ALTER TABLE contacts CHANGE contact_name ctname


char(50);

66
SQL ALTER TABLE statement

• Renaming a column in MySQL


4. ALTER TABLE contacts RENAME COLUMN contact_name TO
ctname ;

5. MODIFY is more convenient to change the definition without


changing the name because it requires the column name only
once:

Example: alter table E modify SN varchar(5);

67
SQL ALTER TABLE statement

• Renaming a column in MySQL

i. To alter a column to change both its name and


definition, use CHANGE,

i.e. ALTER TABLE t1 CHANGE a b BIGINT NOT NULL;

ii. To change a column definition but not its name

i.e. ALTER TABLE t1 CHANGE b b INT NOT NULL;


68
SQL ALTER TABLE statement
• Renaming a column in MySQL

iii. MODIFY is more convenient to change the definition without


changing the name because it requires the column name only
once:

i.e. ALTER TABLE t1 MODIFY b INT NOT NULL;

iv. To change a column name but not its definition, use CHANGE or
RENAME COLUMN. i.e. ALTER TABLE t1 CHANGE b a INT NOT NULL;

69
SQL ALTER TABLE statement
• Renaming a column in MySQL

v. RENAME COLUMN is more convenient to change the name without


changing the definition because it requires only the old and new
names:

i.e. ALTER TABLE t1 RENAME COLUMN b TO a;

70
SQL ALTER TABLE statement

• Deleting a column in MySQL


1. ALTER TABLE table_name DROP COLUMN column_name;

Note that the keyword COLUMN is optional so you can use


the shorter statement as follows:

ALTER TABLE table_name DROP column_name;

71
SQL ALTER TABLE statement

• Deleting a column in MySQL

To remove multiple columns from a table at the same time,


you use the following syntax:

1. ALTER TABLE table_name

2. DROP COLUMN column_1,

3. DROP COLUMN column_2, ... ;


72
SQL SELECT statement

• A simple statement in SQL has the following structure:

• SELECT (attribute list) FROM (relation name) WHERE


(predicates)

• i.e. SELECT Name, Age

• FROM EMP

• WHERE Age>20 and DeptNum=1;


73

You might also like