home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / sql.txt / vtest2 < prev   
Encoding:
Text File  |  1993-07-04  |  7.3 KB  |  445 lines

  1. #0#
  2. create domain s# is char5 where s# is not null;
  3. #1#
  4. create domain p# is char6 where p# is not null;
  5. #2#
  6. create domain j# is char4 where j# is not null;
  7. #3#
  8. create domain name is char20;
  9. #4#
  10. create domain status, weight, qty is int;
  11. #5#
  12. create domain city is char15;
  13. #6#
  14. create domain color is char6 where color in ( "green", "red", "blue");
  15. #7#
  16. create table s
  17.         primary (s#)
  18.         ( s_name, status, city);
  19.  
  20. #8#
  21. create table p
  22.         primary (p#)
  23.         ( p_name, color, weight, city);
  24.  
  25. #9#
  26. create table j
  27.         primary (j#)
  28.         ( j_name, city);
  29.  
  30. #10#
  31. create table spj
  32.         primary (s#, p#, j#) 
  33.         foreign (s# identifies s
  34.                 nulls not allowed
  35.                 delete of s restricted
  36.                 update of s.s# cascades) 
  37.         foreign (p# identifies p
  38.                 nulls not allowed
  39.                 delete of p restricted
  40.                 update of p.p# restricted) 
  41.         foreign (j# identifies j
  42.                 nulls not allowed
  43.                 delete of s restricted
  44.                 update of j.j# cascades) 
  45.         (qty);
  46.  
  47. #11#
  48. create unique index sx  on s (s#);
  49. #12#
  50. create unique index px  on p (p#);
  51. #13#
  52. create unique index jx  on j (j#);
  53. #14#
  54. create unique index spjx        on spj (s#, p#, j#);
  55.  
  56. #15#
  57. insert into s from sdate;
  58. #16#
  59. insert into p from pdate;
  60. #17#
  61. insert into j from jdate;
  62. #18#
  63. insert into spj from spjdate;
  64.  
  65. #19#
  66. create view sp
  67.         as select s#, p# from spj;
  68.  
  69. #20#
  70. select s#, status
  71. from s
  72. where city = 'Paris';
  73.  
  74. #21#
  75. select p#
  76. from sp;
  77.  
  78. #22#
  79. select unique p#
  80. from sp;
  81.  
  82. #23#
  83. select p#, weight * 454
  84. from p;
  85.  
  86. #24#
  87. select p#, "Weight in grams =", weight * 454
  88. from p;
  89.  
  90. #25#
  91. select *
  92. from s;
  93.  
  94. #26#
  95. select s#, s_name, status, city
  96. from s;
  97.  
  98. #27#
  99. select s.*
  100. from s;
  101.  
  102. #28#
  103. select s#
  104. from s
  105. where city = "paris"
  106. and status > 20;
  107.  
  108. #29#
  109. select s#, status
  110. from s
  111. where city = "paris"
  112. order by status desc;
  113.  
  114. #30#
  115. select p#, weight * 454
  116. from p
  117. order by 2, P#;
  118.  
  119. #31#
  120. select p#, pname, color, weight, city
  121. from p
  122. where weight between 16 and 19;
  123.  
  124. #32#
  125. select p#, pname, color, weight, city
  126. from p
  127. where weight not between 16 and 19;
  128.  
  129. #33#
  130. select p#, pname, color, weight, city
  131. from p
  132. where weight in (12, 16, 17);
  133.  
  134. #34#
  135. select p#, pname, color, weight, city
  136. from p
  137. where weight not in (12, 16, 17);
  138.  
  139. #35#
  140. select p#, pname, color, weight, city
  141. from p
  142. where  weight = 12 or weight = 16 or weight = 17;
  143.  
  144. #36#
  145. select p#, pname, color, weight, city
  146. from p
  147. where pname like 'C%';
  148.  
  149. #37#
  150. select s#
  151. from s
  152. where status is null;
  153.  
  154. #38#
  155. select s.*, p.*
  156. from s, p
  157. where s.city = p.city;
  158.  
  159. #39#
  160. select s.*, p.*
  161. from s, p
  162. where s.city > p.city;
  163.  
  164. #40#
  165. select s.*, p.*
  166. from s, p
  167. where s.city = p.city
  168. and  s.status != 20;
  169.  
  170. #41#
  171. select s.s#, p.p#
  172. from s, p
  173. where s.city = p.city;
  174.  
  175. #42#
  176. select unique s.*, p.*
  177. from s, sp, p
  178. where s.s# = sp.s#
  179. and sp.p# = p.p#;
  180.  
  181. #43#
  182. select first.s#, second.s#
  183. from s first, s second
  184. where first.city = second.city;
  185.  
  186. #44#
  187. select first.s#, second.s#
  188. from s first, s second
  189. where first.city = second.city
  190. and first.s# < second.s#;
  191.  
  192. #45#
  193. select s#, p#, j#
  194. from s, p, j
  195. where not
  196.         (s.city = p.city and p.city = j.city);
  197.  
  198. #46#
  199. select s#, p#, j#
  200. from s, p, j
  201. where s.city != p.city
  202. and p.city != j.city
  203. and j.city != s.city;
  204.  
  205. #47#
  206. select unique j#
  207. from spj spjx
  208. where not exists
  209.         (select *
  210.         from spj spjy
  211.         where spjy.j# = spjx.j#
  212.         and  not exists
  213.                 (select *
  214.                 from spj spjz
  215.                 where spjz.p# = spjy.p#
  216.                 and spjz.s# = 'S1'
  217.                 )
  218.         );
  219.  
  220. #48#
  221. select unique j#
  222. from spj spjx
  223. where not exists
  224.         (select *
  225.         from spj spjy
  226.         where exists
  227.                 (select *
  228.                 from spj spja
  229.                 where spja.s# = spjy.s#
  230.                 and spja.p# in
  231.                         (select  p#
  232.                         from p
  233.                         where color = 'red')
  234.                 and not exists
  235.                         (select *
  236.                         from spj spjb
  237.                         where spjb.s# = spjy.s#
  238.                         and spjb.j# = spjx.j# )));
  239.  
  240. #49#
  241. select unique s#
  242. from spj
  243. where p# in
  244.         ( select p#
  245.         from spj
  246.         where s# in
  247.                 (select s#
  248.                 from spj
  249.                 where p# in
  250.                         (select p#
  251.                         from p
  252.                         where color = "red" )));
  253.  
  254. #50#
  255. select unique s#
  256. from spj spjx
  257. where p# = 'p1'
  258. and qty >
  259.         ( select avg(qty)
  260.         from spj spjy
  261.         where p# = 'P1'
  262.         and spjy.j# = spjx.j# );
  263.  
  264. #51#
  265. select count(unique j#)
  266. from spj
  267. where s# = 's1';
  268.  
  269. #52#
  270. select j#
  271. from spj
  272. where p# = 'p1'
  273. group by j#
  274. having avg (qty) >
  275.         (select max(qty)
  276.         from spj
  277.         where j# = 'j1');
  278.  
  279. #53#
  280. select city from s
  281. union
  282. select city from p
  283. union
  284. select city from j
  285. order by 1;
  286.  
  287. #54#
  288. select p#, 'weight >16 lb'
  289. from p
  290. where weight >16
  291. union
  292. select p#, 'supplied by S2'
  293. from sp
  294. where s# = 'S2'
  295. order by 2, 1;
  296.  
  297. #55#
  298. select p.p#, 'Weight in grams =', p.weight * 454, p.color,
  299.         'Max shipped quantity =', max(sp.qty)
  300. from p, sp
  301. where p.p# = sp.p#
  302. and p.color in ('red', 'blue')
  303. and sp.qty >200
  304. group by p.p#, p.weight, p.color
  305. having sum(qty) >350
  306. order by 6, p.p# desc;
  307.  
  308. #56#
  309. select unique spjx.j#
  310. from spj spjx
  311. where exists
  312.         (select *
  313.         from spj spjy
  314.         where spjy.p# = spjx.p#
  315.         and spjy.s# = 's1');
  316.  
  317. #57#
  318. update p
  319. set color = 'Orange'
  320. Where color = 'Red';
  321.  
  322. #58#
  323. delete
  324. from j
  325. where j# not in
  326.         (select j#
  327.         from spj );
  328.  
  329. #59#
  330. create table reds (s#);
  331.  
  332. #60#
  333. insert into red (s#)
  334.         select unique s#
  335.         from spj, p
  336.         where spj.p# = p.p#
  337.         and color = 'Red';
  338.  
  339. #61#
  340. update spj
  341. set qty = qty *1.1
  342. where s# in
  343.         (select s#
  344.         from REDS );
  345.  
  346. #62#
  347. drop table reds;
  348.  
  349. #63#
  350. delete
  351. from spj
  352. where 'Rome' =
  353.         (select city
  354.         from j
  355.         where j.j# = spj.j#);
  356.  
  357. #64#
  358. delete
  359. from j
  360. where city = 'Rome';
  361.  
  362. #65#
  363. insert
  364. into s (s#, s_name, city)
  365. values ('S10', 'white', 'New York');
  366.  
  367. #66#
  368. insert
  369. into s (s#, s_name, status, city)
  370. values ('S11', 'white', NULL, 'New York');
  371.  
  372. #67#
  373. create view spv (s#, p#, qty )
  374.         as select s#, p#, sum (qty)
  375.         from spj
  376.         group by s#, p#;
  377.  
  378. #68#
  379. create view jc (j#, city )
  380.         as select unique j.j#, j.city
  381.         from j, spj
  382.         where j.j# = spj.j#
  383.         and  (spj.s# = 'S1' or spj.p# = 'P1' );
  384.  
  385. #69#
  386. create view jc (j#, city)
  387.         as select j.j#, j.city
  388.         from j
  389.         where j.j# in
  390.                 (select j#
  391.                 from spj
  392.                 where s# = 'S1')
  393.         and j.j# in
  394.                 (select j#
  395.                 from spj
  396.                 where p# = 'P1');
  397.  
  398. #70#
  399. create view non_colocated
  400.         as select s#, p#
  401.         from s, p
  402.         where s.city != p.city;
  403.  
  404. #71#
  405. create view london_suppliers
  406.         as select s#, s_name, status
  407.         from s
  408.         where city = "London";
  409.  
  410. #72#
  411. grant  select on table s to charly;
  412.  
  413. #73#
  414. grant select , update(status, city) on table s
  415.         to judy, jack, john;
  416.  
  417. #74#
  418. grant createtab on database dbx to sharon;
  419.  
  420. #75#
  421. grant select on table s to u2 with grant option;
  422.  
  423. #76#
  424. revoke select on table s from u2;
  425.  
  426. #77#
  427. revoke select, update on table s from u2;
  428.  
  429. #78#
  430. lock table s in exclusive mode;
  431.  
  432. #79#
  433. lock table p in share mode;
  434.  
  435. #80#
  436. store program backup ( name = "p", fname = "/backup/p" )
  437. lock table name in exclusive mode;
  438. dump table name to fname;
  439. lock table name in share mode;
  440. commit work;
  441. end store;
  442.  
  443. #81#
  444. firstprog ( "p", "/backup/p" );
  445.