CHAP 3 Relational Database
CHAP 3 Relational Database
Relational Database
Overview
● One to many:
– The primary key relates to one or many records in related
table
● Many to Many:
– The primary key relates to many records in related table, and a
record in related table can relate to many primary keys on
another table
Storing Relationships using Keys
● Modeling data is one thing, storing it in a database
is another one.
● In relational database, the 'rules' are:
– If the relationship to be stored is 1:N, place
the attribute identified as the primary key from the one
table as a foreign key in another table.
– If the relationship to be stored is M:N, a new
table structure must be created to hold the association.
This 'bridge' table will have as foreign key attributes, the
primary key of each table that is part of relationship
● The key for the 'bridge' table then becomes either:
– The combination of all the foreign keys OR
– A new attribute will be added as a surrogate key
Storing Relationships using Keys
Indexes in MySQL
● A database index is
– a data structure that improves the speed of operations in
a table
– Unseen table created by DB engine that keeps indexed
fields and its pointers to each record into the actual table.
● Indexes in MySQL:
– Primary key
– Unique indexes:
All values in the indexed column must be distinct though
it's unnecessarily indexed as a primary key
– Index:
Refers to a non-unique index, used for speeding the
retrieval
Indexes in MySQL
● Indexes in MySQL:
– Fulltext:
. An index created for full text searches
. Supporting storage engines: InnoDB & MyISAM
. Data type: CHAR, VARCHAR, TEXT
– Spatial Index:
. for spatial data types
. Uses R-tree indexes
● Example of index usage:
– “Find all students with GPA < 1.7”
. May need to scan the entire table
. Index consists of a set of entries pointing to
locations of each search key
Data Type in MySql
● String:
– Char, varchar, text, (tiny, medium, long)
– Binary, varbinary
– Blob (tiny, medium, long), enum, set
● Date & time
● Numeric
– Int (tiny, small, medium, big)
– Decimal, float, double, real
– BIT, boolean, serial
● Spatial:
– Geometry, point, linestring, polygon, etc
SQL
● Structured Query Language (SQL):
– Is a standard language used to communicate with a
relational database.
– Is used in conjunction with procedural or object-oriented
languages/scripts such as Java, Perl, Ruby, Python, etc
● Sql basic conventions:
– Each statement begins with a command,
eg. CREATE, SELECT
– Each statement ends with delimiter usually a semicolon (;)
– Statements are written in a free-form style,
eg. SELECT...FROM... WHERE...
– SQL statement is not case-sensitive, except inside string
constant, eg SELECT...FROM... WHERE SName = 'Yadoll'
Simple SQL Queries
● The basic form of SQL Queries is:
SELECT select-list (column_name)
FROM from-list (table_name)
WHERE condition
● Selecting all students with GPA above 1.7
SELECT Sid, Sname FROM student WHERE GPA <= 1.7
● Selecting all information from a table
SELECT * FROM enrolled
● Selecting course name with pattern matching
SELECT Cname FROM Courses WHERE Cname LIKE
“Machine %”
Simple SQL Querie
● INSERT:
INSERT INTO ˋStudentsˋ VALUES (CL0001, David,
david@cis, 1,3 )
INSERT INTO ˋStudentsˋ VALUES (sid, sname, login, gpa )
● ALTER:
ALTER TABLE ˋStudentsˋ ADD ˋIntakeyearˋ
ALTER TABLE ˋLecturerˋ ADD INDEX(ˋcoursesˋ)
Simple SQL Querie
● Using logical connectives:
– AND, OR, NOT may be used to construct a
condition
SELECT ˋcnameˋ FROM ˋcoursesˋ WHERE
semester = 'summer' AND ctype = 'seminar‘
• Joining tables:
- SELECT ‘Sname’
FROM ‘Students’ ‘Courses’
WHERE Student .sid=Courses.sid
Simple SQL Queries
● Creating Table:
CREATE TABLE ˋStudentsˋ (
ˋSidˋ varchar(6) NOT NULL,
ˋSNameˋ varchar(35) NOT NULL,
ˋLoginˋ varchar(25) NOT NULL,
ˋGPAˋ float(2,1) NOT NULL,
PRIMARY KEY (ˋSidˋ)
ENGINE=InnoDB CHARSET= Latin1
Creating Database Through Terminal
Creating Database Through Terminal
● Open your terminal console
● Go to the path where you save your MySql
● If you install XAMPP :
– You need to start XAMPP as a SU/root
– to get the action commands (in Linux), type:
/opt/lampp/lampp
– Start only MySQL Server, type:
/opt/lampp/lampp startmysql
– To stop MySQL, type:
/opt/lampp/lampp stopmysql
– To start XAMPP (Apache, MySQL & others ), type:
/opt/lampp/lampp star
Creating Database Through Terminal
● If you install XAMPP :
– go to the path where mysql is saved, in Linux it is usually
saved in bin, so type:
/opt/lampp/bin/mysql -uusername -ppassword
– If you are already in mysql path:
● To delete database:
DROP DATABASE database_name ;
● Problems:
– No need to repeatedly store the class time &
Professor ID
– Which one is the key?
Database Normalization
● First Normal Form (1NF):
– A row of data cannot contain a repeating group of data.
– Each row of data must have a unique identifier, i.e primary
key
● This can be done by
– Eliminating the repeated groups of data through creating
separate tables of related data
– Identify each set of related data with a primary key
– All attributes are single valued (1 data type) & non-repeating
● Student information:
Sid Sname Major Minor IntakeYear
● Course information
Cid Cname Lid Time Room
● Lecturer Information
Lid Lname Ltitle
Database Normalization
● Second Normal form (2NF):
– A table should meet 1NF
– There must not be any partial dependency of any
column on primary key (Records should not
depend on anything other than a table's primary key)
● Recall our poor database design:
Sid → Cname or Cname → time ?
Database Normalization
● Second Normal Form (2NF) solution:
– Create separate tables for sets of values that
apply to multiple records
– Relates the tables with a foreign key
– Remove subsets of data that apply to multiple
rows of a table and place them in separate tables
enrolled
Sid Cid grade (?)
– What do we do with the attribute time, room, &
Lid?
Database Normalization
● Third Normal Form (3NF):
– Eliminate all attributes (columns) that do not
directly dependent upon the primary key
– Each non-primary key attribute must be
dependent only on primary key (no transitive
dependency)
– Example:
Student:
Sid Sname Major Minor IntakeYear
● which attribute is not directly dependent on sid?
Student:
Sid Sname Minor Major
Database Normalization
Old design
New design
Database Normalization
● Storing the relation among tables in database
Database Normalization
● Exercise:
– Which normal form does this table violate?
– And how do you normalize it?