ERD & Subquery & View
April 2017
Business Economics & Information Technology
OUTLINE
 weak entity
 Derived attribute
 Multi-valued attribute
 Composite attribute
WEAK ENTITY
◼A weak entity is an entity that depends on the existence of another entity and cannot be uniquely identified
by its attributes
◼A weak entity is represented by double rectangles
◼A weak uses a foreign key combined with its attributed to form the primary key.
Example:
The order item will be meaningless without an order so it depends on the existence of order
.
What is the priamry key of
Order-Item entity?
MORE EXAMPLE OF WEAK ENTITES
Depends
ID
DEP_ID DEP_NAME DEP_DOB
20 Lisbeth 22-jul-1985
30 Rasmus 30-OCT-1990
40 Morten NULL
EMP_ID EMP_NAME EMP_DOB
100 ALLEN 17-DEC-1955
101 SARA 06-APRIL-1959
102 JOHN 22-FEB-1990
EMP_ID
100
100
101
What is the priamry key of
Dependent entity?
What is the cardinality
between tables?
DISCRIMINATOR
◼The primary key of a weak entity is a composite key formed from the primary key of the entity and partial key
of the weak entity.
◼Partial key is called discriminator
◼Payment No Is a discriminator of the payment entity
What is the discrimiator of the dependent
entity?
DERIVED ATTRIBUTE
◼A derived attribute is an attribute whose value is calculated (derived) from other
attributes.
◼The derived attribute need not be physically stored within the database
◼A derived attribute is represented by dashed ellipses
Example:
An employee’s age may be found by computing the integer value of the difference between the current date
and the birth date of the employee
Mention derived attribute of the Case 5 (Danish Consumer Council)
Employee
Multivalued Attribute
◼A multi-valued attribute has more than one value for a particular entity
◼Multivalued attribute is represented by double ellipse
Example:
Phone is a multivalued attribute because it is possible for each person to have more than
employee has more than one phone number.
 Skill is a multivalued attribute for EMPLOYEE entity
Is a table/relation with multivalued
attribute in the first Normal form?
Transform of a multivalued attribute
◼A multivalued attribute can be converted to a weak entity
Example:
What are the primary key of Phone entity?
PERSON_ID
FIRST_NAME
LAST_NAME
PHONE
PERSON_ID
FIRST_NAME
LAST_NAME
PHONE NO
PHONE TYPE
PersonPerson
Phone
1 M
COMPOSITE ATTRIBUTE
◼An attribute is considered composite if it comprises two or more other attributes.
Examples
◼Composite attributes are represented by ellipses that are connected with an ellipse (Tree
structure)
Exercise
Identify composite, multivalued, and derived attributes for Customer entity.
SUBQUERY
◼A subquery/Inner query/Nested query is a SQL query within another SQL query and embedded within the
WHERE clause.
◼Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows:
SELECT column_name FROM table
WHERE column_name OPERATOR
(SELECT column_name FROM
table
[WHERE] )
Example
list the first name, last name, salary of the employees, paid more than 'Alexander’
First_name Last_name salary
John Knudsen 12000
Sara Alexander 9000
Lisbeth Nielsen 8000
Employees
EXAMPLE
List of the employees that paid less than ’Randy’
EXAMPLE
Drop database if exists sub_query;
create database sub_query;
Create table employee_SUB (
ID INT NOT NULL PRIMARY KEY,
FirstName varchar(20),
LastName varchar(20),
dept varchar(10),
salary FLOAT(8,2)
);
insert into employee_SUB values(100,'Thomas', 'Larsen','Sales',5000.98);
insert into employee_SUB values(200,'Jason', 'Johansen','Technology',5500.99);
insert into employee_SUB values(300,'Mayla', 'Nielsen','Technology',7000);
insert into employee_SUB values(400,'Nisha', 'Evanovic','Marketing',9500);
insert into employee_SUB values(500,'Randy', 'Pedersen','Technology',6897.55);
EXAMPLE
SELECT * FROM employee_SUB
WHERE
SALARY <
(SELECT SALARY FROM employee_SUB
WHERE
FirstName='Randy');
EXERCISE
List of the employees that paid less than ’Randy’ and more than ‘Thomas’
EXERCISES
List name of faculty with a salary less than the average salary
List of faculty at Technology Departmen with a salary less than the average salary
Logical representations (Subset of data from one or more tables or views)
◼Table: Physical unit
◼View: Logical representation
View
ViewTable
WHY USE A VIEW?
Restrict database access.
Make complex queries easy.
Represent data from different tables.
Represent different Views of the same data.
Creating a View
CREATE [OR REPLACE] VIEW //REPLACE if view already exists
VIEW_NAME
AS
SELECT COLUMN1, COLUMN2,…
FROM TABLE_NAME
WHERE CONDITION;
 EXAMPLE 1
 EXAMPLE 2
