Dos and Donts of Data Modelling
Dos and Donts of Data Modelling
Some Examples
Data Modelling Do’s and Don’ts
No Cyclic/Transitive Dependencies
• Cyclic Dependencies can cause data duplication leading to
error Appointment Client
Specialist
Specialists
‘Mandatory
‘Optional’,
‘null’ keyword
not necessary
Data Modelling Do’s and Don’ts
Part Two
Data Modelling Do’s and Don’ts
Data Dictionary
• How many errors can you find with this table?
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
id NUMBER 3 Not Null PK
client_id VARCHAR 3 Not Null
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
address VARCHAR 30 Not Null
dob VARCHAR 10
telephone_no NUMBER 13
gender VARCHAR 10 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Data Dictionary
1. Always choose a natural primary key
• There is no need to create a separate id attribute when
client_id is a suitable primary key
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 3 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
address VARCHAR 30 Not Null
dob VARCHAR 10
telephone_no NUMBER 13
gender VARCHAR 10 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Data Dictionary
2. Think about data sizes
– 3 digits for client id means ‘999’ clients maximum, is
that enough?
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 5 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
address VARCHAR 30 Not Null
dob VARCHAR 10
telephone_no NUMBER 13
gender VARCHAR 10 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Data Dictionary
2. Think about data sizes
• Why do we need 10 digits to store ‘M’ or ‘F’ for the
gender?
• We should use data type CHAR for fixed length strings,
and VARCHAR for variable length strings
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 5 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
address VARCHAR 30 Not Null
dob VARCHAR 10
telephone_no NUMBER 13
gender CHAR 1 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
CHAR or …… VARCHAR?
Data Modelling Do’s and Don’ts
Data Dictionary
3. Split data such as addresses into separate fields to
aid searching
• And certainly more than 30 characters to store an
address!
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 5 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
street VARCHAR 30 Not Null And think about
town VARCHAR 20
use of Null/Not
postcode VARCHAR 8 Not Null
null
dob VARCHAR 10
telephone_no NUMBER 13
gender CHAR 1 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Addresses….
• This is wrong…
To Null or not…?
• Use null/not null appropriately
– enforces a constraint which can reduce efficiency
– but increases integrity
– Remember can also enforce optionality
Data Modelling Do’s and Don’ts
Part Three
Data Modelling Do’s and Don’ts
Data Dictionary
4. Some numeric data needs to be VARCHAR
• Telephone, account numbers, etc., can start with 0
• And put it in quotes when inserting in SQL, ‘01234…’
• Otherwise you still lose the leading ‘0’
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 5 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
street VARCHAR 30 Not Null
town VARCHAR 20
postcode VARCHAR 8 Not Null
dob VARCHAR 10
telephone_no VARCHAR 13
gender CHAR 1 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Data Dictionary
5. Some numeric data needs to be VARCHAR
• Telephone numbers, etc. can start with 0
• And put it in quotes when inserting in SQL, ‘01234…’
• Otherwise you still lose the leading ‘0’, e.g.
Data Dictionary
6. Think carefully about data types, and what is
available in your DBMS
• Oracle and most DBMSs provide a DATE type (which
may or may not include time – if it does then there is no
need for a separate attribute)
TABLE Client
Format/
Attribute Name Data Type Size Null/Not Null PK/FK Constraints
client_id VARCHAR 5 Not Null PK
firstname VARCHAR 20 Not Null
lastname VARCHAR 30
street VARCHAR 30 Not Null
town VARCHAR 20
postcode VARCHAR 8 Not Null
dob DATE
telephone_no VARCHAR 13
gender CHAR 1 ‘M’ or ‘F’
Data Modelling Do’s and Don’ts
Data Dictionary
7. And be careful with data sizes and the use of real
numbers for numeric data
• 10 digits is £9,999,999,999, i.e. 10 billion pounds
• VARCHAR is for strings, not numbers
• And we need to specify if we want decimal digits, e.g.
NUMBER(5,2)
TABLE Client
Attribute Name Data Type Size Null/Not Null PK/FK
OrderNo VARCHAR 8 Not Null PK
ItemCode VARCHAR 5 Not Null FK (Item.ItemCode)
Quantity VARCHAR 3 Not Null
TotalCost NUMBER 10 Not Null
Data Modelling Do’s and Don’ts
Currency
8. And we don’t need to store the currency symbol
• It will stop us doing calculations
• And we can always include it in a query
Data Modelling Do’s and Don’ts
Part Four
Data Modelling Do’s and Don’ts
Duplicate Data
And finally…