1. SQL Header of tables: –Head(Custoemrs) = (cid, cname, city, discnt) –Head(Orders) = (ordno, month, cid, pid, qty, dollars) –Head(Products) = (pid, pname,

Slides:



Advertisements
Similar presentations
SQL Database for a Book Store Clinton McKay. Explanation The database contains information about the books held in stock, their authors, publishers, customers,
Advertisements

Transact-SQL. 1. Declare float = 10 select * from customers where discnt
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Data Design The futureERD - CardinalityCODINGRelationshipsDefinition.
1 SQL - Select Join / Outer Join Sub queries Join Join Outer join Left outer join Right outer join.
Structured Query Language NEU – CCIS – CSU430 Tony.
Spring 2003 ECE569 Lecture 06.1 ECE 569 Database System Engineering Spring 2003 Topic VIII: Query Execution and optimization Yanyong Zhang
Introduction to Relational Database ISYS 464. Introduction to Relational Model Data is logically structured within relations. Each relation is a table.
Overview of Database Systems Yanlei Diao University of Massachusetts Amherst.
1 LBSC 690: Week 9 SQL, Web Forms. 2 Discussion Points Websites that are really databases Deep vs. Surface Web.
Relational Databases What is a relational database? What would we use one for? What do they look like? How can we describe them? How can you create one?
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
2.3 Organising Data for Effective Retrieval
1 Translating E/R Diagrams into Relational Schemas.
Your Name Here See Page Notes for Info about Hyperlinks.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Final Exam Guide PHP NOTE: PHP CODE WILL BE BLUE, HTML IS BLACK EXAMPLE
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
Fundamentals of Relational Database Operations
CS 101 – Access notes Databases (Microsoft Access) 4 parts of a database database design –Try to understand the ideas behind database design, not just.
Relational Database. Database Management System (DBMS)
Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21.
1 CSE 2337 Introduction to Data Management Textbook: Chapter 1.
Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation.
DAT602 Database Application Development Lecture 2 Review of Relational Database.
An Entity Relationship (ER) Diagram is a graphic that shows the interrelationship between entities in a database.
1 Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
Advanced SQL: Triggers & Assertions
数据库原理与技术(实例) - 张祖平 1 大型数据库完整性典型实现方式 在大型数据库中的完整性控制 触发器.
Data Modeling with ERD BUS 782. Entities An entity is a person, place, object, event, or concept in the user environment about which the organization.
COMP3030 Database Management System Final Review
MIS 451 Building Business Intelligence Systems Logical Design (1)
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
1 SQL - Select Union / Intersect / Difference / Division.
Understanding Databases Lesson 6. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understanding Relational Database Concepts Understand relational.
1 SQL Insert Update Delete Create table Alter table.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
IBuySPY Shopping Store. Data Model for IBuySPY Shopping Store.
1 The Entity- Relationship Model Instructor: Mohamed Eltabakh Part-2.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
COMP 430 Intro. to Database Systems ER Implementation as Tables Slides use ideas from Chris Ré.
COMP 430 Intro. to Database Systems Entity-Relationship Diagram Basics Slides use ideas from Chris Ré.
Order Database – ER Diagram Prepared by Bryan Borcher Spring 2014.
CS3431: C-Term Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
Data Modeling with ERD ISYS 363.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
Big Data Yuan Xue CS 292 Special topics on.
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),
CO887 – Lecture 8 SQL 2.
Order Database – ER Diagram
Confirm teams. Review of diagrams. SELECT problems.
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
Order Database – ER Diagram
CS311 Database Management system
Order Database – ER Diagram
Order Database – ER Diagram
Cse 344 April 4th – Subqueries.
Lecture#7: Fun with SQL (Part 2)
January 19th – Subqueries 2 and relational algebra
MIS2502: Review for Exam 1 JaeHwuen Jung
Database systems Lecture 3 – SQL + CRUD
Order Database – ER Diagram
Order Database – ER Diagram
Chapter 4 Introduction to MySQL.
Lecture 14: XML Publishing & Storage
Presentation transcript:

1. SQL Header of tables: –Head(Custoemrs) = (cid, cname, city, discnt) –Head(Orders) = (ordno, month, cid, pid, qty, dollars) –Head(Products) = (pid, pname, city, quantity, price) –Head(Agents) = (aid, aname, city, percent)

1.1 SQL basic- projection List all customer name Select cname from customers Select distinct cname from customers

1.1 SQL basic- where List all customers who live in Dallas –Select cid, cname from customers where city = 'Dallas' List all customers who live in Dallas and discount is bigger than 5. –Select cid, cname from customers where city = ‘Dallas’ and discnt > 5

