Uploaded by Saqib Riaz

26-Nov-2020 Thursday Chap 7-6 E R Diagrams [Autosaved]

advertisement
INTRODUCTION TO
COMPUTER
SCIENCE-I
SAQIB RIAZ
(Founder and CEO SRFoundation
Lecturer
ICT EDUCATOR, IT Trainer
Success Coach)
25-November-2020
Lecture NO 106
7
Chapter seven
Database Fundamentals
LESSON NO 7-6
Database Languages &
Database Terminologies
Your Date
Your Footer
2
DATABASE
LANGUAGES
Your Date
Your Footer
4
01
Database Language
•A DBMS has appropriate languages and interfaces to
express database queries and updates.
•Database languages can be used to read, store and
update the data in the database.
5
01
TYPES OF DATABASE
LANGUAGE
6
01
1.
2.
3.
4.
5.
6.
TYPES OF DATABASE LANGUAGE
DATA DEFINITION LANGUAGE (DDL)
DDL defines the statements to implement the database schema.
If a clear separation between a logical (conceptual) and a physical (internal) level is not there, then DDL
defines both the logical and physical schema and it also defines the mapping between logical and
physical schema.
If there is a clear separation between the logical and physical schema, then the storage definition
language (SDL) is used to define the physical schema.
But today, most of the relational DBMS do not use SDL for specifying the physical schema. Instead, the
physical schema is defined using the combination of functions and parameters which allows DBA to
map data to the storage.
After implementing logical and physical schema, it’s time to specify the view (external) schema. For
that view definition language (VDL) is used, which also maps the view schema to the logical schema.
But today in most of the DBMSs, DDL performs the role of VDL.
In all today, DDL specifies all the schemas, i.e. physical, Logical and view schema.
7
01
TYPES OF DATABASE LANGUAGE
DATA DEFINITION LANGUAGE (DDL)
The set of statements in DDL used to implement database schema are as follow:
1. CREATE: This command is used to construct a relation (table) in the database.
2. ALTER: This command is used to reconstruct the data in the database.
3. DROP: This command is used to delete a relation in the database or an entire
database.
4. TRUNCATE: This command deletes all the entries from the relation but keeps the
relation structure secured in the database.
5. RENAME: This command renames the relation in a database.
8
01
DATA
TYPES OF DATABASE LANGUAGE
MANIPULATION LANGUAGE (DML)
A language that offers a set of operations to support the fundamental data manipulation
operations on the data held in the database.
Data Manipulation Language (DML) statements are used to manage data within schema
objects
9
01
DATA
TYPES OF DATABASE LANGUAGE
MANIPULATION LANGUAGE (DML)
Here are the lists of tasks that come under DML:
1. SELECT - It retrieves data from a database
2. INSERT - It inserts data into a table
3. UPDATE - It updates existing data within a table
4. DELETE - It deletes all records from a table, the space for the records remain
5. MERGE - UPSERT operation (insert or update)
6. CALL - It calls a PL/SQL or Java subprogram
7. EXPLAIN PLAN - It explains the access path to data
8. LOCK TABLE - It controls concurrency
10
01
TYPES OF DATABASE LANGUAGE
DATA CONTROL LANGUAGE (DCL)
1. There are two other forms of database sub-languages. The Data Control Language
(DCL) is used to control privilege in Databases.
2. To perform any operation in the database, such as for creating tables, sequences, or
views, we need privileges.
3. Privileges are of two types,
a. System - creating a session, table, etc. are all types of system privilege.
b. Object - any command or query to work on tables comes under object privilege.
DCL is used to define two commands. These are:
c. Grant - It gives user access privileges to a database.
d. Revoke - It takes back permissions from the user.
11
DATABASE
TERMINOLOGIES
18/11/2020
Chapter 7-5
12
02
DATABASE TERMINOLOGIES
Table
1. A relational database is made up of several components, of which the table is most
significant.
2. The database table is where all the data in a database is stored, and without tables,
there would not be much use for relational databases.
3. A database consists of one or more tables.
4. Each table is made up of rows and columns.
5. If you think of a table as a grid, the column go from left to right across the grid and
each entry of data is listed down as a row.
13
02
DATABASE TERMINOLOGIES
Record
1. A record is a database entry that may contain one or more values.
2. Groups of records are stored in a table, which defines what types of data each
record may contain.
3. Databases may contain multiple tables which may each contain multiple records.
4. Records are often called rows since each new record creates a new row in the table.
Individual fields are sometimes called columns since they are the same for each
record within a table.
5. While the words "record" and "row" are often used interchangeably,
most DBMSes use the word "row" for database queries and error messages.
6. Records are an efficient way to store and access data. Since each record may contain
multiple data types, a single record may include many different types of information.
For example, a personnel record may contain an ID number, name, birthdate, and
14
photo, which are all different data types.
02
DATABASE TERMINOLOGIES
Field
1. A database field is a single piece of information from a record.
2. A database record is a set of fields.
3. .
15
02
DATABASE TERMINOLOGIES
Attributes
1. Just as business objects have characteristics that describe them, entities are
described by their attributes.
2. When we represent an entity in a database, what we actually store are that entity’s
attributes.
3. In a nutshell, attributes store data values that either
a. describe
b. identify entities.
16
02
DATABASE TERMINOLOGIES
Attributes
Attributes become fields in a table.
1. Attributes that describe a person (for instance, customer, employee, student, etc.)
would include such things as name, address, and telephone number.
2. Attributes that identify a person would include such things as social security number
or any combination of letters and numbers that uniquely identify a person.
3. Attributes that describe entities are called non-key attributes.
4. Attributes that identify entities (entity identifiers) are called key attributes.
17
02
DATABASE TERMINOLOGIES
Files
1. A file contains groups of records used to provide information for operations,
planning, management, and decision making.
2. The types of files used are discussed first, followed by a description of the many
ways conventional files can be organized.
18
02
DATABASE TERMINOLOGIES
Files
Files can be used for storing data for an indefinite period of time, or they can be used to store
data temporarily for a specific purpose.
Master files and table files are used to store data for a long period.
The temporary files are usually called transaction files, work files, or report files. Master Files
Master files contain records for a group of entities.
The attributes may be updated often, but the records themselves are relatively permanent.
These files tend to have large records containing all the information about a data entity.
Each record usually contains a primary key and several secondary keys.
Although the analyst is free to arrange the data elements in a master file in any order, a
standard arrangement is to place the primary key field first, followed by descriptive elements,
and finally by elements that change frequently with business activities.
Examples of a master file include patient records, customer records, a personnel file, and a parts
inventory file.
19
02
DATABASE TERMINOLOGIES
View
1. A database view is a searchable object in a database that is defined by a query.
2. Though a view doesn’t store data, some refer to a views as “virtual tables,” you can
query a view like you can a table.
3. A view can combine data from two or more table, using joins, and also just contain
a subset of information.
4. This makes them convenient to abstract, or hide, complicated queries.
5. Benefits of a Database View
1. Enforce Business Rules
2. Consistency
3. Security
4. Simplicity
5. Space
20
02
1.
2.
3.
4.
5.
6.
7.
DATABASE TERMINOLOGIES
Data Types
A database data type refers to the format of data storage that can hold a distinct type or range of
values.
When computer programs store data in variables, each variable must be designated a distinct data
type.
Some common data types are as follows: integers, characters, strings, floating point numbers and
arrays.
More specific data types are as follows: varchar (variable character) formats, Boolean values, dates
and timestamps.
There are programming languages that require the programmer to determine the data type of a
variable before attaching a value to it.
While some programming languages can automatically attach a data type to a variable based on the
initial data assigned to the variable.
For example, a variable is assigned with the value “3.75”, then the data type that will be attached to
the variable is floating point.
21
02
DATABASE TERMINOLOGIES
Data Types
1.
2.
Most of the programming languages enable each variable to store only a single data type.
For example, if the data type attached to the variable is integer, when you assign a string data to the
variable, the string data will be converted to an integer format.
3.
4.
5.
6.
Database applications use data types.
Database fields require distinct type of data to be entered.
For example, school record for a student may use a string data type for student’s first and last name.
The student’s date of birth would be stored in a date format and the student’s GPA can be stored as
decimal.
By ensuring that the data types are consistent across multiple records, database applications can
easily perform calculations, comparisons, searching and sorting of fields in different records.
7.
22
02
1.
2.
3.
4.
DATABASE TERMINOLOGIES
Data Types
Integer – is a whole number that can have a positive, negative or zero value. It cannot be a fraction
nor can have decimal places. It is commonly used in programming especially for increasing
values. Addition, subtraction and multiplication of two integers results to an integer. But division of
two integers may result to an integer or a decimal. The resulting decimal can be rounded off or
truncated to produce an integer.
Character – refers to any number, letter, space or symbol that can be entered in a computer. Each
character occupies one byte of space.
String – is used to represent text. It is composed of a set of characters that can have spaces and
numbers. Strings are enclosed in quotation marks to identify the data as string and not a variable
name nor a number.
Floating Point Number – is a number that contains decimals. Numbers that contain fractions are also
considered as floating point numbers.
23
02
DATABASE TERMINOLOGIES
Data Types
Array – contains a group of elements which can be of the same data type like an integer or string. It is
used to organise data for easier sorting and searching of related set of values.
Varchar – as the name implies is variable character as the memory storage has variable length. Each
character occupies one byte of space plus 2 bytes for length information.Note: Use Character for data
entries with fixed length, like phone number. Use Varchar for data entries with variable length, like
address.
Boolean – is used for creating true or false statements. To compare values the following operators are
being used: AND, OR, XOR, and NOT.
24
KEYS & TYPES
18/11/2020
Chapter 7-5
25
03
DATABASE TERMINOLOGIES
Database Keys
What are Keys in DBMS?
1.
2.
3.
4.
5.
KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a
relation(table).
They allow you to find the relation between two tables.
Keys help you uniquely identify a row in a table by a combination of one or more columns in that
table.
Key is also helpful for finding unique record or row from the table.
Database key is also helpful for finding unique record or row from the table.
26
03
DATABASE TERMINOLOGIES
Database Keys
Why we need a Key?
1.
2.
3.
4.
5.
6.
Here are some reasons for using sql key in the DBMS system.
Keys help you to identify any row of data in a table. In a real-world application, a table could contain
thousands of records.
Moreover, the records could be duplicated.
Keys ensure that you can uniquely identify a table record despite these challenges.
Allows you to establish a relationship between and identify the relation between tables
Help you to enforce identity and integrity in the relationship.
27
03
DATABASE TERMINOLOGIES
Database Keys
Types of Keys in Database Management System
There are mainly seven different types of Keys in DBMS and each key has it’s different functionality:
1. Super Key - A super key is a group of single or multiple keys which identifies rows in a table.
2. Primary Key - is a column or group of columns in a table that uniquely identify every row in that
table.
3. Candidate Key - is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super
key with no repeated attributes.
4. Alternate Key - is a column or group of columns in a table that uniquely identify every row in that
table.
5. Foreign Key - is a column that creates a relationship between two tables. The purpose of Foreign
keys is to maintain data integrity and allow navigation between two different instances of an entity.
6. Compound Key - has two or more attributes that allow you to uniquely recognize a specific record. It
is possible that each column may not be unique by itself within the database.
7. Composite Key - An artificial key which aims to uniquely identify each record is called a surrogate
key. These kind of key are unique because they are created when you don't have any natural primary
28
key.
03
DATABASE TERMINOLOGIES
Database Keys
Types of Keys in Database Management System
There are mainly seven different types of Keys in DBMS and each key has it’s different functionality:
1. Super Key - A super key is a group of single or multiple keys which identifies rows in a table.
2. Primary Key - is a column or group of columns in a table that uniquely identify every row in that
table.
3. Candidate Key - is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super
key with no repeated attributes.
4. Alternate Key - is a column or group of columns in a table that uniquely identify every row in that
table.
5. Foreign Key - is a column that creates a relationship between two tables. The purpose of Foreign
keys is to maintain data integrity and allow navigation between two different instances of an entity.
6. Compound Key - has two or more attributes that allow you to uniquely recognize a specific record. It
is possible that each column may not be unique by itself within the database.
7. Composite Key - An artificial key which aims to uniquely identify each record is called a surrogate
key. These kind of key are unique because they are created when you don't have any natural primary
29
key.
03
DATABASE TERMINOLOGIES
Database Keys
1.
2.
3.
What is the Super key?
A superkey is a group of single or multiple keys which identifies rows in a table.
A Super key may have additional attributes that are not needed for unique identification.
Example:
4.
EmpSSN and EmpNum name are superkeys.
30
03
1.
2.
3.
4.
DATABASE TERMINOLOGIES
Database Keys
What is a Primary Key?
PRIMARY KEY is a column or group of columns in a table that uniquely identify every row in that
table.
The Primary Key can't be a duplicate meaning the same value can't appear more than once in the
table.
A table cannot have more than one primary key.
Rules for defining Primary key:
a. Two rows can't have the same primary key value
b. It must for every row to have a primary key value.
c. The primary key field cannot be null.
d. The value in a primary key column can never be modified or updated if any foreign key refers to
that primary key.
31
03
DATABASE TERMINOLOGIES
Database Keys
What is a Primary Key?
1.
Examples
32
03
1.
2.
3.
4.
DATABASE TERMINOLOGIES
Database Keys
What is the Alternate key?
ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that
table.
A table can have multiple choices for a primary key but only one can be set as the primary key.
All the keys which are not primary key are called an Alternate Key.
Example:
In this table, StudID, Roll No, Email are qualified to become a primary key. But since StudID is the
primary key, Roll No, Email becomes the alternative key.
33
03
1.
2.
3.
4.
5.
DATABASE TERMINOLOGIES
Database Keys
What is a Candidate Key?
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super
key with no repeated attributes.
The Primary key should be selected from the candidate keys.
Every table must have at least a single candidate key.
A table can have multiple candidate keys but only a single primary key.
Properties of Candidate key:
a. It must contain unique values
b. Candidate key may have multiple attributes
c. Must not contain null values
d. It should contain minimum fields to ensure uniqueness
e. Uniquely identify each record in a table
34
03
DATABASE TERMINOLOGIES
Database Keys
What is a Candidate Key?
35
03
1.
2.
3.
DATABASE TERMINOLOGIES
Database Keys
What is the Foreign key?
FOREIGN KEY is a column that creates a relationship between two tables.
The purpose of Foreign keys is to maintain data integrity and allow navigation between two different
instances of an entity.
It acts as a cross-reference between two tables as it references the primary key of another table.
36
03
DATABASE TERMINOLOGIES
Database Keys
What is the Foreign key?
37
03
2.
3.
4.
5.
DATABASE TERMINOLOGIES
Database Keys
1. What is the Compound key?
COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record.
It is possible that each column may not be unique by itself within the database.
However, when combined with the other column or columns the combination of composite keys
become unique.
The purpose of the compound key in database is to uniquely identify each record in the table.
38
03
1.
2.
3.
4.
DATABASE TERMINOLOGIES
Database Keys
What is the Composite key?
COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table.
The combination of columns guarantees uniqueness, though individually uniqueness is not
guaranteed.
Hence, they are combined to uniquely identify records in a table.
The difference between compound and the composite key is that any part of the compound key can
be a foreign key, but the composite key may or maybe not a part of the foreign key
39
03
DATABASE TERMINOLOGIES
Database Keys
40
03
DATABASE TERMINOLOGIES
Database Keys
Summary
1. A key in SQL is an attribute or set of attributes which helps you to identify a row(tuple) in a
relation(table)
2. DBMS keys allow you to establish a relationship between and identify the relation between tables
3. Seven Types of DBMS keys are Super, Primary, Candidate, Alternate, Foreign, Compound, Composite,
and Surrogate Key.
4. A super key is a group of single or multiple keys which identifies rows in a table.
5. A column or group of columns in a table which helps us to uniquely identifies every row in that table
is called a primary key
6. All the keys which are not primary key are called an alternate key
7. A super key with no repeated attribute is called candidate key
8. A compound key is a key which has many fields which allow you to uniquely recognize a specific
record
9. A key which has multiple attributes to uniquely identify rows in a table is called a composite key
10. An artificial key which aims to uniquely identify each record is called a surrogate key
41
11. Primary Key never accept null values while a foreign key may accept multiple null values.
06
Homework
(a) Briefly explain
(a) DDL
(b) DML
(c) DCL
(d) Advantages of Views
(e) Data01Types used 02
in Database 03
Deadline: 29-11-20
Submit your assignments at
saqibrk2020@gmail.com
Next Topic
1. Database Planning
43
Thank You
@ SaqibRKyani
@saqibrkyani
@saqibrkyani
SR Foundation
Download