You are on page 1of 36

Database

Management 1
Introduction
DBMS independent

Data
Requirements

Entity Relationship (E-R) Diagrams


Conceptual Database Design UML Class Diagrams

Logical Database Design Logical data model


relatated of the chosen DBMS
choose
DBMS dependent

DBMS File organization and access path,


Physical Database Design based on the options offered by the
chosen DBMS

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

ID NAME AGE ADDRESS SALARY


1 TOUNSI 32 Tunis 20000
2 Kilani 25 Beja 1500
3 Sayari 60 Bizerte 56532
4 Chaibi 45 Nabeul 600
5 Abid 30 Sousse 10000
6 Mahdoui 22 Kef 45000

3
Table Fields
• Table columns are derived from the relational schema. Table columns
represents the attributes
Example:
CUSTOMER (ID, NAME, AGE, ADDRESS, SALARY)

• The fields in the CUSTOMERS table consist of ID, NAME, AGE,


ADDRESS and SALARY.

ID NAME AGE ADDRESS SALARY

• A field is a column in a table that is designed to maintain


specific information about every record in the table.

4
Record
• A record, also called a row of data, is each individual entry that
exists in a table.

• For example there are 6 records in the above CUSTOMERS


table.

• Following is a single row of data or record in the CUSTOMERS


table:

1 TOUNSI 32 Tunis 20000

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

• or example, a column in the CUSTOMERS table is ADDRESS,


which represents location description and would consist of the
following: ADDRESS

Tunis

Beja

Bizerte

Nabeul

Sousse
6
Kef
How to manage the data ?
• Language for describing database definition, manipulation, and
applications

• Structured Query Language (SQL)

• The language includes statements that:


• specify and modify database schemas
• manipulate the database content

7
Create & modify database
schema
• Data Definition Language (DDL) of SQL supports the definition
of the physical structures of databases.

• The most used DDL statements in SQL are:


• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies an existing database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies an existing table
• DROP TABLE - deletes an existing table

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).

• The different commands defined by the DML part of SQL are:


• SELECT - extracts data from an existing table
• UPDATE - updates data in an existing table
• DELETE - deletes data from an existing table
• INSERT INTO - inserts new data into the table

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.

• Constraints can be at a column level or at a table level. Column


level constraints are applied only to one column whereas table
level constraints are applied to the whole table.

10
commonly used constraints in
SQL

• NOT NULL Constraint: Ensures that a column cannot have


NULL value.
• DEFAULT Constraint: Provides a default value for a column
when none is specified.
• UNIQUE Constraint: Ensures that all values in a column are
different.
• PRIMARY Key: Unique identifier of each row/record in a
database table.
• FOREIGN Key: Unique identifier of a row/record in any another
database table. 11
Deep view on DDL

12
Database statements
• To create a new database, we use:
CREATE DATABASE database_name

• To remove an existing database, we use:


DROP 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.

• SQL supports a specific collection of attribute types, such as :


• Numeric attribute types include integers (such as int,
smallint, and long), floating point types of various lengths
(for example, float and double), and formatted numbers
(such as decimal)…
• Date types include a date and a time (datetime), a date
with no time (date), and a time with no date (time)…
• Character and string types include varchar type having a 14
varying number of characters, up to some limit, and a char
type that has a specific fixed number of characters…
Table creation: Example
CREATE TABLE movie (
movie_id int(11) NOT NULL ,
title varchar(30) DEFAULT NULL,
genre varchar(30) DEFAULT NULL,
length int(11) DEFAULT NULL,
PRIMARY KEY (movie_id)
)

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.

CREATE TABLE video (


video_id int(11) NOT NULL,
date_acquired date DEFAULT NULL,
movie_id int(11) DEFAULT NULL,
PRIMARY KEY (video_id),
FOREIGN KEY (movie_id) REFERENCES movie
(movie_id) 16
)
Adding, Removing, and
Modifying Attributes
• The alter table statement is used to add, modify, and
remove attributes.
• To add an extra column to an existing table:

ALTER TABLE table_name ADD column_name datatype

• To remove an exisitng column:


ALTER TABLE table_name DROP COLUMN column_name

• To change the data type of an exisitng column:

ALTER TABLE table_name MODIFY column_name datatype 17


Remove a table
• Drop table statement is used to remove a table from a
database

DROP TABLE table _name

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.

DELETE FROM movie

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

SELECT <Column names> / Function (<column name>)


FROM <Table name>
[WHERE <condition>]
[OREDER BY <column name>]
[GROUP BY <column name>]

25
Projection
SELECT title, genre FROM movie

Note: The asterisk (*) is a quick way of selecting all columns!

SELECT DISTINCT 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

<> Not 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

With the LIKE operator, two patterns (% and _ ) are used:


29
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character
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.

• The default order is ascending.


SELECT * FROM movie
WHERE genre LIKE '%comedy' AND length>90

ORDER BY title, genre

SELECT * FROM movie


WHERE genre LIKE '%comedy' AND length>90 30

ORDER BY title DESC


GROUP BY clause
• used to arrange identical data into groups with the help of
some functions.
• Two types exist:
• Group By single column: Group By single column means, to
place all the rows with same value of only that particular
column in one group.
• Group By multiple columns: This means to place all the rows
with same values of both the
columns column1 and column2 in one group

31
GROUP BY clause
• GROUP BY clause is used with the SELECT statement.

• In the query, GROUP BY clause is placed after the WHERE


clause.

• In the query, GROUP BY clause is placed before ORDER BY


clause if used any.

32
GROUP BY clause
• Display total number video by each movie separately with the
acquired date between 11/03/2010 and 08/10/2013.

SELECT movie_id, COUNT (video_id)


FROM video, movie
WHERE movie.movie_id = video. movie_id
AND (date_acquired BETWEEN 11-03-2010 AND 08-10-
2013)
GROUP BY movie_id
33
SQL Aggregate functions

34
HAVING clause
• WHERE clause is used to place conditions on columns.

• If we want to place conditions on groups???

 This is where HAVING clause comes into use.

• We cannot use the aggregate functions like SUM(), COUNT()


etc. with WHERE clause.

• We have to use HAVING clause if we want to use any of these


functions in the conditions
35
HAVING clause
• Display the list of movies having at least 3 videos

SELECT movie_id, COUNT (video_id) AS ‘number of videos’


FROM movie, video
WHERE movie.movie_id = video. movie_id
GROUP BY movie_id
Having COUNT (video_id) >= 3

36

You might also like