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.

Slides:



Advertisements
Similar presentations
The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb.
Advertisements

Concepts of Database Management Sixth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
© Steven Alter, 2007, all rights reserved Database concepts Difference between a database and the Internet Reason for having a defined data structure Relational.
Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Databases Using MySQL Creating Tables Queries. Databases  A database is a collection of data organized for efficient access  A relational database is.
Structured Query Language Part I Chapter Three CIS 218.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Data type – determines the type of data and range of values that can be entered in a field.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction to SQL J.-S. Chou Assistant Professor.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
Basis Data Terapan Yoannita. SQL Server Data Types Character strings: Data typeDescriptionStorage char(n)Fixed-length character string. Maximum 8,000.
The single entity I want to be alone Greta Garbo.
The single entity I want to be alone Greta Garbo.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Database Management The single entity, the single table, plus some basic SQL.
Concepts and Terminology Introduction to Database.
 A database is a collection of data that is organized so that its contents can easily be accessed, managed, and updated. What is Database?
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
CSC 2720 Building Web Applications Database and SQL.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Relational Databases (MS Access)
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
Relational Databases. Relational database  data stored in tables  must put data into the correct tables  define relationship between tables  primary.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Database Management System Conducted By Provincial Computer Resource Centre Gurudeniya Jayaratna Alahakoon.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
The One-to-Many Relationship Cow of many-well milked and badly fed Spanish proverb.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
How to: SQL By: Sam Loch.
I want to be alone Greta Garbo
The questing beast Sir Thomas Mallory
Data Structure and Storage
The One-to-Many Relationship
Data Structure and Storage
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Structured Query Language
Shelly Cashman: Microsoft Access 2016
Lecture 2 Lecturer: awdang aziz MS access
Presentation transcript:

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 technique for capturing reality

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

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 an attribute or collection of attributes An identifier can be created if there is no obvious attribute(s) A leading asterisk denotes an identifier

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

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

Defining a table

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

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)

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

Inserting rows

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

Querying a table List all data in the share table. 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

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

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

Restrict Choosing rows A horizontal slice 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 Get all firms with a price-earnings ratio less than 12. SELECT * FROM shr WHERE shrpe < 12; shrcodeshrfirmshrpriceshrqtyshrdivshrpe PTPatagonian Tea BEBurmese Elephant BSBolivian Sheep NGNigerian Geese ROFRoyal Ostrich Farms

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 >= ; shrfirmshrpriceshrqtyshrdiv Burmese Elephant Bolivian Sheep Royal Ostrich Farms

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

Primary key retrieval A query not using the primary key can return more than one row Report firms with a dividend of SELECT * FROM shr WHERE shrdiv = 2.5; shrcodeshrfirmshrpriceshrqtyshrdivshrpe PTPatagonian Tea CSCanadian Sugar

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 ARAbyssinian Ruby SLGSri Lankan Gold

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 Ruby SLGSri Lankan Gold ILZIndian Lead & Zinc BEBurmese Elephant BSBolivian Sheep NGNigerian Geese ROFRoyal Ostrich Farms

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

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

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 Copper SLGSri Lankan Gold CSCanadian Sugar ARAbyssinian Ruby ILZIndian Lead & Zinc

Calculating Get firm name, price, quantity, and firm yield. SELECT shrfirm, shrprice, shrqty, shrdiv/shrprice*100 AS yield FROM shr; shrfirmshrpriceshrqtyyield Freedonia Copper , Patagonian Tea , Abyssinian Ruby , Sri Lankan Gold , Indian Lead & Zinc37.756, Burmese Elephant , Bolivian Sheep , Nigerian Geese , Canadian Sugar52.784, Royal Ostrich Farms33.751,234,

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

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

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

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

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

DISTINCT Eliminating duplicate rows Report the different values of the PE ratio. SELECT DISTINCT shrpe FROM shr; shrpe

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

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

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';