L02 Introduction To Relational Model 2 46
L02 Introduction To Relational Model 2 46
➢Domain:
• A set of atomic values allowed for an attribute
➢Relation Schema:
• Describes a relation
• Made up of a relation name R and a having a list
of attributes A1,A2,A3……………..An
STUDENT(Name,Rollno,Age,Address,Phone,Grade)
STUDENT(Name
string:,Rollno:integer,Age:integer,Address:string,Ph
one:integer,Grade: string)
Relational Data Model
STUDENT(Name,Rollno,Age,Address,Phone,Grade)
Terminologies
➢ Cardinality:
• Total number of tuples present in a
relation schema
➢ PRIMARY KEY
➢ CANDIDATE KEY
➢ ALTERNATE KEY
➢ COMPOSITE KEY
➢ FOREGIN KEY
➢ SUPER KEY
Relational Model Constraints
➢ Constraints on databases:
• Inherent Model - Based: Inherent in the model(Already existing )
• Eg: duplicate records are not allowed
➢ Schema based:
• Defined directly in the schemas of the data model
• Eg: Age of an employee should be between 25-65
➢ Application based:
• Must be expressed and enforced by the application programs.
CONSTRAINTS
The Constraints can be placed at the column level or table level.
Column Level Constraint: These constraints are defined along with the column
definition . These constraints can be applied to any one column at a time. If the
constraints spans across multiple columns , then the table level constraints are
used.
Table Level Constraint: If the data constraint attached to a specific cell in a table
references the content of another cell in the table then the table level constraint
is used.
16
Relational Model Constraints: Schema Based
Constraints
➢ Domain Constraints
➢ Key Constraints
➢ Constraints on NULL
➢ Domain Constraints
o It specifies that within each tuple or within each row the value of
an attribute has to be atomic or individual.
o Performs the datatype check of each attribute.
STUDENT ROLL_No NAME AGE
1 Preeti 34
2 Ishani 14
3 Sonal A
Violates Domain
Constraint
Schema Based Constraints
➢ Key Constraints
o An attribute that can uniquely identify each tuple in a relation is
called a Key
20
CONSTRAINTS
21
Add/Remove Constraints
• After you create a table, you can use the Alter Table statement to
• add or remove a primary key, unique, foreign key, or check constraint
• To drop a table's primary key constraint, just specify the Primary Key keywords:
Alter Table Sale
Drop Primary Key
• To drop a unique, foreign key, or check constraint, you must specify the
constraint name:
• To add a new constraint, use the same constraint syntax as in a Create Table
statement:
Alter Table Sale
Add Constraint SaleSaleTotChk Check( SaleTot >= 0 )
22
Schema Based Constraints
➢ Candidate Keys:
o Set of attributes that uniquely identify the tuples in a relation.
Schema Based Constraints
following:
o Same domain
following:
o Same domain
Data values being inserted into the column CUST_NO must start with the capital
letter C
Data values being inserted into the column FNAME, MNAME and LNAME should
be in UPPERCASE
number(4))
DELETE FROM STUDENT WHERE
STUDENT_NUM=2
select * from hostel_info
ORA-02292: integrity constraint
(SQL_OQWECISXRJQQVHFYUMWHYESUK.SYS_C0078555716) violated - child
record found ORA-06512: at "SYS.DBMS_SQL", line 1721
Foreign Key with ON DELETE CASCADE