Presentation is loading. Please wait.

Presentation is loading. Please wait.

The single entity. Modeling reality A database must mirror the real world if it is to answer questions about the real world Data modeling is a design.

Similar presentations


Presentation on theme: "The single entity. Modeling reality A database must mirror the real world if it is to answer questions about the real world Data modeling is a design."— Presentation transcript:

1 The single entity

2 Modeling reality A database must mirror the real world if it is to answer questions about the real world Data modeling is a design technique for capturing reality

3 An entity Some thing in the environment Represented by a rectangle An instance is a particular occurrence of an entity

4 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

5 Identifiers Every instance of an entity must be uniquely identified An identifier can be an attribute or collection of attributes An identifier can be created if there is no obvious attribute(s) A leading asterisk denotes an identifier

6 Rules for creating a table Each entity becomes a table The entity name becomes the table name Each attribute becomes a column The identifier becomes the primary key

7 Defining a table CREATE TABLE shr ( shrcodeCHAR(3), shrfirmVARCHAR(20)NOT NULL, shrpriceDECIMAL(6,2), shrqtyDECIMAL(8), shrdivDECIMAL(5,2), shrpeDECIMAL(2), PRIMARY KEY(shrcode));

8 Defining a table

9 Allowable data types DB2 NumericintegerA 31-bit signed binary value smallintA 15-bit signed binary value float(p)A scientific format number of p binary digits precision decimal(p,q)A packed decimal number of p digits total length; q decimal places to the right of the decimal point may be specified Stringchar(n)A fixed length character string of n characters varchar(n)A variable length character string up to n characters long varcharA variable length character string Date/timedateDate in the form yyyymmdd timeTime in the form hhmmss timestampA combination of date and time to the nearest microsecond Graphicgraphic(n)A fixed length graphic string of n 16-bit bytes vargraphic(n)A variable length graphic string of up to n 16-bit bytes long vargraphicA variable length graphic string of 16-bit bytes

10 Allowable data types MS Access TextA variable length character string of up to 255 characters MemoA variable length character string of up to 64,000 characters NumberByteA 8-bit unsigned binary value IntegerA 15-bit signed binary value Long IntegerA 32-bit signed binary value SingleA signed number with an exponent in the range -45 to +38 DoubleA signed number with an exponent in the range -324 to +308 Date/timeA formatted date or time for the years 100 through 9999 CurrencyA monetary value AutoNumberA unique sequential number or random number assigned by Access whenever a new record is added to a table Yes/NoA binary field that contains one of two values (Yes/No, True/False, or On/Off) OLE ObjectAn object, such as a spreadsheet, document, graphic, sound, or other binary data. HyperlinkA hyperlink address (e.g., a URL)

11 Inserting rows INSERT INTO shr (shrcode,shrfirm,shrprice,shrqty,shrdiv,shrpe) VALUES ('FC','Freedonia Copper',27.5,10529,1.84,16); Or INSERT INTO shr VALUES ('FC','Freedonia Copper',27.5,10529,1.84,16);

12 Inserting rows

13 The SHR table shr 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

14 Querying a table List all data in the share table. SELECT * FROM shr; 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

15 Project Choosing columns A vertical slice shr 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

16 Project Report a firm’s name and price–earnings ratio. SELECT shrfirm, shrpe FROM shr; shrfirmshrpe Freedonia Copper16 Patagonian Tea10 Abyssinian Ruby13 Sri Lankan Gold16 Indian Lead & Zinc12 Burmese Elephant3 Bolivian Sheep11 Nigerian Geese10 Canadian Sugar15 Royal Ostrich Farms6

17 Restrict Choosing rows A horizontal slice shr 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

18 Restrict Get all firms with a price-earnings ratio less than 12. SELECT * FROM shr WHERE shrpe < 12; shrcodeshrfirmshrpriceshrqtyshrdivshrpe PTPatagonian Tea55.25126352.5010 BEBurmese Elephant0.071547130.013 BSBolivian Sheep12.752316781.7811 NGNigerian Geese35.00123231.6810 ROFRoyal Ostrich Farms33.7512349233.006

19 Project and restrict combo Choosing rows and columns List the firm’s name, price, quantity, and dividend where share holding is at least 100,000. SELECT shrfirm, shrprice, shrqty, shrdiv FROM shr WHERE shrqty >= 100000; shrfirmshrpriceshrqtyshrdiv Burmese Elephant0.071547130.01 Bolivian Sheep12.752316781.78 Royal Ostrich Farms33.7512349233.00

20 Primary key retrieval A query using the primary key returns at most one row Report firms whose code is AR. SELECT * FROM shr WHERE shrcode = 'AR'; shrcodeshrfirmshrpriceshrqtyshrdivshrpe ARAbyssinian Ruby 31.82 22010 1.32 13

21 Primary key retrieval A query not using the primary key can return more than one row Report firms with a dividend of 2.50. SELECT * FROM shr WHERE shrdiv = 2.5; shrcodeshrfirmshrpriceshrqtyshrdivshrpe PTPatagonian Tea 55.25 12635 2.50 10 CSCanadian Sugar 52.78 4716 2.50 15

