Jawzjan University
Computer Learning Department
Fundamentals of
Database
Prepared by: Khudaydad MAHMOODI
Overview
Table
Data Types
Database Keys
Table
Table is an element of database and is used for storing data.
Table consist of rows and columns.
You can create one or more tables in a database.
A table represent an entity.
Tables are always named in the singular, such as: Student, Course,
Grade, etc.
Table
Row (Record, Tuple)
Column
Column (Field, Attribute)
student_id name father_name birth_date
1701001 M. Nawab M. Alim 04.10.1997
Row 1701002 Bijan Hidayatullah 15.12.1998
1701003 Omidah M. Alem 01.01.1997
1701004 Suhrab M. Hasan 11.06.1995
1701005 Ebadullah Aq Murad 18.11.1996
1701006 Rona Asadullah 23.05.1998
1701007 Sima Sardar 09.03.1999
Data Types
All the data which are kept in the database has not same type. For
example:
student_no Integer
name String
birth_date Date
When you create a database, you must first define table type then the
table fields types.
Data Types
A data type determines the type of data that can be stored in a database
column.
String CHAR(n) Fixed length string of exactly n 8-bit
characters.
VARCHAR(n) Varying length string of up to n 8-bit
characters.
Numeric INT Integer numbers between -2147483648 and
+2147483647
SMALLINT Integer numbers between -32768 and
+32767
DECIMAL(x,y) Floating point number
REAL Floating point number
Date and Time DATETIME Date and Time
Database Keys
Super Key
Candidate Key
Primary Key
Foreign Key
Composite Key
Alternate Key
Super Key
An attribute or a combination of attribute that is used to
identify the records uniquely is known as Super Key. A table
can have many Super Keys.
id nic name phone Super Keys
(id)
1 0127001 Ali 0777010203 (nic)
2 0695382 Ali 0780998877 (id, nic)
3 1872549 Babur 0700424202 (id, name)
(id, phone)
4 2946109 Karim 0789597909 (nic, name)
5 8642771 Omar 0785009412 (nic, phone)
6 4721780 Salim 0788373747 (id, nic, name)
(id, nic, phone)
(nic, name, phone)
(id, nic, name, phone)
Candidate Key
It can be defined as minimal Super Key or irreducible Super
Key. In other words an attribute or a combination of
attribute that identifies the record uniquely but none of its
proper subsets can identify the records uniquely.
Super Keys Candidate Keys
(id) (id)
(nic) (nic)
(id, nic)
(id, name)
(id, phone)
(nic, name)
(nic, phone)
(id, nic, name)
(id, nic, phone)
(nic, name, phone)
(id, nic, name, phone)
Primary Key
A primary key is the combination of one or more column values in a table
that make a row of data unique within the table. Primary keys are
typically used to join related tables. Even if a table has no child table, a
primary key can be used to disallow the entry of duplicate records into a
table.
Super Keys Candidate Keys Primary Keys
(id) (id) (id)
(nic) (nic)
(id, nic)
(id, name)
(id, phone)
(nic, name)
(nic, phone)
(id, nic, name)
(id, nic, phone)
(nic, name, phone)
(id, nic, name, phone)
Primary Key
– Value must be unique for each row in a table
– Primary key can not be NULL and can not be used twice
– Auto number guarantees uniqueness but does not carry any
useful information
Foreign Key
A foreign key is the combination of one or more column
values in a table that reference a primary key in another
table. Foreign keys are defined in child tables. A foreign key
ensures that a parent record has been created before a child
record. Conversely, a foreign key also ensures that the child
record is deleted before the parent record.
– Used to create relationships between tables
– No uniqueness constraint for foreign keys
Database Keys
Primary Key student_id name father_name birth_date
1701001 M. Nawab M. Alim 04.10.1997
1701002 Bijan Hidayatullah 15.12.1998
1701003 Omidah M. Alem 01.01.1997
1701004 Suhrab M. Hasan 11.06.1995
1701005 Ebadullah Aq Murad 18.11.1996
1701006 Rona Asadullah 23.05.1998
1701007 Sima Sardar 09.03.1999
Foreign Key student_id Course
1701001 Fundamentals of Database
1701006 Fundamentals of Database
1701003 Data Structures
1701007 Operating System 2
Composite Key
If we use multiple attributes to create a Primary Key then
that Primary Key is called Composite Key (also called a
Compound Key or Concatenated Key).
Alternate Key
Alternate Key can be any of the Candidate Keys except for
the Primary Key.