Professional Documents
Culture Documents
Chapter 6 Database BuildingMgt
Chapter 6 Database BuildingMgt
Management 1
Introduction
DBMS independent
Data
Requirements
Database Implementation 2
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)
4
Record
• A record, also called a row of data, is each individual entry that
exists in a table.
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
Sousse
6
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).
9
SQL contraints
• Constraints are the rules enforced on data columns or tables.
They are used to limit the type of data that can go into a table.
They ensures the accuracy and reliability of the data in the
database.
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.
Attributes have other characteristics other than simply a name and a type.
To declare a primary key (that is, a field that uniquely identifies a row), we u 15
se the primary key constraint
How to define a Foreign Key
• to specify a foreign key (a representation of a relationship type
as a reference to the primary key of another table), we create
foreign key with a references clause.
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.
• SQL uses single quotes around text values. However, numeric
values should not be enclosed in quotes.
• The AND & OR operators are used to filter records based on
more than one condition.
• The AND operator displays a record if both the first
condition and the second condition are true.
• The OR operator displays a record if either the first
condition or the second condition is true.
27
WHERE clause
• With the WHERE clause, the following operators can be used:
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
31
GROUP BY clause
• GROUP BY clause is used with the SELECT statement.
32
GROUP BY clause
• Display total number video by each movie separately with the
acquired date between 11/03/2010 and 08/10/2013.
34
HAVING clause
• WHERE clause is used to place conditions on columns.
36