0% found this document useful (0 votes)
12 views

8 - Databases

Uploaded by

Aayan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

8 - Databases

Uploaded by

Aayan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Databases

Databases:
It is a collection of data which is stored in an organized way.
Relation Database:
Database in which data items are linked to each other by pointers.
Benefits of a database approach:
Items are stored once so storage space isn’t wasted.
Data is consistent as when it is altered in one application it is also edited in the other.
Data is independent as the enquires are not dependent on the structure of data.
Entity:
Something about which data is stored.
Table:
It is a group of similar data consisting of rows and columns. The rows store instances of the entities while
the columns store the attributes.
Record:
It is a row in a table.
Field:
It is a column in a table.
Tuple:
It is the instance of an entity which is stored in a row in a table.
Attribute:
An individual data item stored for an entity.
Primary key:
it is used to uniquely identify a record in a table. For example, the student roll number is a primary key in
a table of STUDENTS.
Composite key:
When 2 or more attributes are used as a primary key. For example, in an attendance relation, the date,
time and registration# will serve as the composite key.
Foreign key:
It is a reference to the primary key of another table. Its purpose is to create a relation between 2 tables.
For example, in a fee relation, the registration number will be the foreign key.

1
Databases
Secondary/ Alternate key:
It is used to access data in a relation without the primary key. Indexing is the setting up of secondary
keys.
Candidate key:
It is used to serve as a primary key. It is unique to each element. All attributes are primary keys.

However, most tables have candidate keys which serve as the primary key.

Types of Databases:
Flat File

 All records are stored in one place.


 It is easy to setup via standard office applications.
 It is easy to understand.
 Simple sorting of records can be carried out.
 Records can be viewed or extracted on the basis of simple criteria.
Relational

 The files are interlinked with each other which reduces the repetition of data. This results in less
size of the database.
 Data is consistent as when changes are made to one file, all the instances of the data are updated.
 When validation constraints are modified, it is applied across the whole database. Therefore, it
has high data integrity.
 Complex queries can be easily written to find specific data.
 Different users can be given different access/ viewership rights which maintains the privacy and
security of the data.

Relationships in databases:

2
Databases
A relationship in a database is formed when one table has a foreign key which refers to the primary key of
another table in the same database.
Take these 2 tables as an example. In the table STUDENT, the Class ID field is the foreign key which
refers to the Class ID field in the CLASS table, which is a primary key.

STUDENT table

CLASS table

There are 4 types of relationships:


One to One
One to Many
Many to One
Many to Many
The symbols of relationships are as follows:

Entity- Relationship (E-R) Diagram:


It is used to document the design of a database.

3
Databases