CREATE VIEW [List of Chair] AS
SELECT ProductID, ProductName, productprice
FROM Products
WHERE productName = ‘Chair’;
CREATE VIEW [List of products more than 1000] AS
SELECT ProductID, ProductName
FROM Products
WHERE productprice > 1000’;
Example
Create a view called VIEW_DEPT_TECH with Firstname, salary, dept for employees in
Technology department only (Use employee_SUB table)
CREATE VIEW VIEW_DEPT_TECH Name of the view
AS
SELECT Firstname, salary,dept
FROM employee_SUB
WHERE dept='Technology';
Display the data from VIEW_DEPT_TECH view.
SELECT * FROM VIEW_DEPT_TECH;
Create a view called VIEW_SALARY with minimum, maximum, and average of salary
Display the data from VIEW_SALARY view.
Exercises
Exercises
Create a view called VIEW_SALARY_LESS_AVERAGE with name and salary of faculty that have
a salary less than the average salary
Display the data from VIEW_SALARY_LESS_AVERAGE
drop database if exists threetables;
create database threetables;
use threetables;
create table Customers
(
customerID int not null,
customerName varchar(50),
customerLastName varchar(50),
primary key (customerID)
);
create table Products
(
productID int not null,
productName varchar(100),
productPrice float,
primary key (productID)
);
create table Orders
(
OrderID int not null primary key,
customerID int not null,
productID int not null,
Quantity INT,
OrderDate Date,
FOREIGN KEY (customerID) REFERENCES Customers(customerID),
FOREIGN KEY (productID) REFERENCES Products(productID)
);
Exercises
Exercises
insert into Customers values (1,'Steen','Andersen');
insert into Customers values(2,'Helle','Rasmusen');
insert into Customers values(3,'Mette','Nilsen');
insert into Products values(1,'Samsung', 5000.0);
insert into Products values(2,'Apple', 4500.0);
insert into Products values(3,'Asus', 5500.0);
◼ insert into Orders values (1,1,1,3,current_date());
◼ insert into Orders values (2,1,2,5,current_date());
◼ insert into Orders values (3,2,2,1,current_date());
◼ insert into Orders values (4,1,3,2,'2015-02-02');
◼ insert into Orders values (5,2,2,6,'2016-03-02');
Exercises
Create a view called TOTAL with attributes cutomerID, ProductID, ProductPrice, Quantity and
Total amount per order
Display the data from TOTAL
Exercises
Create a view called TOTAL_2017 with attributes cutomerName, ProductName,
OrderID, ProductPrice, Quantity and Total amount per order for 2017
Display the data from TOTAL_2017
READ ONLY OPTIONS
CREATE VIEW PRODUCTS_VIEW
AS
SELECT productName, productPrice FROM Products where productName= 'Apple'
WITH READ ONLY;
//Data may not be modified
Removing a View
DROP VIEW TOTAL;
HAND-IN
30
NAME
CPR
ADDRESS
ZIP_CODE
PHONE
BIRTH_OF_DATE
GENDER
RACE
SMOKER
DRUG_ABUSE
DRIVINGLICENSNUM
DRIVINGLICENSESTATE
NEAREST RELATIVE
PHONE NUMBER OF NEAREST RELATIVE
INSURANCE NUMBER
INSURANCE NAME
DATE OF INSURANCE COVERAGE
REASON OF HOSPITALIZATION
DATE OF HOSPITALIZATION
Draw an ERD with the following information. Identify entitets, weak entities, derived attribute, multivalues
attribute, composite attribute, keys and the cardinalities between the tables. Implements the tables in MYSQL.
Medicine used
Dosage of Medicine
Date medicine started
Date medicine end

