home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ppc / instpubs.sql < prev    next >
Encoding:
Text File  |  1995-11-09  |  31.3 KB  |  959 lines

  1.  
  2. -- -  InstPubs.SQL   1995/11/03 13:40
  3. GO
  4.  
  5. set nocount    on
  6. set dateformat mdy
  7.  
  8. USE master
  9.  
  10. GO
  11.  
  12. declare @dttm varchar(55)
  13. select  @dttm=convert(varchar,getdate(),113)
  14. raiserror('Beginning InstPubs.SQL at %s ....',1,1,@dttm) with nowait
  15.  
  16. if exists (select * from sysdatabases where name='pubs')
  17.   DROP database pubs
  18.  
  19. GO
  20.  
  21. CHECKPOINT
  22.  
  23. GO
  24.  
  25. CREATE DATABASE pubs
  26.    on master = 3
  27.  
  28. GO
  29.  
  30. CHECKPOINT
  31.  
  32. GO
  33.  
  34. USE pubs
  35.  
  36. GO
  37.  
  38. if db_name() = 'pubs'
  39.    raiserror('''pubs'' database created, and context now in use.',1,1)
  40. else
  41.    raiserror('Error in InstPubs.SQL, ''USE pubs'' failed!  Killing the SPID now.'
  42.             ,22,127) with log
  43.  
  44. GO
  45.  
  46. execute sp_dboption 'pubs' ,'trunc. log on chkpt.' ,'true'
  47.  
  48. execute sp_addtype id      ,'varchar(11)' ,'NOT NULL'
  49. execute sp_addtype tid     ,'varchar(6)'  ,'NOT NULL'
  50. execute sp_addtype empid   ,'char(9)'     ,'NOT NULL'
  51.  
  52. GO
  53.  
  54. raiserror('Now at the create table section ....',1,1)
  55.  
  56. GO
  57.  
  58. CREATE TABLE authors
  59. (
  60.    au_id          id
  61.  
  62.          CHECK (au_id like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]')
  63.  
  64.          CONSTRAINT UPKCL_auidind PRIMARY KEY CLUSTERED,
  65.  
  66.    au_lname       varchar(40)       NOT NULL,
  67.    au_fname       varchar(20)       NOT NULL,
  68.  
  69.    phone          char(12)          NOT NULL
  70.  
  71.          DEFAULT ('UNKNOWN'),
  72.  
  73.    address        varchar(40)           NULL,
  74.    city           varchar(20)           NULL,
  75.    state          char(2)               NULL,
  76.  
  77.    zip            char(5)               NULL
  78.  
  79.          CHECK (zip like '[0-9][0-9][0-9][0-9][0-9]'),
  80.  
  81.    contract       bit               NOT NULL
  82. )
  83.  
  84. GO
  85.  
  86. CREATE TABLE publishers
  87. (
  88.    pub_id         char(4)           NOT NULL
  89.  
  90.          CONSTRAINT UPKCL_pubind PRIMARY KEY CLUSTERED
  91.  
  92.          CHECK (pub_id in ('1389', '0736', '0877', '1622', '1756')
  93.             OR pub_id like '99[0-9][0-9]'),
  94.  
  95.    pub_name       varchar(40)           NULL,
  96.    city           varchar(20)           NULL,
  97.    state          char(2)               NULL,
  98.  
  99.    country        varchar(30)           NULL
  100.  
  101.          DEFAULT('USA')
  102. )
  103.  
  104. GO
  105.  
  106. CREATE TABLE titles
  107. (
  108.    title_id       tid
  109.  
  110.          CONSTRAINT UPKCL_titleidind PRIMARY KEY CLUSTERED,
  111.  
  112.    title          varchar(80)       NOT NULL,
  113.  
  114.    type           char(12)          NOT NULL
  115.  
  116.          DEFAULT ('UNDECIDED'),
  117.  
  118.    pub_id         char(4)               NULL
  119.  
  120.          REFERENCES publishers(pub_id),
  121.  
  122.    price          money                 NULL,
  123.    advance        money                 NULL,
  124.    royalty        int                   NULL,
  125.    ytd_sales      int                   NULL,
  126.    notes          varchar(200)          NULL,
  127.  
  128.    pubdate        datetime          NOT NULL
  129.  
  130.          DEFAULT (getdate())
  131. )
  132.  
  133. GO
  134.  
  135. CREATE TABLE titleauthor
  136. (
  137.    au_id          id
  138.  
  139.          REFERENCES authors(au_id),
  140.  
  141.    title_id       tid
  142.  
  143.          REFERENCES titles(title_id),
  144.  
  145.    au_ord         tinyint               NULL,
  146.    royaltyper     int                   NULL,
  147.  
  148.  
  149.    CONSTRAINT UPKCL_taind PRIMARY KEY CLUSTERED(au_id, title_id)
  150. )
  151.  
  152. GO
  153.  
  154. CREATE TABLE stores
  155. (
  156.    stor_id        char(4)           NOT NULL
  157.  
  158.          CONSTRAINT UPK_storeid PRIMARY KEY CLUSTERED,
  159.  
  160.    stor_name      varchar(40)           NULL,
  161.    stor_address   varchar(40)           NULL,
  162.    city           varchar(20)           NULL,
  163.    state          char(2)               NULL,
  164.    zip            char(5)               NULL
  165. )
  166.  
  167. GO
  168.  
  169. CREATE TABLE sales
  170. (
  171.    stor_id        char(4)           NOT NULL
  172.  
  173.          REFERENCES stores(stor_id),
  174.  
  175.    ord_num        varchar(20)       NOT NULL,
  176.    ord_date       datetime          NOT NULL,
  177.    qty            smallint          NOT NULL,
  178.    payterms       varchar(12)       NOT NULL,
  179.  
  180.    title_id       tid
  181.  
  182.          REFERENCES titles(title_id),
  183.  
  184.  
  185.    CONSTRAINT UPKCL_sales PRIMARY KEY CLUSTERED (stor_id, ord_num, title_id)
  186. )
  187.  
  188. GO
  189.  
  190. CREATE TABLE roysched
  191. (
  192.    title_id       tid
  193.  
  194.          REFERENCES titles(title_id),
  195.  
  196.    lorange        int                   NULL,
  197.    hirange        int                   NULL,
  198.    royalty        int                   NULL
  199. )
  200.  
  201. GO
  202.  
  203. CREATE TABLE discounts
  204. (
  205.    discounttype   varchar(40)       NOT NULL,
  206.  
  207.    stor_id        char(4) NULL
  208.  
  209.          REFERENCES stores(stor_id),
  210.  
  211.    lowqty         smallint              NULL,
  212.    highqty        smallint              NULL,
  213.    discount       dec(4,2)          NOT NULL
  214. )
  215.  
  216. GO
  217.  
  218. CREATE TABLE jobs
  219. (
  220.    job_id         smallint          IDENTITY(1,1)
  221.  
  222.          PRIMARY KEY CLUSTERED,
  223.  
  224.    job_desc       varchar(50)       NOT NULL
  225.  
  226.          DEFAULT 'New Position - title not formalized yet',
  227.  
  228.    min_lvl        tinyint           NOT NULL
  229.  
  230.          CHECK (min_lvl >= 10),
  231.  
  232.    max_lvl        tinyint           NOT NULL
  233.  
  234.          CHECK (max_lvl <= 250)
  235. )
  236.  
  237. GO
  238.  
  239. CREATE TABLE pub_info
  240. (
  241.    pub_id         char(4)           NOT NULL
  242.  
  243.          REFERENCES publishers(pub_id)
  244.  
  245.          CONSTRAINT UPKCL_pubinfo PRIMARY KEY CLUSTERED,
  246.  
  247.    logo           image                 NULL,
  248.    pr_info        text                  NULL
  249. )
  250.  
  251. GO
  252.  
  253. CREATE TABLE employee
  254. (
  255.    emp_id         empid
  256.  
  257.          CONSTRAINT PK_emp_id PRIMARY KEY NONCLUSTERED
  258.  
  259.          CONSTRAINT CK_emp_id CHECK (emp_id LIKE
  260.             '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' or
  261.             emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]'),
  262.  
  263.    fname          varchar(20)       NOT NULL,
  264.    minit          char(1)               NULL,
  265.    lname          varchar(30)       NOT NULL,
  266.  
  267.    job_id         smallint          NOT NULL
  268.  
  269.          DEFAULT 1
  270.  
  271.          REFERENCES jobs(job_id),
  272.  
  273.    job_lvl        tinyint
  274.  
  275.          DEFAULT 10,
  276.  
  277.    pub_id         char(4)           NOT NULL
  278.  
  279.          DEFAULT ('9952')
  280.  
  281.          REFERENCES publishers(pub_id),
  282.  
  283.    hire_date      datetime          NOT NULL
  284.  
  285.          DEFAULT (getdate())
  286. )
  287.  
  288. GO
  289.  
  290. raiserror('Now at the create trigger section ...',1,1)
  291.  
  292. GO
  293.  
  294. CREATE TRIGGER employee_insupd
  295. ON employee
  296. FOR insert, UPDATE
  297. AS
  298. --Get the range of level for this job type from the jobs table.
  299. declare @min_lvl tinyint,
  300.    @max_lvl tinyint,
  301.    @emp_lvl tinyint,
  302.    @job_id smallint
  303. select @min_lvl = min_lvl,
  304.    @max_lvl = max_lvl,
  305.    @emp_lvl = i.job_lvl,
  306.    @job_id = i.job_id
  307. from employee e, jobs j, inserted i
  308. where e.emp_id = i.emp_id AND i.job_id = j.job_id
  309. IF (@job_id = 1) and (@emp_lvl <> 10)
  310. begin
  311.    raiserror ('Job id 1 expects the default level of 10.',16,1)
  312.    ROLLBACK TRANSACTION
  313. end
  314. ELSE
  315. IF NOT (@emp_lvl BETWEEN @min_lvl AND @max_lvl)
  316. begin
  317.    raiserror ('The level for job_id:%d should be between %d and %d.',
  318.       16, 1, @job_id, @min_lvl, @max_lvl)
  319.    ROLLBACK TRANSACTION
  320. end
  321.  
  322. GO
  323.  
  324. raiserror('Now at the inserts to authors ....',1,1)
  325.  
  326. GO
  327.  
  328. insert authors
  329.    values('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
  330.    '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
  331. insert authors
  332.    values('213-46-8915', 'Green', 'Marjorie', '415 986-7020',
  333.    '309 63rd St. #411', 'Oakland', 'CA', '94618', 1)
  334. insert authors
  335.    values('238-95-7766', 'Carson', 'Cheryl', '415 548-7723',
  336.    '589 Darwin Ln.', 'Berkeley', 'CA', '94705', 1)
  337. insert authors
  338.    values('998-72-3567', 'Ringer', 'Albert', '801 826-0752',
  339.    '67 Seventh Av.', 'Salt Lake City', 'UT', '84152', 1)
  340. insert authors
  341.    values('899-46-2035', 'Ringer', 'Anne', '801 826-0752',
  342.    '67 Seventh Av.', 'Salt Lake City', 'UT', '84152', 1)
  343. insert authors
  344.    values('722-51-5454', 'DeFrance', 'Michel', '219 547-9982',
  345.    '3 Balding Pl.', 'Gary', 'IN', '46403', 1)
  346. insert authors
  347.    values('807-91-6654', 'Panteley', 'Sylvia', '301 946-8853',
  348.    '1956 Arlington Pl.', 'Rockville', 'MD', '20853', 1)
  349. insert authors
  350.    values('893-72-1158', 'McBadden', 'Heather',
  351.    '707 448-4982', '301 Putnam', 'Vacaville', 'CA', '95688', 0)
  352. insert authors
  353.    values('724-08-9931', 'Stringer', 'Dirk', '415 843-2991',
  354.    '5420 Telegraph Av.', 'Oakland', 'CA', '94609', 0)
  355. insert authors
  356.    values('274-80-9391', 'Straight', 'Dean', '415 834-2919',
  357.    '5420 College Av.', 'Oakland', 'CA', '94609', 1)
  358. insert authors
  359.    values('756-30-7391', 'Karsen', 'Livia', '415 534-9219',
  360.    '5720 McAuley St.', 'Oakland', 'CA', '94609', 1)
  361. insert authors
  362.    values('724-80-9391', 'MacFeather', 'Stearns', '415 354-7128',
  363.    '44 Upland Hts.', 'Oakland', 'CA', '94612', 1)
  364. insert authors
  365.    values('427-17-2319', 'Dull', 'Ann', '415 836-7128',
  366.    '3410 Blonde St.', 'Palo Alto', 'CA', '94301', 1)
  367. insert authors
  368.    values('672-71-3249', 'Yokomoto', 'Akiko', '415 935-4228',
  369.    '3 Silver Ct.', 'Walnut Creek', 'CA', '94595', 1)
  370. insert authors
  371.    values('267-41-2394', 'O''Leary', 'Michael', '408 286-2428',
  372.    '22 Cleveland Av. #14', 'San Jose', 'CA', '95128', 1)
  373. insert authors
  374.    values('472-27-2349', 'Gringlesby', 'Burt', '707 938-6445',
  375.    'PO Box 792', 'Covelo', 'CA', '95428', 3)
  376. insert authors
  377.    values('527-72-3246', 'Greene', 'Morningstar', '615 297-2723',
  378.    '22 Graybar House Rd.', 'Nashville', 'TN', '37215', 0)
  379. insert authors
  380.    values('172-32-1176', 'White', 'Johnson', '408 496-7223',
  381.    '10932 Bigge Rd.', 'Menlo Park', 'CA', '94025', 1)
  382. insert authors
  383.    values('712-45-1867', 'del Castillo', 'Innes', '615 996-8275',
  384.    '2286 Cram Pl. #86', 'Ann Arbor', 'MI', '48105', 1)
  385. insert authors
  386.    values('846-92-7186', 'Hunter', 'Sheryl', '415 836-7128',
  387.    '3410 Blonde St.', 'Palo Alto', 'CA', '94301', 1)
  388. insert authors
  389.    values('486-29-1786', 'Locksley', 'Charlene', '415 585-4620',
  390.    '18 Broadway Av.', 'San Francisco', 'CA', '94130', 1)
  391. insert authors
  392.    values('648-92-1872', 'Blotchet-Halls', 'Reginald', '503 745-6402',
  393.    '55 Hillsdale Bl.', 'Corvallis', 'OR', '97330', 1)
  394. insert authors
  395.    values('341-22-1782', 'Smith', 'Meander', '913 843-0462',
  396.    '10 Mississippi Dr.', 'Lawrence', 'KS', '66044', 0)
  397.  
  398. GO
  399.  
  400. raiserror('Now at the inserts to publishers ....',1,1)
  401.  
  402. GO
  403.  
  404. insert publishers values('0736', 'New Moon Books', 'Boston', 'MA', 'USA')
  405. insert publishers values('0877', 'Binnet & Hardley', 'Washington', 'DC', 'USA')
  406. insert publishers values('1389', 'Algodata Infosystems', 'Berkeley', 'CA', 'USA')
  407. insert publishers values('9952', 'Scootney Books', 'New York', 'NY', 'USA')
  408. insert publishers values('1622', 'Five Lakes Publishing', 'Chicago', 'IL', 'USA')
  409. insert publishers values('1756', 'Ramona Publishers', 'Dallas', 'TX', 'USA')
  410. insert publishers values('9901', 'GGG&G', 'München', NULL, 'Germany')
  411. insert publishers values('9999', 'Lucerne Publishing', 'Paris', NULL, 'France')
  412.  
  413. GO
  414.  
  415. raiserror('Now at the inserts to pub_info ....',1,1)
  416.  
  417. GO
  418.  
  419. insert pub_info values('0736', 0xFFFFFFFF, 'None yet')
  420. insert pub_info values('0877', 0xFFFFFFFF, 'None yet')
  421. insert pub_info values('1389', 0xFFFFFFFF, 'None yet')
  422. insert pub_info values('9952', 0xFFFFFFFF, 'None yet')
  423. insert pub_info values('1622', 0xFFFFFFFF, 'None yet')
  424. insert pub_info values('1756', 0xFFFFFFFF, 'None yet')
  425. insert pub_info values('9901', 0xFFFFFFFF, 'None yet')
  426. insert pub_info values('9999', 0xFFFFFFFF, 'None yet')
  427.  
  428. GO
  429.  
  430. raiserror('Now at the inserts to titles ....',1,1)
  431.  
  432. GO
  433.  
  434. insert titles values ('PC8888', 'Secrets of Silicon Valley', 'popular_comp', '1389',
  435. $20.00, $8000.00, 10, 4095, 'Muckraking reporting on the world''s largest computer
  436. hardware and software manufacturers.', '06/12/94')
  437.  
  438. insert titles values ('BU1032', 'The Busy Executive''s Database Guide', 'business',
  439. '1389', $19.99, $5000.00, 10, 4095, 'An overview of available database systems with
  440. emphasis on common business applications. Illustrated.', '06/12/91')
  441.  
  442. insert titles values ('PS7777', 'Emotional Security: A New Algorithm', 'psychology',
  443. '0736', $7.99, $4000.00, 10, 3336, 'Protecting yourself and your loved ones from undue
  444. emotional stress in the modern world. Use of computer and nutritional aids emphasized.',
  445. '06/12/91')
  446.  
  447. insert titles values ('PS3333', 'Prolonged Data Deprivation: Four Case Studies',
  448. 'psychology', '0736', $19.99, $2000.00, 10, 4072, 'What happens when the data runs dry?
  449. Searching evaluations of information-shortage effects.', '06/12/91')
  450.  
  451. insert titles values ('BU1111', 'Cooking with Computers: Surreptitious Balance Sheets',
  452. 'business', '1389', $11.95, $5000.00, 10, 3876, 'Helpful hints on how to use your
  453. electronic resources to the best advantage.', '06/09/91')
  454.  
  455. insert titles values ('MC2222', 'Silicon Valley Gastronomic Treats', 'mod_cook', '0877',
  456. $19.99, $0.00, 12, 2032, 'Favorite recipes for quick, easy, and elegant meals.',
  457. '06/09/91')
  458.  
  459. insert titles values ('TC7777', 'Sushi, Anyone?', 'trad_cook', '0877', $14.99, $8000.00,
  460. 10, 4095, 'Detailed instructions on how to make authentic Japanese sushi in your spare
  461. time.', '06/12/91')
  462.  
  463. insert titles values ('TC4203', 'Fifty Years in Buckingham Palace Kitchens', 'trad_cook',
  464. '0877', $11.95, $4000.00, 14, 15096, 'More anecdotes from the Queen''s favorite cook
  465. describing life among English royalty. Recipes, techniques, tender vignettes.',
  466. '06/12/91')
  467.  
  468. insert titles values ('PC1035', 'But Is It User Friendly?', 'popular_comp', '1389',
  469. $22.95, $7000.00, 16, 8780, 'A survey of software for the naive user, focusing on the
  470. ''friendliness'' of each.', '06/30/91')
  471.  
  472. insert titles values('BU2075', 'You Can Combat Computer Stress!', 'business', '0736',
  473. $2.99, $10125.00, 24, 18722, 'The latest medical and psychological techniques for living
  474. with the electronic office. Easy-to-understand explanations.',
  475. '06/30/91')
  476.  
  477. insert titles values('PS2091', 'Is Anger the Enemy?', 'psychology', '0736', $10.95,
  478. $2275.00, 12, 2045, 'Carefully researched study of the effects of strong emotions on the
  479. body. Metabolic charts included.', '06/15/91')
  480.  
  481. insert titles values('PS2106', 'Life Without Fear', 'psychology', '0736', $7.00, $6000.00,
  482. 10, 111, 'New exercise, meditation, and nutritional techniques that can reduce the shock
  483. of daily interactions. Popular audience. Sample menus included, exercise video available
  484. separately.', '10/05/91')
  485.  
  486. insert titles values('MC3021', 'The Gourmet Microwave', 'mod_cook', '0877', $2.99,
  487. $15000.00, 24, 22246, 'Traditional French gourmet recipes adapted for modern microwave
  488. cooking.', '06/18/91')
  489.  
  490. insert titles values('TC3218', 'Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean',
  491. 'trad_cook', '0877', $20.95, $7000.00, 10, 375, 'Profusely illustrated in
  492. color, this makes a wonderful gift book for a cuisine-oriented friend.', '10/21/91')
  493.  
  494. insert titles (title_id, title, pub_id) values('MC3026', 'The Psychology of Computer Cooking',
  495. '0877')
  496.  
  497. insert titles values ('BU7832', 'Straight Talk About Computers', 'business', '1389',
  498. $19.99, $5000.00, 10, 4095, 'Annotated analysis of what computers can do for you: a no-
  499. hype guide for the critical user.', '06/22/91')
  500.  
  501. insert titles values('PS1372', 'Computer Phobic AND Non-Phobic Individuals: Behavior Variations',
  502. 'psychology', '0877', $21.59, $7000.00, 10, 375, 'A must for the specialist,
  503. this book examines the difference between those who hate and fear computers and those who
  504. don''t.', '10/21/91')
  505.  
  506. insert titles (title_id, title, type, pub_id, notes) values('PC9999', 'Net Etiquette',
  507. 'popular_comp', '1389', 'A must-read for computer conferencing.')
  508.  
  509. GO
  510.  
  511. raiserror('Now at the inserts to titleauthor ....',1,1)
  512.  
  513. GO
  514.  
  515. insert titleauthor values('409-56-7008', 'BU1032', 1, 60)
  516. insert titleauthor values('486-29-1786', 'PS7777', 1, 100)
  517. insert titleauthor values('486-29-1786', 'PC9999', 1, 100)
  518. insert titleauthor values('712-45-1867', 'MC2222', 1, 100)
  519. insert titleauthor values('172-32-1176', 'PS3333', 1, 100)
  520. insert titleauthor values('213-46-8915', 'BU1032', 2, 40)
  521. insert titleauthor values('238-95-7766', 'PC1035', 1, 100)
  522. insert titleauthor values('213-46-8915', 'BU2075', 1, 100)
  523. insert titleauthor values('998-72-3567', 'PS2091', 1, 50)
  524. insert titleauthor values('899-46-2035', 'PS2091', 2, 50)
  525. insert titleauthor values('998-72-3567', 'PS2106', 1, 100)
  526. insert titleauthor values('722-51-5454', 'MC3021', 1, 75)
  527. insert titleauthor values('899-46-2035', 'MC3021', 2, 25)
  528. insert titleauthor values('807-91-6654', 'TC3218', 1, 100)
  529. insert titleauthor values('274-80-9391', 'BU7832', 1, 100)
  530. insert titleauthor values('427-17-2319', 'PC8888', 1, 50)
  531. insert titleauthor values('846-92-7186', 'PC8888', 2, 50)
  532. insert titleauthor values('756-30-7391', 'PS1372', 1, 75)
  533. insert titleauthor values('724-80-9391', 'PS1372', 2, 25)
  534. insert titleauthor values('724-80-9391', 'BU1111', 1, 60)
  535. insert titleauthor values('267-41-2394', 'BU1111', 2, 40)
  536. insert titleauthor values('672-71-3249', 'TC7777', 1, 40)
  537. insert titleauthor values('267-41-2394', 'TC7777', 2, 30)
  538. insert titleauthor values('472-27-2349', 'TC7777', 3, 30)
  539. insert titleauthor values('648-92-1872', 'TC4203', 1, 100)
  540.  
  541. GO
  542.  
  543. raiserror('Now at the inserts to stores ....',1,1)
  544.  
  545. GO
  546.  
  547. insert stores values('7066','Barnum''s','567 Pasadena Ave.','Tustin','CA','92789')
  548. insert stores values('7067','News & Brews','577 First St.','Los Gatos','CA','96745')
  549. insert stores values('7131','Doc-U-Mat: Quality Laundry and Books',
  550.       '24-A Avogadro Way','Remulade','WA','98014')
  551. insert stores values('8042','Bookbeat','679 Carson St.','Portland','OR','89076')
  552. insert stores values('6380','Eric the Read Books','788 Catamaugus Ave.',
  553.       'Seattle','WA','98056')
  554. insert stores values('7896','Fricative Bookshop','89 Madison St.','Fremont','CA','90019')
  555.  
  556. GO
  557.  
  558. raiserror('Now at the inserts to sales ....',1,1)
  559.  
  560. GO
  561.  
  562. insert sales values('7066', 'QA7442.3', '09/13/94', 75, 'ON invoice','PS2091')
  563. insert sales values('7067', 'D4482', '09/14/94', 10, 'Net 60','PS2091')
  564. insert sales values('7131', 'N914008', '09/14/94', 20, 'Net 30','PS2091')
  565. insert sales values('7131', 'N914014', '09/14/94', 25, 'Net 30','MC3021')
  566. insert sales values('8042', '423LL922', '09/14/94', 15, 'ON invoice','MC3021')
  567. insert sales values('8042', '423LL930', '09/14/94', 10, 'ON invoice','BU1032')
  568. insert sales values('6380', '722a', '09/13/94', 3, 'Net 60','PS2091')
  569. insert sales values('6380', '6871', '09/14/94', 5, 'Net 60','BU1032')
  570. insert sales values('8042','P723', '03/11/93', 25, 'Net 30', 'BU1111')
  571. insert sales values('7896','X999', '02/21/93', 35, 'ON invoice', 'BU2075')
  572. insert sales values('7896','QQ2299', '10/28/93', 15, 'Net 60', 'BU7832')
  573. insert sales values('7896','TQ456', '12/12/93', 10, 'Net 60', 'MC2222')
  574. insert sales values('8042','QA879.1', '5/22/93', 30, 'Net 30', 'PC1035')
  575. insert sales values('7066','A2976', '5/24/93', 50, 'Net 30', 'PC8888')
  576. insert sales values('7131','P3087a', '5/29/93', 20, 'Net 60', 'PS1372')
  577. insert sales values('7131','P3087a', '5/29/93', 25, 'Net 60', 'PS2106')
  578. insert sales values('7131','P3087a', '5/29/93', 15, 'Net 60', 'PS3333')
  579. insert sales values('7131','P3087a', '5/29/93', 25, 'Net 60', 'PS7777')
  580. insert sales values('7067','P2121', '6/15/92', 40, 'Net 30', 'TC3218')
  581. insert sales values('7067','P2121', '6/15/92', 20, 'Net 30', 'TC4203')
  582. insert sales values('7067','P2121', '6/15/92', 20, 'Net 30', 'TC7777')
  583.  
  584. GO
  585.  
  586. raiserror('Now at the inserts to roysched ....',1,1)
  587.  
  588. GO
  589.  
  590. insert roysched values('BU1032', 0, 5000, 10)
  591. insert roysched values('BU1032', 5001, 50000, 12)
  592. insert roysched values('PC1035', 0, 2000, 10)
  593. insert roysched values('PC1035', 2001, 3000, 12)
  594. insert roysched values('PC1035', 3001, 4000, 14)
  595. insert roysched values('PC1035', 4001, 10000, 16)
  596. insert roysched values('PC1035', 10001, 50000, 18)
  597. insert roysched values('BU2075', 0, 1000, 10)
  598. insert roysched values('BU2075', 1001, 3000, 12)
  599. insert roysched values('BU2075', 3001, 5000, 14)
  600.  
  601. GO
  602.  
  603. insert roysched values('BU2075', 5001, 7000, 16)
  604. insert roysched values('BU2075', 7001, 10000, 18)
  605. insert roysched values('BU2075', 10001, 12000, 20)
  606. insert roysched values('BU2075', 12001, 14000, 22)
  607. insert roysched values('BU2075', 14001, 50000, 24)
  608. insert roysched values('PS2091', 0, 1000, 10)
  609. insert roysched values('PS2091', 1001, 5000, 12)
  610. insert roysched values('PS2091', 5001, 10000, 14)
  611. insert roysched values('PS2091', 10001, 50000, 16)
  612. insert roysched values('PS2106', 0, 2000, 10)
  613.  
  614. GO
  615.  
  616. insert roysched values('PS2106', 2001, 5000, 12)
  617. insert roysched values('PS2106', 5001, 10000, 14)
  618. insert roysched values('PS2106', 10001, 50000, 16)
  619. insert roysched values('MC3021', 0, 1000, 10)
  620. insert roysched values('MC3021', 1001, 2000, 12)
  621. insert roysched values('MC3021', 2001, 4000, 14)
  622. insert roysched values('MC3021', 4001, 6000, 16)
  623. insert roysched values('MC3021', 6001, 8000, 18)
  624. insert roysched values('MC3021', 8001, 10000, 20)
  625. insert roysched values('MC3021', 10001, 12000, 22)
  626.  
  627. GO
  628.  
  629. insert roysched values('MC3021', 12001, 50000, 24)
  630. insert roysched values('TC3218', 0, 2000, 10)
  631. insert roysched values('TC3218', 2001, 4000, 12)
  632. insert roysched values('TC3218', 4001, 6000, 14)
  633. insert roysched values('TC3218', 6001, 8000, 16)
  634. insert roysched values('TC3218', 8001, 10000, 18)
  635. insert roysched values('TC3218', 10001, 12000, 20)
  636. insert roysched values('TC3218', 12001, 14000, 22)
  637. insert roysched values('TC3218', 14001, 50000, 24)
  638. insert roysched values('PC8888', 0, 5000, 10)
  639. insert roysched values('PC8888', 5001, 10000, 12)
  640.  
  641. GO
  642.  
  643. insert roysched values('PC8888', 10001, 15000, 14)
  644. insert roysched values('PC8888', 15001, 50000, 16)
  645. insert roysched values('PS7777', 0, 5000, 10)
  646. insert roysched values('PS7777', 5001, 50000, 12)
  647. insert roysched values('PS3333', 0, 5000, 10)
  648. insert roysched values('PS3333', 5001, 10000, 12)
  649. insert roysched values('PS3333', 10001, 15000, 14)
  650. insert roysched values('PS3333', 15001, 50000, 16)
  651. insert roysched values('BU1111', 0, 4000, 10)
  652. insert roysched values('BU1111', 4001, 8000, 12)
  653. insert roysched values('BU1111', 8001, 10000, 14)
  654.  
  655. GO
  656.  
  657. insert roysched values('BU1111', 12001, 16000, 16)
  658. insert roysched values('BU1111', 16001, 20000, 18)
  659. insert roysched values('BU1111', 20001, 24000, 20)
  660. insert roysched values('BU1111', 24001, 28000, 22)
  661. insert roysched values('BU1111', 28001, 50000, 24)
  662. insert roysched values('MC2222', 0, 2000, 10)
  663. insert roysched values('MC2222', 2001, 4000, 12)
  664. insert roysched values('MC2222', 4001, 8000, 14)
  665. insert roysched values('MC2222', 8001, 12000, 16)
  666.  
  667. GO
  668.  
  669. insert roysched values('MC2222', 12001, 20000, 18)
  670. insert roysched values('MC2222', 20001, 50000, 20)
  671. insert roysched values('TC7777', 0, 5000, 10)
  672. insert roysched values('TC7777', 5001, 15000, 12)
  673. insert roysched values('TC7777', 15001, 50000, 14)
  674. insert roysched values('TC4203', 0, 2000, 10)
  675. insert roysched values('TC4203', 2001, 8000, 12)
  676. insert roysched values('TC4203', 8001, 16000, 14)
  677. insert roysched values('TC4203', 16001, 24000, 16)
  678. insert roysched values('TC4203', 24001, 32000, 18)
  679.  
  680. GO
  681.  
  682. insert roysched values('TC4203', 32001, 40000, 20)
  683. insert roysched values('TC4203', 40001, 50000, 22)
  684. insert roysched values('BU7832', 0, 5000, 10)
  685. insert roysched values('BU7832', 5001, 10000, 12)
  686. insert roysched values('BU7832', 10001, 15000, 14)
  687. insert roysched values('BU7832', 15001, 20000, 16)
  688. insert roysched values('BU7832', 20001, 25000, 18)
  689. insert roysched values('BU7832', 25001, 30000, 20)
  690. insert roysched values('BU7832', 30001, 35000, 22)
  691. insert roysched values('BU7832', 35001, 50000, 24)
  692.  
  693. GO
  694.  
  695. insert roysched values('PS1372', 0, 10000, 10)
  696. insert roysched values('PS1372', 10001, 20000, 12)
  697. insert roysched values('PS1372', 20001, 30000, 14)
  698. insert roysched values('PS1372', 30001, 40000, 16)
  699. insert roysched values('PS1372', 40001, 50000, 18)
  700.  
  701. GO
  702.  
  703. raiserror('Now at the inserts to discounts ....',1,1)
  704.  
  705. GO
  706.  
  707. insert discounts values('Initial Customer', NULL, NULL, NULL, 10.5)
  708. insert discounts values('Volume Discount', NULL, 100, 1000, 6.7)
  709. insert discounts values('Customer Discount', '8042', NULL, NULL, 5.0)
  710.  
  711. GO
  712.  
  713. raiserror('Now at the inserts to jobs ....',1,1)
  714.  
  715. GO
  716.  
  717. insert jobs values ('New Hire - Job not specified', 10, 10)
  718. insert jobs values ('Chief Executive Officer', 200, 250)
  719. insert jobs values ('Business Operations Manager', 175, 225)
  720. insert jobs values ('Chief Financial Officier', 175, 250)
  721. insert jobs values ('Publisher', 150, 250)
  722. insert jobs values ('Managing Editor', 140, 225)
  723. insert jobs values ('Marketing Manager', 120, 200)
  724. insert jobs values ('Public Relations Manager', 100, 175)
  725. insert jobs values ('Acquisitions Manager', 75, 175)
  726. insert jobs values ('Productions Manager', 75, 165)
  727. insert jobs values ('Operations Manager', 75, 150)
  728. insert jobs values ('Editor', 25, 100)
  729. insert jobs values ('Sales Representative', 25, 100)
  730. insert jobs values ('Designer', 25, 100)
  731.  
  732. GO
  733.  
  734. raiserror('Now at the inserts to employee ....',1,1)
  735.  
  736. GO
  737.  
  738. insert employee values ('PTC11962M', 'Philip', 'T', 'Cramer', 2, 215, '9952', '11/11/89')
  739. insert employee values ('AMD15433F', 'Ann', 'M', 'Devon', 3, 200, '9952', '07/16/91')
  740. insert employee values ('F-C16315M', 'Francisco', '', 'Chang', 4, 227, '9952', '11/03/90')
  741. insert employee values ('LAL21447M', 'Laurence', 'A', 'Lebihan', 5, 175, '0736',
  742. '06/03/90')
  743. insert employee values ('PXH22250M', 'Paul', 'X', 'Henriot', 5, 159, '0877', '08/19/93')
  744. insert employee values ('SKO22412M', 'Sven', 'K', 'Ottlieb', 5, 150, '1389', '04/05/91')
  745. insert employee values ('RBM23061F', 'Rita', 'B', 'Müller', 5, 198, '1622', '10/09/93')
  746. insert employee values ('MJP25939M', 'Maria', 'J', 'Pontes', 5, 246, '1756', '03/01/89')
  747. insert employee values ('JYL26161F', 'Janine', 'Y', 'Labrune', 5, 172, '9901', '05/26/91')
  748. insert employee values ('CFH28514M', 'Carlos', 'F', 'Hern dez', 5, 211, '9999',
  749. '04/21/89')
  750. insert employee values ('VPA30890F', 'Victoria', 'P', 'Ashworth', 6, 140, '0877',
  751. '09/13/90')
  752. insert employee values ('L-B31947F', 'Lesley', '', 'Brown', 7, 120, '0877', '02/13/91')
  753. insert employee values ('ARD36773F', 'Anabela', 'R', 'Domingues', 8, 100, '0877',
  754. '01/27/93')
  755. insert employee values ('M-R38834F', 'Martine', '', 'Rancé', 9, 75, '0877', '02/05/92')
  756. insert employee values ('PHF38899M', 'Peter', 'H', 'Franken', 10, 75, '0877', '05/17/92')
  757. insert employee values ('DBT39435M', 'Daniel', 'B', 'Tonini', 11, 75, '0877', '01/01/90')
  758. insert employee values ('H-B39728F', 'Helen', '', 'Bennett', 12, 35, '0877', '09/21/89')
  759. insert employee values ('PMA42628M', 'Paolo', 'M', 'Accorti', 13, 35, '0877', '08/27/92')
  760. insert employee values ('ENL44273F', 'Elizabeth', 'N', 'Lincoln', 14, 35, '0877',
  761. '07/24/90')
  762. insert employee values ('MGK44605M', 'Matti', 'G', 'Karttunen', 6, 220, '0736',
  763. '05/01/94')
  764. insert employee values ('PDI47470M', 'Palle', 'D', 'Ibsen', 7, 195, '0736', '05/09/93')
  765. insert employee values ('MMS49649F', 'Mary', 'M', 'Saveley', 8, 175, '0736', '06/29/93')
  766. insert employee values ('GHT50241M', 'Gary', 'H', 'Thomas', 9, 170, '0736', '08/09/88')
  767. insert employee values ('MFS52347M', 'Martín', 'F', 'Sommer', 10, 165, '0736', '04/13/90')
  768. insert employee values ('R-M53550M', 'Roland', '', 'Mendel', 11, 150, '0736', '09/05/91')
  769. insert employee values ('HAS54740M', 'Howard', 'A', 'Snyder', 12, 100, '0736', '11/19/88')
  770. insert employee values ('TPO55093M', 'Timothy', 'P', 'O''Rourke', 13, 100, '0736',
  771. '06/19/88')
  772. insert employee values ('KFJ64308F', 'Karin', 'F', 'Josephs', 14, 100, '0736', '10/17/92')
  773. insert employee values ('DWR65030M', 'Diego', 'W', 'Roel', 6, 192, '1389', '12/16/91')
  774. insert employee values ('M-L67958F', 'Maria', '', 'Larsson', 7, 135, '1389', '03/27/92')
  775. insert employee values ('PSP68661F', 'Paula', 'S', 'Parente', 8, 125, '1389', '01/19/94')
  776. insert employee values ('MAS70474F', 'Margaret', 'A', 'Smith', 9, 78, '1389', '09/29/88')
  777. insert employee values ('A-C71970F', 'Aria', '', 'Cruz', 10, 87, '1389', '10/26/91')
  778. insert employee values ('MAP77183M', 'Miguel', 'A', 'Paolino', 11, 112, '1389',
  779. '12/07/92')
  780. insert employee values ('Y-L77953M', 'Yoshi', '', 'Latimer', 12, 32, '1389', '06/11/89')
  781. insert employee values ('CGS88322F', 'Carine', 'G', 'Schmitt', 13, 64, '1389', '07/07/92')
  782. insert employee values ('PSA89086M', 'Pedro', 'S', 'Afonso', 14, 89, '1389', '12/24/90')
  783. insert employee values ('A-R89858F', 'Annette', '', 'Roulet', 6, 152, '9999', '02/21/90')
  784. insert employee values ('HAN90777M', 'Helvetius', 'A', 'Nagy', 7, 120, '9999', '03/19/93')
  785. insert employee values ('M-P91209M', 'Manuel', '', 'Pereira', 8, 101, '9999', '01/09/89')
  786. insert employee values ('KJJ92907F', 'Karla', 'J', 'Jablonski', 9, 170, '9999',
  787. '03/11/94')
  788. insert employee values ('POK93028M', 'Pirkko', 'O', 'Koskitalo', 10, 80, '9999',
  789. '11/29/93')
  790. insert employee values ('PCM98509F', 'Patricia', 'C', 'McKenna', 11, 150, '9999',
  791. '08/01/89')
  792.  
  793. GO
  794.  
  795. raiserror('Now at the create index section ....',1,1) with nowait
  796.  
  797. GO
  798.  
  799. CREATE CLUSTERED INDEX employee_ind ON employee(lname, fname, minit)
  800.  
  801. GO
  802.  
  803. CREATE NONCLUSTERED INDEX aunmind ON authors (au_lname, au_fname)
  804. GO
  805. CREATE NONCLUSTERED INDEX titleidind ON sales (title_id)
  806. GO
  807. CREATE NONCLUSTERED INDEX titleind ON titles (title)
  808. GO
  809. CREATE NONCLUSTERED INDEX auidind ON titleauthor (au_id)
  810. GO
  811. CREATE NONCLUSTERED INDEX titleidind ON titleauthor (title_id)
  812. GO
  813. CREATE NONCLUSTERED INDEX titleidind ON roysched (title_id)
  814. GO
  815.  
  816. raiserror('Now at the create view section ....',1,1)
  817.  
  818. GO
  819.  
  820. CREATE VIEW titleview
  821. AS
  822. select title, au_ord, au_lname, price, ytd_sales, pub_id
  823. from authors, titles, titleauthor
  824. where authors.au_id = titleauthor.au_id
  825.    AND titles.title_id = titleauthor.title_id
  826.  
  827. GO
  828.  
  829. raiserror('Now at the create procedure section ....',1,1)
  830.  
  831. GO
  832.  
  833. CREATE PROCEDURE byroyalty @percentage int
  834. AS
  835. select au_id from titleauthor
  836. where titleauthor.royaltyper = @percentage
  837.  
  838. GO
  839.  
  840. raiserror('(Next is the first Grant embedded in the proc section.)',1,1)
  841.  
  842. GRANT execute ON byroyalty TO public
  843.  
  844. GO
  845.  
  846. CREATE PROCEDURE reptq1 AS
  847. select pub_id, title_id, price, pubdate
  848. from titles
  849. where price is NOT NULL
  850. order by pub_id
  851. COMPUTE avg(price) BY pub_id
  852. COMPUTE avg(price)
  853.  
  854. GO
  855.  
  856. GRANT execute ON reptq1 TO public
  857.  
  858. GO
  859.  
  860. CREATE PROCEDURE reptq2 AS
  861. select type, pub_id, titles.title_id, au_ord,
  862.    Name = substring (au_lname, 1,15), ytd_sales
  863. from titles, authors, titleauthor
  864. where titles.title_id = titleauthor.title_id AND authors.au_id = titleauthor.au_id
  865.    AND pub_id is NOT NULL
  866. order by pub_id, type
  867. COMPUTE avg(ytd_sales) BY pub_id, type
  868. COMPUTE avg(ytd_sales) BY pub_id
  869.  
  870. GO
  871.  
  872. GRANT execute ON reptq2 TO public
  873.  
  874. GO
  875.  
  876. CREATE PROCEDURE reptq3 @lolimit money, @hilimit money,
  877. @type char(12)
  878. AS
  879. select pub_id, type, title_id, price
  880. from titles
  881. where price >@lolimit AND price <@hilimit AND type = @type OR type LIKE '%cook%'
  882. order by pub_id, type
  883. COMPUTE count(title_id) BY pub_id, type
  884.  
  885. GO
  886.  
  887. GRANT execute ON reptq3 TO public
  888.  
  889. GO
  890.  
  891. GRANT CREATE PROCEDURE TO public
  892.  
  893. GO
  894.  
  895. raiserror('Now at the GUEST section ....',1,1)
  896.  
  897. GO
  898.  
  899. execute sp_adduser guest
  900.  
  901. GO
  902.  
  903. GRANT ALL ON publishers TO guest
  904. GRANT ALL ON pub_info TO guest
  905. GRANT ALL ON employee TO guest
  906. GRANT ALL ON jobs TO guest
  907. GRANT ALL ON titles TO guest
  908. GRANT ALL ON authors TO guest
  909. GRANT ALL ON titleauthor TO guest
  910. GRANT ALL ON sales TO guest
  911. GRANT ALL ON roysched TO guest
  912. GRANT ALL ON stores TO guest
  913. GRANT ALL ON discounts TO guest
  914. GRANT ALL ON titleview TO guest
  915.  
  916. GRANT execute ON byroyalty TO guest
  917.  
  918. GRANT CREATE TABLE TO guest
  919. GRANT CREATE VIEW TO guest
  920. GRANT CREATE RULE TO guest
  921. GRANT CREATE DEFAULT TO guest
  922. GRANT CREATE PROCEDURE TO guest
  923.  
  924. GO
  925.  
  926. UPDATE STATISTICS publishers
  927. UPDATE STATISTICS employee
  928. UPDATE STATISTICS jobs
  929. UPDATE STATISTICS pub_info
  930. UPDATE STATISTICS titles
  931. UPDATE STATISTICS authors
  932. UPDATE STATISTICS titleauthor
  933. UPDATE STATISTICS sales
  934. UPDATE STATISTICS roysched
  935. UPDATE STATISTICS stores
  936. UPDATE STATISTICS discounts
  937.  
  938. GO
  939.  
  940. CHECKPOINT
  941.  
  942. GO
  943.  
  944. USE master
  945.  
  946. GO
  947.  
  948. CHECKPOINT
  949.  
  950. GO
  951.  
  952. declare @dttm varchar(55)
  953. select  @dttm=convert(varchar,getdate(),113)
  954. raiserror('Ending InstPubs.SQL at %s ....',1,1,@dttm) with nowait
  955.  
  956. GO
  957. -- -
  958.  
  959.