Professional Documents
Culture Documents
Chapter 5 (Database)
Chapter 5 (Database)
Definitions
A database (DB) is a collection of data that is organized so that it can be easily accessed, managed, and
updated. Examples of database applications include computerized library systems, flight reservation
systems, and many content management systems that store websites as collections of webpages in a
database.
Databases offer many benefits such as reducing the amount of time spent adding, deleting, and changing
data easily, improving the quality and consistency of information, reducing and controlling of data
redundancy, sharing of data and multiusers system, support for multiple views of data, easier to maintain
data security, and the ability to recover from crashes and errors.
Some of the well-known DBMS include Microsoft Access, MySQL, PostgreSQL, Microsoft SQL
Server, Oracle, Sybase and IBM DB2.
Most DBMSs are based on a client-server model (see Figure 14). In this model, the database is stored on
a server that is only used
is installed on a computer interrogates the server and gets the answer from it.
There are many types of DBMSs. They range from small systems that run on personal computers to huge
systems that run on mainframes (computers that support hundreds of users simultaneously). DBMS are
often classified according to the database model that they support; the most popular database systems
since the 1980s have all supported the relational model as represented by the Structured Query Language
(SQL).
SQL SELECT
The SELECT command enables to extract data from a database and returns a set of records in a table.
This command can select one or many columns of a table by separating them by a comma (,). The general
form of SELECT is as follows:
SELECT column_name
FROM table
WHERE one or many conditions
Example of a database of one table called « Client » that contains information about enterprise clients.
Find all the clients that live in Paris, we use the WHERE clause that filters the rows:
We can also use MIN, AVG, COUNT, and SUM functions as shown next.
COUNT(*)
SELECT COUNT(*) FROM Client ;
3
This query counts the number of records in the Client table and returns the answer = 3.
You can also rename a column in the query result using the AS keyword:
This query counts the number of clients having a FirstName. The answer here is 3 but it could be different
when a client does not have a name in the table. An attribute without a value is considered as NULL
value, which means empty.
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, NOT
BETWEEN, LIKE, and IS NULL.
In order to make complex conditions, we write in the WHERE clause either OR or AND operator. These
operators are used to filter records based on more than one condition:
The OR operator works as a union of records. First, it selects the records that satisfy the first condition,
SELECT Id, FirstName, Salary FROM Client WHERE AND Salary> 1500;
The AND operator works as an intersection of common records. First, it selects the records that satisfy
the first condition, then the second one. Finally, it keeps only the common records.
You can use the IN operator when the column value is one of many values of a list. The list could be a
list of numbers, a list of dates, a list of names, etc.
SELECT Id, LastName FROM Client WHERE Salary BETWEEN 100 AND 1000;
The Between operator is used when you want to select the value of a column from a range of values or
an interval. The interval could be an interval of numbers or dates.
SELECT Id, LastName FROM Client WHERE Salary NOT BETWEEN 100 AND 1000;
EXERCISES
Exercise 1: Consider relational database (DB) storing information about Master students and their
supervisors. The DB is made of only one relation (table) called R shown below:
1) Find the student names that live in Zahlé or having a major Math.
2) Find the name and the city of supervisors that supervise students that live in Baalbek
7)
8) Give the cities of supervisors that supervise the students that live in cities where their name starts
with the letter B.
9) Find the major of students and the name of their supervisor that terminated their Master in 2016.
10) Find the information about students having an age either 26 or 28 or 25 and that live in Tripoli.
11) Give the name of students and their major for students that they have already finished their Master.
I1100
Exercise 2: Consider a relational database made of three relations (tables) books, students, and libraries
presented below:
Lib_Name Lib_address
Du Liban Zouk Mosbeh
Antoine Sin el Fil
LDLP Beyrouth
Give the question for every SQL query and its execution result:
1) SELECT COUNT(*)
FROM Student
2) SELECT Title
FROM Book
6) SELECT *
FROM Book
7) SELECT *
FROM BOOK
8) SELECT Author
FROM Book