Subquery & view

  • 1.
    ERD & Subquery& View April 2017 Business Economics & Information Technology
  • 2.
    OUTLINE  weak entity Derived attribute  Multi-valued attribute  Composite attribute
  • 3.
    WEAK ENTITY ◼A weakentity is an entity that depends on the existence of another entity and cannot be uniquely identified by its attributes ◼A weak entity is represented by double rectangles ◼A weak uses a foreign key combined with its attributed to form the primary key. Example: The order item will be meaningless without an order so it depends on the existence of order . What is the priamry key of Order-Item entity?
  • 4.
    MORE EXAMPLE OFWEAK ENTITES Depends ID DEP_ID DEP_NAME DEP_DOB 20 Lisbeth 22-jul-1985 30 Rasmus 30-OCT-1990 40 Morten NULL EMP_ID EMP_NAME EMP_DOB 100 ALLEN 17-DEC-1955 101 SARA 06-APRIL-1959 102 JOHN 22-FEB-1990 EMP_ID 100 100 101 What is the priamry key of Dependent entity? What is the cardinality between tables?
  • 5.
    DISCRIMINATOR ◼The primary keyof a weak entity is a composite key formed from the primary key of the entity and partial key of the weak entity. ◼Partial key is called discriminator ◼Payment No Is a discriminator of the payment entity What is the discrimiator of the dependent entity?
  • 6.
    DERIVED ATTRIBUTE ◼A derivedattribute is an attribute whose value is calculated (derived) from other attributes. ◼The derived attribute need not be physically stored within the database ◼A derived attribute is represented by dashed ellipses Example: An employee’s age may be found by computing the integer value of the difference between the current date and the birth date of the employee Mention derived attribute of the Case 5 (Danish Consumer Council) Employee
  • 7.
    Multivalued Attribute ◼A multi-valuedattribute has more than one value for a particular entity ◼Multivalued attribute is represented by double ellipse Example: Phone is a multivalued attribute because it is possible for each person to have more than employee has more than one phone number.  Skill is a multivalued attribute for EMPLOYEE entity Is a table/relation with multivalued attribute in the first Normal form?
  • 8.
    Transform of amultivalued attribute ◼A multivalued attribute can be converted to a weak entity Example: What are the primary key of Phone entity? PERSON_ID FIRST_NAME LAST_NAME PHONE PERSON_ID FIRST_NAME LAST_NAME PHONE NO PHONE TYPE PersonPerson Phone 1 M
  • 9.
    COMPOSITE ATTRIBUTE ◼An attributeis considered composite if it comprises two or more other attributes. Examples ◼Composite attributes are represented by ellipses that are connected with an ellipse (Tree structure)
  • 10.
    Exercise Identify composite, multivalued,and derived attributes for Customer entity.
  • 11.
    SUBQUERY ◼A subquery/Inner query/Nestedquery is a SQL query within another SQL query and embedded within the WHERE clause. ◼Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows: SELECT column_name FROM table WHERE column_name OPERATOR (SELECT column_name FROM table [WHERE] )
  • 12.
    Example list the firstname, last name, salary of the employees, paid more than 'Alexander’ First_name Last_name salary John Knudsen 12000 Sara Alexander 9000 Lisbeth Nielsen 8000 Employees
  • 13.
    EXAMPLE List of theemployees that paid less than ’Randy’
  • 14.
    EXAMPLE Drop database ifexists sub_query; create database sub_query; Create table employee_SUB ( ID INT NOT NULL PRIMARY KEY, FirstName varchar(20), LastName varchar(20), dept varchar(10), salary FLOAT(8,2) ); insert into employee_SUB values(100,'Thomas', 'Larsen','Sales',5000.98); insert into employee_SUB values(200,'Jason', 'Johansen','Technology',5500.99); insert into employee_SUB values(300,'Mayla', 'Nielsen','Technology',7000); insert into employee_SUB values(400,'Nisha', 'Evanovic','Marketing',9500); insert into employee_SUB values(500,'Randy', 'Pedersen','Technology',6897.55);
  • 15.
    EXAMPLE SELECT * FROMemployee_SUB WHERE SALARY < (SELECT SALARY FROM employee_SUB WHERE FirstName='Randy');
  • 16.
    EXERCISE List of theemployees that paid less than ’Randy’ and more than ‘Thomas’
  • 17.
    EXERCISES List name offaculty with a salary less than the average salary List of faculty at Technology Departmen with a salary less than the average salary
  • 18.
    Logical representations (Subsetof data from one or more tables or views) ◼Table: Physical unit ◼View: Logical representation View ViewTable
  • 19.
    WHY USE AVIEW? Restrict database access. Make complex queries easy. Represent data from different tables. Represent different Views of the same data.
  • 20.
    Creating a View CREATE[OR REPLACE] VIEW //REPLACE if view already exists VIEW_NAME AS SELECT COLUMN1, COLUMN2,… FROM TABLE_NAME WHERE CONDITION;  EXAMPLE 1  EXAMPLE 2 CREATE VIEW [List of Chair] AS SELECT ProductID, ProductName, productprice FROM Products WHERE productName = ‘Chair’; CREATE VIEW [List of products more than 1000] AS SELECT ProductID, ProductName FROM Products WHERE productprice > 1000’;
  • 21.
    Example Create a viewcalled VIEW_DEPT_TECH with Firstname, salary, dept for employees in Technology department only (Use employee_SUB table) CREATE VIEW VIEW_DEPT_TECH Name of the view AS SELECT Firstname, salary,dept FROM employee_SUB WHERE dept='Technology'; Display the data from VIEW_DEPT_TECH view. SELECT * FROM VIEW_DEPT_TECH;
  • 22.
    Create a viewcalled VIEW_SALARY with minimum, maximum, and average of salary Display the data from VIEW_SALARY view. Exercises
  • 23.
    Exercises Create a viewcalled VIEW_SALARY_LESS_AVERAGE with name and salary of faculty that have a salary less than the average salary Display the data from VIEW_SALARY_LESS_AVERAGE
  • 24.
    drop database ifexists threetables; create database threetables; use threetables; create table Customers ( customerID int not null, customerName varchar(50), customerLastName varchar(50), primary key (customerID) ); create table Products ( productID int not null, productName varchar(100), productPrice float, primary key (productID) ); create table Orders ( OrderID int not null primary key, customerID int not null, productID int not null, Quantity INT, OrderDate Date, FOREIGN KEY (customerID) REFERENCES Customers(customerID), FOREIGN KEY (productID) REFERENCES Products(productID) ); Exercises
  • 25.
    Exercises insert into Customersvalues (1,'Steen','Andersen'); insert into Customers values(2,'Helle','Rasmusen'); insert into Customers values(3,'Mette','Nilsen'); insert into Products values(1,'Samsung', 5000.0); insert into Products values(2,'Apple', 4500.0); insert into Products values(3,'Asus', 5500.0); ◼ insert into Orders values (1,1,1,3,current_date()); ◼ insert into Orders values (2,1,2,5,current_date()); ◼ insert into Orders values (3,2,2,1,current_date()); ◼ insert into Orders values (4,1,3,2,'2015-02-02'); ◼ insert into Orders values (5,2,2,6,'2016-03-02');
  • 26.
    Exercises Create a viewcalled TOTAL with attributes cutomerID, ProductID, ProductPrice, Quantity and Total amount per order Display the data from TOTAL
  • 27.
    Exercises Create a viewcalled TOTAL_2017 with attributes cutomerName, ProductName, OrderID, ProductPrice, Quantity and Total amount per order for 2017 Display the data from TOTAL_2017
  • 28.
    READ ONLY OPTIONS CREATEVIEW PRODUCTS_VIEW AS SELECT productName, productPrice FROM Products where productName= 'Apple' WITH READ ONLY; //Data may not be modified
  • 29.
  • 30.
    HAND-IN 30 NAME CPR ADDRESS ZIP_CODE PHONE BIRTH_OF_DATE GENDER RACE SMOKER DRUG_ABUSE DRIVINGLICENSNUM DRIVINGLICENSESTATE NEAREST RELATIVE PHONE NUMBEROF NEAREST RELATIVE INSURANCE NUMBER INSURANCE NAME DATE OF INSURANCE COVERAGE REASON OF HOSPITALIZATION DATE OF HOSPITALIZATION Draw an ERD with the following information. Identify entitets, weak entities, derived attribute, multivalues attribute, composite attribute, keys and the cardinalities between the tables. Implements the tables in MYSQL. Medicine used Dosage of Medicine Date medicine started Date medicine end