Presentation is loading. Please wait.

Presentation is loading. Please wait.

The questing beast Sir Thomas Mallory

Similar presentations


Presentation on theme: "The questing beast Sir Thomas Mallory"— Presentation transcript:

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

2 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

3 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

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

5 Data manipulation statements
INSERT UPDATE DELETE SELECT

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

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

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

9 Data types

10 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

11 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 ( 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

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

13 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

14 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 Æ Ø Å

15 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

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

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

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

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

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

21 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

22 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

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

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

25 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  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

26 Correlated subquery

27 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

28 Aggregate functions COUNT SUM AVG MAX MIN

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

30 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

31 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

32 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

33 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

34 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

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


Download ppt "The questing beast Sir Thomas Mallory"

Similar presentations


Ads by Google