The questing beast Sir Thomas Mallory

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.
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Database Management Fall 2003 The one-to-many relationship Joins, Views, Subqueries & Group by.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
SQL The questing beast Sir Thomas Mallory. SQL A standard ANSI ISO SQL skills are in demand Developed by IBM Object-oriented extensions created.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Management Fall 2003 Functions, Procedures and Triggers Chapter 10.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management Sixth Edition
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.
The single entity I want to be alone Greta Garbo.
The single entity I want to be alone Greta Garbo.
Chapter 3 Single-Table Queries
Database Management The single entity, the single table, plus some basic SQL.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
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.
CSc-340 3b1 Intermediate SQL Chapter 4 [2 of 2] Phase 1 of Student Projects SQL Data Types & Schemas Authorization.
Concepts of Database Management Seventh Edition
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SQL Overview Structured Query Language
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
© 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.
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Big Data Yuan Xue CS 292 Special topics on.
I want to be alone Greta Garbo
SQL Query Getting to the data ……..
TABLES AND INDEXES Ashima Wadhwa.
The One-to-Many Relationship
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Database Systems: Design, Implementation, and Management Tenth Edition
DATABASE MANAGEMENT SYSTEM
STRUCTURED QUERY LANGUAGE
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
The questing beast Sir Thomas Mallory
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
SQL .. An overview lecture3.
SQL-1 Week 8-9.
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
IST 318 Database Administration
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

The questing beast Sir Thomas Mallory SQL The questing beast Sir Thomas Mallory

SQL A standard for relational database SQL skills are in demand American National Standards Institute (ANSI) International Organization for Standardization (ISO) SQL skills are in demand Developed by IBM Object-oriented extensions created

SQL A complete database language Data definition Data manipulation Definition of tables and views CREATE Data manipulation Specifying queries Maintaining a database SELECT, INSERT, UPDATE, and DELETE Data control Controlling a database GRANT and REVOKE

CREATE CREATE DATABASE whatever COLLATE latin1_general_cs; USE whatever; CREATE TABLE share ( shrcode CHAR(3), shrfirm VARCHAR(20)NOT NULL, shrprice DECIMAL(6,2), shrqty DECIMAL(8), shrdiv DECIMAL(5,2), shrpe DECIMAL(2), PRIMARY KEY(shrcode));

Data manipulation statements INSERT UPDATE DELETE SELECT

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

UPDATE and DELETE UPDATE share SET shrprice = 31.50 WHERE shrcode = 'FC’ DELETE FROM share WHERE shrfirm = 'Freedonia Copper’; NOTE: DELETE only eliminates one or multiple rows. If you want to delete ALL rows (i.e., delete an entire table) you need to use DROP (i.e., DROP TABLE share)

SELECT General format SELECT * FROM share WHERE shrpe >= 12 SELECT[DISTINCT] [SUM, AVG, MIN, or MAX] column(s) FROM table(s) [WHERE condition] [GROUP BY column(s)] [HAVING condition] [ORDER BY column(s)]; SELECT * FROM share WHERE shrpe >= 12 ORDER BY shrpe DESC, shrfirm;

Data types

Check the manual for full details Data types BOOLEAN True, False, or Unknown INTEGER 31 binary digits good for ± 2 billion SMALLINT 15 binary digits good for ± 32000 DECIMAL(p,q) Commercial applications. If p=3 and q=2,what is the maximum possible value in dollars? FLOAT Scientific work Check the manual for full details