1.1 SQL basic- join List all customer (cid and cname) who ordered comb –Select distinct c.cid, c.cname from customers c, products p, orders o where c.cid = o.cid and p.pid = o.pid and p.pname = 'comb' List all agents (aid, aname) who have customers who live in ‘Dallas’ –Select distinct a.aid, a.aname from customers c, agents a, orders o where c.cid = o.cid and a.aid = o.aid and c.city = 'Dallas'

1.1 SQL basic- group by List the total order amount (dollars) of each customer (cid) –Select cid, sum(dollars) from orders group by cid List total order amount (dollars) of each customer (cid, cname) –Select o.cid, c.cname, sum(o.dollars) from customers c, orders o where c.cid = o.cid group by o.cid, c.cname

1.1 SQL basic- group by List total order amount (dollars) of each customer (cid, cname) whose total order amount is more than 1500 –Select o.cid, c.cname, sum(o.dollars) as total_sales from customers c, orders o where c.cid = o.cid group by o.cid, c.cname having sum(dollars) > 1500

1.2 SQL advanced - join For each agent (aid, aname) count the number of customers who place orders with them. –Select a.aid, a.aname, count(cid) as number_of_customers from agents a, orders o where a.aid = o.aid group by a.aid, a.aname

1.2 SQL advanced - join For each agent (aid, aname) count the number of customers who place orders with them. If an agent does not have any orders with any customer, his/her info should also be included in the result. –Select a.aid, a.aname, count(cid) as number_of_customers from agents a left join orders o on a.aid = o.aid group by a.aid, a.aname

1.2 SQL advanced – MAX/MIN List agents (aid, aname) who sold most products (dollars). –select a.aid, a.aname from agents a, orders o where a.aid = o.aid group by a.aid, aname having sum(dollars) >= all (select sum(dollars) from orders group by aid )

1.2 SQL advanced – MAX/MIN List agents (aid, aname) who sold most products (dollars) in jan. –select a.aid, a.aname from agents a, orders o where a.aid = o.aid and o.month = 'jan' group by a.aid, aname having sum(dollars) >= all (select sum(dollars) from orders where month = 'jan' group by aid)

1.2 SQL advanced – MAX/MIN List products (pid, pname) that are ordered most (qty) by customers who live in Dallas –select p.pid, p.pname from products p, orders o, customers c –where p.pid = o.pid and c.cid = o.cid and c.city = 'Dallas' –group by p.pid, p.pname –having sum(qty) >= all –(select sum(qty) from orders o, customers c where c.cid = o.cid and c.city = 'Dallas' group by o.pid)

1.2 SQL advanced – All List agents (aid, aname) who place orders with all customers in Dallas –Select a.aid, a.aname from agents a where not exists (select * from customers c where c.city = 'dallas' and not exists (select * from orders o where o.cid = c.cid and o.aid = a.aid))

1.2 SQL advanced – All List customers (cid, cname) who order all the products produced in Newark –select c.cid, c.cname from customers c where not exists (select * from products p where city = 'Newark' and not exists (select * from orders o where o.pid = p.pid and o.cid = c.cid))

2. Advance SQL topics Transact SQL Stored Procedure User Defined Function Cursor Trigger View

2.2 Stored Procedure create proc int output as = count(*) from customers where city return int exec count_customer_by_city output

2.3 UDF create function char(4)) returns money as begin money = sum(dollars) from orders where cid End SELECT *, zhoupf.total_dollars(cid) as total_order_amount FROM customers select * from zhoupf.total_dollars('c001') exec zhoupf.total_dollars('c001')

2.3 UDF CREATE FUNCTION total_avg_orders char(4) ) TABLE ( total_order money, avg_order money ) AS BEGIN SELECT sum(dollars), avg(dollars) FROM orders WHERE cid RETURN END select * from total_avg_orders('c001') //This is correct because the return value is a table

2.5 Trigger create table Boston_Customers ( cid char(4) not null, cname varchar(15), city varchar(15), discnt decimal(10,2), primary key(cid) );

2.5 Trigger create trigger insert_customer on customers after insert as insert into Boston_customers select * from inserted I where I.city = ‘Boston’ insert into customers values ('c019', 'Tony', 'Boston', 16) select * from boston_customers

3. ER diagram Entity (has attributes) Relationship (can also has attributes) –Many-to-many relationship –Many-to-one relationship –One-to-one relationship

3. ER diagram NU library –Each borrower has a borrower_id, name. –Each library staff has a staff_id, name. –Each (type) book has a ISBN, name, author, number_of_copies. –Each borrower can borrow multiple books. Each (type) of book can be borrowed by multiple borrowers. –When a borrower borrows a book from a staff, the borrow date and return date will be recorded.