introduction to database lei yang computer science department

21
Introduction to Database Lei Yang Computer Science Department

Upload: arthur-casey

Post on 28-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Database Lei Yang Computer Science Department

Introduction to DatabaseLei YangComputer Science Department

Page 2: Introduction to Database Lei Yang Computer Science Department

Why we use database?You have a company with more

than 10000 employees…Someday, you want to find out

◦The average salary of employees who own Ph.d degree…

◦The average age of your company…◦…

Manually?No!

An example

Page 3: Introduction to Database Lei Yang Computer Science Department

Manage Employees in the CompanyEmployee Information

◦Name: Jack, Ram…◦Gender: Male, Female◦Degree: Bachelor, Master, Ph.d…◦Department: HR, Market

Department…◦Job: Engineer, Salesman, …◦Salary: 10k, 15k, …◦Manager: Jason, …◦…Problem: How to store these data?

Page 4: Introduction to Database Lei Yang Computer Science Department

TableDatabase Store Data in Tables

Employee ID Name Gender

Degree

Salary

001 James Male Ph.D 15k

002 Lina Female MS 10k

003 Bogdan Male MS 12k

004 Mary Female Ph.D 14k

ROW: a record

COLUMNA field or an

attributeNUMBE

RText Currency

Question: how to distinguish two employee with nearly the same information?

Page 5: Introduction to Database Lei Yang Computer Science Department

Primary Key

Employee ID Name Gender

Degree

Salary

001 James Male Ph.D 15k

002 James Male Ph.D 15k

Primary Key

Unique

Page 6: Introduction to Database Lei Yang Computer Science Department

Data Type - 1Text

◦ Stores text, numbers, or a combination of both, up to 255 characters long. Text fields are the most common of all data types.

Memo◦ Stores long text entries up to 64,000 characters

long.Number: int, float…Date/Time

◦ Stores dates, times, or both.Currency

◦ Stores numbers and symbols that represent money.

Page 7: Introduction to Database Lei Yang Computer Science Department

Data Type - 2AutoNumber

◦ Automatically fills in a unique number for each record. AutoNumber is used as primary key in many tables.

Yes/No◦ Stores only one of two values, such as Yes or No,

True or False, etc.OLE Object

◦ Stores objects created in other programs such as a graphic, Excel spreadsheet, or Word document.

Hyperlink◦ Stores clickable links to files on your computer, on

the network, or to Web pages on the Internet.

Page 8: Introduction to Database Lei Yang Computer Science Department

Data Type - 3Lookup Wizard

◦ A wizard that helps you create a field whose values are selected from a table, query, or a preset list of values.

Page 9: Introduction to Database Lei Yang Computer Science Department

Query Answer in Database-1

Query: Find out the names of all employees whose salary is greater than 12k?

Employee ID Name Gender Degree

Salary

001 James Male Ph.D 15k

002 Lina Female MS 10k

003 Bogdan Male MS 12k

004 Mary Female Ph.D 14k

Page 10: Introduction to Database Lei Yang Computer Science Department

Query in Database-2

Employee ID Name Gender Degree

Salary

001 James Male Ph.D 15k

002 Lina Female MS 10k

003 Bogdan Male MS 12k

004 Mary Female Ph.D 14k

SELECTION PROJECTION

James, Mary

Page 11: Introduction to Database Lei Yang Computer Science Department

Query in Database-3SELECTION

◦ Selecting records which satisfies certain conditions

Projection◦ Projecting a field into query simply means

including it in the query

Page 12: Introduction to Database Lei Yang Computer Science Department

Query in Database-4SQL(Structured Query Language)

◦Used in Database◦A simple structure

SELECT <fields> FROM <table> WHERE <condition>

An example show how it works

Page 13: Introduction to Database Lei Yang Computer Science Department

Query in Database-4Query: Find out the names of all employees

whose salary is greater than 12k?

SELECT FROM WHERE

<field><table

><condition>

NameEmploye

eSalary > 12k

PROJECTION

SELECTION

SELECTION: Selecting records which satisfies certain conditions.

PROJECTION: Projecting a field into query simply means including it in the query

Page 14: Introduction to Database Lei Yang Computer Science Department

Query in Database-5How to search in two tables?

Employee ID Name DptID

001 Jimy D004

002 Karl D005

DptID DptName

D004 HR

D005 ITFind out the name of employees in HR?

Page 15: Introduction to Database Lei Yang Computer Science Department

Query in Database-6Cartesian Product

Employee ID

Name

DptID

001 Jimy D004

002 Karl D005

DptID DptName

D004 HR

D005 ITX

=

Employee ID

Name DptID DptID DptName

001 Jimy D004 D004 HR

001 Jimy D004 D005 IT

002 Karl D005 D004 HR

002 Karl D005 D005 IT

Employee

Department

Page 16: Introduction to Database Lei Yang Computer Science Department

Query in Database-7

SELECT <Name>FROM <Employee, Department>WHERE <DepartmentName = “HR” and

Employee.DptID = Department.DptID>

Employee ID

Name DptID DptID Department Name

001 Jimy D004 D004 HR

001 Jimy D004 D005 IT

002 Karl D005 D004 HR

002 Karl D005 D005 IT

JOINCartesianProduct

Page 17: Introduction to Database Lei Yang Computer Science Department

Query in Database-8Operators in conditions

Operator Example Description

= = “Apple” Finds records equal to Apple.

< < 10 Finds records less than 10.

<= <= 10 Finds records less than or equal to 10.

> > 10 Finds records greater than 10.

>= Finds records greater than or equal to 10.

LIKE Like “test*”

Finds text beginning with the word “test." You can use LIKE withwildcards such as *.

Page 18: Introduction to Database Lei Yang Computer Science Department

Query in Database-9Aggregate Function

Employee ID Name DptID

001 Jimy D004

002 Karl D005

003 Laura D004

005 Ram D004

006 James D005

How many employees are there in department D004?

Page 19: Introduction to Database Lei Yang Computer Science Department

Query in Database-10

SELECT count(EmployeeID) FROM <Employee> HAVING DptID = “D004”

Employee ID Name DptID

001 Jimy D004

002 Karl D005

003 Laura D004

005 Ram D004

006 James D005

Aggregate Function

Page 20: Introduction to Database Lei Yang Computer Science Department

Query in Database-11Aggregate Functions

Function Description

Group By Groups the values in the field so that you can perform calculations on the groups

Sum Calculates the total (sum) of values in a field.

Avg Calculates the average of values in a field.

Count Counts the number of entries in a field, not including blank (Null) records.

Max Finds the highest value in a field.

… …

Page 21: Introduction to Database Lei Yang Computer Science Department

Question