Commit 30acb698 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596527990

0 → 120000
+1 −0
Changes for 9780596527990: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596527990
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# SQL Hacks, by Andrew Cumming
	  
[![SQL Hacks, by Andrew Cumming](http://akamaicovers.oreilly.com/images/9780596527990/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596527993//)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

logo.png

0 → 100644
+2.88 KiB
Loading image diff...

sqlhks/README

0 → 100644
+12 −0
Changes for sqlhks/README: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
SQL Hacks Source code
Associated with SQL Hacks, Andrew Cumming and Gordon Russell, Pub O'Reilly
Buy the book! http://www.oreilly.com/catalog/sqlhks/reviews.html

Most hacks have three sections:

setup - These SQL commands create the tables or views required to run the hack

hack -  This is the code that generates the main hack. Sometimes there
        are several commands associated with a hack.

tidy -  This code removes the tables created by setup or sql.
+79 −0
Changes for sqlhks/mysql/hack03.sql: 79 added lines, 0 removed lines.
Original line number Diff line number Diff line
--SQL Hacks, Hack 03: Conditional INSERT Statement
--mysql version

--------------
--   setup  --
--------------

CREATE TABLE libraryLoan
(member CHAR(5)
,book CHAR(5)
,dueDate DATE 
);
INSERT INTO libraryLoan VALUES ('jim01','bk002',DATE '2005-03-22');
INSERT INTO libraryLoan VALUES ('jim01','bk002',DATE '2005-09-21');
INSERT INTO libraryLoan VALUES ('ann02','bk005',DATE '2005-09-21');
INSERT INTO libraryLoan(member,book,dueDate)
  VALUES ('jim01','bk002',
          CURRENT_DATE + INTERVAL '14' DAY 
         );
INSERT INTO libraryLoan(member,book,dueDate)
  VALUES ('ann02','bk005',
          CURRENT_DATE - INTERVAL '1' DAY 
         );
CREATE TABLE libraryReturn
(book  CHAR(5)
,member CHAR(5)
,returnDate DATE
,fine       DECIMAL(10,2)
);

CREATE TABLE roomBooking
(who   VARCHAR(20)
,wht   VARCHAR(20) NOT NULL
,whn   DATE
            NOT NULL
,PRIMARY KEY (whn,wht)
);
--------------------
--       hack     --
--------------------

INSERT INTO libraryReturn(book,member,returnDate,fine)
  VALUES ('jim01','bk002',CURRENT_DATE,
          (SELECT 0.20
             FROM libraryLoan
           WHERE member='jim01' AND book='bk002'
           HAVING MAX(dueDate)<CURRENT_DATE));
INSERT INTO libraryReturn(book,member,returnDate,fine)
  VALUES ('ann02','bk005',CURRENT_DATE,
          (SELECT 0.20
             FROM libraryLoan
           WHERE member='ann02' AND book='bk005'
           HAVING MAX(dueDate)<CURRENT_DATE));
SELECT * FROM libraryReturn;

INSERT INTO roomBooking VALUES ('Col. Mustard','Ballroom',DATE '2006-07-13');

INSERT INTO roomBooking(whn,wht,who)
  SELECT DATE '2006-07-13','Billiard Room','Prof. Plum'
    FROM dual
    WHERE NOT EXISTS (SELECT who FROM roomBooking
                       WHERE whn = DATE '2006-07-13'
                         AND wht='Billiard room');

INSERT INTO roomBooking(whn,wht,who)
  SELECT DATE '2006-07-13','Ballroom','Miss. Scarlet'
    FROM dual
    WHERE NOT EXISTS (SELECT who FROM roomBooking
                       WHERE whn = DATE '2006-07-13'
                         AND wht='Ballroom');

SELECT * FROM roomBooking;
----------------
--    tidy    --
----------------

DROP TABLE libraryLoan;
DROP TABLE libraryReturn;
DROP TABLE roomBooking;
Loading