i1100 - introduction to computer science · database management systems •generally, these...
TRANSCRIPT
Outline• What is a Database?
• Database Management Systems
• Relational Database Design
• Entity-Relationship Model
• Structured Query Language
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
428
What is a Database?• A database is a collection of data.
• A database could be as simple as a text file with a list of names.
• Or it could be as complex as a large, relational database management system, complete with in-built tools to help you maintain the data.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
429
Text File• Imagine we have a text file called data.txt, and that the contents look like
this:
• We could also call each row a record. Therefore, we currently have 4 records in our database.
• With a small list like this, a text file may serve our purposes perfectly.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
430
Database Software• A better option would be to store the data in a database table using
specialized database software, such as Microsoft Access.
Something like this:
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
431
Database Management Systems
• A Database Management System (DBMS), is a software program that enables the creation and management of databases.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
432
Database Management Systems• Generally, these databases will be more complex than the text file
example. In fact, most of today's database systems are referred to as a Relational Database Management System (RDBMS), because of their ability to store related data across multiple tables.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
433
Database Management Systems• Some of the more popular relational database
management systems include:� Microsoft Access
� Filemaker
� Microsoft SQL Server
� MySQL
� Oracle
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
434
Database Management Systems• A typical DBMS has users with different rights and permissions who use it
for different purposes. Some users retrieve data and some back it up. The users of a DBMS can be broadly categorized as follows −
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
435
Database Management Systems• Administrators − Administrators maintain the DBMS and are responsible
for administrating the database.
• Designers − Designers are the group of people who actually work on the designing part of the database.
• End Users − End users are those who actually reap the benefits of having a DBMS.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
436
What is a Table?• In database terms, a table is responsible for storing data in the database.
Database tables consist of rows and columns.
• In the following example, the second row is highlighted:
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
437
What is a Table?• The second column is highlighted. This column has been given a name of
"FirstName":
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
438
What is a Table?• A row contains each record in the table, and the column is responsible for
defining the type of data that goes into each cell.
• Therefore, if we need to add a new person to our table, we would create a new row with the person's details.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
439
Microsoft Access TutorialQuerying a Database• Queries are one of the things that make databases so powerful.
• A "query" refers to the action of retrieving data from your database.
• For example, you might only want to see how many individuals in your database live in a given city.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
440
Relational Database Design• Most popular database management systems are relational systems, and are
usually referred to as Relational Database Management Systems (RDBMS). What this means is that their databases can contain multiple tables, some (or all) of which are related to each other.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
441
Relational Database Design• In this example, the database has 2 tables. Each table serves a specific
purpose.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
442
Relational Database Design• Using the example, the Individual table can
hold data that is strictly about the individual. The City table can hold a list of all cities. If we want to know which city each individual lives, we could store a "pointer" in the Individual table to that city in the City table.
• This example demonstrates the relationship between the Users table and the City table. The users in the “Users" table live in cities that are defined in the "City" table. Therefore, we can cross-reference each “Users" record with a "City" record.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
443
How Does This Work?• Firstly, in the City table, each record has a unique identifier. A unique identifier is a value that
is unique to each record. This identifier can be as simple as an incrementing number. So, in our City table, the first record has a number of 1, the second record has a number of 2, and so on.
• Secondly, when entering each user into the Users table, instead of writing out the full city name in that table, we only need to add the city's unique identifier. In this case, the unique identifier is a number, so we enter this number into the "CityId" column of the “Users" table.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
444
How Does This Work?
• So, by looking at both tables, we can determine that Khanafer lives in Mcharafieh, Chaiblives in Hadath, and both Itani and Damaj live in Msaytbeh. At this stage, nobody in our database lives in Basta or Khalde.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
445
Structured Query Language• SELECT one or more column_name
FROM table WHERE one or many conditions
• Example: table called CLIENT
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
446
Structured Query Language• SELECT City
FROM Client;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
447
Structured Query Language• SELECT FirstName, LastName
FROM Client;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
448
Structured Query Language• SELECT *
FROM Client;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
449
Structured Query Language• SELECT DISTINCT FirstName
FROM Client;
! 2 times Pierre !!, to solve next slide
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
450
Structured Query Language• SELECT DISTINCT FirstName
FROM Client;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
451
Structured Query Language
• Find all the clients that live in Paris
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
452
SELECT * FROM Client WHERE City = ‘Paris’;
Structured Query Language
• Fin the maximum salary of clients
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
453
SELECT MAX(Salary) FROM Client;
We can also use MIN, AVG, COUNT, and SUM functions
Structured Query Language
• Count the number of records in the Client table
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
454
SELECT COUNT(*) AS ClientsNumber FROM Client;
Structured Query Language
• SELECT COUNT(FirstName) FROM Client WHERE FirstName=”Mary”;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
455
Structured Query Language
• SELECT COUNT(DISTINCT FirstName) FROM Client ;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
456
Structured Query Language
• SELECT SUM(Salary) FROM Client;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
457
Structured Query Language
• SELECT AVG(Salary) FROM Client WHERE FirstName = ‘Pierre’;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
458
The query computes the average of salaries for clients having the first name
=”Pierre”, it is (2,000 + 1,500)/2, and the answer is 1,750$.
Structured Query Language• There are many comparison operators that can be written in WHERE such
as :
� equal =,
� different <> or !=,
� greater than >,
� less than <,
� greater or equal >=,
� less than or equal <=,
� IN, BETWEEN, N
� OT BETWEEN,
� LIKE, and
� IS NULL.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
459
Structured Query Language
• SELECT Id, FirstNameFROM Client WHERE City=’Paris’ OR City=’Nantes’;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
460
Structured Query Language
• SELECT Id, FirstName, Salary FROM Client WHERE FirstName =’Pierre’ AND Salary> 1500;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
461
Structured Query Language
• SELECT FirstNameFROM Client WHERE LastName= ‘Dupont’ OR LastName = ‘Durant’ OR LastName= ‘Matrin’;
SELECT FirstName FROM Client WHERE LastName IN (‘Durant’, ‘Dupont’, ‘Martin’);
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
462
Structured Query Language
• SELECT Id, LastName FROM Client WHERE Salary BETWEEN 100 AND 1000;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
463
Structured Query Language
• SELECT Id, LastName FROM Client WHERE Salary NOT BETWEEN 100 AND 1000;
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
464
Structured Query Language
• SELECT FirstName, LastName FROM Client WHERE LastName LIKE “%D”; => LastName ends with letter D.
• SELECT FirstName, LastName FROM Client WHERE LastName LIKE “D%”; => LastName starts with letter D.
• SELECT FirstName, LastName FROM Client WHERE LastName LIKE “%D%”; => LastName contains letter D.
• SELECT FirstName, LastName FROM Client WHERE LastName LIKE “D%t”; => LastName starts with D and ends with t.
• SELECT FirstName, LastName FROM Client WHERE LastName LIKE “A_C”; => The underscore “_” character will be replaced by any character such as ABC, AEC, etc. It means that the Last name contains exactly 3 characters, the first one is A, the last one is C, and a character.
Intr
od
uct
ion
to
Co
mp
ute
r S
cie
nce
(I1
10
0)
20
18
-20
19
465