30
Database Management The single entity, the single table, plus some basic SQL

Database Management The single entity, the single table, plus some basic SQL

Embed Size (px)

Citation preview

Page 1: Database Management The single entity, the single table, plus some basic SQL

Database Management

The single entity,the single table,

plus some basic SQL

Page 2: Database Management The single entity, the single table, plus some basic SQL

Data Modeling – Top-down approach

Data Model

Data Definition

Database Table

Page 3: Database Management The single entity, the single table, plus some basic SQL

The database development lifecycle (DDLC)…and the Term Project Assignments

Assignment 1:Data Model

Assignment 1: Database Dictionary

Assignment 2: Database Prototype

P R O JE C TP L A N N IN G

E V O L U T IO N

U S E

IM P L E M E N T A T IO N

C O N S T R U C T IO N

D E S IG N

R EQ UIR EM EN T SD EF IN IT IO N

Assignment 3: SQL Queries

Completed byInstructor

Page 4: Database Management The single entity, the single table, plus some basic SQL

Essential Terminology

EntityA category representing a type of person, place, thing or event that we want to keep information about.

AttributeA characteristic of an entity that we keep information about.

RelationA two-dimensional table, with rows (or records) representing real-world instances of the entity in question, and columns (or fields) representing the attributes of interest.

Identifier (primary key)A field (or combination of fields) that takes on a unique value for each record in the relation, and is used to distinguish that record from all others.

Page 5: Database Management The single entity, the single table, plus some basic SQL

• Some thing in the environment

• Represented by a rectangle

• A horizontal line separates the name from the attribute list

• An instance is a particular occurrence of an entity

An entity

SHARE

attributes

Page 6: Database Management The single entity, the single table, plus some basic SQL

Attributes

• An attribute is a discrete data element that describes an entity

• Attribute names must be unique within a data model

• Attribute names must be meaningful

SHARE

share codeshare nameshare price

share quantityshare dividend

share PE

Page 7: Database Management The single entity, the single table, plus some basic SQL

Identifiers

• Every instance of an entity must be uniquely identified

• An identifier can be a single attribute or a collection of attributes

• An identifier can be created if there is no obvious attribute(s)

• Identifiers are underlined. (Watson uses *asterix)

S HAR E

S h ar e c o d eS h ar e n am eS h ar e p r ic eS h ar e q u an tityS h ar e d iv id en dS h ar e P E

Page 8: Database Management The single entity, the single table, plus some basic SQL

Data modeling – representing the single entity

Watson’s looks like this:

Ours will look like this:

Underline = primary key

S HAR E

* s h ar e c o d es h ar e n am es h ar e p r ic e

s h ar e q u an titys h ar e d iv id en d

s h ar e P E

S HAR E

S h ar e c o d eS h ar e n am eS h ar e p r ic eS h ar e q u an tityS h ar e d iv id en dS h ar e P E

Asterix = primary key

Page 9: Database Management The single entity, the single table, plus some basic SQL

Primary Key Rules

• Unique• Not Null

Null means

• No Value• Not zero• Not empty string, “”

Page 10: Database Management The single entity, the single table, plus some basic SQL

o An entity in the data model becomes a table (relation) in the database.

o The attributes of the entity in the data model become the fields (columns) in the table in the database.

o The entity’s unique identifier in the data model becomes the table’s primary key in the database.

Plus: Instances are represented by records (rows) in the table.

Database design for beginners:

Page 11: Database Management The single entity, the single table, plus some basic SQL

shrcode shrfirm shrprice

shrqty shrdiv shrpe

FC Freedonia Copper 27.50 10529 1.84 16

PT Patagonian Tea 55.25 12635 2.50 10

AR Abyssinian Ruby 31.82 22010 1.32 13

SLG Sri Lankan Gold 50.37 32868 2.68 16

ILZ Indian Lead & Zinc

37.75 6390 3.00 12

BE Burmese Elephant

0.07 154713 0.01 3

BS Bolivian Sheep 12.75 231678 1.78 11

NG Nigerian Geese 35.00 12323 1.68 10

CS Canadian Sugar 52.78 4716 2.50 15

ROF Royal Ostrich Farms

33.75 1234923

3.00 6

Example: the shares table, “shr”

Page 12: Database Management The single entity, the single table, plus some basic SQL

Your RDBMS will typically give you two options…

o QBE (Query By Example), a GUI-based interfacefor creating, as well as modifying, populating, and querying database tables.

o The SQL Create Statement. Example:

CREATE TABLE TRACK(Trkid CHAR(4) NOT NULL, Trknum INT(4), Trktitle CHAR(30), Trklength Decimal(4,2), PRIMARY KEY(Trkid) )

Creating the actual table in the actual database …

Page 13: Database Management The single entity, the single table, plus some basic SQL

