0% found this document useful (0 votes)
27 views34 pages

Lec5 Se DB

The document provides an introduction to databases and database management systems (DBMS). It discusses what a database is, the purpose of databases, and what a DBMS is. It also covers relational databases and SQL, explaining that SQL is the standard language used to communicate with relational databases to perform operations like creating, reading, updating and deleting data. Finally, it discusses some key concepts like the relational model, entity relationship diagrams, and CREATE and ALTER statements in SQL.

Uploaded by

jfzaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views34 pages

Lec5 Se DB

The document provides an introduction to databases and database management systems (DBMS). It discusses what a database is, the purpose of databases, and what a DBMS is. It also covers relational databases and SQL, explaining that SQL is the standard language used to communicate with relational databases to perform operations like creating, reading, updating and deleting data. Finally, it discusses some key concepts like the relational model, entity relationship diagrams, and CREATE and ALTER statements in SQL.

Uploaded by

jfzaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CSEN406

SOFTWARE
ENGINEERING
DATABASE
TABLE OF CONTENTS

01 DATABASE

02 SQL

03 DBMS
INTRODUCTION

WHAT IS A DATABASE?
Organized collection of data

WHAT IS THE PURPOSE?


Store, manage, analyze, and use data

Dr. JOHN ZAKI 3


DBMS
Database Management System.

▪ It is a software that allows you to create a database and all the


objects within (e.g tables, views..etc).
▪ It allows you to retrieve the data.
▪ It allows you to modify the data.
▪ It provides security to the data.

One thing relational DBMS share is the SQL (Structured Query Language)

It is how we talk to a DB. How we create/read/update/delete (CRUD) data


in the DB among other things….

EX: Relational: MySQL, PostgreSQL, Oracle


Non-relational: MongoDB, Cassandra, Redis, Apache Hbase
Dr. JOHN ZAKI 4
DB SYSTEM ENVIRONMENT
USER

APPLICATION PROGRAMS THE EXTERNAL LEVEL

DBMS
SOFTWARE TO PROCESS QUERIES
THE LOGICAL LEVEL
SOFTWARE TO ACCESS THE STORED DATA

DEFINITIONS & STORED THE PHYSICAL LEVEL


META DATA DATABASE

Dr. JOHN ZAKI 5


RELATIONAL MODEL
1. A technique for structuring data using relations; the basis of relational databases (tables).
2. Each relation (table) has a name, attributes (columns), and rows (records) where each
record hold one data per attribute

Database schema: the logical representation of the database (like variable data type in
programming)

Database instance: a snapshot of the data at any given time


Col/attribute

CUSTOMERS Cust_ID Name Phone


Row/record
1 John 01234

2 Ahmed 01235 Table/relation


Table name
3 Amir 01237

Dr. JOHN ZAKI 6


RELATIONAL MODEL
1. Primary Key: unique number identifying the records. Fill in automatically from the DBMS

2. Foreign key: set a link between two tables. Enforces data integrity. It is a foreign key in one table that
references a primary key in another table and used to set relationships. This enables the use of SQL join
query to allow us pull data from two related tables (creating a view)

3. VIEWS: used to pull data out of tables, and they are secure as enables the users to pull data out of tables
without actually giving them access to tables. Simplify the output of the tables (selects only the required
col/records)

4. INDEX: put an index on col so that a query finds it quicker. Exactly like you look in a textbook. Quickly and
efficiently retrieve the data

5. Stored PROCEDURES: similar to views, container for sql code. They are stored and you can pass
parameters to it to retrieve the data.

Dr. JOHN ZAKI 7


DBMS LANGUAGE
DDL
Data definition language

DML
Data manipulation language

TCL
Transaction control language

DQL
Data query language

DCL
Data control language

Dr. JOHN ZAKI 8


ENTITY RELATIONSHIP DIAGRAM (ERD)
1. A pictorial representation of data that describes how data is communicated and
related to each other

2. Entities: (rows/records) are objects that are tracked in the database.


Represented using the rectangle.

3. Attributes: (columns) properties or traits. represented using ellipse shape. Each


ellipse is an attribute.

4. Relationships: how entities interact with each other. Represented using a


diamond (1:1, 1:M, M:1, M:M)

5. Cardinalities (multiplicity): define the relationship in a numerical context.


