Lecture 3 Examples
Lecture 3 Examples
Database
Lecture 3 Examples
Topics: Integrity Constraints, Derived Attributes,
Attribute Types, Relationship Types
student_roll_n
student_name student_age student_class
umber
101 Adam 14 6
102 Steve 16 8
103 David 8 4
104 Bruce 18 12
105 Tim 6 A
In the above student table, the value A in the last row last
column violates the domain integrity constraint because the Class
attribute contains only integer values while A is a character.
Entity Integrity Constraint
Keys are the set of entities that are used to identify an entity
within its entity set uniquely.
There could be multiple keys in a single entity set, but out of
these multiple keys, only one key will be the primary key.
A primary key can only contain unique and not null values in the
relational database table.
See Example 4
Example 4: Key Constraint
Consider a student table
student_roll_n
student_name student_age student_class
umber
101 Adam 14 6
102 Steve 16 8
103 David 8 4
104 Bruce 18 12
102 Tim 6 2
The last row of the student table violates the key integrity
constraint since student_roll_number 102 is repeated twice in
the primary key column. A primary key must be unique and not null
therefore duplicate values are not allowed in the
student_roll_number column of the above student table.
Data type Description
CHARACTER(n) Character string. Fixed-length n
Text Character string. Variable length
VARCHAR(n) or CHARACTER Character string. Variable length. Maximum
VARYING(n) length n
The data type not only tells NUMERIC(p,s) Exact numerical, precision p, scale s. (Same as
DECIMAL)
the column which type of FLOAT(p) Approximate numerical, mantissa precision p. A
values can be inserted but also floating number in base 10 exponential notation.
REAL Approximate numerical
what values cannot be used in
FLOAT Approximate numerical
a column DOUBLE PRECISION Approximate numerical
Some common datatypes in DATE Stores year, month, and day values
TIME Stores hour, minute, and second values
SQL are shown in the table TIMESTAMP Stores year, month, day, hour, minute, and
second values
Keep in mind that each INTERVAL Composed of a number of integer fields,
database system recognizes its representing a period of time, depending on the
type of interval
own set of datatypes ARRAY A set-length and ordered collection of elements
MULTISET A variable-length and unordered collection of
elements
XML Stores XML data
SQLite Datatypes
STORAGE
USES
CLASS
Foreign keys can be established now that the constraint has been enabled.
Note: You will have to enable foreign keys every time you reopen SQLite on your computer.
Without enabling foreign keys, you will be still be able to add data, so make sure to enable FK’s.
Establishing Relationships
Suppose we add more data to the employee table, and we want
to establish a relationship between the employee and seller
table. This would be a One-One relationship.
We can write the query as shown below to create a seller table
that consists of a Foreign Key (employee_id) referencing the
employee_id from the employee table
We see that we are not able to delete employee_id = 1 due to the restrict action