Name Class Size DescriptionInt integer 4 bytes whole numbers from -

2,147,483,648 to 2,147,483,647

Decimal(p,s) decimal varies decimal numbers with range (1 – 38) and scale (0 – p)

Float (n) float 4 or 8 approximate numbers -1.79E + 308 to 1.79E + 308

Money money 8 bytes money from -2.0000^63 to 2.0000^63

Datetime date/time 8 bytes from 1/1/1753 to 12/31/9999

Char (n) character varies fixed length field; max length of 8000

Varchar (n) character varies variable length field; max length of 8000

Text character varies max length of 2,147,483,647 characters

SQL Server Data Types

A subset of commonly used SQL Server datatypes

Page 14: Database Management The single entity, the single table, plus some basic SQL

SQL (Structured Query Language)4GL, Non-procedural language for working with

relations and manipulating data

• INSERT

• UPDATE

• DELETE

• SELECT

Create records

Change records

Remove records

Retrieve records

Page 15: Database Management The single entity, the single table, plus some basic SQL

INSERT INTO shr VALUES ('FC','Freedonia Copper',27.5,10529,1.84,16)

UPDATE shr SET shrprice = 31.50

WHERE shrcode = 'FC'

SELECT * FROM shr

DELETE FROM shr

WHERE shrfirm = 'Burmese Elephant'

SQL Syntax

Page 16: Database Management The single entity, the single table, plus some basic SQL

SELECT Syntax

SELECT col1, col2, …FROM table1, table2, … [ WHERE search_condition

AND search_condition

OR search_condition] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ]]

• SELECT, columns, FROM and table names are mandatory. The rest are optional.

• SELECT * will select all columns.

Page 17: Database Management The single entity, the single table, plus some basic SQL

The SELECT statement: Retrieving records. Retrieving selected fields, or “projection.” Retrieving selected records, or “restriction”

(WHERE clause, logical AND, logical OR, comparison operators, IN & NOT IN).

Ordering columns. Ordering records (ORDER BY, DESC). Derived data through SQL functions (COUNT,

AVG, SUM, MIN, MAX). Creating an alias for a results column (AS) Pattern matching (LIKE, %, _ ) Eliminating duplicate records (DISTINCT)

Query Options

Page 18: Database Management The single entity, the single table, plus some basic SQL

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper

27.50 10529 1.84 16

PT Patagonian Tea

55.25 12635 2.50 10

AR Abyssinian Ruby

31.82 22010 1.32 13

SLG Sri Lankan Gold

50.37 32868 2.68 16

ILZ Indian Lead & Zinc

37.75 6390 3.00 12

BE Burmese Elephant

0.07 154713 0.01 3

BS Bolivian Sheep

12.75 231678 1.78 11

NG Nigerian Geese

35.00 12323 1.68 10

CS Canadian Sugar

52.78 4716 2.50 15

ROF Royal Ostrich Farms

33.75 1234923

3.00 6

SELECT * FROM shr

Page 19: Database Management The single entity, the single table, plus some basic SQL

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper

27.50 10529 1.84 16

PT Patagonian Tea

55.25 12635 2.50 10

AR Abyssinian Ruby

31.82 22010 1.32 13

SLG Sri Lankan Gold

50.37 32868 2.68 16

ILZ Indian Lead & Zinc

37.75 6390 3.00 12

BE Burmese Elephant

0.07 154713 0.01 3

BS Bolivian Sheep 12.75 231678 1.78 11

NG Nigerian Geese

35.00 12323 1.68 10

CS Canadian Sugar

52.78 4716 2.50 15

ROF Royal Ostrich Farms

33.75 1234923

3.00 6

Projection

SELECT shrcode, shrprice, shrdiv, shrpe FROM shr

Page 20: Database Management The single entity, the single table, plus some basic SQL

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper

27.50 10529 1.84 16

PT Patagonian Tea

55.25 12635 2.50 10

AR Abyssinian Ruby

31.82 22010 1.32 13

SLG Sri Lankan Gold

50.37 32868 2.68 16

ILZ Indian Lead & Zinc

37.75 6390 3.00 12

BE Burmese Elephant

0.07 154713 0.01 3

BS Bolivian Sheep

12.75 231678 1.78 11

NG Nigerian Geese

35.00 12323 1.68 10

CS Canadian Sugar

52.78 4716 2.50 15

ROF Royal Ostrich Farms

33.75 1234923

3.00 6

Restrict

SELECT * FROM shr WHERE shrpe > 10

Page 21: Database Management The single entity, the single table, plus some basic SQL

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper

27.50 10529 1.84 16

PT Patagonian Tea 55.25 12635 2.50 10

AR Abyssinian Ruby

31.82 22010 1.32 13

SLG Sri Lankan Gold

50.37 32868 2.68 16

ILZ Indian Lead & Zinc

37.75 6390 3.00 12