Attribute Attribute Attribute attribute

Entity

Entity

Entity

Dr. JOHN ZAKI 9


ERD MODEL

PICTURE COURTSEY OF ERMODELEXAMPLE.COM

Dr. JOHN ZAKI 10


ERD MODEL

Dr. JOHN ZAKI 11


ERD MODEL
1. Primary Key: is an attribute or field that uniquely
identifies a single record in a table
▪ Unique
▪ Never changing
▪ Never null

2. Data Types:
a. Numeric data types
b. String data types
c. Date & Time data types
d. XML data type
e. Spatial data type

Dr. JOHN ZAKI 12


ERD MODEL
1. Foreign Key: it is the primary key of a table but exists in a foreign table (another table). Can be
repeated or duplicated.

2. Composite Primary Key: two or more foreign keys are used as a Primary key.

Dr. JOHN ZAKI 13


ERD MODEL
Cardinality (multiplicity)
The relationship between two entities, specifically the number of instances of
one entity that can be associated with a single instance of another entity

One

Many

Zero to many

1 to many

1 (and only one)

Zero or 1

Dr. JOHN ZAKI 14


ERD MODEL
1. Cardinality (multiplicity): the relationship between two entities, specifically the number of
instances of one entity that can be associated with a single instance of another entity

Dr. JOHN ZAKI 15


SQL
STRUCTURED QUERY LANGUAGE

Dr. JOHN ZAKI 16


SQL

1. The language we use to communicate with relational database.

2. Used for organizing, managing, and retrieving data from a database.

3. Most famous commands are categorized under DDL & DML

Dr. JOHN ZAKI 17


DBMS LANGUAGE
DDL
Data definition language

DML
Data manipulation language

TCL
Transaction control language

DQL
Data query language

DCL
Data control language

Dr. JOHN ZAKI 18


DDL

Used to create the database structure are known as data definition language (DDL).

CREATE: creates a new table, view, index or other objects in the DB

ALTER: modifies an existing DB object such as a table or a view…etc

DROP: deletes a table or a view or other objects.

Dr. JOHN ZAKI 19


DML

Update the DB with new data

INSERT: creates a record


UPDATE: modifies existing records
DELETE: deletes existing records

SELECT: retrieves certain records from one or more tables. Considered as DQL

Dr. JOHN ZAKI 20


DCL

Used to control or grant other users access to databases

GRANT: give authorization or privilege to a user

REVOKE: removes an authorization or privilege

Dr. JOHN ZAKI 21


DATA TYPES

1. NUMERIC: bigint, int, smallint, tinyint, bit, numeric, money, smallmoney, float, real
2. STRING:
(non-Unicode) char, varchar, varchar(max), text
(Unicode) nchar, Nvarchar,nvarchar(max)
1. DATE AND TIME: date, time, datetime

Dr. JOHN ZAKI 22


CREATE TABLE
CREATE TABLE table_name (
Column1 datatype (size),
Column2 datatype (size),
…..);

CREATE TABLE "Customers" (


"CustomerID" INT,
"FirstName" varchar(50),
"LastName" varchar(50), PRIMARY KEY
"Street" varchar(50), 1. UNIQUE
"City" varchar(50), 2. AUTO INCREMENT
"ZipCode" varchar(50), 3. NOT NULL
"Phone" varchar(11),
PRIMARY KEY ("CustomerID")
);

Dr. JOHN ZAKI 23


CREATE TABLE
CREATE TABLE Order (
OrderID SERIAL PRIMARY KEY,

FOREIGN KEY(CustomerID)
REFERENCES Customers(CustomerID),

CustomerName varchar(100),
To_Street varchar(50),
To_City varchar(50),
To_ZipCode varchar(50),
ShippingDate datetime,
OrderDate datetime,
ProductID int,
);

Dr. JOHN ZAKI 24


CREATE TABLE
CREATING FROM EXISTING TABLE AND COPYING DATA
CREATE TABLE new_table_name AS
SELECT * FROM old_table_name;

CREATING FROM EXISTING TABLE WITHOUT COPYING DATA


CREATE TABLE new_table_name (LIKE old_table_name);

EX.
CREATE TABLE administrators (LIKE Customers);

Dr. JOHN ZAKI 25


