2022-2023 CS120 Chapter Database BuildingMgt
2022-2023 CS120 Chapter Database BuildingMgt
Management 11
Introduction
Data
Requiremen
independent
ts
DBMS
Database Implementation 22
How the data is stored int the DB
• The data in RDBMS is stored in database objects called
tables. The table is a collection of related data entries
and it consists of columns and rows.
• Example: CUSTOMERS table
3
Table Fields
• Table columns are derived from the relational schema. Table
columns represents the attributes
Example:
CUSTOMER (ID, NAME, AGE, ADDRESS, SALARY)
5
Table column
• A column is a vertical entity in a table that
contains all information associated with a specific
field in a table.F
Tunis
Beja
Bizerte
Nabeul
6
Sousse
Kef
How to manage the data ?
• Language for describing database definition,
manipulation, and applications
7
Create & modify database schema
• Data Definition Language (DDL) of SQL supports the
definition of the physical structures of databases.
8
insert & manage data
• Data Manipulation Language (DML) of SQL supports
queries that extract data from databases (select
statements), add new rows to tables (insert statements),
and modify attribute values of existing rows (update
statements).
10
commonly used constraints in
SQL
12
Database statements
• To create a new database, we use:
CREATE DATABASE database_name
13
Table creation
• To create a table, we have to specify the table name and the
list of attributes of this new table.
• The statement must specify a name and type for each
attribute.
18
Deep view on
DML
19
Insert Statement
• To insert a row into a table, an SQL programmer
needs to specify attributes’ values of the table.
INSERT INTO movie (movie_id, title, genre, length)
VALUES (123,'Annie Hall', 'Romantic Comedy',110)
20
Insert Staement
INSERT INTO video (video_id, date_acquired, movie_id)
VALUES (100, ‘2013-07-15 ',123)
21
Update Statement
• An update statement in SQL lets us change one or more
rows of an existing table.
• An update statement has three clauses:
• The update clause specifies which table to update.
• The set clause contains one or more assignments
that specify which attributes to change and what
their new values are.
• The where clause specifies which rows to change.
UPDATE movie
SET length=120
WHERE movie_id=123 22
Delete Statement
• To delete rows in a table, we must write a delete
statement that specifies the table and a selection
condition.
• Each row of the table that satisfies the condition is
deleted when the statement is executed.
• For example, the following statement deletes every row of
the Movie table where length is less than 50.
WHERE length<30
23
Select Statement
• To extract information from a relational database, we use
the Select Statement.
• The select query produces a table as its result.
• The basic select statement includes three clauses:
• The select clause specifies the attributes that go in
the
results table.
• The from clause specifies the source tables
that the database will access in order to
execute the query.
• The where clause specifies the selection conditions,
including the join condition. 24
Select Statement
25
Projection
SELECT title, genre FROM movie
26
WHERE clause
• Used to extract only the records that fulfill a specified
condition.
Operator Description
= Equal
> Greaterthan
< Lessthan
>= Greaterthan or equal
<= Lessthan or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
28
NOT LIKE
IN To specify multiple possible values for a column
Example
• The following query selects the movies which their genre
ends with the word comedy and their length is superior
to 90.
SELECT * FROM movie
WHERE genre LIKE '%comedy' AND length>90
30
JOIN condition (or clause)
Display the number of video per movie
Syntax:
SELECT movie_id, COUNT (video_id)
FROM movie, video
WHERE movie.movie_id = video. movie_id
OR
SELECT movie_id, COUNT (video_id)
FROM movie
FULL JOIN video ON movie.movie_id = video. movie_id 31
ORDER BY clause
• The ORDER BY clause orders or sorts the result of a query
according to the values in one or more specific columns.
More than one column can be ordered one within another.
33
GROUP BY clause
• GROUP BY clause is used with the SELECT
statement.
34
GROUP BY clause
• Display total number video by each movie separately with
the acquired date between 11/03/2010 and 08/10/2013.
36
HAVING clause
• WHERE clause is used to place conditions on columns.
38