SlideShare a Scribd company logo
1 of 54
Bordoloi
Database Design: Relational
Model
Dr. Bijoy Bordoloi
Bordoloi
Relational Database
• A relational database is a database that is
perceived by its users as a set of tables and
nothing but tables
Bordoloi
Relational Model
• Tabular data structure - table, row, column,
data type, null value
• Eight operators - restrict, project, join,
union, difference, intersect, product, divide
• Integrity rules - primary and foreign keys,
entity integrity, referential integrity
• Three parts of relational model correspond
to information, process, and integrity
disciplines of software engineering
Bordoloi
Data Structure
• Table approximates the formal term relation
and the physical file
• Row approximates the formal term tuple and
the physical record
• Column approximates the formal term
attribute and the physical field
• Data type approximates the formal term
domain
Bordoloi
Characteristics 0f a Relation
(Table)
• The order of rows and columns immaterial.
• All values are atomic – each row/column intersection represents
a single value. In other words, ‘repeating groups’ are not
allowed.
• Every value in a column must be a member of a conceptual set of
atomic values called a domain.
• A value may be null, that is, not known or inapplicable
• A relation, by definition, cannot have duplicate rows. Every table
must have a ‘Primary Key’ which guarantees that there are no
duplicate rows (discussed in depth later).
Data Structure
EMP
EMP_NBR EMP_NME DEPT# SAL HRE_DTE
012 SMITH 25 20000 10/26/1984
007 BOND 01 29000 09/01/1983
127 JONES 25 62000 05/14/1969
061 CLARKE 17 45000 04/01/1978
065 CUMMINGS 17 33000 06/12/1981
032 SIMPSON 01 95000 09/15/1986
099 CROCKET 25 46000 01/03/1990
COLUMN_NAME
NNNNNNNNAME
ROW
ATOMIC
DATA
VALUES DOMAIN
TABLE NAME
Bordoloi
Example: Repeating Groups
Bordoloi
Importance of Attribute Domain
and Data Types
• A relational DBMS can relate
any data field in one table
to any data field in another
table as long as the two
tables share a data field
that is defined on the same
‘domain’ (the same data
type).
Bordoloi
Attribute Domain and Data Types
• Consider the the following two tables,
Student and Employee. Do these tables
share any ‘common’ columns?
Student:
Employee:
SSN (Number (9)) St_Name (Char (16))
SSN (Char (9)) E_Name (Char (22)) Phone (Number (10))
Bordoloi
Semantic Data Types
(User Definable Data Types)
• User-friendly data type names
• User-friendly value sets
• Composite data types
Bordoloi
Semantic Data Types
CREATE DATATYPE ID [1 . . . 9999]
CREATE DATATYPE SOCSEC INTEGER
CREATE DATATYPE SEX [M, F]
CREATE DATATYPE GIVENNAME CHAR (12)
CREATE DATATYPE FAMILYNAME CHAR (25)
CREATE DATATYPE FULLNAME (GIVENNAME,
GIVENNAME,
FAMILYNAME)
CREATE TABLE EMPLOYEE (
EMP# ID
SOCSEC SOCSEC
NAME FULLNAME
SEX SEX
. . . )
Bordoloi
Checking for Compatible Data Types
Operations combining different data types are disallowed in
general…
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EMP# = SOCSEC
…however DBMS might automatically convert physical
dimensions…
…or user may define appropriate conversion procedures
Bordoloi
Benefits of Semantic Data Types
• Automatic validation of column values and
checking for compatible data types reduces
errors.
• Data type names provide additional
semantic information for users.
• Productivity benefits of composite data
types.
Bordoloi
Null Values
• Null – a special symbol, independent of
data type, which means either unknown or
inapplicable.
• Result of comparison operators is null when
either argument is null.
• Result of arithmetic operators is null when
either argument is null.
Bordoloi
Examples of Operations on Nulls
Table: Compensation
EMP# JOBCODE SALARY COMMISSION
E10 SALES 12500 32090
E11 NULL 25000 8000
E12 SALES 44000 0
E13 SALES 44000 NULL
E14 PROG 19500 NULL
E15 CLERK NULL NULL
Bordoloi
Examples of Operations on Nulls
What is the output of the following query?
SELECT EMP#
FROM COMPENSATION
WHERE JOBCODE = “SALES”
AND (SALARY + COMMISSION) > 30000
Bordoloi
Table Definition in SQL (DB2)
• CREATE TABLE EMPLOYEE (
• EMP# SMALLINT NOT NULL,
• SOCSEC INTEGER,
• FNAME VARCHAR (12) NOT NULL,
• LNAME VARCHAR (25) NOT NULL,
• SEX CHAR (1),
• SPOUSE SMALLINT,
• SALARY FLOAT,
• JOBCODE VARCHAR (6),
• DIVNAME VARCHAR (12) NOT NULL,
• DEPT# SMALLINT NOT NULL
• )
• Each column has a name that is unique within the table
and is specified to store a specific type of data
including whether NULL values are allowed or not.
Bordoloi
Oracle: NOT NULL Constraint
• A NOT NULL constraint means that a data row
must have a value for the column specified as
NOT NULL.
• A fairly standard practice is to assign each
constraint a unique constraint name.
• In Oracle, if constraints are not named, then
Oracle assigns meaningless system-generated
names to each constraint.
Bordoloi
Oracle Example: Not Null Constraint
fname VARCHAR2(15)
CONSTRAINT nn_emp_last_name NOT
NULL,
lname VARCHAR2(25)
CONSTRAINT nn_emp_first_name
NOT NULL,
Bordoloi
Characteristics 0f a Relation
(Table)
• The order of rows and columns immaterial.
• All values are atomic – each row/column intersection represents
a single value. In other words, ‘repeating groups’ are not
allowed.
• Every value in a column must be a member of a conceptual set of
atomic values called a domain.
• A value may be null, that is, not known or inapplicable
• A relation, by definition, cannot have duplicate rows. Every table
must have a ‘Primary Key’ which guarantees that there are no
duplicate rows (discussed in depth later).
Bordoloi
KEYS
Relational DBMS uses associative addressing.
– Identify and locate rows by value
– Physical address is transparent to user
Bordoloi
KEYS
A B C
C Z
C X Y
ASSOCIATIVE ADDRESSING
A B C
Z *
X Y *
PHYSICAL ADDRESSING
*
*
Bordoloi
KEYS
• Associative addressing is simpler for the
end-user.
• Physical data independence – storage
structures and access paths are transparent
to user and application programs
Bordoloi
KEYS
• Associative addressing is based on keys – a column, or
group of columns, used to identify rows.
• Simple key – a key formed from a single column
• Composite key – a key formed from several columns
• The relational model has five kinds of keys
• Super
• Candidate
• Primary
• Alternate (secondary)
• Foreign
Bordoloi
KEYS
• In relational DBMS, a key is not the
same as an index!
Keys identify rows (logical design)
Indexes locate rows (physical design)
Bordoloi
Candidate Keys
• Candidate Key – any (simple or composite)
column of a table which is both unique and
minimal.
• Uniqueness – no two rows in a table may have
same candidate key value at any time.
• Minimality – every column of a composite
candidate key must be necessary for uniqueness.
Bordoloi
Primary Key
• Primary Key – a candidate key chosen by the
database designer to identify rows of a table in
queries
• The primary key is the only guaranteed way to
identify rows in queries
UPDATE COMPENSATION
SET SALARY = 30000
WHERE EMP# = E3
• Primary keys must be unique, minimal, non-null,
and preferably time-invariant.
• Alternate key – any candidate key which is not a
primary key – may have null values.
Bordoloi
Candidate Keys/Primary Key
• Assume every employee has a phone#, only one phone# ,
and must have a phone# and that no two employees share
the same phone#.
• What is(are) the Candiadate Key(s)?
• What would you choose as the Primary Key of table
EMPLOYEE?
EMPLOYEE
EMP-ID SS-NUM EMP- NAME PHONE
Bordoloi
Primary Key
• The Primary Key MUST of course be a
Determinant
- i.e., all the other non-key attributes of a table
must be functionally dependent on the primary
key.
• In other words, for any given value of the primary
key, one should get one and only value of the one
non-key attributes
Bordoloi
Functional Dependency
• Example
– SOC_SEC_NBR EMP_NME
SOC_SEC_NBR EMP_NME
-One and only one EMP_NME for a specific SOC_SEC_NBR
- SOC_SEC_NBR is the determinant of EMP_NME
- EMP_NME is functionally dependent on SOC_SEC_NBR
Bordoloi
Determinants and Keys
What is (are) the determinant (s)?
What is (are) the candidate key (s)?
What is the primary key?
Table: Student-Dorm-Fee
SID DORM FEE
101 Oracle 1000
102 Oracle 1000
103 DB2 800
104 DB2 800
105 Sybase 500
Bordoloi
Primary Key
• Assume every employee must have a phone# , can have
more than one phone #, and more than one employee
share the same phone#.
• What is the Primary Key?
EMPLOYEE
EMP-ID SS-NUM EMP- NAME PHONE
Bordoloi
Primary Key
• Assume every employee must have a phone# , can have
more than one phone #, but no two employees can share
the same phone#.
• What is the Primary Key?
EMPLOYEE
EMP-ID SS-NUM EMP- NAME PHONE
Bordoloi
Entity Integrity
• Entity Integrity – If the primary key (PK) is
a composite key then all columns of the
primary key must be non-null.
• The primary key is the only
guaranteed way to positively
identify rows in queries
Bordoloi
Questions
• What similarities and differences do you
find between an Entity and a Table?
Bordoloi
Foreign Keys
• Foreign key – a (simple or composite)
column which refers to the primary key of
some table in a database.
• Foreign and primary keys must be defined
on same data type.
• A foreign key may be contained in a
primary key or another foreign key.
Bordoloi
Foreign Keys Defined
EMP
EMP_NBR EMP_NME DEPT# SAL HRE_DTE
012 SMITH 25 20000 10/26/1984
007 BOND 01 29000 09/01/1983
127 JONES 25 62000 05/14/1969
061 CLARKE 17 45000 04/01/1978
065 CUMMINGS 17 33000 06/12/1981
032 SIMPSON 01 95000 09/15/1986
099 CROCKET 25 46000 01/03/1990
DEPT
DEPT# DEPT_NAME MGR_EMP_NBR
17 ACCOUNTING 007
12 MARKETING ?
25 LEGAL 099
A foreign key
is a column or
columns in a table
that matches a primary
key in some table
in the database.
Are there any
other foreign keys?
Bordoloi
In-Class Exercise
• To be handed out in class
Bordoloi
Referential Integrity
• Referential Integrity – a foreign key which
identifies primary key of table T must either
be wholly null or match the value of the
primary key of some row in T
Bordoloi
Rationale for Referential Integrity
• Any non-primary key column may be
unknown or inapplicable (wholly null).
• An unmatched non-null foreign key
identifies a non-existent object and is in
error
Bordoloi
Referential Integrity Rules
(Foreign Key Rules)
How is referential integrity maintained in a database?
Some operations that may cause a violation …
– Insert of PK values – no problem
– Update of PK values – what happens to matching foreign keys?
– Delete of PK values – what happens to matching foreign keys?
– Insert of FK values – disallowed unless matching primary key
exists
– Update of FK values – disallowed unless matching primary key
exists
– Delete of FK values (FK Values set to NULL) – no problem as
long as NULL values are allowed in the FK
Bordoloi
Referential Integrity Rules
So, for each FK in each table the database
designer must specify:
• Whether or not NULLs allowed in the FK
• What should happen to the FK values
should the related PK values in the PK table
are deleted or updated
Bordoloi
Null Rule Alternatives
• Nulls allowed in foreign key columns
(minimum cardinality 0)
• Nulls disallowed in foreign key columns
(minimum cardinality 1)
Bordoloi
Delete Rule Alternatives
• Delete of primary key cascades to foreign
keys
• Delete of primary key nullifies foreign keys
• Delete of primary key is restricted if there
are any matching foreign keys
Bordoloi
Update Rule Alternatives
• Update of primary key cascades to foreign
keys
• Update of primary key nullifies foreign
keys
• Update of primary key is restricted if there
are any matching foreign keys
Bordoloi
Rule Alternatives: Meaning
(Oracle Syntax)
On Delete/Update Cascade:
Any delete/update made to the PK table
should be cascaded through to the FK table.
NOTE:
If a PK value is deleted in the PK table then
all the rows in the FK table with matching
FK values are also deleted in entirety.
Bordoloi
Rule Alternatives: Meaning
(Oracle Syntax)
On Delete/Update Set Null:
Any PK values that are deleted/updated in
the PK table, cause affected FK values in
the FK table to be set to null.
Unlike Delete Cascade, the entire row is
not deleted, only the affected FK values are
set to null.
Bordoloi
Rule Alternatives: Meaning
On Delete/Update Restrict:
Any updates made to the PK table that would
delete or change a primary key value will be
rejected unless no foreign key references that
value in the FK table(s).
In other words, you are restricted to deleting or
updating only those PK values in the PK table
which do NOT appear as FK values.
Example:
Bordoloi
Referential Integrity : Examples
What might be the appropriate referential
integrity rules for the tables Employee,
Division and Department?
Bordoloi
Documentation
• Database designer specifies
– Primary keys
– Alternate keys
– Foreign keys
– Foreign key rules, for each foreign key
– Semantic data types
– Default values (optional)
Bordoloi
Enhanced SQL
CREATE TABLE TABLENAME (
COLUMNNAME DATATYPENAME NULLS [NOT] ALLOWED
COLUMNNAME DATATYPENAME NULLS [NOT] ALLOWED
. . . )
PRIMARY KEY COLUMNNAME
ALTERNATE KEY COLUMNNAME
ALTERNATE KEY COLUMNNAME
FOREIGN KEY (
COLUMNNAME IDENTIFIES PKTABLE
NULLS [NOT] ALLOWED
DELETE OF PKTABLE [CASCADES] [RESTRICTED] [NULLIFIES]
UPDATE OF PKTABLE [CASCADES] [RESTRICTED] [NULLIFIES]
)
FOREIGN KEY ( . . . )
FOREIGN KEY ( . . . )
Bordoloi
Example (DB2)
CREATE TABLE EMPLOYEE (
EMP# ID NOT NULL,
SOCSEC SOCSEC,
NAME FULLNAME, NOT NULL,
SEX SEX,
SPOUSE ID,
SALARY MONEY,
JOBCODE CHAR (16),
DIVNAME VARCHAR (12), NOT NULL,
DEPT# DEPARTMENT , NOT NULL )
PRIMARY KEY EMP#
ALTERNATE KEY SOCSEC
FOREIGN KEY (
SPOUSE IDENTIFIES EMPLOYEE
NULLS ALLOWED
DELETE OF EMPLOYEE NULLIFIES
UPDATE OF EMPLOYEE CASCADES )
FOREIGN KEY (
(DIVNAME, DEPT#) IDENTIFIES DEPARTMENT
NULLS NOT ALLOWED
DELETE OF DEPARTMENT RESTRICTED
UPDATE OF DEPARTMENT CASCADES )
Bordoloi
Example (Oracle)
CREATE TABLE EMPLOYEE (
EMP# ID
CONSTRAINT pk_employee PRIMARY KEY,
SOCSEC SOCSEC,
NAME FULLNAME
CONSTRAINT nn_emt_name NOT NULL,
SEX SEX,
SPOUSE ID
CONSTRAINT fk_emp_spouse FOREIGN KEY (spouse)
REFERENCES Employee ON DELETE SET NULL,
SALARY MONEY,
JOBCODE CHAR (16),
DIVNAME VARCHAR (12),
DEPT# DEPARTMENT,
CONSTRAINT fk_emp_dept FOREIGN KEY (divname,
dept#) REFERENCES Department ON UPDATE
CASCADE
);
Bordoloi
IN-Class Exercise
• Distributed in class

More Related Content

Similar to Relational_Model.ppt

Similar to Relational_Model.ppt (20)

DBMS:Relational Modal
DBMS:Relational ModalDBMS:Relational Modal
DBMS:Relational Modal
 
Relational database management system
Relational database management systemRelational database management system
Relational database management system
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Introduction to the Relational Model and SQL
Introduction to the Relational Model and SQLIntroduction to the Relational Model and SQL
Introduction to the Relational Model and SQL
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
 
2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload
 
Databases - Unit 2.pdf
Databases - Unit 2.pdfDatabases - Unit 2.pdf
Databases - Unit 2.pdf
 
Ankit
AnkitAnkit
Ankit
 
DB2 on Mainframe
DB2 on MainframeDB2 on Mainframe
DB2 on Mainframe
 
19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf
 
Dbms keys
Dbms keysDbms keys
Dbms keys
 
Advance database system(part 5)
Advance database system(part 5)Advance database system(part 5)
Advance database system(part 5)
 
Relational model
Relational modelRelational model
Relational model
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
Les09
Les09Les09
Les09
 
Keys.pptx
Keys.pptxKeys.pptx
Keys.pptx
 
Create table
Create tableCreate table
Create table
 
computer-210809080138.pdf
computer-210809080138.pdfcomputer-210809080138.pdf
computer-210809080138.pdf
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Recently uploaded (20)

Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Relational_Model.ppt

  • 2. Bordoloi Relational Database • A relational database is a database that is perceived by its users as a set of tables and nothing but tables
  • 3. Bordoloi Relational Model • Tabular data structure - table, row, column, data type, null value • Eight operators - restrict, project, join, union, difference, intersect, product, divide • Integrity rules - primary and foreign keys, entity integrity, referential integrity • Three parts of relational model correspond to information, process, and integrity disciplines of software engineering
  • 4. Bordoloi Data Structure • Table approximates the formal term relation and the physical file • Row approximates the formal term tuple and the physical record • Column approximates the formal term attribute and the physical field • Data type approximates the formal term domain
  • 5. Bordoloi Characteristics 0f a Relation (Table) • The order of rows and columns immaterial. • All values are atomic – each row/column intersection represents a single value. In other words, ‘repeating groups’ are not allowed. • Every value in a column must be a member of a conceptual set of atomic values called a domain. • A value may be null, that is, not known or inapplicable • A relation, by definition, cannot have duplicate rows. Every table must have a ‘Primary Key’ which guarantees that there are no duplicate rows (discussed in depth later).
  • 6. Data Structure EMP EMP_NBR EMP_NME DEPT# SAL HRE_DTE 012 SMITH 25 20000 10/26/1984 007 BOND 01 29000 09/01/1983 127 JONES 25 62000 05/14/1969 061 CLARKE 17 45000 04/01/1978 065 CUMMINGS 17 33000 06/12/1981 032 SIMPSON 01 95000 09/15/1986 099 CROCKET 25 46000 01/03/1990 COLUMN_NAME NNNNNNNNAME ROW ATOMIC DATA VALUES DOMAIN TABLE NAME
  • 8. Bordoloi Importance of Attribute Domain and Data Types • A relational DBMS can relate any data field in one table to any data field in another table as long as the two tables share a data field that is defined on the same ‘domain’ (the same data type).
  • 9. Bordoloi Attribute Domain and Data Types • Consider the the following two tables, Student and Employee. Do these tables share any ‘common’ columns? Student: Employee: SSN (Number (9)) St_Name (Char (16)) SSN (Char (9)) E_Name (Char (22)) Phone (Number (10))
  • 10. Bordoloi Semantic Data Types (User Definable Data Types) • User-friendly data type names • User-friendly value sets • Composite data types
  • 11. Bordoloi Semantic Data Types CREATE DATATYPE ID [1 . . . 9999] CREATE DATATYPE SOCSEC INTEGER CREATE DATATYPE SEX [M, F] CREATE DATATYPE GIVENNAME CHAR (12) CREATE DATATYPE FAMILYNAME CHAR (25) CREATE DATATYPE FULLNAME (GIVENNAME, GIVENNAME, FAMILYNAME) CREATE TABLE EMPLOYEE ( EMP# ID SOCSEC SOCSEC NAME FULLNAME SEX SEX . . . )
  • 12. Bordoloi Checking for Compatible Data Types Operations combining different data types are disallowed in general… SELECT FNAME, LNAME FROM EMPLOYEE WHERE EMP# = SOCSEC …however DBMS might automatically convert physical dimensions… …or user may define appropriate conversion procedures
  • 13. Bordoloi Benefits of Semantic Data Types • Automatic validation of column values and checking for compatible data types reduces errors. • Data type names provide additional semantic information for users. • Productivity benefits of composite data types.
  • 14. Bordoloi Null Values • Null – a special symbol, independent of data type, which means either unknown or inapplicable. • Result of comparison operators is null when either argument is null. • Result of arithmetic operators is null when either argument is null.
  • 15. Bordoloi Examples of Operations on Nulls Table: Compensation EMP# JOBCODE SALARY COMMISSION E10 SALES 12500 32090 E11 NULL 25000 8000 E12 SALES 44000 0 E13 SALES 44000 NULL E14 PROG 19500 NULL E15 CLERK NULL NULL
  • 16. Bordoloi Examples of Operations on Nulls What is the output of the following query? SELECT EMP# FROM COMPENSATION WHERE JOBCODE = “SALES” AND (SALARY + COMMISSION) > 30000
  • 17. Bordoloi Table Definition in SQL (DB2) • CREATE TABLE EMPLOYEE ( • EMP# SMALLINT NOT NULL, • SOCSEC INTEGER, • FNAME VARCHAR (12) NOT NULL, • LNAME VARCHAR (25) NOT NULL, • SEX CHAR (1), • SPOUSE SMALLINT, • SALARY FLOAT, • JOBCODE VARCHAR (6), • DIVNAME VARCHAR (12) NOT NULL, • DEPT# SMALLINT NOT NULL • ) • Each column has a name that is unique within the table and is specified to store a specific type of data including whether NULL values are allowed or not.
  • 18. Bordoloi Oracle: NOT NULL Constraint • A NOT NULL constraint means that a data row must have a value for the column specified as NOT NULL. • A fairly standard practice is to assign each constraint a unique constraint name. • In Oracle, if constraints are not named, then Oracle assigns meaningless system-generated names to each constraint.
  • 19. Bordoloi Oracle Example: Not Null Constraint fname VARCHAR2(15) CONSTRAINT nn_emp_last_name NOT NULL, lname VARCHAR2(25) CONSTRAINT nn_emp_first_name NOT NULL,
  • 20. Bordoloi Characteristics 0f a Relation (Table) • The order of rows and columns immaterial. • All values are atomic – each row/column intersection represents a single value. In other words, ‘repeating groups’ are not allowed. • Every value in a column must be a member of a conceptual set of atomic values called a domain. • A value may be null, that is, not known or inapplicable • A relation, by definition, cannot have duplicate rows. Every table must have a ‘Primary Key’ which guarantees that there are no duplicate rows (discussed in depth later).
  • 21. Bordoloi KEYS Relational DBMS uses associative addressing. – Identify and locate rows by value – Physical address is transparent to user
  • 22. Bordoloi KEYS A B C C Z C X Y ASSOCIATIVE ADDRESSING A B C Z * X Y * PHYSICAL ADDRESSING * *
  • 23. Bordoloi KEYS • Associative addressing is simpler for the end-user. • Physical data independence – storage structures and access paths are transparent to user and application programs
  • 24. Bordoloi KEYS • Associative addressing is based on keys – a column, or group of columns, used to identify rows. • Simple key – a key formed from a single column • Composite key – a key formed from several columns • The relational model has five kinds of keys • Super • Candidate • Primary • Alternate (secondary) • Foreign
  • 25. Bordoloi KEYS • In relational DBMS, a key is not the same as an index! Keys identify rows (logical design) Indexes locate rows (physical design)
  • 26. Bordoloi Candidate Keys • Candidate Key – any (simple or composite) column of a table which is both unique and minimal. • Uniqueness – no two rows in a table may have same candidate key value at any time. • Minimality – every column of a composite candidate key must be necessary for uniqueness.
  • 27. Bordoloi Primary Key • Primary Key – a candidate key chosen by the database designer to identify rows of a table in queries • The primary key is the only guaranteed way to identify rows in queries UPDATE COMPENSATION SET SALARY = 30000 WHERE EMP# = E3 • Primary keys must be unique, minimal, non-null, and preferably time-invariant. • Alternate key – any candidate key which is not a primary key – may have null values.
  • 28. Bordoloi Candidate Keys/Primary Key • Assume every employee has a phone#, only one phone# , and must have a phone# and that no two employees share the same phone#. • What is(are) the Candiadate Key(s)? • What would you choose as the Primary Key of table EMPLOYEE? EMPLOYEE EMP-ID SS-NUM EMP- NAME PHONE
  • 29. Bordoloi Primary Key • The Primary Key MUST of course be a Determinant - i.e., all the other non-key attributes of a table must be functionally dependent on the primary key. • In other words, for any given value of the primary key, one should get one and only value of the one non-key attributes
  • 30. Bordoloi Functional Dependency • Example – SOC_SEC_NBR EMP_NME SOC_SEC_NBR EMP_NME -One and only one EMP_NME for a specific SOC_SEC_NBR - SOC_SEC_NBR is the determinant of EMP_NME - EMP_NME is functionally dependent on SOC_SEC_NBR
  • 31. Bordoloi Determinants and Keys What is (are) the determinant (s)? What is (are) the candidate key (s)? What is the primary key? Table: Student-Dorm-Fee SID DORM FEE 101 Oracle 1000 102 Oracle 1000 103 DB2 800 104 DB2 800 105 Sybase 500
  • 32. Bordoloi Primary Key • Assume every employee must have a phone# , can have more than one phone #, and more than one employee share the same phone#. • What is the Primary Key? EMPLOYEE EMP-ID SS-NUM EMP- NAME PHONE
  • 33. Bordoloi Primary Key • Assume every employee must have a phone# , can have more than one phone #, but no two employees can share the same phone#. • What is the Primary Key? EMPLOYEE EMP-ID SS-NUM EMP- NAME PHONE
  • 34. Bordoloi Entity Integrity • Entity Integrity – If the primary key (PK) is a composite key then all columns of the primary key must be non-null. • The primary key is the only guaranteed way to positively identify rows in queries
  • 35. Bordoloi Questions • What similarities and differences do you find between an Entity and a Table?
  • 36. Bordoloi Foreign Keys • Foreign key – a (simple or composite) column which refers to the primary key of some table in a database. • Foreign and primary keys must be defined on same data type. • A foreign key may be contained in a primary key or another foreign key.
  • 37. Bordoloi Foreign Keys Defined EMP EMP_NBR EMP_NME DEPT# SAL HRE_DTE 012 SMITH 25 20000 10/26/1984 007 BOND 01 29000 09/01/1983 127 JONES 25 62000 05/14/1969 061 CLARKE 17 45000 04/01/1978 065 CUMMINGS 17 33000 06/12/1981 032 SIMPSON 01 95000 09/15/1986 099 CROCKET 25 46000 01/03/1990 DEPT DEPT# DEPT_NAME MGR_EMP_NBR 17 ACCOUNTING 007 12 MARKETING ? 25 LEGAL 099 A foreign key is a column or columns in a table that matches a primary key in some table in the database. Are there any other foreign keys?
  • 38. Bordoloi In-Class Exercise • To be handed out in class
  • 39. Bordoloi Referential Integrity • Referential Integrity – a foreign key which identifies primary key of table T must either be wholly null or match the value of the primary key of some row in T
  • 40. Bordoloi Rationale for Referential Integrity • Any non-primary key column may be unknown or inapplicable (wholly null). • An unmatched non-null foreign key identifies a non-existent object and is in error
  • 41. Bordoloi Referential Integrity Rules (Foreign Key Rules) How is referential integrity maintained in a database? Some operations that may cause a violation … – Insert of PK values – no problem – Update of PK values – what happens to matching foreign keys? – Delete of PK values – what happens to matching foreign keys? – Insert of FK values – disallowed unless matching primary key exists – Update of FK values – disallowed unless matching primary key exists – Delete of FK values (FK Values set to NULL) – no problem as long as NULL values are allowed in the FK
  • 42. Bordoloi Referential Integrity Rules So, for each FK in each table the database designer must specify: • Whether or not NULLs allowed in the FK • What should happen to the FK values should the related PK values in the PK table are deleted or updated
  • 43. Bordoloi Null Rule Alternatives • Nulls allowed in foreign key columns (minimum cardinality 0) • Nulls disallowed in foreign key columns (minimum cardinality 1)
  • 44. Bordoloi Delete Rule Alternatives • Delete of primary key cascades to foreign keys • Delete of primary key nullifies foreign keys • Delete of primary key is restricted if there are any matching foreign keys
  • 45. Bordoloi Update Rule Alternatives • Update of primary key cascades to foreign keys • Update of primary key nullifies foreign keys • Update of primary key is restricted if there are any matching foreign keys
  • 46. Bordoloi Rule Alternatives: Meaning (Oracle Syntax) On Delete/Update Cascade: Any delete/update made to the PK table should be cascaded through to the FK table. NOTE: If a PK value is deleted in the PK table then all the rows in the FK table with matching FK values are also deleted in entirety.
  • 47. Bordoloi Rule Alternatives: Meaning (Oracle Syntax) On Delete/Update Set Null: Any PK values that are deleted/updated in the PK table, cause affected FK values in the FK table to be set to null. Unlike Delete Cascade, the entire row is not deleted, only the affected FK values are set to null.
  • 48. Bordoloi Rule Alternatives: Meaning On Delete/Update Restrict: Any updates made to the PK table that would delete or change a primary key value will be rejected unless no foreign key references that value in the FK table(s). In other words, you are restricted to deleting or updating only those PK values in the PK table which do NOT appear as FK values. Example:
  • 49. Bordoloi Referential Integrity : Examples What might be the appropriate referential integrity rules for the tables Employee, Division and Department?
  • 50. Bordoloi Documentation • Database designer specifies – Primary keys – Alternate keys – Foreign keys – Foreign key rules, for each foreign key – Semantic data types – Default values (optional)
  • 51. Bordoloi Enhanced SQL CREATE TABLE TABLENAME ( COLUMNNAME DATATYPENAME NULLS [NOT] ALLOWED COLUMNNAME DATATYPENAME NULLS [NOT] ALLOWED . . . ) PRIMARY KEY COLUMNNAME ALTERNATE KEY COLUMNNAME ALTERNATE KEY COLUMNNAME FOREIGN KEY ( COLUMNNAME IDENTIFIES PKTABLE NULLS [NOT] ALLOWED DELETE OF PKTABLE [CASCADES] [RESTRICTED] [NULLIFIES] UPDATE OF PKTABLE [CASCADES] [RESTRICTED] [NULLIFIES] ) FOREIGN KEY ( . . . ) FOREIGN KEY ( . . . )
  • 52. Bordoloi Example (DB2) CREATE TABLE EMPLOYEE ( EMP# ID NOT NULL, SOCSEC SOCSEC, NAME FULLNAME, NOT NULL, SEX SEX, SPOUSE ID, SALARY MONEY, JOBCODE CHAR (16), DIVNAME VARCHAR (12), NOT NULL, DEPT# DEPARTMENT , NOT NULL ) PRIMARY KEY EMP# ALTERNATE KEY SOCSEC FOREIGN KEY ( SPOUSE IDENTIFIES EMPLOYEE NULLS ALLOWED DELETE OF EMPLOYEE NULLIFIES UPDATE OF EMPLOYEE CASCADES ) FOREIGN KEY ( (DIVNAME, DEPT#) IDENTIFIES DEPARTMENT NULLS NOT ALLOWED DELETE OF DEPARTMENT RESTRICTED UPDATE OF DEPARTMENT CASCADES )
  • 53. Bordoloi Example (Oracle) CREATE TABLE EMPLOYEE ( EMP# ID CONSTRAINT pk_employee PRIMARY KEY, SOCSEC SOCSEC, NAME FULLNAME CONSTRAINT nn_emt_name NOT NULL, SEX SEX, SPOUSE ID CONSTRAINT fk_emp_spouse FOREIGN KEY (spouse) REFERENCES Employee ON DELETE SET NULL, SALARY MONEY, JOBCODE CHAR (16), DIVNAME VARCHAR (12), DEPT# DEPARTMENT, CONSTRAINT fk_emp_dept FOREIGN KEY (divname, dept#) REFERENCES Department ON UPDATE CASCADE );