Check the manual for full details Data types CHAR and VARCHAR Character strings (i.e., non-numeric) What is the difference between CHAR and VARCHAR? DATE, TIME, TIMESTAMP, and INTERVAL Date (yyyymmdd (2012-11-04 for November 4, 2012) Time (hhmmss) (20:30:01 for 8:30pm and 1s) Interval (single value expressed in some unit or units of time (e.g., 6 years) BLOB and CLOB Binary large object (e.g., graph, audio, image) Character large object (e.g., reports, contracts) Check the manual for full details

Formatting Number Date FORMAT(x,d) formats the number x with d decimal places with commas SELECT FORMAT(amount,2) FROM Payments; Date DATE_FORMAT (date, format) provides a flexible way of reporting dates SELECT DATE_FORMAT(orderDate, '%W, %M %Y') from Orders; SELECT DATE_FORMAT(orderDate, '%Y-%m-%d') from Orders; SELECT DATE_FORMAT(orderDate, '%D, %M %Y') from Orders;

Exercise Using the ClassicModels database, report customer name and the total payment amount for each customer to the nearest dollar and list in descending value

Collation sequence Defines how to sort individual characters in a particular language English A B C … X Y Z Norwegian A B C … X Y Z Æ Ø Å

Collation sequence Can specify a collation sequence at the database, table, and, column level Good practice to specify at the database level CREATE DATABASE ClassicModels COLLATE latin1_general_cs; cs indicates case sensitivity

Creating a View A view does not physically exist as stored data; it is an imaginary (virtual) table constructed from existing tables as required General format CREATE VIEW view (column(s)) AS subquery;

When Do We Create Views? Restrict access to a table Create a view to restrict access to the table share. CREATE VIEW stklist AS SELECT stkfirm, stkprice FROM stock; Handle derived data Create a view with stock’s yield computed. CREATE VIEW stk AS SELECT stkfirm, stkprice, stkqty, stkdiv/stkprice*100 FROM stock;

When Do We Create Views? Avoid writing common SQL queries Create a view to perform the frequent join between stock and nation and to convert all share prices from the local currency to British pounds. CREATE VIEW stkvalue AS SELECT natname, stkfirm, stkprice*exchrate, stkqty, stktprice*exchrate*stkqty FROM stock, nation WHERE stock.natcode = nation.natcode;

Dropping a View Used to delete a view from the system. A view may be dropped because it needs to be redefined or is no longer used General format DROP VIEW view;

Product All rows of the first table concatenated with all possible rows of the second table Form the product of stock and nation SELECT * FROM stock, nation;

Product – CREATE A VIEW Find the percentage of Australian stocks in the portfolio. CREATE VIEW austotal AS SELECT COUNT(*) AS ’A' FROM nation, stock WHERE natname = 'Australia' AND nation.natcode = stock.natcode; CREATE VIEW total AS SELECT COUNT(*) AS ’B' FROM stock; SELECT A/B*100 AS percentage FROM austotal, total; 18.75

PRODUCT (alternative) Find the percentage of Australian stocks in the portfolio. SELECT FORMAT((SELECT COUNT(*) FROM nation, stock WHERE nation.natcode = stock.natcode AND natname = 'Australia')*100/(SELECT COUNT(*) FROM stock),2) AS Percentage; SELECT (SELECT COUNT(*) FROM nation, stock WHERE nation.natcode = stock.natcode AND natname = 'Australia')*100/(SELECT COUNT(*) FROM stock) AS Percentage; 18.75

Join Join creates a new table from two existing tables by matching on a column common to both tables Equijoin The new table contains two identical columns SELECT * FROM stock, nation WHERE stock.natcode = nation.natcode;

Correlated subquery The inner query is evaluated many times rather than once Find those stocks where the quantity is greater than the average for that country. SELECT natname, stkfirm, stkqty FROM stock, nation WHERE stock.natcode = nation.natcode AND stkqty > (SELECT AVG(stkqty) FROM stock WHERE stock.natcode = nation.natcode);

Correlated subquery SELECT natname, stkfirm, stkqty FROM stock, nation WHERE stock.natcode = nation.natcode AND stkqty > (SELECT AVG(stkqty) FROM stock WHERE stock.natcode = nation.natcode); nation natcode natname exchrate UK United Kingdom 1.00 USA United States 0.67 AUS Australia 0.46 IND India 0.0228 stock stkcode stkfirm stkprice stkqty stkdiv stkpe natcode FC Freedonia Copper 27.50 10529 1.84 16 UK PT Patagonian Tea 55.25 12635 2.50 10 AR Abyssinian Ruby 31.82 22010 1.32 13 SLG Sri Lankan Gold 50.37 32868 2.68 ILZ Indian Lead &Zinc 37.75 6390 3.00 12 BE Burmese Elephant .07 154713 0.01 3 BS Bolivian Sheep 12.75 231678 1.78 11 NG Nigerian Geese 35.00 12323 1.68 CS Canadian Sugar 52.78 4716 15 ROF Royal Ostrich Farms 33.75 1234923  6 MG Minnesota Gold 53.87 816122 1.00 25 USA GP Georgia Peach 2.35 387333 .20 5 NE Narembeen Emu 12.34 45619 8 AUS QD Queensland Diamond 6.73 89251 .50 7 IR Indooroopilly Ruby 15.92 56147 20 BD Bombay Duck 25.55 167382 IND

Correlated subquery

Correlated subquery Clue The need to compare each row of a table against a function (e.g., average or count) for some rows of a column Find those stocks where the quantity is greater than the average for that country

Aggregate functions COUNT SUM AVG MAX MIN

Security Data is a valuable resource Access should be controlled SQL security procedures CREATE VIEW Authorization commands

GRANT Defines a user’s privileges The keyword privilege can be ALL PRIVILEGES or chosen from SELECT UPDATE DELETE INSERT Privileges can be granted to everybody using the keyword PUBLIC or to selected users by specifying their user identifier

GRANT General format WITH GRANT OPTION GRANT [ALL PRIVILEGES, SELECT, UPDATE, DELETE, INSERT] ON [table(s), view(s)] TO [user(s), PUBLIC] [WITH GRANT OPTION]; WITH GRANT OPTION Permits a user to pass privileges to another user

Using GRANT Give Alice all rights to the STOCK table. GRANT ALL PRIVILEGES ON stock TO alice; Permit the accounting staff, Todd and Nancy, to update the price of a stock. GRANT UPDATE (stkprice) ON stock TO todd, nancy; Give all staff the privilege to select rows from ITEM. GRANT SELECT ON item TO PUBLIC; Give Alice all rights to view STK. GRANT SELECT, UPDATE, DELETE, INSERT ON stk TO alice; Why wasn’t ALL PRIVILEGES used? Since STK is a view, ALICE cannot be granted ALL PRIVILEGES since this would include ALTER and INDEX privileges for a view

REVOKE Removes privileges General Format Cascading REVOKE REVOKE privileges ON object FROM users; Cascading REVOKE Reverses use of the WITH GRANT OPTION When a user’s privileges are revoked, all users whose privileges were established using WITH GRANT OPTION are also revoked

Using REVOKE General Format REVOKE privileges ON object FROM users; Remove Sophie's ability to select from ITEM. REVOKE SELECT ON item FROM sophie; Nancy is no longer permitted to update stock prices. REVOKE UPDATE ON stock FROM nancy; A revoked UPDATE is not column specific

Key points—SQL Not a complete programming language Data definition, manipulation, and control Several data types (STRING, NUMERIC) Formatting capability (FORMAT (__,__)) Correlated subquery