Week12 Database
Week12 Database
Consider a C program:
• The view for the users is the
#include<stdio.h> executed printf and scanf options.
int main() { • The program has a logic behind
int a; this with a set of statements.
printf(“Enter a number”); • The physical aspect of this program
scanf(“%d”,&a); is with regard to its storage space
printf(“The entered number is %d”, a); requirements.
return 0;
}
2
Data Models
• Data model:
• A collection of conceptual tools for describing data, data
relationships, data semantics, and consistency constraints.
• The data models can be classified into four different categories:
• Relational Model
• Entity-Relationship Model
• Semi-structured Data Model
• Object-Based Data Model
3
Relational Model
• In the relational model, data are represented in the form
of tables.
• Each table has multiple columns, and each column has a
unique name.
• Each row of the table represents one piece of information.
4
Entity-Relationship Model
• Diagrammatic representation.
• Collection of objects called Entities.
• Relationship between the entities.
• Commonly used in database design.
• Example:
• Entity - instructor and Student
• Relationship - advisor
5
Three level Data Abstraction
• ANSI/SPARC Architecture
• Physical level
• Logical level
• View level
6
Three Levels of Data Abstraction
• Physical Level
• The lowest level of abstraction describes how the data are actually stored.
• The physical level describes complex low-level data structures in detail.
• Logical Level
• Describes what data are stored in the database, and what relationships exist among those data.
• Therefore, the entire database in terms of a small number of relatively simple structures.
• View Level
• The highest level of abstraction describes only part of the entire database.
• Simplifies the interaction between user and the system.
7
Instances And Schema
8
Example of schemas
9
Example of Instance
10
Database Constraints
• Defined either during the table creation or later with some database
commands.
11
Most Commonly used Constraints
• Unique Constraint
• Default Constraint
• Check Constraint
12
Unique Constraint
13
Not Null Constraint
14
Default Constraint
15
Check Constraint
16
Database Keys & Constraints
You can define this column
with Check Constraint You can define this
Primary Key column with Not
Unique Key NULL Constraint You can define
Table: Students this column with
Roll No Name Address Date of Birth Registration Foreign Key Default Constraint
No.
BCA101 Alan Mumbai 20-10-1989 6087452099 Table: Marks
Roll No Semester Subject Marks Faculty
BCA102 Dev Chennai 05-02-1990 9789112902
BCA102 II Maths 89 Helen
BCA103 Ziva Kolkata 27-05-1989 8340008479
BCA103 I English 94 Yami
17
Need of Database Keys
18
Database Keys
• Keys are fields in a database table which participate in below
activities in RDBMS systems:
19
Types of Keys
• Candidate Key
• Primary Key
• Unique Key
• Super Key
• Foreign Key
20
Candidate Key
• The minimal set of fields which can uniquely identify
each record in a table.
• It is an attribute or a set of attributes that can act as a
Primary Key for a table to uniquely identify each
record in that table.
• There can be more than one candidate key.
Candidate Keys
Employee Table
21
Criteria for a Candidate Key
22
Primary Key
• A primary key field identifies the table throughout the
database structure and helps establish relationships with
other tables.
• It’s a candidate key which is most appropriate to become
main key of any table.
• A primary key value uniquely identifies a given record within a
table and represents that record throughout the entire
database. It also helps to guard against duplicate records.
Primary Key
Employee Table
Table: Employee_ID Employee_Name Address License_Number Passport_Number
Employee Emp101 John Mumbai MI490587 WBH874520995
Emp102 Riya New Delhi DL123456 SQV789112902
Emp103 Bob Chennai CH231458 EHN340008479
23
Criteria for a Primary Key
24
Unique Key
• The set of one or more attribute that can be used
to uniquely identify the records in table.
• Unique key is similar to primary key but unique key
field can contain a “Null” value but primary key
doesn’t allow “Null” value.
Unique Keys
Employee Table
25
Super Key
• A set of one or more than one keys that can be used to uniquely
identify the record in table.
• It is a set of one or more attributes whose combined value uniquely
identifies the entity in the entity set.
• It is simply a non-minimal Candidate Key, that is to say one with
additional columns not strictly required to ensure uniqueness of
the row.
26
Super Key
Super Key
Employee Table
Employee_ID Employee_Name Address License_Number Passport_Number
Emp101 John Mumbai MI490587 WBH874520995
Emp102 Riya New Delhi DL123456 SQV789112902
Emp103 Bob Chennai CH231458 EHN340008479
27
Foreign Key
• It is used to generate the relationship between the tables.
• It is a field in database table that is Primary key in another table.
• It can accept null and duplicate value.
Foreign Key
Primary Key
Salary Table
Employee_ID Month_Year Amount
Emp102 Aug-2020 45000
Employee Table
Emp103 Aug-2020 50000
Employee Employee Address License_ Passport_
_ID _ Name Number Number Emp101 Aug-2020 38000
Emp101 John Mumbai MI490587 WBH874520995 Emp103 Sept-2020 50000
Emp102 Riya New Delhi DL123456 SQV789112902 Emp101 Sept-2020 38000
Emp103 Bob Chennai CH231458 EHN340008479 Emp102 Sept-2020 45000
28
SQL
29
Features of SQL
• The first commercial DBMS that supported SQL was Oracle in 1979.
30
The SQL Environment
31
SQL Datatypes
Data Type Description
32
What can SQL do ?
33
Types of SQL Commands
34
DDL - Data Definition Language
36
DCL - Data Control Language
37
TCL - Transaction Control Language
38
Summary
39
Reference & Image Courtesy
40