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

Slides:



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

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.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Information Resources Management February 27, 2001.
Introduction to Structured Query Language (SQL)
Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by.
Introduction to Structured Query Language (SQL)
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
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.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
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.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
The single entity I want to be alone Greta Garbo.
The single entity I want to be alone Greta Garbo.
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
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
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.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
I want to be alone Greta Garbo
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
The questing beast Sir Thomas Mallory
The One-to-Many Relationship
SQL FUNDAMENTALS CDSE Days 2018.
Structured Query Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Access: SQL Participation Project
Contents Preface I Introduction Lesson Objectives I-2
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

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)