ALTER TABLE
ALTER TABLE table_name ACTION; RENAME, DROP, ADD COLUMN, DROP COLUMN,
ALTER COLUMN, ADD CONSTRAINT, SET
DEFAULT, ADD CHECK….
ALTER TABLE old_table_name ALTER TABLE Customers
RENAME TO new_table_name; RENAME TO Users;
ALTER TABLE table_name ALTER TABLE Customers
RENAME COLUMN old_col_name RENAME FirstName
T0 new_col_name; TO Fname;
ALTER TABLE table_name ALTER TABLE Customers
ADD COLUMN col_name datatype(size) CONSTRAINT, ADD COLUMN Email VARCHAR(50));
ADD COLUMN col_name datatype(size) CONSTRAINT
…………; ALTER TABLE Customers
ADD COLUMN Email VARCHAR(50) NOT NULL
DEFAULT 'N/A';
ALTER TABLE table_name ALTER TABLE Customers
DROP COLUMN col_name, DROP COLUMN Email;
DROP COLUMN col_name …………;

Dr. JOHN ZAKI 26


WHAT WOULD HAPPEN IF YOUR TABLE
HAD DATA WHILE EXECUTING THIS
COMMAND
ALTER TABLE Customers
ADD COLUMN Email VARCHAR(50) NOT NULL;
DROP TABLE
DROP TABLE [IF EXISTS] table_name
[CASCADE | RESTRICT];

DROP TABLE customers;


DROP TABLE [IF EXISTS] customers

Restrict: won’t delete the table if there are other dependencies


Cascade: will delete the table and the objects which depend on the table.

Dr. JOHN ZAKI 28


INSERT
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT ONE RECORD


INSERT INTO Customers (CustomerID, FirstName, LastName, Street, City, ZipCode, Phone)
VALUES (1, ‘John’, ‘Zaki’, ‘ABC’, ‘Mansoura’, ‘35511’, ‘01000100100’);

INSERT MORE THAN ONE RECORD WITH PK


INSERT INTO Customers (CustomerID, FirstName, LastName, Street, City, ZipCode, Phone)
VALUES (1, ‘John’, ‘Zaki’, ‘ABC’, ‘Mansoura’, ‘35511’, ‘01000100100’),
VALUES(2, ‘Ahmed’, ‘Sherif’, ‘DEF’, ‘Cairo’, ‘51144’, ‘01111101111’),
VALUES(3, Amir, Haitham, XYZ, Ismailia, ‘33311’, ‘01222112222’);

INSERT MORE THAN ONE RECORD WITHOUT PK


INSERT INTO Customers (FirstName, LastName, Street, City, ZipCode, Phone)
VALUES ( ‘John’, ‘Zaki’, ‘ABC’, ‘Mansoura’, ‘35511’, ‘01000100100’),
VALUES( ‘Ahmed’, ‘Sherif’, ‘DEF’, ‘Cairo’, ‘51144’, ‘01111101111’),
VALUES( Amir, Haitham, XYZ, Ismailia, ‘33311’, ‘01222112222’);

Dr. JOHN ZAKI 29


SELECT

SELECT * SELECT *
FROM table_name; FROM Customers;

SELECT col1, col2,col3 SELECT CustomerID, FirstName, LastName


FROM table_name; FROM Customers;

SELECT col1, col2,col3 SELECT CustomerID, FirstName, LastName


FROM table_name FROM Customers
WHERE condition; WHERE FirstName= ‘john’ ;

Dr. JOHN ZAKI 30


UPDATE
UPDATE table_name
SET column1 = value1,
column2 = value2, ...

WHERE condition;
RETURNING *;

EX
UPDATE Customers
SET Firstname=‘Jo’
WHERE CustomerID=1;

IF YOU DON’T USE A WHERE CLAUSE, YOU WILL UPDATE ALL THE DATA IN
TABLE

Dr. JOHN ZAKI 31


DELETE

DELETE FROM table_name


WHERE condition;

DELETE FROM Customers


WHERE CustomerID=1;

If you don’t use a where condition, all the data in the table will be deleted.

Dr. JOHN ZAKI 32


SUMMARY

01 DBMS

02 ERD

03 SQL

Dr. JOHN ZAKI 33


THANK
YOU
Dr. JOHN ZAKI 34

You might also like