BE Burmese Elephant

0.07 154713 0.01 3

BS Bolivian Sheep 12.75 231678 1.78 11

NG Nigerian Geese 35.00 12323 1.68 10

CS Canadian Sugar

52.78 4716 2.50 15

ROF Royal Ostrich Farms

33.75 1234923

3.00 6

Combined Projection & Restriction

SELECT shrcode, shrprice, shrdiv, shrpe FROM shr WHERE shrpe > 10

Page 22: Database Management The single entity, the single table, plus some basic SQL

Query Functions and Operators

• Arithmetic: + - * /

• Aggregate: sum, avg, max, min

• Comparison: =, <=, >=, >, <, <> between

• Logical: not, or, and

• Set: count, distinct, in

Page 23: Database Management The single entity, the single table, plus some basic SQL

Report a firm’s name and price–earnings ratio.SELECT shrfirm, shrpe FROM shr

Get all firms with a price-earnings ratio less than 12.SELECT * FROM shr WHERE shrpe < 12

Report firms whose code is AR.SELECT * FROM shr WHERE shrcode = 'AR’

Report data on firms with codes of FC, AR, or SLG.SELECT * FROM shr WHERE shrcode IN ('FC','AR','SLG');

List all firms where PE is at least 12, and order by descending PE.SELECT * FROM shr WHERE shrpe >= 12

ORDER BY shrpe DESC;

List all firms with a name starting with ‘F’.SELECT shrfirm FROM shr WHERE shrfirm LIKE 'F%‘

Find the number of different PE ratios.SELECT COUNT(DISTINCT shrpe) AS ‘Unique PE' FROM shr

Page 24: Database Management The single entity, the single table, plus some basic SQL

WHERE clause Example

SELECT * FROM shr WHERE shrpe < 12

shrcode shrfirm shrprice shrqty shrdiv shrpe

BE Burmese Elephant .07 154713 .01 3

BS Bolivian Sheep 12.75 231678 1.78 11

NG Nigerian Geese 35.00 12323 1.68 10

PT Patagonian Tea 55.25 12635 2.50 10

ROF Royal Ostrich Farms 33.75 1234923 3.00 6

Page 25: Database Management The single entity, the single table, plus some basic SQL

WHERE clause examplesSELECT * FROM shr WHERE shrcode = 'AR'

shrcode

shrfirm shrprice

shrqty shrdiv

shrpe

AR Abyssinian Ruby

31.82 22010 1.32 13

SELECT * FROM shr WHERE shrcode IN ('FC','AR','SLG')

shrcode

shrfirm shrprice

shrqty shrdiv shrpe

FC Freedonia Copper

27.50 10529 1.84 16

AR Abyssinian Ruby

31.82 22010 1.32 13

SLG Sri Lankan Gold

50.37 32868 2.68 16

Page 26: Database Management The single entity, the single table, plus some basic SQL

Order by example

SELECT * FROM shr WHERE shrpe >= 12

ORDER BY shrpe DESC;

shrcode shrfirm shrprice

shrqty shrdiv shrpe

FC Freedonia Copper 27.50 10529 1.84 16

SLG Sri Lankan Gold 50.37 32868 2.68 16

CS Canadian Sugar 52.78 4716 2.50 15

AR Abyssinian Ruby 31.82 22010 1.32 13

ILZ Indian Lead & Zinc 37.75 6390 3.00 12

Page 27: Database Management The single entity, the single table, plus some basic SQL

Pattern matching examplesSELECT shrfirm FROM shr WHERE shrfirm LIKE 'B%'

shrfirm

Burmese Elephant

Bolivian Sheep

SELECT * FROM shr WHERE shrpe LIKE '%6'

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper 27.50 10529 1.84 16

ROF Royal Ostrich Farms 33.75 1234923 3.00 6

SLG Sri Lankan Gold 50.37 32868 2.68 16SELECT * FROM shr WHERE shrpe LIKE ‘_6'

shrcode shrfirm shrprice shrqty shrdiv shrpe

FC Freedonia Copper 27.50 10529 1.84 16

SLG Sri Lankan Gold 50.37 32868 2.68 16

Page 28: Database Management The single entity, the single table, plus some basic SQL

COUNT and DISTINCTSELECT COUNT(*) FROM shr

COUNT(*)

10

SELECT DISTINCT(shrpe) AS PE FROM shr

PE

6

10

11

12

13

15

16

Page 29: Database Management The single entity, the single table, plus some basic SQL

COUNT and DISTINCT

SELECT COUNT(DISTINCT shrpe) AS 'Unique PE' FROM shr

Unique PE

8

Page 30: Database Management The single entity, the single table, plus some basic SQL

Subquery

• Select firm that has the maximum price

SELECT shrfirm, shrprice FROM shr WHERE shrprice = (select max(price) from shr)