Database Management The single entity, the single table, plus some basic SQL
Data Modeling – Top-down approach Data Model Data Definition Database Table
The database development lifecycle (DDLC)… and the Term Project Assignments Assignment 1: Data Model Assignment 1: Database Dictionary Assignment 2: Database Prototype Assignment 3: SQL Queries Completed by Instructor
Essential Terminology Entity A category representing a type of person, place, thing or event that we want to keep information about. Attribute A characteristic of an entity that we keep information about. Relation A 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.
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 attributes
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
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)
Data modeling – representing the single entity Watson’s looks like this: Ours will look like this: Underline = primary key Asterix = primary key
Primary Key Rules Unique Not Null Null means No Value Not zero Not empty string, “”
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:
shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper PTPatagonian Tea ARAbyssinian Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese CSCanadian Sugar ROFRoyal Ostrich Farms Example: the shares table, “shr”
Your RDBMS will typically give you two options… o QBE (Query By Example), a GUI-based interface for creating, as well as modifying, populating, and querying database tables. oThe SQL Create Statement. Example: CREATE TABLE TRACK (TrkidCHAR(4) NOT NULL, TrknumINT(4), TrktitleCHAR(30), TrklengthDecimal(4,2), PRIMARY KEY(Trkid) ) Creating the actual table in the actual database …
NameClassSizeDescription Intinteger4 byteswhole numbers from - 2,147,483,648 to 2,147,483,647 Decimal(p,s)decimalvariesdecimal numbers with range (1 – 38) and scale (0 – p) Float (n)float4 or 8approximate numbers -1.79E to 1.79E Moneymoney8 bytesmoney from ^63 to ^63 Datetimedate/time8 bytesfrom 1/1/1753 to 12/31/9999 Char (n)charactervariesfixed length field; max length of 8000 Varchar (n)charactervariesvariable length field; max length of 8000 Textcharactervariesmax length of 2,147,483,647 characters SQL Server Data Types A subset of commonly used SQL Server datatypes
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
INSERT INTO shr VALUES ('FC','Freedonia Copper',27.5,10529,1.84,16) UPDATE shr SET shrprice = WHERE shrcode = 'FC' SELECT * FROM shr DELETE FROM shr WHERE shrfirm = 'Burmese Elephant ' SQL Syntax
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.
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
shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper PTPatagonian Tea ARAbyssinian Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese CSCanadian Sugar ROFRoyal Ostrich Farms SELECT * FROM shr
shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper PTPatagonian Tea ARAbyssinian Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese CSCanadian Sugar ROFRoyal Ostrich Farms Projection SELECT shrcode, shrprice, shrdiv, shrpe FROM shr
shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper PTPatagonian Tea ARAbyssinian Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese CSCanadian Sugar ROFRoyal Ostrich Farms Restrict SELECT * FROM shr WHERE shrpe > 10
shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper PTPatagonian Tea ARAbyssinian Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese CSCanadian Sugar ROFRoyal Ostrich Farms Combined Projection & Restriction SELECT shrcode, shrprice, shrdiv, shrpe FROM shr WHERE shrpe > 10
Query Functions and Operators Arithmetic: + - * / Aggregate: sum, avg, max, min Comparison: =, =, >, between Logical: not, or, and Set: count, distinct, in
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
WHERE clause Example SELECT * FROM shr WHERE shrpe < 12 shrcodeshrfirmshrpriceshrqtyshrdivshrpe BEBurmese Elephant BSBolivian Sheep NGNigerian Geese PTPatagonian Tea ROFRoyal Ostrich Farms
WHERE clause examples SELECT * FROM shr WHERE shrcode = 'AR' shrcodeshrfirmshrpriceshrqtyshrdivshrpe ARAbyssinian Ruby SELECT * FROM shr WHERE shrcode IN ('FC','AR','SLG') shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper ARAbyssinian Ruby SLGSri Lankan Gold
Order by example SELECT * FROM shr WHERE shrpe >= 12 ORDER BY shrpe DESC; shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper SLGSri Lankan Gold CSCanadian Sugar ARAbyssinian Ruby ILZIndian Lead & Zinc
Pattern matching examples SELECT shrfirm FROM shr WHERE shrfirm LIKE 'B%' shrfirm Burmese Elephant Bolivian Sheep SELECT * FROM shr WHERE shrpe LIKE '%6' shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper ROFRoyal Ostrich Farms SLGSri Lankan Gold SELECT * FROM shr WHERE shrpe LIKE ‘_6' shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper SLGSri Lankan Gold
COUNT and DISTINCT SELECT COUNT(*) FROM shr COUNT(*) 10 SELECT DISTINCT(shrpe ) AS PE FROM shr PE
COUNT and DISTINCT SELECT COUNT(DISTINCT shrpe) AS 'Unique PE' FROM shr Unique PE 8
Subquery Select firm that has the maximum price SELECT shrfirm, shrprice FROM shr WHERE shrprice = (select max(price) from shr)