DATA MANAGEMENT
RELATIONAL MODEL
At the end of the lesson, Students should be able to:
Explain how data is represented
Explain how data can be created and modified
Explain how data can be manipulated and queried
Explain how to obtain a database design in relational Model.
RELATIONAL MODEL
CREATING AND MODIFYING RELATION USING SQL
The relational model for database management is a database model base on first – order predicate logic, first
formulated and proposed in 1969 by E.F Codd.
The purpose of the relational model is to provide a declarative method for specifying data and queries; users
directly state what information they want from it, and let the database management system software (DBMS)
take care of describing data structures for storing the data and retrieval procedure for answering queries.
Example of DBMS based on Codd’s ideas, includes;
1. IBMs DB2
2. Oracle
3. Microsoft SQL server
4. Postgre SQL
5. My SQL, etc.
SQL
SQL (Structure Query language) is a programming language designed for managing data in relational
database managing systems (RDBMS).
Originally based upon relational algebra and tuple relational calculus, its scope includes;
A. Database creation and modification
B. Query
C. Update and delete
D. Data insert
E. Data access control, etc.
Examples of SQL Data Manipulation language (DML)
1. SELECT:- Extracts data from a database.
2. UPDATE:- Update data in a database.
3. DELETE: - Deletes data from a database.
4. INSERT INTO:- Inserts new data into a database.
Examples of SQL Data Definition Language (DDL)
1. CREATE DATABASE:- Creates a new database
2. ALTER DATABASE :- Modifies a database
3. CREATE TABLE:- Create a new table
4. ALTER TABLE:- Modifies a table
5. CREATE INDEX:- Creates index (search key)
6. DROP INDEX :- Deletes an index
The CREATE DATABASE statement
SYNTAX:
CREATE DATABASE database_name
Example:
CREATE DATABASE Joshua _db
The CREATE TABLE statement
SYNTAX:
CREATE TABLE table_name (
Column_name 1 data type,
Column_name 2 data type,
Column_name 3 data type,
….
Column_Name N data type,
PRIMARY KEY (one or more columns));
NOTE: The data type specifies what type of data the column can hold.
Example
CREATE TABLE persons
(
P_ID int,
Last Name varchar (255),
First Name varchar (255),
Address varchar (255)
City varchar (255)
)
The P_ID column is of type int and will hold a number, other will hold type varchar with a maximum
length of length of 255 characters.
The empty “person” table will now look like this:
P_ID Last Name First Name Address City
The empty table can be filled with data with INSERT INTO statement.
INTEGRITY CONSTRAINTS OVER RELATION
Constraints are used to limit the type of data that can go into a table.
Integrity Constraints are used to ensure accuracy and consistency of data in a relational primary key
or database.
Data integrity is handled in relational database through the concept of referential integrity.
TYPES OF INTEGRITY CONSTRAINT
1. Primary key constraints: is the term used to identify one or more column in a table that makes a row
of data unique.
The objective is for every record to have a unique primary key or value for the employee’s
identification number. The primary key is assigned at table creation.
2. Foreign key constraints: A foreign key is a column in a child table that references a primary key in the
parent table.
A foreign key is a column in a child table that references a primary key in the parent table.
A foreign key constraint is the main mechanism used to enforce referential integrity between tables in a
relational database.
3. NOT NULL constraints: It is a constraint that you can place on a table’s column. This constraint
disallows the entrance of NULL values into a column.
4. Check Constraints: Check (CHK) constraints can be utilized to check the validity of data entered into
particular table columns. They are used to provide back-end database edits and front end application as
well.
DROPPING CONSTRAINT
Any constraint that you have defined can be dropped using the ALTER TABLE command with the
DROP CONSTRAINT option.
OTHER TYPES OF CONSTRAINT
5. Unique constraint.
6. Default constraint.
ENFORCING INTEGRITY CONSTRAINTS
1. Primary Key Constraint
CREATE TABLE persons
(
P_ID int , NOT NULL PRIMARY KEY,
Last Name varchar (255) NOT NULL,
First Name varchar (255),
Address varchar (255),
)
To create a primary Key Constraint on the “P_ID” Column when the table is already created, use the
following SQL statements.
ALTER TABLE persons
ADD PRIMARY KEY (P_ID)
2. Foreign Key Constraint
Let’s illustrate the foreign key with example. Look at the following table;
Persons Table
P_ID LastName FirstName Address City
1 JOY GIFT 10 LOVAS Sandnes
2 ADE BOLE 20 Simson Sandnes
3 HANSEN TOVE 18 Avena Stavanber
Orders Table
O_ID OrderNo P_ID
1 77875 3
2 44678 3
3 22456 2
4 24562 1
NOTE
“P_ID” column in the ‘Order table points to the “P_ID” column in the “person table”.
The “P_ID” column in the “person table” is the PRIMARY KEY in the “Persons table”.
The “P_ID” column in the “Orders table” is a FOREIGN KEY in the “Orders table”.
The Foreign Key Constraint is used to link two tables together, it also prevent action that would destroy
links between tables
CREATE TABLE Orders
(
O_ID int , NOT NULL PRIMARY KEY,
OrderNo int , NOT NULL ,
P_ID int , FOREIGN KEY REFERENCES Persons (P_ID)
)
To create a foreign key constraint on the “ P_ID” column when the “orders” table is already created, use the
following SQL statement.
ALTER TABLE Orders
ADD FOREIGN KEY (P_ID)
REFERENCES Persons (P_ID)
3. TO DROP A FOREIGN KEY Constraint
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
QUERYING RELATIONAL DATA SQL STATEMENTS
1. SELECT Statement
A. SELECT * FROM Persons
The statement will select all the records in the “persons table”
B. SELECT Last Name, First Name,
FROM Person
The statement will only select all the Last Name and First Name from the “Person table
C. SELECT Last Name, First Name, City
From person
WHERE City = Sandness
The statement will select last name, first name and city of record which leave in the city “Sandness
2. UPDATE Statement
UPDATE Persons
SET Address = ‘11 Lovas’, City = ‘Miamo’
WHERE Lastname = ‘JOY’ AND FirstName = ‘GIFT’
The statement update the Address and City of JOY GIFT.
3. DELETE Statement
DELETE FROM person
Where Last Name = ‘ADE AND First Name = ‘BOLE’
The statement will delete the record of ADE BOLE
4. ORDER BY statement
This SQL statement is used to sort or order a column or field of a table either in ascending or descending
order.
A. SELECT * FROM Persons
ORDER BY LastName DESC
The statement selects all last name from the “person table” and sort them in descending order (Z-A).
B. SELECT * FROM Persons
ORDER BY Last Name ASC
The statement selects all last names from the “person table” and sorts them in ascending order (A-Z).