home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 4.img / SCRIPTS / INSTPUBS.SQL < prev   
Encoding:
Text File  |  1992-05-17  |  22.4 KB  |  949 lines

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