22 IN Used with a list of values Report data on firms with codes of FC, AR, or SLG. SELECT * FROM shr WHERE shrcode IN ('FC','AR','SLG'); or SELECT * FROM shr WHERE shrcode = 'FC' OR shrcode = 'AR' OR shrcode = 'SLG'; shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper 27.50 10529 1.84 16 ARAbyssinian Ruby 31.82 22010 1.32 13 SLGSri Lankan Gold 50.37 32868 2.68 16

23 NOT IN Not in a list of values Report all firms other than those with the code CS or PT. SELECT * FROM shr WHERE shrcode NOT IN ('CS', 'PT’); is equivalent to: SELECT * FROM shr WHERE shrcode <> 'CS' AND shrcode <> 'PT'; shrcodeshrfirmshrpriceshrqtyshrdivshrpe 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 ROFRoyal Ostrich Farms33.7512349233.006

24 Ordering output Ordering columns –Columns are reported in the order specified in the SQL command Ordering rows –Rows are ordered using the ORDER BY clause

25 Ordering columns SELECT shrcode, shrfirm FROM shr WHERE shrpe = 10; SELECT shrfirm, shrcode FROM shr WHERE shrpe = 10; shrcodeshrfirm PTPatagonian Tea NGNigerian Geese shrfirmshrcode Patagonian TeaPT Nigerian GeeseNG

26 Ordering rows List all firms where PE is at least 12, and order the report in descending PE. Where PE ratios are identical, list firms in alphabetical order. SELECT * FROM shr WHERE shrpe >= 12 ORDER BY shrpe DESC, shrfirm; shrcodeshrfirmshrpriceshrqtyshrdivshrpe FCFreedonia Copper27.50105291.8416 SLGSri Lankan Gold50.37328682.6816 CSCanadian Sugar52.7847162.5015 ARAbyssinian Ruby31.82220101.3213 ILZIndian Lead & Zinc37.7563903.0012

27 Calculating Get firm name, price, quantity, and firm yield. SELECT shrfirm, shrprice, shrqty, shrdiv/shrprice*100 AS yield FROM shr; shrfirmshrpriceshrqtyyield Freedonia Copper27.5010,5296.69 Patagonian Tea55.2512,6354.52 Abyssinian Ruby31.8222,0104.15 Sri Lankan Gold50.3732,8685.32 Indian Lead & Zinc37.756,3907.95 Burmese Elephant0.07154,71314.29 Bolivian Sheep12.75231,67813.96 Nigerian Geese35.0012,3234.80 Canadian Sugar52.784,7164.74 Royal Ostrich Farms33.751,234,9238.89

28 Built-in functions COUNT, AVG, SUM, MIN, and MAX Find the average dividend. SELECT AVG(shrdiv) AS avgdiv FROM shr; What is the average yield for the portfolio? SELECT AVG(shrdiv/shrprice*100) AS avgyield FROM shr; avgdiv 2.03 avgyield 7.53

29 Subqueries A query within a query Report all firms with a PE ratio greater than the average for the portfolio. SELECT shrfirm, shrpe FROM shr WHERE shrpe >(SELECT AVG(shrpe)FROM shr); shrfirmshrpe Freedonia Copper16 Abyssinian Ruby13 Sri Lankan Gold16 Indian Lead & Zinc12 Canadian Sugar15

30 LIKE - Pattern matching List all firms with a name starting with ‘F’. SELECT shrfirm FROM shr WHERE shrfirm LIKE 'F%'; List all firms containing ‘Ruby’ in their name. SELECT shrfirm FROM shr WHERE shrfirm LIKE '%Ruby%'; shrfirm Freedonia Copper shrfirm Abyssinian Ruby

31 LIKE - Pattern matching Find firms with ‘t’ as the third letter of their name. SELECT shrfirm FROM shr WHERE shrfirm LIKE '__t%'; Find firms not containing an ‘s’ in their name. SELECT shrfirm FROM shr WHERE shrfirm NOT LIKE '%S%' AND shrfirm NOT LIKE '%s%'; shrfirm Patagonian Tea shrfirm Freedonia Copper Patagonian Tea Indian Lead & Zinc

32 DISTINCT Eliminating duplicate rows Find the number of different PE ratios. SELECT COUNT(DISTINCT shrpe)AS ' Different PEs ' FROM shr; Different PEs 8

33 DISTINCT Eliminating duplicate rows Report the different values of the PE ratio. SELECT DISTINCT shrpe FROM shr; shrpe 3 6 10 11 12 13 15 16

34 DELETE - deleting rows Erase the data for Burmese Elephant. All the shares have been sold. DELETE FROM shr WHERE shrfirm = 'Burmese Elephant';

35 UPDATE - changing rows Change the share price of FC to 31.50. UPDATE shr SET shrprice = 31.50 WHERE shrcode = 'FC';

36 UPDATE - changing rows Increase the total number of shares for Nigerian Geese by 10% because of the recent bonus issue. UPDATE shr SET shrqty = shrqty*1.1 WHERE shrfirm = 'Nigerian Geese';


Download ppt "The single entity. Modeling reality A database must mirror the real world if it is to answer questions about the real world Data modeling is a design."

Similar presentations


Ads by Google