Dependency:
It occurs in a database when information in the table and an attribute uniquely determines other
information in the table. For example, Attribute A is dependent on attribute B.
Types of Dependency:
1. Full Dependency
 When one or more primary keys determine another attribute.
 STUDENT (Roll#, Surname, Subject)
2. Partial Dependency
 When the key is composite but not all of the columns of the key determine another
attribute.
 COURSE(Coursecode, Roll#, Coursename, Studentname)
3. Transitive Dependency
 When non- key attribute determines another attribute.
 COUNTRY(Code, City, Countryname, Population)
Normalization:
It is used to remove the redundancy of data. It uses full dependency only.
Types of Normalization:
1. First Normal Form (1NF)
 All fields/ attributes must have a single value.
 Remove all repeating groups.
 All tables/ relations must have a primary key.
2. Second Normal Form (2NF)
 Database must be in 1NF.
 Every non- key attribute must be fully dependent on the primary key.
 Remove the partial dependencies.
 Non key attributes must be partially dependent on the primary key.
 Remove transitive dependencies.
3. Third Normal Form (3NF)
 Database must be in 2NF.

4
Databases
 All non- key attributes must be fully dependent on the primary key.
 Remove partial dependency.
Database Management System (DBMS)
Data Redundancy:
Refers to the presence of the same data within the same table.
Solution to Data Redundancy:

 Storing the data in separate linked tables.


 This reduces the repetition of data since the data items are stored once.
Data Inconsistency:

 Data items will be stored once which will help improve data integrity.
Data Dependency issue:

 Data is independent of the apps in the database.


 Changes to the data will be managed by the DBMS.
 Information from a database is made available more easily.
DBMS Approach to Management

 Pre-defined data structure.


 Allows to create and set a new database.
 Addition, deletion and editing of data is all done by the DBMS.
Data Dictionary:

 Stores the definition of tables, attributes and relationship between tables in a database.
 Also allows to define the validation rules.
 Improves the integrity of the data stored.
Data Modelling:

 Shows the structure of a database.


Security features of DBMS:

 Using usernames and passwords.


 Using access rights to prevent editing to the files.
 Automatically schedule backup of data.
 Encryption of the stored data.
Developer Interface:

 Allows developer to write commands in Standard Query Language (SQL).


 These are then processed and executed by the Query processor.
Query Processor:

5
Databases
 It processes and executes the instructions written in SQL.
 Includes the DDL interpreter and DML compiler.
 These are then recorded in the database data dictionary.
Name and describe 2 levels of the schema of a database.
External:
How the individuals view the data.
Physical:
How data is stored on media.88
DDL (Creation) Commands

SQL (DDL) command Description Examples


CREATE DATABASE Creates a database. CREATE DATABASE
databasename;
CREATE TABLE Creates a table within a database. CREATE TABLE tablename
(column1 datatype, column2
datatype);
ALTER TABLE Allows to amend the table. ALTER TABLE tablename
ADD column_name datatype;
PRIMARY KEY Adds a primary key to the table. Adding a primary key when
creating a table:
CREATE TABLE
tablename(…..
PRIMARY
KEY(headingname));
Adding primary key in an
existing table:
ALTER TABLE tablename
ADD PRIMARY
KEY(headingname);
FOREIGN KEY Adds a foreign key to the table. Adding foreign on Create
Table:
CREATE TABLE
tablename(… PRIMARY KEY
(headingname), FOREIGN
KEY(foreignkeyname field)
REFERENCES
tablename(referencename));

Foreign Key on Alter Table:


ALTER TABLE Orders
ADD FOREIGN KEY
(foreignkeyname)
REFERENCES
referencetablename(field)

6
Databases

PRODUCTS PRODUCTS PRODUCTS


Product_ID Product_name Price($)
PI1 Fridge 500
PI2 Air Conditioner 250
PI3 Dispenser 100
PI4 Television 600

ORDERS ORDERS ORDERS ORDERS


CustomerID Customername OrderID Product_name

SQL (DML) Description Examples


Commands
SELECT Select and From are interlinked. Select SELECT: Product_ID,
FROM includes the names of the fields. Product_name, Price
From includes the name of the table. FROM: Products
WHERE Includes the location of the data in the WHERE: Price>10 AND Price<20
table.
ORDER BY Sort the data in the table in ascending or ORDER BY Price (DESC/ASC
descending. ASC sorts in ascending order. depending and optional)
DSC Sorts in descending order.
GROUP BY Arranges data into groups. GROUP BY Product_name;
INNER JOIN Used to create a relationship between 2 INNER JOIN Orders on Product
tables if the join condition is true. WHERE Products. Product_name=
Orders. Product_name AND
(Condition)
SUM Gives the sum of all the values by adding SUM (Price)
them together.
COUNT Used to count. COUNT (Product_name)
AVG Used to calculate the average of numbers. AVG (Price)
INSERT INTO Adds new row(s) at the end of the table. INSERT INTO Products (Products)
VALUES (‘Hand Blender’)
DELETE FROM Removes row(s) from a table. DELETE FROM Products
WHERE Product_name=
‘Dispenser'
UPDATE Edit row(s) in a table. UPDATE Products
SET Product_name= ‘Fridge’

7
Databases
WHERE Product_ID= PI1

Past Papers:
Tasks that can be performed using DBMS developer interface. [3]

 Create table.
 Create relations.
 Create forms, reports and queries.
Reasons why a database is fully normalized:
It has no repeating groups (1NF).
It has no partial dependencies. (2NF).
It has no transitive dependencies. (3NF).
Example of Insert Into:
INSERT INTO SHOP-SUPPLIER
(ShopID, SupplierID)
VALUES (8765, ‘SUP89’);
What is meant by referential integrity? [3]
Makes sure that the unexisting data doesn’t get referenced.
Primary keys can’t be deleted without the deletion of all dependent records.
Primary keys can’t be updated without the updating of all dependent records.

You might also like