Uploaded by Dwayne Goffe

Data Modelling

advertisement
Database Management Systems II
•
Data Modelling
• Constraints
DBMS 1, Spring 2019
1
WHAT IS DATA MODELLING?
DBMS 1, Spring 2019
2
Data Modeling
Data modeling is the process of creating a data
model for the data to be stored in a Database. This
data model is a conceptual representation of:
• Data objects
• The associations between different data objects
• The rules.
DBMS 1, Spring 2019
3
Data Modeling
Data modeling helps in the visual representation of
data and enforces business rules, regulatory
compliances, and government policies on the data.
Data Models ensure consistency in naming
conventions, default values, semantics, security while
ensuring quality of the data.
DBMS 1, Spring 2019
4
Data Modeling
Data model emphasizes on what data is needed and
how it should be organized instead of what
operations need to be performed on the data. Data
Model is like architect's building plan which helps to
build a conceptual model and set the relationship
between data items.
DBMS 1, Spring 2019
5
Data Modeling
The two types of Data Models techniques are:
• Entity Relationship (E-R) Model
• UML (Unified Modelling Language)
DBMS 1, Spring 2019
6
Data Modeling
Goals of Data Modeling
• Ensures that all data objects required by the database
are accurately represented.
• A data model helps design the database at the
conceptual, physical and logical levels.
• Data Model structure helps to define the relational
tables, primary and foreign keys and stored procedures.
DBMS 1, Spring 2019
7
Data Modeling
Goals of Data Modeling (Cont’d)
• It provides a clear picture of the base data and can be
used by database developers to create a physical
database.
• It is also helpful to identify missing and redundant data.
• It makes your IT infrastructure upgrade and
maintenance cheaper and faster.
DBMS 1, Spring 2019
8
Data Modeling
TYPES OF DATA MODELS
There are mainly three different types of data
models:
• CONCEPTUAL – The “What”
• LOGICAL – The “How”
• PHYSICAL – The “How with specific DBMS”
DBMS 1, Spring 2019
9
Data Modeling
THE CONCEPTUAL MODEL
The main aim of this model is to establish the
entities, their attributes, and their relationships. In
this Data modeling level, there is hardly any detail
available of the actual Database structure.
DBMS 1, Spring 2019
10
Data Modeling
THE CONCEPTUAL MODEL (Cont’d)
DBMS 1, Spring 2019
11
Data Modeling
CHARACTERISTICS OF THE CONCEPTUAL MODEL
• Offers Organization-wide coverage of the business
concepts.
• Designed and developed for a business audience.
• Developed independently of hardware specifications
like data storage capacity, location or software
DBMS 1, Spring 2019
12
Data Modeling
LOGICAL DATA MODEL
Logical data models add further information to the
conceptual model elements. It defines the structure of
the data elements and set the relationships between
them.
DBMS 1, Spring 2019
13
Data Modeling
LOGICAL DATA MODEL (Cont’d)
The advantage of the Logical data model is to provide
a foundation to form the base for the Physical model.
However, the modeling structure remains generic.
At this Data Modeling level, no primary or secondary
key is defined. At this Data modeling level, you need
to verify and adjust the connector details that were
set earlier for relationships.
DBMS 1, Spring 2019
14
Data Modeling
LOGICAL DATA MODEL (Cont’d)
DBMS 1, Spring 2019
15
Data Modeling
CHARACTERISTICS OF LOGICAL DATA MODEL
• Describes data needs for a single project but could
integrate with other data models based on the
scope of the project.
• Designed and developed independently from the
DBMS.
• Data attributes will have datatypes with exact
precisions and length.
DBMS 1, Spring 2019
16
Data Modeling
PHYSICAL DATA MODEL
A Physical Data Model describes the database specific
implementation of the data model. It offers an
abstraction of the database and helps generate
schema. This is because of the richness of meta-data
offered by a Physical Data Model.
DBMS 1, Spring 2019
17
Data Modeling
PHYSICAL DATA MODEL (Cont’d)
This type of Data model also helps to visualize
database structure. It helps to model database columns
keys, constraints, indexes, triggers, and other RDBMS
features.
DBMS 1, Spring 2019
18
Data Modeling
PHYSICAL DATA MODEL (Cont’d)
DBMS 1, Spring 2019
19
Data Modeling
CHARACTERISTICS OF PHYSICAL DATA MODEL
• The physical data model describes data need for a
single project or application
• Data Model contains relationships between tables
that which addresses cardinality and nullability of
the relationships.
• Developed for a specific version of a DBMS,
location, data storage or technology to be used in
the project.
DBMS 1, Spring 2019
20
Data Modeling
CHARACTERISTICS OF PHYSICAL DATA MODEL (Cont’d)
• Columns should have exact datatypes, lengths
assigned and default values.
• Primary and Foreign keys, views, indexes, access
profiles, and authorizations, etc. are defined.
DBMS 1, Spring 2019
21
Constraints
CONTRAINTS IN DBMS
DBMS 1, Spring 2019
22
Constraints
WHAT ARE RELATIONAL
CONSTRAINTS?
Relational constraints are the restrictions imposed on
the database contents and operations.
They ensure the correctness of data in the database.
DBMS 1, Spring 2019
23
Constraints
In DBMS, there are following 5 different types of relational
constraints:
DBMS 1, Spring 2019
24
Constraints
DOMAIN CONSTRAINTS
• Domain constraint defines the domain or set of values for
an attribute.
• It specifies that the value taken by the attribute must be
the atomic value from its domain.
• The data type of domain includes string, character, integer,
time, date, currency, etc. The value of the attribute must
be available in the corresponding domain.
DBMS 1, Spring 2019
25
Constraints
ExampleConsider the following Student table:
STU_ID
Name
Age
S001
Akshay
20
S002
Abhishek
21
S003
Shashank
20
S004
Rahul
A
DBMS 1, Spring 2019
26
Constraints
STU_ID
Name
Age
S001
Akshay
20
S002
Abhishek
21
S003
Shashank
20
S004
Rahul
A
Here, value ‘A’ is not allowed since only integer values can be taken by
the age attribute.
DBMS 1, Spring 2019
27
Constraints
Tuple Uniqueness (Unique) Constraints
Tuple Uniqueness constraint specifies that all the
tuples must be necessarily unique in any relation.
The UNIQUE constraint restricts one or more columns
to contain unique values within a table.
DBMS 1, Spring 2019
28
Tuple Uniqueness (Unique) Constraints
Constraints
Consider the following Student tableSTU_ID
Name
Age
S001
Akshay
20
S002
Abhishek
21
S003
Shashank
20
S004
Rahul
20
This relation satisfies the tuple uniqueness constraint since here all the
tuples are unique.
DBMS 1, Spring 2019
29
Tuple Uniqueness (Unique) Constraints
Constraints
Consider the following Student tableSTU_ID
Name
Age
S001
Akshay
20
S001
Akshay
20
S003
Shashank
20
S004
Rahul
20
This relation does not satisfy the tuple uniqueness constraint since here
all the tuples are not unique.
DBMS 1, Spring 2019
30
Constraints
Primary Key Constraints
The PRIMARY KEY constraint identify the column or
set of columns that have values that uniquely identify
a row in a table. No two rows in a table can have
the same primary key value. Also, you cannot enter
NULL value in a primary key column.
DBMS 1, Spring 2019
31
Constraints
Primary Key Constraints
Consider the following Student table:
STU_ID
Name
Age
S001
Akshay
20
S001
Abhishek
21
S003
Shashank
20
S004
Rahul
20
This relation does not satisfy the key constraint as here all the values
of primary key are not unique.
DBMS 1, Spring 2019
32
Constraints
Entity Integrity Constraint
Entity integrity constraint specifies that no attribute
of primary key must contain a null value in any
relation.
This is because the presence of null value in the
primary key violates the uniqueness property.
DBMS 1, Spring 2019
33
Entity Integrity Constraint
Constraints
Consider the following Student table:
STU_ID
Name
Age
S001
Akshay
20
S002
Abhishek
21
S003
Shashank
20
Rahul
20
This relation does not satisfy the entity integrity constraint as here the
primary key contains a NULL
value.
DBMS 1, Spring 2019
34
Constraints
Referential Integrity Constraint
This constraint is enforced when a foreign key
references the primary key of a relation.
It specifies that all the values taken by the
foreign key must either be available in the
relation of the primary key or be null.
DBMS 1, Spring 2019
35
Referential Integrity Constraint
Constraints
The following two important rules emerges due to
referential integrity constraint:
• We can not insert a record into a referencing
relation if the corresponding record does not exist
in the referenced relation.
• We can not delete or update a record of the
referenced relation if the corresponding record
exists in the referencing relation.
DBMS 1, Spring 2019
36
Referential Integrity Constraint
Constraints
STU_ID
Name
Dept_no
Dept_no
Dept_name
S001
Akshay
D10
D10
ASET
S002
Abhishek
D10
D11
ALS
S003
Shashank
D11
D12
ASFL
S004
Rahul
D14
D13
ASHS
DBMS 1, Spring 2019
37
Practice Question #1
write a SQL statement that creates a table
named persons with four columns, out of which
three columns, id, name and phone do not accept
NULL values.
Constraints
DBMS 1, Spring 2019
38
Practice Question #1 (Answer)
Constraints
CREAT TABLE person(
Id INT NOT NULL,
Name VARCHAR(30) NOT NULL,
Birth_date DATE,
Phone VARCHAR(15) NOT NULL
);
DBMS 1, Spring 2019
39
Practice Question #2
Write a SQL statement that creates a table
named persons with columns id, name,
birth_date, phone, and specifies the id column
as the primary key. That means this field does
not allow NULL or duplicate values.
Constraints
DBMS 1, Spring 2019
40
Practice Question #2 (Answer)
Constraints
CREATE TABLE persons(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Birth_date DATE,
Phone VARCHAR(15) NOT NULL
);
DBMS 1, Spring 2019
41
Practice Question #3
Write a SQL statement that creates a table
named persons with columns id, name,
birth_date, phone, country. Include a SQL
statement that creates a default for the country
column.
Constraints
DBMS 1, Spring 2019
42
Practice Question #3 (Answer)
Constraints
CREATE TABLE persons(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Birth_date DATE,
Phone VARCHAR(15) NOT NULL UNIQUE,
Country VARCHAR(30) NOT NULL DEFAULT ‘Jamaica’
);
DBMS 1, Spring 2019
43
Practice Question #4
Write a SQL statement to create table
employees and columns emp_id, emp_name,
hire_date, salary, dept_id. Use the CHECK
constraint to allow only entry of salary ranging
from 3,000 to 10,000.
Constraints
DBMS 1, Spring 2019
44
Practice Question #4
Constraints
CREATE TABLE employees(
Emp_id NOT NULL PRIMARY KEY,
Emp_name VARCHAR(30) NOT NULL,
hire_date DATE NOT NULL,
Salary INT NOT NULL CHECK(salary>=3000 AND salary<=10000),
Dept_id INT,
FOREIGN KEY(dept_id) REFERENCES departments(dep_id)
);
DBMS 1, Spring 2019
45
Download