0% found this document useful (0 votes)
12 views40 pages

Week12 Database

The document provides an overview of relational database management systems (RDBMS) and SQL. It discusses the different views of data, data models including relational and entity-relationship models, and database concepts such as schema and constraints.

Uploaded by

Ninad
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)
12 views40 pages

Week12 Database

The document provides an overview of relational database management systems (RDBMS) and SQL. It discusses the different views of data, data models including relational and entity-relationship models, and database concepts such as schema and constraints.

Uploaded by

Ninad
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/ 40

`

Introduction to RDBMS &


SQL
View of Data
• One main purpose of a DBMS:
• Provide users with abstract view of data
• Hiding details of how data are stored and maintained
• Hiding the complexity from the naïve users

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.

A sample relational database

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

• The three levels are:

• 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

• The collection of information stored in the database at a particular moment is


called an instance of the database.

• The overall design of the database is called the database schema.

• Based on the levels of abstraction:


• Physical Schema – Database design at the physical level
• Logical schema – Database design at the logical level
• Subschemas – Different views of the database

8
Example of schemas

Department (dept_name, building, budget)


Student (ID, name, dept name, tot cred)
Instructor (id, name, dept_name, salary)
Advise (s id, i id)
Course (Course_id, title, dept_name, credits)

9
Example of Instance

• Information stored in the database at a


particular moment
• E.g. Set of instructor's information at a
particular time t
• Insertions, Updates, Deletions to this
data later, t+1
• new instance

10
Database Constraints

• Constraints are certain rules defined to limit the type of data


inserted into the table.

• It ensures the reliability and accuracy of data in the database.

• It can be applied either at the column level or at the table level.

• Defined either during the table creation or later with some database
commands.

11
Most Commonly used Constraints

• Unique Constraint

• Not null Constraint

• Default Constraint

• Check Constraint

12
Unique Constraint

• It’s a column level constraint.

• Ensures that all values in a column are different.

• It’s different than primary key.

13
Not Null Constraint

• It’s a column level constraint.

• Ensures that a column cannot have a NULL value.

14
Default Constraint

• It’s a column level constraint.

• Provides a default value for a column where no value


specified during insertion of record.

15
Check Constraint

• It’s a column level constraint.

• Ensures that all the values in a column satisfy certain


conditions.

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

BCA101 IV C/C++ 90 Ann

BCA103 II Maths 91 Helen

BCA101 III Computer 95 John

17
Need of Database Keys

• In today’s world, the data is rapidly increasing, GB to TB of data


generating per day.

• Challenge: Instantly store and retrieve the large set of data in


real-time.

• Database Keys: Ensures each record in a table is precisely


identified.

18
Database Keys
• Keys are fields in a database table which participate in below
activities in RDBMS systems:

▪ Establish relationships between multiple tables in the database.


▪ To maintain uniqueness in a table.
▪ To keep consistent and valid data in database.
▪ Might help in fast data retrieval by facilitating indexes on column(s).

“A Key is either a single column (or an attribute) or a group of


columns that can uniquely identify rows (or tuples) in a table.”

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

Table: Employee_ID Employee_Name Address License_Number Passport_Number


Emp101 John Mumbai MI490587 WBH874520995
Employee
Emp102 Riya New Delhi DL123456 SQV789112902
Emp103 Bob Chennai CH231458 EHN340008479

21
Criteria for a Candidate Key

• It must contain unique values.


• It cannot contain null values.
• It comprises a minimum number of fields necessary to define
uniqueness.
• Its values must uniquely and exclusively identify each record in
the table.
• Its value must exclusively identify the value of each field within a
given record.
• Its value can be modified only in rare or extreme cases.

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

• It must contain unique values.


• It cannot contain null values.
• It comprises a minimum number of fields necessary to define
uniqueness.
• Its values must uniquely and exclusively identify each record in the
table.
• Its value must exclusively identify the value of each field within a
given record.
• Its value can be modified only in rare or extreme cases.

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

Table: Employee_ID Employee_Name Address License_Number Passport_Number


Emp101 John Mumbai MI490587 WBH874520995
Employee
Emp102 Riya New Delhi DL123456 SQV789112902
Emp103 Bob Chennai CH231458 EHN340008479

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

Primary Key Candidate Keys Unique 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

• SQL - Structured Query Language : A relational database language.


• It is a standard programming language designed for Storing, Retrieving and
Manipulating the data that exist inside a RDBMS.

• It is a medium used to communicate to the DBMS.


• SQL commands consist of English like statements which are used to query,
insert, update and delete data.

• It is also referred to as “non-procedural database language”.

29
Features of SQL

• SQL is a language used to interact with the database.

• SQL is a data access language.

• SQL is based on relational tuple calculus.

• SQL is a standard Relational Database Management Language.

• The first commercial DBMS that supported SQL was Oracle in 1979.

• SQL is a “nonprocedural” or “declarative” language.

30
The SQL Environment

31
SQL Datatypes
Data Type Description

32
What can SQL do ?

• Execute queries against a database.

• Retrieve data from a database.

• Insert, Update, Delete records in a database.

• Create new databases.

• Create new tables, stored procedures, views in a database.

• Set permissions on tables, procedures, and views.

33
Types of SQL Commands

• DDL - Data Definition Language

• DML – Data Manipulation Language

• DCL – Data Control Language

• TCL - Transaction Control Language

34
DDL - Data Definition Language

• DDL commands can be used to define the database schema.


• DDL commands are :
• CREATE – Create an object. i.e. create a database, table, triggers, index,
functions, stored procedures etc.

• DROP - To delete objects. Eg: delete tables, delete a database, etc.


• ALTER - Used to alter the existing database or its object structures.
• TRUNCATE - Removes records from tables.

• RENAME - Renaming the database objects.


35
DML - Data Manipulation Language

• DML commands are used for performing queries on the


data within schema objects.
• DML commands are :
• SELECT – Select records or data from a table.
• INSERT INTO – Insert data into a database table.
• UPDATE – Update existing records within a table.

• DELETE – Delete unwanted records from a table.

36
DCL - Data Control Language

• DCL commands will control the Data access permission.


• DCL commands are :
• GRANT – It permits users to access the database.
• REVOKE – It withdraws the permission given by GRANT to
access the database.

37
TCL - Transaction Control Language

• TCL commands deals with the transaction within the database.


• TCL commands are :
• COMMIT – Commit the running transaction.
• ROLLBACK – Rollback the current transaction.
• SAVEPOINT – Set a save point so that, next time it will start from here.
• SET TRANSACTION – Specify the characteristics of the transactions.

38
Summary

• In this session, we discussed about an overview of Structured query language


(SQL).
• Evolution and benefits of SQL.
• A simplified schematic of a typical SQL Environment.
• Datatypes in SQL.

39
Reference & Image Courtesy

• Sumathi, Sai, and S. Esakkirajan. Fundamentals of relational


database management systems. Vol. 47. Springer, 2007.

• Modern Database Management, 10th Edition By Jeffrey A. Hoffer,


2011.

40

You might also like