Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Database Management The single entity, the single table, plus some basic SQL."— Presentation transcript:

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

2 Data Modeling – Top-down approach Data Model Data Definition Database Table

3 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

4 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.

5 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

6 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

7 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)

8 Data modeling – representing the single entity Watson’s looks like this: Ours will look like this: Underline = primary key Asterix = primary key

9 Primary Key Rules Unique Not Null Null means No Value Not zero Not empty string, “”

10 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:

11 shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead & Zinc37.7563903.0012 BEBurmese Elephant0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms33.7512349233.006 Example: the shares table, “shr”

12 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 …

13 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 + 308 to 1.79E + 308 Moneymoney8 bytesmoney from -2.0000^63 to 2.0000^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

14 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

15 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

16 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.

17 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

18 shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper 27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead & Zinc 37.7563903.0012 BEBurmese Elephant 0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms 33.7512349233.006 SELECT * FROM shr

19 shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead & Zinc 37.7563903.0012 BEBurmese Elephant 0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms 33.7512349233.006 Projection SELECT shrcode, shrprice, shrdiv, shrpe FROM shr

20 shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper 27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead & Zinc 37.7563903.0012 BEBurmese Elephant 0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms 33.7512349233.006 Restrict SELECT * FROM shr WHERE shrpe > 10

21 shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 PTPatagonian Tea55.25126352.5010 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816 ILZIndian Lead & Zinc37.7563903.0012 BEBurmese Elephant0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 CSCanadian Sugar52.7847162.5015 ROFRoyal Ostrich Farms 33.7512349233.006 Combined Projection & Restriction SELECT shrcode, shrprice, shrdiv, shrpe FROM shr WHERE shrpe > 10

22 Query Functions and Operators Arithmetic: + - * / Aggregate: sum, avg, max, min Comparison: =, =, >, between Logical: not, or, and Set: count, distinct, in

23 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

24 WHERE clause Example SELECT * FROM shr WHERE shrpe < 12 shrcodeshrfirmshrpriceshrqtyshrdivshrpe BEBurmese Elephant.07154713.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 PTPatagonian Tea55.25126352.5010 ROFRoyal Ostrich Farms33.7512349233.006

25 WHERE clause examples SELECT * FROM shr WHERE shrcode = 'AR' shrcodeshrfirmshrpriceshrqtyshrdivshrpe ARAbyssinian Ruby31.82220101.3213 SELECT * FROM shr WHERE shrcode IN ('FC','AR','SLG') shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 ARAbyssinian Ruby31.82220101.3213 SLGSri Lankan Gold50.37328682.6816

26 Order by example SELECT * FROM shr WHERE shrpe >= 12 ORDER BY shrpe DESC; shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 SLGSri Lankan Gold50.37328682.6816 CSCanadian Sugar52.7847162.5015 ARAbyssinian Ruby31.82220101.3213 ILZIndian Lead & Zinc37.7563903.0012

27 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 Copper27.50105291.8416 ROFRoyal Ostrich Farms33.7512349233.006 SLGSri Lankan Gold50.37328682.6816 SELECT * FROM shr WHERE shrpe LIKE ‘_6' shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 SLGSri Lankan Gold50.37328682.6816

28 COUNT and DISTINCT SELECT COUNT(*) FROM shr COUNT(*) 10 SELECT DISTINCT(shrpe ) AS PE FROM shr PE 6 10 11 12 13 15 16

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

30 Subquery Select firm that has the maximum price SELECT shrfirm, shrprice FROM shr WHERE shrprice = (select max(price) from shr)


Download ppt "Database Management The single entity, the single table, plus some basic SQL."

Similar presentations


Ads by Google