Database Terms

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1) Database terms

Flat File & Relational Database


Records & Fields
Data Types
Entity
Primary Key
Relationship Types
Candidate key/Primary Key
Advantages & Drawbacks of relational database
Foreign Key
2) Normalization
3) SQL
i) DDL
ii) DML

Data is always stored in the form of tables.

IDs Names Phone Number


1 Haseeb (Entity) +927621378213
2
3
4 Haseeb
5
6
7 Haseeb

Vertical: Columns/Fields/Attributes = 2

Horizontal: Rows/Records/Tuples = 7 (Excluding the first one, as first row always represents the field
names)

Data Types:

1) STRING
Alphanumeric…alphanumeric+symbols
Haseeb…haseeb123…haseeb123@gmail.com, IDS, Serial Numbers
IDS, Serial Numbers (Non-calculative)
VARCHAR(10)
2) INTEGER
Whole number
3) REAL
Fractional part
4) BOOLEAN
Two states (BIT)
5) DATE
Any date
6) TIME
Any time
Primary Key: (KEY…FIELD) (Serial Numbers/IDs)
A field in a table that is unique and can be used to distinguish in between different records of
the table.
NOTE : All the entities in a relational data base are linked with each other.
Relationship Types:
1) One to one
2) One to Many
3) Many to One
4) Many to Many

Candidate Key:

1) Primary Key
Unique/No repetitions
2) Candidate Key
Could have been selected as a primary key, wasn’t unique but is still able to search through the
table. Example: People use names to search.

We need understand the FOREIGN KEY…

Teachers: (All the tables must have a primary key)

Teacher ID (PK) Name Subject


1 Hasan ISL
2 Husain PST
3 Haseeb CS

Students:

Students ID (PK) Name Subjects Teacher ID (FK)


1 Ali CS 3
2 Usama CS 3
3 Ibrahim ISL 1
4 Hamza PST 2
5 Umer ISL 1
6 Arsal PST 2

One to many relationship in between teachers & Students table

Foreign Key: A key in a table that is used to interlink the tables or it is connected to the primary key of
another table.

NOTE: Relationship is always from Primary to Foreign Key.


Normalization:

Process of storing the data in an organized way based on at least two tables, interlinked with each other,
i.e., they must have a relationship in between them to reduce data repetition.

1st Normal Form:

1) Primary Key
2) No Groups
3) Reduced data repetition

2nd Normal Form:

1) 1st Normal
2) No Partial Dependencies
When a field in a table isn’t dependent on primary key, but is dependent on composite key.
Composite Key: Primary key made up of two fields is a composite key.

3rd Normal Form:

1) 2nd Normal Form


2) No Transitive Dependencies
When a field in a table isn’t dependent on primary key, but is dependent on a local
attribute/field in a table.

1st Normal Form:

1) Primary Key
2) No Groups
3) Reduced data repetition

Teachers Table Shape 1:

Teacher ID Teacher Name Subject


1 Hasan ISL
2 Husain ISL,PST
3 Haseeb ICT,CS

To be in its 1st normal form there should be no data groups.


Let’s remove the groups!

Teachers Table Shape 2:


Teacher ID Teacher Name Subject
1 Hasan ISL
2 Husain-1 ISL
3 Husain-2 PST
4 Haseeb-1 ICT
5 Haseeb-2 CS
To be in its 1st normal form there should be least data repetition.

Teachers Table Shape 3:


Teacher ID Teacher Name
1 Hasan
2 Husain
3 Haseeb

Creating new table for Subjects:


Subjects Table:
Subject ID Subjects Teacher ID
1 ISL-Hasan 1
2 ISL-Husain 2
3 PST 2
4 CS 3
5 ICT 3

Have you successfully developed a one to many relationship. YES..!

2nd Normal Form:

1) 1st Normal
2) No Partial Dependencies
When a field in a table isn’t dependent on primary key, but is dependent on composite key.
Composite Key: Primary key made up of two fields is a composite key.

Score Table:
Score ID Subject ID Student ID Marks Teacher
1 10 1 81 Haseeb
2 10 2 91 Husain
3 11 1 87 Hasan
4 11 2 56 Haseeb
5 11 1 43 Hamza
6 11 2 31 Husain

Subject ID + Student ID = Composite Key….Partial Dependency.


New Score ID:
Score ID Teacher
1 Haseeb
2 Husain
3 Hasan
4 Haseeb
5 Hamza
6 Husain

Marks Table:
Marks ID Marks Subject ID Student ID
1 81 10 1
2 91 10 2
3 87 11 1
4 56 11 2
5 43 11 1
6 31 11 2

3rd Normal Form:

1) 2nd Normal Form


2) No Transitive Dependencies
When a field in a table isn’t dependent on primary key, but is dependent on a local
attribute/field in a table.

Exam Table:
Exam ID Exam Type Marks
1 Assessment 20
2 Class Test 10
3 MOCKS 100
4 Viva 40
New Exam Table:
Exam ID Exam Type
1 Assessment
2 Class Test
3 MOCKS
4 Viva
Marks Table:
Marks ID Marks Exam ID
1 20 1
2 10 2
3 100 3
4 40 4

A2: Code Compilation/BNF

AS: Databases Terms & Chapter 7 (Security 😊)


SQL:
Structured Query Language:
1) DDL (Data Definition Language)
Develop Tables/Set Data Types/ Set Primary Key
ADD or DEL Fields/Records
2) DML (Data Manipulation Language)
Construct Queries/Searches
ADD/SUB/AVG

Example 1: Database : Information


Serial Name Address Salary Gender(M Age Job Type
Number or F)

CREATE DATABASE Information


CREATE TABLE Employee(
SerialNumber VARCHAR(5),
Name VARCHAR(20),
Address VARCHAR(30),
Salary REAL,
Gender BOOLEAN,
Age INTEGER,
PRIMARY KEY (SerialNumber));

ALTER TABLE Employee


ADD PRIMARY KEY(SerialNumber);

ALTER TABLE Employee


ADD JobType VARCHAR(12);

You might also like