home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a520 / 3.ddi / WINTUTOR.WTS / WINTUTOR.WTS
Encoding:
Text File  |  1990-10-28  |  72.5 KB  |  2,198 lines

  1. REMARK
  2. \
  3. The following tutorial is intended to illustrate the  capabilities  of the
  4. SQL language as implemented in the  SQLBASE  database management  system.
  5. Since SQLBASE is intended  primarily  as  a  database  engine  for  serious
  6. application development on the IBM PC family of computers and  networks,  a
  7. basic level of familiarity with database concepts is assumed.
  8.  
  9. Note also that the program you are currently running is called SQLTALK.  It
  10. is an Interactive Data Manager that enables you  to  execute  SQL  commands
  11. (either interactively from the keyboard or  by executing  a  batch  file).
  12. SQLTALK also contains certain non-SQL commands.  For example,  PAUSE  is  a
  13. SQLTALK command that causes a pause to occur in the output.  Press Carriage
  14. Return to proceed after the pause.
  15.  
  16. NOTE: This tutorial assumes that you are connected to the DEMO database. If
  17.       you are not, halt this tutorial, connect to DEMO and begin again.
  18. /
  19. PAUSE
  20. /
  21. REMARK
  22. \
  23. The following SQLTALK command will cause the time taken by the database for
  24. performing each operation to be displayed after the  results. In  queries
  25. involving screen output, the time reflects how long the database  takes  to
  26. process the query rather than the elapsed time.  Thus, screen display  time
  27. is not included in this measurement.
  28. /
  29. SET TIME ON
  30. /
  31. REMARK
  32. \
  33. Other SQLTALK commands include REMARK, RUN, STORE, EXECUTE etc.  that  make
  34. this tutorial more easy to  read  or  are  intended  to  perform  functions
  35. usually  available  only  through  the C  Application Program  Interface.
  36. SQLTALK can also be used to format reports based  on  the  results  of SQL
  37. queries.  These report writing facilities are illustrated towards  the end
  38. of the tutorial.  In addition, SQLTALK has commands  that  are useful for
  39. database administrators (such as CONNECT,  DISCONNECT, LOAD  and  UNLOAD).
  40. All commands issued to SQLTALK (whether they are SQL statements or  SQLTALK
  41. commands) must be terminated by a / on a new line or a ; at the end of the
  42. last line of the command.  Throughout this session, we will  use  a  /  to
  43. signify the end of a command.
  44.  
  45. During this session, simply press Carriage Return when you are ready to  go
  46. to the next command.  In case a query results in more than one screenful of
  47. data, press Carriage Return to see the next screen or <A> to abort  and  go
  48. to the next command.  To exit from the SQLTALK demo, type in Control-C.
  49.  
  50. We start off by first creating a table called PRESIDENT.
  51. /
  52. CREATE TABLE PRESIDENT
  53. (PRES_NAME VARCHAR(20) NOT NULL,BIRTH_DATE TIMESTAMP,YRS_SERV INTEGER,
  54. DEATH_AGE INTEGER,
  55. PARTY VARCHAR (20),STATE_BORN VARCHAR(20))
  56. /
  57. REMARK
  58. \
  59. In the above table, we defined six columns.  The data in those columns can
  60. be of four types: a character field, a number field, a date field or a long
  61. field. SQLBASE supports  the  following  SQL  data  type  keywords:  CHAR,
  62. VARCHAR, SMALLINT, INTEGER, FLOAT,  DECIMAL,  and  LONG  VARCHAR.   In the
  63. PRESIDENT table,  PRES_NAME,  PARTY  and  STATE_BORN  are  variable  length
  64. character fields with a maximum length of 250. The PRES_NAME is  specified
  65. to be a NOT NULL field, ie. it may not contain a blank.
  66.  
  67. BIRTH_DATE is a date  (and  may  be  entered  in  one  of  three  formats).
  68. YRS_SERV and DEATH_AGE are  numeric  data  types  and  may  be entered  as
  69. integers.  No LONG VARCHAR data field is defined  (these  may  be  variable
  70. length character fields of unlimited length and can contain text or  binary
  71. data). The SQLBASE Language Reference Guide contains more information on
  72. SQL data types.
  73. /
  74. PAUSE
  75. /
  76. REMARK
  77. \
  78. The following INSERT command adds one row of data to the  PRESIDENT  table.
  79. Note all character data must be enclosed in single  quotes.   Numbers  have
  80. been entered as integers and the date field  is  specified  in dd-mon-yyyy
  81. format.
  82. /
  83. INSERT INTO PRESIDENT
  84. VALUES ('Washington G','1732-02-22-00.00.00.000000',4,67,'Federalist','Virginia')
  85. /
  86. REMARK
  87. \
  88. In the above SQL statement, an entire row of data  was entered.   You may
  89. also enter data selectively into some columns only, and  not  into  others.
  90. To do this, you must specify the columns into which data is to be inserted.
  91. The remaining columns will all contain the null character.   The  following
  92. statement inserts data into all six columns of the  PRESIDENTS  table but
  93. lists them individually.
  94. /
  95. INSERT INTO PRESIDENT (PRES_NAME, BIRTH_DATE, YRS_SERV,
  96. DEATH_AGE, PARTY, STATE_BORN)
  97. VALUES ('Jefferson T','1743-04-13-00.00.00.000000',8,83,'Demo-Rep','Virginia')
  98. /
  99. REMARK
  100. \
  101. Although the above example did not show this, note that SQLBASE will  allow
  102. you to use the decimal or scientific notation to enter numbers.   Magnitude
  103. of the numbers may range from E-99 to  E+99.   Decimal precision  upto  18
  104. digits is maintained automatically.  Dates (as in BIRTH_DATE above) may  be
  105. entered in mm-dd-yy,  mm/dd/yy or  dd-mon-yy  format.  The  'yy'  may  be
  106. expanded to 'yyyy' if the year is not in the current century.
  107. /
  108. PAUSE
  109. /
  110. REMARK
  111. \
  112. The following is an example of the use of an INSERT statement to enter more
  113. than one row into a table with a single command.  Here the :1,:2,:3, :4, :5
  114. and :6 are called 'Bind Variables'.  The SQLBASE C Program Interface  Guide
  115. contains more on Bind  Variables.   Note  however  that  in  the  following
  116. example, the SQL command is followed by a \, then by the individual rows of
  117. data in comma separated format, and then by a / to signify  termination  of
  118. the SQL command.  This convention is specific to the SQLTALK  program.  If
  119. you create your own command files for  use  with  SQLTALK,  make  sure you
  120. follow this convention.
  121. /
  122. INSERT INTO PRESIDENT (PRES_NAME, BIRTH_DATE, YRS_SERV, DEATH_AGE, PARTY,
  123. STATE_BORN) VALUES (:1, :2, :3, :4, :5, :6)
  124. \
  125. $datatypes character,datetime,numeric,numeric,character,character
  126. Adams J,30-Oct-1735,4,90,Federalist,Massachusetts
  127. Madison J,16-Mar-1751,8,85,Demo-Rep,Virginia
  128. Monroe J,28-Apr-1758,8,73,Demo-Rep,Virginia
  129. Adams J Q,11-Jul-1767,4,80,Demo-Rep,Massachusetts
  130. Jackson A,15-Mar-1767,8,78,Democratic,South Carolina
  131. Van Buren M,05-Dec-1782,4,79,Democratic,New York
  132. Harrison W H,09-Feb-1773,0,68,Whig,Virginia
  133. Tyler J,29-Mar-1790,3,71,Whig,Virginia
  134. Polk J K,02-Nov-1795,4,53,Democratic,North Carolina
  135. Taylor Z,24-Nov-1784,1,65,Whig,Virginia
  136. Fillmore M,07-Jan-1800,2,74,Whig,New York
  137. Pierce F,23-Nov-1804,4,64,Democratic,New Hampshire
  138. Buchanan J,23-Apr-1791,4,77,Democratic ,Pennsylvania
  139. Lincoln A,12-Feb-1809,4,56,Republican,Kentucky
  140. Johnson A,29-Dec-1808,3,66,Democratic ,North Carolina
  141. Grant U S,27-Apr-1822,8,63,Republican,Ohio
  142. Hayes R B,04-Oct-1822,4,70,Republican,Ohio
  143. Garfield J A,19-Nov-1831,0,49,Republican,Ohio
  144. Arthur C A,05-Oct-1829,3,56,Republican,Vermont
  145. Cleveland G,18-Mar-1837,8,71,Democratic,New Jersey
  146. Harrison B,20-Aug-1833,4,67,Republican,Ohio
  147. McKinley W,29-Jan-1843,4,58,Republican,Ohio
  148. Roosevelt T,27-Oct-1858,7,60,Republican,New York
  149. Taft W H,15-Sep-1857,4,72,Republican,Ohio
  150. Wilson W,28-Dec-1856,8,67,Democratic,Virginia
  151. Harding W G,02-Nov-1865,2,57,Republican,Ohio
  152. Coolidge C,4-Jul-1872,5,60,Republican,Vermont
  153. Hoover H C,10-Aug-1874,4,90,Republican,Iowa
  154. Roosevelt F D,30-Jan-1882,12,63,Democratic,New York
  155. Truman H S,08-May-1884,7,88,Democratic,Missouri
  156. Eisenhower D D,14-Oct-1890,8,79,Republican,Texas
  157. Kennedy J F,29-May-1917,2,46,Democratic,Massachusetts
  158. Johnson L B,27-Aug-1908,5,65,Democratic,Texas
  159. Nixon R M,09-Jan-1913,5,,Republican,California
  160. Ford G R,14-Jul-1913,2,,Republican,Nebraska
  161. Carter J E,01-Oct-1924,4,,Democratic,Georgia
  162. Reagan R, 6-Feb-1911,4,,Republican,Illinois
  163. /
  164. REMARK
  165. \
  166. The following command (COMMIT) will be explained  later.   It  is  used  to
  167. ensure that all changes made to a database since the  previous COMMIT (or
  168. since the database was opened) are written to disk and will not be lost  in
  169. case of system failure.
  170. /
  171. COMMIT
  172. /
  173. REMARK
  174. \
  175. The SELECT command is used to query the  database.   The  following  is  an
  176. example of a very simple SELECT command that requests all  of  the  columns
  177. and rows in the PRESIDENT table.
  178. /
  179. SELECT *
  180. FROM   PRESIDENT
  181. /
  182. REMARK
  183. \
  184. In the following example, only the  PRES_NAME, BIRTH_DATE,  DEATH_AGE and
  185. STATE_BORN columns are requested.
  186. /
  187. SELECT PRES_NAME, BIRTH_DATE, DEATH_AGE, STATE_BORN
  188. FROM   PRESIDENT
  189. /
  190. REMARK
  191. \
  192. Here, in addition to limiting the request to the president's name  and his
  193. birth state, only those rows are selected where the birth  state  is  Ohio.
  194. Note that since Ohio is a  character  constant,  it  must  be  enclosed  in
  195. quotes.
  196. /
  197. SELECT PRES_NAME, STATE_BORN
  198. FROM PRESIDENT
  199. WHERE STATE_BORN = 'Ohio'
  200. /
  201. REMARK
  202. \
  203. Here, only the presidents not born in Ohio are selected.
  204. /
  205. SELECT PRES_NAME, STATE_BORN
  206. FROM PRESIDENT
  207. WHERE STATE_BORN != 'Ohio'
  208. /
  209. REMARK
  210. \
  211. The query doesnt always need to be based on an exact match.   For  example,
  212. in the following query, we want to see the  names  of  all  the  presidents
  213. whose name contains the suffix 'son'.
  214. /
  215. SELECT PRES_NAME
  216. FROM PRESIDENT
  217. WHERE PRES_NAME LIKE '%son%'
  218. /
  219. REMARK
  220. \
  221. The % sign signifies a wild card character.  In the above query,  it  means
  222. that any name that contains the character string 'son' ( regardless of what
  223. comes before or after it ) will be retrieved.  The following query performs
  224. a more exact match.  Only those names are requested where the string  'son'
  225. is preceded by any four characters.
  226. /
  227. SELECT PRES_NAME
  228. FROM PRESIDENT
  229. WHERE PRES_NAME LIKE '____son%'
  230. /
  231. REMARK
  232. \
  233. The _ character is thus a more restrictive wild card character than the  %.
  234. Both of them may be intermixed in all sorts of combinations.  The following
  235. is an example of such intermixing.
  236. /
  237. SELECT PRES_NAME
  238. FROM PRESIDENT
  239. WHERE PRES_NAME LIKE 'J___son%'
  240. /
  241. REMARK
  242. \
  243. The above were examples where character strings were matched either exactly
  244. or partially.  The following two queries perform numeric comparisons.  They
  245. display the names and ages of all the presidents who died at age less  than
  246. 60 and at age greater than or equal to 60.
  247. /
  248. SELECT PRES_NAME, DEATH_AGE
  249. FROM PRESIDENT
  250. WHERE DEATH_AGE < 60
  251. /
  252. SELECT PRES_NAME, DEATH_AGE
  253. FROM PRESIDENT
  254. WHERE DEATH_AGE >= 60
  255. /
  256. REMARK
  257. \
  258. Note that the names of living presidents did not show up in the  above two
  259. queries.  That's because in the database, their death_age is represented by
  260. a null character.  A null implies that the value of a particular  field  is
  261. 'unknown'.  To retrieve a field on the basis of a null match, the  IS  NULL
  262. function must be used.
  263. /
  264. SELECT PRES_NAME, DEATH_AGE
  265. FROM PRESIDENT
  266. WHERE DEATH_AGE IS NULL
  267. /
  268. REMARK
  269. \
  270. SQLBASE also allows comparisons  to  be  made  for  date  fields.   In the
  271. following query, the names and birth dates of all the presidents  who  were
  272. born after the year 1900 are requested.
  273. /
  274. SELECT PRES_NAME, BIRTH_DATE
  275. FROM   PRESIDENT
  276. WHERE  BIRTH_DATE > 1-Jan-1900
  277. /
  278. REMARK
  279. \
  280. Note that while date output is always in dd-mon-yyyy format,  SQLBASE  will
  281. accept input in the mm-dd-yy and mm/dd/yy formats  too.   By  specifying  a
  282. column as a DATE, you can make intelligent queries that dates follow  their
  283. own special logic and arithmetic.  The following query results in an  error
  284. because February did not have 29 days in the  year  1900  (centenary  years
  285. have a leap year only once every four hundred years).
  286. /
  287. SELECT PRES_NAME, BIRTH_DATE
  288. FROM   PRESIDENT
  289. WHERE  BIRTH_DATE > 29-Feb-1900
  290. /
  291. REMARK
  292. \
  293. But this query works just fine.
  294. /
  295. SELECT PRES_NAME, BIRTH_DATE
  296. FROM   PRESIDENT
  297. WHERE  BIRTH_DATE > 29-Feb-1904
  298. /
  299. REMARK
  300. \
  301. The BETWEEN predicate is used to check if the data in given  field  belongs
  302. to range of values.  The following query retrieves the names  and  ages  of
  303. those presidents who were born between January 1,  1901  and  December 31,
  304. 1925.
  305. /
  306. SELECT PRES_NAME, BIRTH_DATE
  307. FROM PRESIDENT
  308. WHERE BIRTH_DATE BETWEEN 1/1/01 and 12/31/25
  309. /
  310. REMARK
  311. \
  312. The IN (list) predicate is used to retrieve rows based on data in  a  given
  313. set of values. The following query returns the names of all the presidents
  314. who were born in the states of New York, California or Texas.
  315. /
  316. SELECT PRES_NAME, STATE_BORN
  317. FROM PRESIDENT
  318. WHERE STATE_BORN IN  ('New York', 'California', 'Texas')
  319. /
  320. REMARK
  321. \
  322. The above queries (BETWEEN and IN) could  also have  been  constructed  by
  323. ORing together two or more search conditions.  Queries that  contain  many
  324. more complex conditions can be constructed by using a combination  of  ORs,
  325. ANDs and parentheses.  In the following example, only the currently  living
  326. presidents who either (a) belong to the democratic party or (b)  were  born
  327. in the state of Illinois, are selected.
  328. /
  329. SELECT PRES_NAME, PARTY, STATE_BORN
  330. FROM PRESIDENT
  331. WHERE DEATH_AGE IS NULL
  332. AND (PARTY = 'Democratic' OR STATE_BORN = 'Illinois')
  333. /
  334. REMARK
  335. \
  336. Here the entire search condition of the previous query is  negated  by the
  337. NOT operator.
  338. /
  339. SELECT PRES_NAME, PARTY, STATE_BORN
  340. FROM PRESIDENT
  341. WHERE NOT (DEATH_AGE IS NULL
  342. AND (PARTY = 'Democratic' OR STATE_BORN = 'Illinois'))
  343. /
  344. REMARK
  345. \
  346. Note also that even though DEATH_AGE is referenced in the WHERE clause,  it
  347. does not appear in the list  of  columns  being  selected.   SQL  does not
  348. require that the columns participating in a search condition also be in the
  349. select list or that they appear in the same sequence as they  were  entered
  350. into the table.  Infact, the select list may contain arithmetic expressions
  351. that contain any combination of column names and constants.  The  following
  352. query computes the number of terms a president has served (by dividing the
  353. number of years served by 4).
  354. /
  355. SELECT PRES_NAME, YRS_SERV, YRS_SERV/4
  356. FROM PRESIDENT
  357. /
  358. REMARK
  359. \
  360. Arithmetic expressions may also appear in the WHERE clause.
  361. /
  362. SELECT PRES_NAME, YRS_SERV, YRS_SERV/4
  363. FROM PRESIDENT
  364. WHERE YRS_SERV/4 > 2
  365. /
  366. REMARK
  367. \
  368. Arithmetic expressions can also be  used  for  DATE  data  types.   SQLBASE
  369. understands that date arithmetic is not the same as ordinary arithmetic and
  370. will perform the appropriate  calculations  automatically.   The  following
  371. query selects the name and bithdate of each president, and then  calculates
  372. the approximate date of his death (BIRTHDATE + 365*DEATH_AGE).
  373. /
  374. SELECT PRES_NAME, BIRTH_DATE, BIRTH_DATE + 365*DEATH_AGE
  375. FROM PRESIDENT
  376. /
  377. REMARK
  378. \
  379. Date expressions of the above type can be included anywhere that arithmetic
  380. expressions can exist (ie. in a SELECT list or in the WHERE Clause).   Date
  381. expressions can also include certain built-in functions  such  as  @YEARNO,
  382. @QUARTER, @MONTH and @WEEKDAY. These compute the year, quarter,  month  or
  383. weekday of a given date.  The following query computes the  year  of  each
  384. president's birth and death.
  385. /
  386. SELECT PRES_NAME, BIRTH_DATE, @YEARNO(BIRTH_DATE + 365*DEATH_AGE)
  387. FROM PRESIDENT
  388. /
  389. REMARK
  390. \
  391. A SQL query may contain an ORDER BY clause.  This enables rows to be sorted
  392. by a specified order.  The following query selects the names  of  all the
  393. presidents in the PRESIDENT table and displays them in alphabetical order.
  394. /
  395. SELECT PRES_NAME
  396. FROM PRESIDENT
  397. ORDER BY PRES_NAME
  398. /
  399. REMARK
  400. \
  401. Sorting may be in  ascending  or  descending  order.   DESCending  must  be
  402. specified if desired.  ASCending is the default.
  403. /
  404. SELECT PRES_NAME, DEATH_AGE
  405. FROM PRESIDENT
  406. ORDER BY DEATH_AGE DESC
  407. /
  408. REMARK
  409. \
  410. Note from the above example that  if  the  sort  field contains  any  null
  411. values, then those  rows  containing  nulls  are  displayed  prior  to the
  412. remaining rows.  One primary sort  and multiple  secondary  sorts  may  be
  413. specified.  The following query sorts them first by their state  of  birth,
  414. and then in descending order by their date of birth.
  415. /
  416. SELECT PRES_NAME, STATE_BORN, BIRTH_DATE
  417. FROM PRESIDENT
  418. ORDER BY STATE_BORN, BIRTH_DATE DESC
  419. /
  420. REMARK
  421. \
  422. Sorting may be done by a column that does not appear in  the  select  list.
  423. It may also reference a computed column (ie. an expression)  by  using its
  424. relative position number in the select list.  The following query  computes
  425. the year of death of each president and orders the result by that field.
  426. /
  427. SELECT PRES_NAME, @YEARNO(BIRTH_DATE + 365*DEATH_AGE)
  428. FROM PRESIDENT
  429. ORDER BY 2
  430. /
  431. PAUSE
  432. /
  433. REMARK
  434. \
  435. Now let us demonstrate the ability of SQLBASE to  perform  aggregations  on
  436. various columns of a table.  To illustrate this  effectively,  we  need  to
  437. create another table called ELECTION.  This table will contain the names of
  438. all the candidates in each presidential election  since  independence, and
  439. data regarding electoral votes cast in his favour, and a one character code
  440. indicating whether he won or lost  the election.   Note  the  use  of the
  441. SMALLINT and VARCHAR datatypes (these are used to demonstrate compatibility
  442. with DB 2 language syntax).
  443. /
  444. CREATE TABLE ELECTION (ELECTION_YEAR SMALLINT, CANDIDATE VARCHAR(20),
  445. VOTES SMALLINT, WINNER_LOSER_INDIC CHAR(1))
  446. /
  447. INSERT INTO ELECTION (ELECTION_YEAR,CANDIDATE,VOTES,WINNER_LOSER_INDIC)
  448. VALUES (:1, :2, :3, :4)
  449. \
  450. 1789,Washington G,69,W
  451. 1789,Adams J,34,L
  452. 1789,Jay J,9,L
  453. 1789,Harrison R H,6,L
  454. 1789,Rutledge J,6,L
  455. 1789,Hancock J,4,L
  456. 1789,Clinton G,3,L
  457. 1789,Huntington S,2,L
  458. 1789,Milton J,2,L
  459. 1789,Armstrong J,1,L
  460. 1789,Lincoln B,1,L
  461. 1789,Telfair E,1,L
  462. 1792,Washington G,132,W
  463. 1792,Adams J,77,L
  464. 1792,Clinton G,50,L
  465. 1792,Jefferson T,4,L
  466. 1792,Burr A,1,L
  467. 1796,Adams J,71,W
  468. 1796,Jefferson T,68,L
  469. 1796,Pinckney T,59,L
  470. 1796,Burr A,30,L
  471. 1796,Adams S,15,L
  472. 1796,Ellsworth O,11,L
  473. 1796,Clinton G,7,L
  474. 1796,Jay J,5,L
  475. 1796,Iredell J,3,L
  476. 1796,Henry J,2,L
  477. 1796,Johnson S,2,L
  478. 1796,Washington G,2,L
  479. 1796,Pinckney C C,1,L
  480. 1800,Jefferson T,73,W
  481. 1800,Burr A,73,L
  482. 1800,Adams J,65,L
  483. 1800,Pinckney C C,64,L
  484. 1800,Jay J,1,L
  485. 1804,Jefferson T,162,W
  486. 1804,Pinckney C C,14,L
  487. 1808,Madison J,122,W
  488. 1808,Pinckney C C,47,L
  489. 1808,Clinton G,6,L
  490. 1812,Madison J,128,W
  491. 1812,Clinton G,89,L
  492. 1816,Monroe J,183,W
  493. 1816,King R,34,L
  494. 1820,Monroe J,231,W
  495. 1820,Adams J Q,1,L
  496. 1824,Adams J Q,84,W
  497. 1824,Jackson A,99,L
  498. 1824,Crawford W H,41,L
  499. 1824,Clay H,37,L
  500. 1828,Jackson A,178,W
  501. 1828,Adams J,83,L
  502. 1832,Jackson A,219,W
  503. 1832,Clay H,49,L
  504. 1832,Floyd J,11,L
  505. 1832,Wirt W,7,L
  506. 1836,Van Buren M,170,W
  507. 1836,Harrison W H,73,L
  508. 1836,White H L,26,L
  509. 1836,Webster D,14,L
  510. 1836,Mangum W P,11,L
  511. 1840,Harrison W H,234,W
  512. 1840,Van Buren M,60,L
  513. 1844,Polk J K,170,W
  514. 1844,Clay H,105,L
  515. 1848,Taylor Z,163,W
  516. 1848,Cass L,127,L
  517. 1852,Pierce F,254,W
  518. 1852,Scott W,42,L
  519. 1856,Buchanan J,174,W
  520. 1856,Fremont J C,114,L
  521. 1856,Fillmore M,8,L
  522. 1860,Lincoln A,180,W
  523. 1860,Breckinridge J,72,L
  524. 1860,Bell J,39,L
  525. 1860,Douglas S,12,L
  526. 1864,Lincoln A,212,W
  527. 1864,McClellan G B,21,L
  528. 1868,Grant U S,214,W
  529. 1868,Seymour,80,L
  530. 1872,Grant U S,286,W
  531. 1872,Hendricks T A,42,L
  532. 1872,Brown B G,18,L
  533. 1872,Jenkins C J,2,L
  534. 1872,Davis D,1,L
  535. 1876,Hayes R B,185,W
  536. 1876,Tilden S J,184,L
  537. 1880,Garfield J A,214,W
  538. 1880,Hancock W S,155,L
  539. 1884,Cleveland G,219,W
  540. 1884,Blaine J G,182,L
  541. 1888,Harrison B,233,W
  542. 1888,Cleveland G,168,L
  543. 1892,Cleveland G,277,W
  544. 1892,Harrison B,145,L
  545. 1892,Weaver J B,22,L
  546. 1896,McKinley W,271,W
  547. 1896,Bryan W J,176,L
  548. 1900,McKinley W,292,W
  549. 1900,Bryan W J,155,L
  550. 1904,Roosevelt T,336,W
  551. 1904,Parker A B,140,L
  552. 1908,Taft W H,321,W
  553. 1908,Bryan W J,162,L
  554. 1912,Wilson W,435,W
  555. 1912,Roosevelt T,88,L
  556. 1912,Taft W H,8,L
  557. 1916,Wilson W,277,W
  558. 1916,Hughes C E,254,L
  559. 1920,Harding W G,404,W
  560. 1920,Cox W W,127,L
  561. 1924,Coolidge C,382,W
  562. 1924,Davis J W,136,L
  563. 1924,La Follette R M,13,L
  564. 1928,Hoover H C,444,W
  565. 1928,Smith A E,87,L
  566. 1932,Roosevelt F D,472,W
  567. 1932,Hoover H C,59,L
  568. 1936,Roosevelt F D,523,W
  569. 1936,Landon A M,8,L
  570. 1940,Roosevelt F D,449,W
  571. 1940,Wilkie W L,82,L
  572. 1944,Roosevelt F D,432,W
  573. 1944,Dewey T E,99,L
  574. 1948,Truman H S,303,W
  575. 1948,Dewey T E,189,L
  576. 1948,Thurmond J S,39,L
  577. 1952,Eisenhower D D,442,W
  578. 1952,Stevenson A,89,L
  579. 1956,Eisenhower D D,457,W
  580. 1956,Stevenson A,73,L
  581. 1956,Jones W B,1,L
  582. 1960,Kennedy J F,303,W
  583. 1960,Nixon R M,219,L
  584. 1960,Byrd N,15,L
  585. 1964,Johnson L B,486,W
  586. 1964,Goldwater B,52,L
  587. 1968,Nixon R M,301,W
  588. 1968,Humphrey H H,191,L
  589. 1968,Wallace G C,46,L
  590. 1972,Nixon R M,520,W
  591. 1972,McGovern G S,17,L
  592. 1972,Hospers J,1,L
  593. 1976,Carter J E,297,W
  594. 1976,Ford G R,240,L
  595. 1980,Reagan R,489,W
  596. 1980,Carter J E,49,L
  597. 1984,Reagan R,528,W
  598. 1984,Mondale W F,10,L
  599. /
  600. REMARK
  601. \
  602. A user may compute the MIN, MAX, AVG, SUM  and COUNT  of  the data  in  a
  603. specified column of a table.  These are known as  aggregate  functions and
  604. may be applied to an entire table or to groups of rows within a table. The
  605. following query returns the count of all the  candidates  in  the  ELECTION
  606. table.
  607. /
  608. SELECT COUNT(*)
  609. FROM ELECTION
  610. /
  611. REMARK
  612. \
  613. The * is interpreted to mean "all the rows".  It may only be  used  in the
  614. COUNT  aggregate  function.   Strictly speaking,  the query  should  have
  615. requested count(candidate).  That's because all rows were counted  even  if
  616. some columns in a row were null values.  If an election ever  had  occurred
  617. where no candidates ran, then count(candidate) would  have  ignored  those
  618. rows and returned a correct result.   Since  no  such  election  has  taken
  619. place, count(candidate) is the same as count(*).  The above  query  may  be
  620. further restricted to count only those candidates who won.
  621. /
  622. SELECT COUNT(CANDIDATE)
  623. FROM ELECTION
  624. WHERE WINNER_LOSER_INDIC = 'W'
  625. /
  626. REMARK
  627. \
  628. Aggregate functions ignore null values in a column.  You may  also  specify
  629. that duplicate values of the data in a column be ignored.  For example, the
  630. following query counts the total  number  of  presidential  elections  ever
  631. held.  It counts only the distinct values of an election_year.
  632. /
  633. SELECT COUNT (DISTINCT ELECTION_YEAR)
  634. FROM ELECTION
  635. /
  636. REMARK
  637. \
  638. The * is only valid for the COUNT function but the keyword DISTINCT may  be
  639. used with any aggregate function.  The following query computes the minimum
  640. and maximum electoral votes ever cast for a candidate.
  641. /
  642. SELECT MIN(VOTES), MAX(VOTES)
  643. FROM ELECTION
  644. /
  645. REMARK
  646. \
  647. Sometimes, you may want to compute an aggregate for a  specified  group  of
  648. rows.  For example, suppose you would like to know the count  of  all the
  649. candidates in each  presidential  election  ever  held,  and  the  minimum,
  650. maximum, average and sum of all the electoral votes cast in that  election.
  651. The following query will return the desired result.
  652. /
  653. SELECT ELECTION_YEAR, COUNT(*), MIN(VOTES), MAX(VOTES), AVG(VOTES), SUM(VOTES)
  654. FROM ELECTION
  655. GROUP BY ELECTION_YEAR
  656. /
  657. REMARK
  658. \
  659. You can even compute an aggregate of the above results.  For  example, the
  660. following query returns the minimum and maximum number of  electoral  votes
  661. ever cast in an election.
  662. /
  663. SELECT MIN(SUM(VOTES)), MAX (SUM(VOTES))
  664. FROM ELECTION
  665. GROUP BY ELECTION_YEAR
  666. /
  667. REMARK
  668. \
  669. Sometimes it is desirable to be able to group the results of a query by  an
  670. expression.  This can be done by specifying  in  the  GROUP  BY  clause,  a
  671. number denoting the relative position of the expression in the SELECT list.
  672. This syntax is similar to the  one  used  for  the  ORDER  BY  clause  when
  673. expressions are involved.  The following query computes the count  and the
  674. sum of the years served by presidents, grouped by the year of the month  of
  675. their birth.
  676. /
  677. SELECT @MONTH(BIRTH_DATE), COUNT(YRS_SERV), SUM(YRS_SERV)
  678. FROM PRESIDENT
  679. GROUP BY 1
  680. /
  681. REMARK
  682. \
  683. When a GROUP BY clause is used in conjunction with a WHERE clause,  SQLBASE
  684. first selects the rows that meet the WHERE condition, and then computes the
  685. requested aggregates for the specified group.  If however, you would  like
  686. to restrict the output of the aggregate rows after they have been computed,
  687. you may use a HAVING clause to accomplish this result. The following query
  688. computes the minimum and maximum votes for each election, but restricts the
  689. output to only those rows where the maximum was  at  least  ten  times the
  690. minimum.
  691. /
  692. SELECT ELECTION_YEAR, MIN(VOTES), MAX(VOTES)
  693. FROM ELECTION
  694. GROUP BY ELECTION_YEAR
  695. HAVING MAX(VOTES) / MIN(VOTES) >10
  696. /
  697. REMARK
  698. \
  699. The GROUP BY capability is a very powerful feature of SQLBASE  and  can  be
  700. used to perform a  a  number  of  queries  that  otherwise  may  require  a
  701. procedural language capability.  Any number of columns may participate in a
  702. GROUP BY and any number of conditions may be present in  a  HAVING  clause.
  703. The only restrictions on their use are that both the SELECT  list  and the
  704. HAVING clause must contain only aggregate functions (MIN,  MAX,  AVG,  SUM,
  705. COUNT) or column names (or expressions) that are present in  the  GROUP  BY
  706. clause.  Thus the following queries are illegal.
  707. /
  708. SELECT ELECTION_YEAR, COUNT(*)
  709. FROM ELECTION
  710. /
  711. SELECT ELECTION_YEAR, COUNT(*)
  712. FROM ELECTION
  713. GROUP BY ELECTION_YEAR
  714. HAVING WINNER_LOSER_INDIC = 'W'
  715. /
  716. REMARK
  717. \
  718. In all of the above queries, SQLBASE scans the entire table looking  for  a
  719. match based on the  search  criteria.  Sometimes,  especially  for  large
  720. tables, it is useful to create an index for one or more columns.   Creating
  721. an index has only one effect.  It improves query performance. It  has  no
  722. effect on the results of the query.  Lets run the following  query  without
  723. an index.  It will search the entire ELECTION table looking for those  rows
  724. where the election year is 1984 and the candidate is 'Reagan R'.
  725. /
  726. SELECT ELECTION_YEAR, CANDIDATE
  727. FROM ELECTION
  728. WHERE ELECTION_YEAR = 1984
  729. AND CANDIDATE = 'Reagan R'
  730. /
  731. REMARK
  732. \
  733. The following statement creates an index for the ELECTION_YEAR column.
  734. /
  735. CREATE INDEX X_ELECTION_YEAR ON ELECTION (ELECTION_YEAR)
  736. /
  737. REMARK
  738. \
  739. The previous query now runs faster than before.
  740. /
  741. SELECT ELECTION_YEAR, CANDIDATE
  742. FROM ELECTION
  743. WHERE ELECTION_YEAR = 1984
  744. AND CANDIDATE = 'Reagan R'
  745. /
  746. REMARK
  747. \
  748. Indices can be created on  every  column  in  a  table.   Indices  can  be
  749. specified to be in ASCending or DESCending  order  (ASC  is  the  default).
  750. Indices can also be specified to be UNIQUE (to ensure unique data  in  each
  751. row).  Indices may also be created for a  concatenation  of  two  or  more
  752. columns.  The maximum  size  of  an  index  must  not  however exceed 250
  753. characters.  The following SQL statement creates  a  unique  index  on the
  754. concatenation of ELECTION_YEAR and CANDIDATE.
  755. /
  756. CREATE UNIQUE INDEX X_YEAR_CANDIDATE
  757. ON ELECTION (ELECTION_YEAR, CANDIDATE)
  758. /
  759. REMARK
  760. \
  761. If the concatenation of ELECTION_YEAR and CANDIDATE were found to  be not
  762. unique in the ELECTION table, the above statment  would  have  returned  an
  763. error. Also, from now on, any insertion or updating of  the  data  in the
  764. ELECTION table will only be allowed if it does not violate  the  uniqueness
  765. condition imposed by the above CREATE INDEX command.
  766.  
  767. An index may be removed with the DROP INDEX command.
  768. /
  769. DROP INDEX X_YEAR_CANDIDATE
  770. /
  771. PAUSE
  772. /
  773. REMARK
  774. \
  775. Now let us demonstrate SQLBASE's capability to JOIN two or more  tables  of
  776. data.  Prior to doing that, the following table called PRES_MARRIAGE  needs
  777. to be created. This table will contain information about the  spouses and
  778. children of each president.
  779. /
  780. CREATE TABLE PRES_MARRIAGE
  781. (PRES_NAME VARCHAR(20), SPOUSE_NAME VARCHAR(20),
  782. PR_AGE INTEGER, SP_AGE INTEGER, NR_CHILDREN INTEGER, MAR_YEAR INTEGER)
  783. /
  784. INSERT INTO PRES_MARRIAGE (PRES_NAME, SPOUSE_NAME, PR_AGE, SP_AGE,
  785. NR_CHILDREN, MAR_YEAR) VALUES (:1, :2, :3, :4, :5, :6)
  786. \
  787. Washington G,Custis M D,26,27,0,1759
  788. Adams J,Smith A,28,19,5,1764
  789. Jefferson T,Skelton M W,28,23,6,1772
  790. Madison J,Todd D D P,43,26,0,1794
  791. Monroe J,Kortright E,27,17,3,1786
  792. Adams J Q,Johnson L C,30,22,4,1797
  793. Jackson A,Robards R D,26,26,0,1794
  794. Van Buren M,Hoes H,24,23,4,1807
  795. Harrison W H,Symmes A T,22,20,10,1795
  796. Tyler J,Christian L,23,22,8,1813
  797. Tyler J,Gardiner J,54,24,7,1844
  798. Polk J K,Childress S,28,20,0,1824
  799. Taylor Z,Smith M M,25,21,6,1810
  800. Fillmore M,Powers A,26,27,2,1826
  801. Fillmore M,McIntosh C C,58,44,0,1858
  802. Pierce F,Appleton J M,29,28,3,1834
  803. Buchanan J,,,,0,,
  804. Lincoln A,Todd M,33,23,4,1842
  805. Johnson A,McCardle E,18,16,5,1827
  806. Grant U S,Dent J B,26,22,4,1848
  807. Hayes R B,Webb L W,30,21,8,1852
  808. Garfield J A,Rudolph L,26,26,7,1858
  809. Arthur C A,Herndon E L,29,22,3,1859
  810. Cleveland G,Folson F,49,21,5,1886
  811. Harrison B,Scott C L,20,21,2,1853
  812. Harrison B,Dimmick M S L,62,37,1,1896
  813. McKinley W,Saxton I,27,23,2,1871
  814. Roosevelt T,Lee A H,22,19,1,1880
  815. Roosevelt T,Carow E K,28,25,5,1886
  816. Taft W H,Herron H,28,25,3,1886
  817. Wilson W,Axson E L,28,25,3,1885
  818. Wilson W,Galt E B,58,43,0,1915
  819. Harding W G,De Wolfe F K,25,30,0,1891
  820. Coolidge C,Goodhue G A,33,26,2,1905
  821. Hoover H C,Henry L,24,23,2,1899
  822. Roosevelt F D,Roosevelt A E,23,20,6,1905
  823. Truman H S,Wallace E V,35,34,1,1919
  824. Eisenhower D D,Doud G,25,19,2,1916
  825. Kennedy J F,Bouvier J L,36,24,3,1953
  826. Johnson L B,Taylor C A,26,21,2,1934
  827. Nixon R M,Ryan T C,27,28,2,1940
  828. Ford G R,Warren E B,35,30,4,1948
  829. Carter J E,Smith R,21,18,4,1946
  830. Reagan R,Wyman J,28,25,2,1940
  831. Reagan R,Davis N,41,28,2,1952
  832. /
  833. REMARK
  834. \
  835. Let us create indices on the PRES_NAME column of the  PRESIDENT  and  PRES_
  836. MARRIAGE tables  and  the  MAR_YEAR  column  of  the  PRES_MARRIAGE  table.
  837. Indices are not essential for  doing  a  join.  But  as  in  single  table
  838. queries, if an index is present, performance of a  join  query (especially
  839. involving a large number of rows) is improved.
  840. /
  841. CREATE INDEX X_PRES_NAME ON PRESIDENT(PRES_NAME)
  842. /
  843. CREATE INDEX Y_PRES_NAME ON PRES_MARRIAGE(PRES_NAME)
  844. /
  845. CREATE INDEX X_MAR_YEAR ON PRES_MARRIAGE(MAR_YEAR)
  846. /
  847. REMARK
  848. \
  849. Note that the data contained  in  the  PRESIDENT  table  and  PRES_MARRIAGE
  850. tables is different from each other.  The only common column in them is the
  851. PRES_NAME.  If we need to find out the date of birth of each president and
  852. the names(s) of his spouse(s), we will need to look up the PRESIDENT  table
  853. for the birth date and the PRES_MARRIAGE table for the spouse  name(s) and
  854. relate these two pieces of information via their common element -- ie. the
  855. president's name.  The following query will accomplish this objective. The
  856. operation is known as a JOIN.
  857. /
  858. SELECT PRESIDENT.PRES_NAME, BIRTH_DATE, SPOUSE_NAME
  859. FROM   PRESIDENT, PRES_MARRIAGE
  860. WHERE  PRESIDENT.PRES_NAME = PRES_MARRIAGE.PRES_NAME
  861. /
  862. REMARK
  863. \
  864. In the above example, the PRES_NAME column had to be prefixed with a  table
  865. name to ensure that the query was unambiguous (a column that is unique to a
  866. table does not need to be prefixed by its table name).  The  WHERE  clause
  867. specified that only those rows should be returned where  the  PRES_NAME  in
  868. the PRESIDENT table is the same  as  the  PRES_NAME  in  the  PRES_MARRIAGE
  869. table. If this condition (known as the JOIN condition) were not  supplied,
  870. the results would consist of all possible combinations of the rows  of the
  871. two tables (ie. a total of 39 times 44 rows). This  combination  is  also
  872. known as the cartesian product of two tables.
  873. /
  874. PAUSE
  875. /
  876. REMARK
  877. \
  878. A JOIN condition does not have to be based on an equality.  Any of the SQL
  879. comparison operators (=,<,>,<=,>=) may be used.  The joining columns do not
  880. have to have the same name either.  More than two tables may participate in
  881. a join (the maximum number is 250), and as in a  single  table query, the
  882. WHERE clause can contain any number of conditions.   The  following  query
  883. finds (from the PRESIDENT, ELECTION and PRES_MARRIAGE tables) the  name  of
  884. each president (and his spouse) who was married after he took office.
  885. /
  886. SELECT PRESIDENT.PRES_NAME, SPOUSE_NAME, ELECTION_YEAR, MAR_YEAR
  887. FROM   PRESIDENT, ELECTION, PRES_MARRIAGE
  888. WHERE  PRESIDENT.PRES_NAME = ELECTION.CANDIDATE
  889. AND    PRESIDENT.PRES_NAME = PRES_MARRIAGE.PRES_NAME
  890. AND    PRES_MARRIAGE.MAR_YEAR > ELECTION.ELECTION_YEAR
  891. AND    WINNER_LOSER_INDIC = 'W'
  892. /
  893. REMARK
  894. \
  895. Joins may also be performed in conjunction with groupings  and aggregates.
  896. The results may be displayed in any order.  The  following  query  displays
  897. the name of each president, the number of times he  was  married,  and the
  898. total number of children he had.
  899. /
  900. SELECT PRESIDENT.PRES_NAME, COUNT(SPOUSE_NAME), SUM(NR_CHILDREN)
  901. FROM   PRESIDENT, PRES_MARRIAGE
  902. WHERE  PRESIDENT.PRES_NAME = PRES_MARRIAGE.PRES_NAME
  903. GROUP BY PRESIDENT.PRES_NAME
  904. ORDER BY PRESIDENT.PRES_NAME
  905. /
  906. REMARK
  907. \
  908. A table may be joined to itself.  This is useful for queries involving the
  909. "nesting" of conditions within the same table. In the following query,  we
  910. request the names of the presidents (and their birth states) who were  born
  911. in the same state as General Grant.  To accomplish this type of  query,  we
  912. give the PRESIDENT table two temporary names (called labels)-- A & B.  Then
  913. tables A & B are joined to each other based on the criteria contained below
  914. in the query. type of join is known as a self-join.
  915. /
  916. SELECT B.PRES_NAME, B.STATE_BORN
  917. FROM PRESIDENT A, PRESIDENT B
  918. WHERE A.STATE_BORN = B.STATE_BORN
  919. AND A.PRES_NAME = 'Grant U S'
  920. /
  921. REMARK
  922. \
  923. The above result could also have been accomplished  by executing  what  is
  924. known as a Nested Query.  In this type of statement,  the  results  of one
  925. query (called a Subselect) are used to drive another  query.   The  example
  926. below illustrates a nested query to find all the presidents who  were  born
  927. in the same state as General Grant.
  928. /
  929. SELECT PRES_NAME, STATE_BORN
  930. FROM PRESIDENT
  931. WHERE STATE_BORN =
  932.    (SELECT STATE_BORN
  933.     FROM PRESIDENT
  934.     WHERE PRES_NAME = 'Grant U S')
  935. /
  936. REMARK
  937. \
  938. Nested queries can be used with the IN predicate also. That is,  they may
  939. be used to compare a value to a collection  of values returned  from the
  940. inner query.  In the following example, the names of those  presidents are
  941. requested who were born in any of the states in which either General  Grant
  942. or President Lincoln were born.
  943. /
  944. SELECT PRES_NAME, STATE_BORN
  945. FROM PRESIDENT
  946. WHERE STATE_BORN IN
  947.    (SELECT STATE_BORN
  948.     FROM PRESIDENT
  949.     WHERE PRES_NAME IN ('Grant U S', 'Lincoln A'))
  950. /
  951. REMARK
  952. \
  953. Nested Queries can  be  used  to  construct  even  more  complex  database
  954. requests.  For example, the following query requests  the  names  of  those
  955. candidates in each election year who  obtained greater  than  the  average
  956. number of electoral college votes cast per candidate.  This type  of  query
  957. is called a correlated subquery because the inner query  is  executed  once
  958. for each row of the outer query (ie. for each candidate  of  each  election
  959. year). This is a fairly complex query and may take as much as a minute  on
  960. a PC/XT.  Note the use of the correlation-name X and its similarity to the
  961. temporary table name given to tables in a self-join.
  962. /
  963. SELECT ELECTION_YEAR, CANDIDATE, VOTES
  964. FROM ELECTION X
  965. WHERE VOTES >
  966.   (SELECT AVG(VOTES)
  967.    FROM ELECTION
  968.    WHERE ELECTION_YEAR = X.ELECTION_YEAR)
  969. /
  970. REMARK
  971. \
  972. Nested queries can also be used to select rows from one  table and  insert
  973. them into another.  The  following  statement  creates a  temporary  table
  974. called WINNER that has three columns: the president's NAME, the YEAR he was
  975. elected in and the VOTES he  attained  from  the  electoral  college. The
  976. INSERT statement following the table creation causes the names of  all the
  977. candidates from the election table who have a winner_loser_indic of W to be
  978. inserted into the WINNER table.
  979. /
  980. CREATE TABLE WINNER (NAME CHAR(20), YEAR_ELECTED SMALLINT, VOTES SMALLINT)
  981. /
  982. INSERT INTO WINNER
  983. SELECT CANDIDATE, ELECTION_YEAR, VOTES
  984. FROM ELECTION
  985. WHERE WINNER_LOSER_INDIC = 'W'
  986. /
  987. REMARK
  988. \
  989. You may have noticed that the above table doesnt  necessarily  contain all
  990. U.S. presidents.  For example, Gerald Ford was not elected  to the  office
  991. but attained it as a result of the rules of  presidential  succession.  In
  992. order to find the presidents who were not  elected  to office,  we  use  a
  993. correlated subquery but this time with an EXISTS predicate.  The latter may
  994. only be used in a subquery.  It does not return  any  values. Instead  it
  995. returns to the outer query a binary YES/NO response for the inner query.
  996. /
  997. SELECT PRES_NAME
  998. FROM PRESIDENT X
  999. WHERE NOT EXISTS
  1000. (SELECT *
  1001.  FROM WINNER
  1002.  WHERE NAME = X.PRES_NAME)
  1003. /
  1004. REMARK
  1005. \
  1006. Sometimes it is useful to query two tables and merge  the  rows  that are
  1007. returned into one set of results.  This merging is called  a  UNION  and  a
  1008. special operator exists in SQL to accomplish it.  In the  following  query,
  1009. we request to see the names of all presidents who were elected to office by
  1010. popular vote and who were born in the state of Nebraska or Illinois.   This
  1011. involves a UNION of two queries, one on the WINNER table, and the other  on
  1012. the PRESIDENT table.
  1013. /
  1014. SELECT NAME FROM WINNER
  1015. UNION
  1016. SELECT PRES_NAME FROM PRESIDENT
  1017. WHERE STATE_BORN IN ('Nebraska','Illinois')
  1018. /
  1019. REMARK
  1020. \
  1021. The above query will display all of the winners and the name of Gerald Ford
  1022. who was born  in  Nebraska.   Notice  also  that  all  duplicate  rows are
  1023. automatically suppressed.  UNION also requires that both queries  have the
  1024. same number of columns in the select list and that  they  be  identical  in
  1025. data type.
  1026. /
  1027. REMARK
  1028. \
  1029. You may have noticed that the results of all  SQL  queries,  regardless  of
  1030. complexity, are always presented as a table  of  data.  Sometimes,  it  is
  1031. desirable to define the results of a query to be a VIEW.  By giving a  name
  1032. to the result table of a query, this subsequent VIEW can then be treated as
  1033. if it were a table.  You can  retrieve data  from  it without  having  to
  1034. specify all the search criteria that went into the  original  query. The
  1035. following statement creates a view called WINNER_VIEW.
  1036. /
  1037. CREATE VIEW WINNER_VIEW (NAME, YEAR_ELECTED, VOTES) AS
  1038. SELECT CANDIDATE, ELECTION_YEAR, VOTES
  1039. FROM ELECTION
  1040. WHERE WINNER_LOSER_INDIC = 'W'
  1041. /
  1042. REMARK
  1043. \
  1044. Note that the above view contains the same results  as the  WINNER  table.
  1045. However, the only difference between them  is  that  while  the  latter  is
  1046. defined as  physical  table  in  the  database,  and  contains real  data,
  1047. WINNER_VIEW does not actually exist  in  the  database except as  a  name
  1048. assigned to a query.  When the view is invoked in a subsequent query, the
  1049. underlying query that comprises WINNER_VIEW is executed, and the  resultant
  1050. table is used to retrieve the desired data.  In the following  example,  we
  1051. select the name of all the presidents who were not  elected. Instead  of
  1052. using the WINNER table, we use the the WINNER_VIEW view.
  1053. /
  1054. SELECT PRES_NAME
  1055. FROM PRESIDENT X
  1056. WHERE NOT EXISTS
  1057. (SELECT *
  1058.  FROM WINNER_VIEW
  1059.  WHERE NAME = X.PRES_NAME)
  1060. /
  1061. REMARK
  1062. \
  1063. VIEWS can be used to perform queries that simply cannot be  done  with one
  1064. SQL statement alone.  For example, suppose we wish to know the name of each
  1065. winner in a presidential election, the number of  votes  he  got,  and the
  1066. percentage of total votes obtained by him in the election.   The  following
  1067. two SQL statements will provide the results desired.
  1068. /
  1069. CREATE VIEW VOTES_TOTAL (YEAR_ELECTED, TOTAL) AS
  1070. SELECT ELECTION_YEAR, SUM(VOTES)
  1071. FROM ELECTION
  1072. GROUP BY ELECTION_YEAR
  1073. /
  1074. REMARK
  1075. \
  1076. The above view contains the total votes cast in each election. We now join
  1077. it to the ELECTION table.
  1078. /
  1079. SELECT CANDIDATE, VOTES, TOTAL
  1080. FROM ELECTION, VOTES_TOTAL
  1081. WHERE ELECTION.ELECTION_YEAR = VOTES_TOTAL.YEAR_ELECTED
  1082. /
  1083. REMARK
  1084. \
  1085. VIEWS can also be updated, deleted from or inserted into, as long  as  they
  1086. contain the name of only one table in the FROM clause, and do  not  contain
  1087. any  derived  columns  (based  on  group   by functions   or  arithmetic
  1088. expressions).  The following view simply contains all the rows and  columns
  1089. in the PRESIDENT table and is updatable.
  1090. /
  1091. CREATE VIEW PRES_VIEW AS
  1092. SELECT * FROM PRESIDENT
  1093. /
  1094. REMARK
  1095. \
  1096. PRES_VIEW could also have been specified as updatable with a CHECK  option.
  1097. This means that if an UPDATE, DELETE or INSERT operation  is  performed  on
  1098. the view, the system will check to see that  the  view definition  is not
  1099. violated as a result of this operation.  The USE of  the  CHECK  option  on
  1100. Views has significant ramifications for  implementing  what  are  known  in
  1101. database  terminology  as  Integrity  Constraints.  These   can   be   best
  1102. illustrated by means of an example.
  1103.  
  1104. Let us first create a table called STATES and insert into it, the names  of
  1105. all the 50 states in the union.  We will then drop PRES_VIEW  and  redefine
  1106. it to include an integrity constraint specifying that no presidents in that
  1107. view can  be  from  non-existent  states,  ie. the  STATE_BORN  column  in
  1108. PRES_VIEW must exist in the STATES table.  PRES_VIEW will be defined with a
  1109. CHECK option.
  1110. /
  1111. CREATE TABLE STATES (STATE CHAR(20))
  1112. /
  1113. INSERT INTO STATES (STATE)
  1114. VALUES (:1)
  1115. \
  1116. Alabama
  1117. Alaska
  1118. Arizona
  1119. Arkansas
  1120. California
  1121. Colorado
  1122. Connecticut
  1123. Delaware
  1124. D.C.
  1125. Florida
  1126. Georgia
  1127. Hawaii
  1128. Idaho
  1129. Illinois
  1130. Indiana
  1131. Iowa
  1132. Kansa
  1133. Kentucky
  1134. Louisiana
  1135. Maryland
  1136. Massachusetts
  1137. Michigan
  1138. Minnesota
  1139. Mississippi
  1140. Missouri
  1141. Montana
  1142. Nebraska
  1143. Nevada
  1144. New Hampshire
  1145. New Jersey
  1146. New Mexico
  1147. New York
  1148. North Carolina
  1149. North Dakota
  1150. Ohio
  1151. Oklahoma
  1152. Oregon
  1153. Pennsylvania
  1154. Rhode Island
  1155. South Carolina
  1156. South Dakota
  1157. Tennessee
  1158. Texas
  1159. Utah
  1160. Vermont
  1161. Virginia
  1162. Washington
  1163. West Virginia
  1164. Wisconsin
  1165. Wyoming
  1166. /
  1167. DROP VIEW PRES_VIEW
  1168. /
  1169. CREATE VIEW PRES_VIEW AS
  1170. SELECT * FROM PRESIDENT
  1171. WHERE STATE_BORN IN
  1172. (SELECT STATE FROM STATES)
  1173. WITH CHECK OPTION
  1174. /
  1175. REMARK
  1176. \
  1177. In the following  SQL  statement,  since  PRES_VIEW  is  identical  to the
  1178. PRESIDENT table in terms of its contents, inserting a record into PRES_VIEW
  1179. is the same as inserting it into PRES_VIEW.  Since PRES_VIEW  is  identical
  1180. to the PRESIDENT table in terms of its contents, inserting  a  record  into
  1181. PRES_VIEW is the same as inserting it into PRESIDENT about with  one  added
  1182. benefit.  The integrity constraint that ensures the validity of  STATE_BORN
  1183. is enforced during any data manipulation operation.  This type of integrity
  1184. checking is known as referential integrity.
  1185. /
  1186. INSERT INTO PRES_VIEW (PRES_NAME, STATE_BORN)
  1187. VALUES ('Ferdinand Marcos','Philippines')
  1188. /
  1189. REMARK
  1190. \
  1191. As you can see, VIEWS can be a powerful  means  to  perform  queries  that
  1192. otherwise might require significant amounts of programming.   The  benefit
  1193. they have over creating temporary tables is that they are always  uptodate.
  1194. They do not  require  storage  of  redundant  data  and  are  automatically
  1195. maintained current by SQLBASE. BY using the CHECK option on updatable views,
  1196. they can be used  to  enforce  a  wide variety  of  integrity constraints
  1197. including referential integrity.  Later in this tutorial, you will also see
  1198. how views can be used to implement field and row level security  within  a
  1199. table by assigning access privileges on views.
  1200. /
  1201. PAUSE
  1202. /
  1203. REMARK
  1204. \
  1205. Now let us demonstrate the use of the LONG VARCHAR datatype.  In  order  to
  1206. do so, we must first create a table called BIOGRAPHY.  The first column  in
  1207. the table is called PRES_NAME and the second is the TEXT column.  TEXT is a
  1208. VARCHAR LONG column.
  1209. /
  1210. CREATE TABLE BIOGRAPHY
  1211. (PRES_NAME VARCHAR(20),
  1212. TEXT LONG VARCHAR)
  1213. /
  1214. REMARK
  1215. \
  1216. To insert data into a VARCHAR LONG column, you must  always  use  the  bind
  1217. variable approach.  The following command inserts the name 'Reagan R'  into
  1218. the PRES_NAME column, and then a short biography  into  the  TEXT  column.
  1219. Note that the special keyword $long that must precede entry  of  a  VARCHAR
  1220. LONG field.  If you examine the DEMO command file, you will notice  that  a
  1221. // terminates entry of each VARCHAR  LONG  field.   These  conventions for
  1222. entering long data are specific to SQLTALK only.  For more  information  on
  1223. the calls required to read and write VARCHAR LONG data from  a C  program,
  1224. consult the SQLBASE C Program Interface Guide.
  1225. /
  1226. INSERT INTO BIOGRAPHY (PRES_NAME, TEXT)
  1227. VALUES (:1,:2)
  1228. \
  1229. Reagan R, $long
  1230. Ronald Wilson Reagan, 40th president, Republican, was born Feb. 6, 1911, in
  1231. Tapico, Ill., the son of John Edward  Reagan  and  Nellie  Wilson.   Reagan
  1232. graduated from Eureka (Ill) Cllege in 1932.  Following his  graduation,  he
  1233. worked for 5 years as a sports announce in Des Moines, Iowa.
  1234.  
  1235. Reagan began a successful career as a film actor in 1937,  and starred  in
  1236. many movies, and later television, until the 1960s.  He was  a captain  in
  1237. the Army Air Force during World War II.  He  served  as  President  of the
  1238. Screen Actor's guild from 1947 to 1952, and in 1959.
  1239.  
  1240. Once a liberal democrat, Reagan became active in Republican politics during
  1241. the 1964  presidential camapaign  of  Barry  Goldwater.   He  was  elected
  1242. governor of California in 1966,  and  reelected  in  1970.   Following his
  1243. retirement as  governor,  Reagan  became  the  leading spokesman  for the
  1244. conservative wing of the Republican Party, and made a strong  bid  for the
  1245. party's 1976 presidential nomination.
  1246.  
  1247. In 1980 he gained the Republican nomination and  won  a  landslide  victory
  1248. over Jimmy Carter.  As president,  he successfully  forged  a  bipartisan
  1249. coalition in Congress which led to enactment of an economic  program  which
  1250. included the largest budget and tax cuts in  U.S.  history,  and  a  Social
  1251. Security reform bill designed  to  ensure  the longterm  solvency  of the
  1252. system.
  1253.  
  1254. He was reelected  by  an  overwhelming margin in  the  1984  presidential
  1255. elections.  His opponent, Walter Mondale managed to  carry  only  his  home
  1256. state of Minnesota while Reagan won all the other 49 states.
  1257. //
  1258. /
  1259. REMARK
  1260. \
  1261. The following query displays the contents of the BIOGRAPHY table.
  1262. /
  1263. SELECT PRES_NAME, TEXT
  1264. FROM  BIOGRAPHY
  1265. /
  1266. REMARK
  1267. \
  1268. SQLTALK also allows you to enter VARCHAR LONG  data  directly  from  a DOS
  1269. file.  The command below inserts one row into  the  BIOGRAPHY  table. The
  1270. first column contains an entry for 'Carter  J' and  into  the second,  we
  1271. insert a  file called bio.txt  which  contains  a  brief  biography  of
  1272. President Jimmy Carter.
  1273. /
  1274. INSERT INTO BIOGRAPHY(PRES_NAME, TEXT)
  1275. VALUES (:1,:2)
  1276. \
  1277. Carter J E, $long bio.txt
  1278. /
  1279. REMARK
  1280. \
  1281. The following query displays only the biography for Jimmy Carter.
  1282. /
  1283. SELECT PRES_NAME, TEXT
  1284. FROM BIOGRAPHY
  1285. WHERE PRES_NAME = 'Carter J E'
  1286. /
  1287. PAUSE
  1288. /
  1289. REMARK
  1290. \
  1291. Upto now, we have demonstrated the creation of tables and indices (CREATE),
  1292. data entry (INSERT) and data query (SELECT).  Now let us look  at  examples
  1293. of data modification (UPDATE and DELETE).
  1294.  
  1295. The following command updates the BIOGRAPHY  table  and  changes  the  name
  1296. 'Carter J E' to 'Jimmy Carter'
  1297. /
  1298. UPDATE BIOGRAPHY
  1299. SET PRES_NAME = 'Jimmy Carter'
  1300. WHERE PRES_NAME = 'Carter J E'
  1301. /
  1302. REMARK
  1303. \
  1304. Note the use of the WHERE clause in an UPDATE statment.  Any  WHERE  clause
  1305. that can be constructed for a single table SELECT statement can be used  in
  1306. an UPDATE statement.  If no WHERE clause is specified, then all the rows of
  1307. the table that contain data in the specified column are updated.  An UPDATE
  1308. can only be applied to a  single  table.   The SET  clause  in  an  UPDATE
  1309. statement may contain constants, column names  or  arithmetic  expressions.
  1310. More than one column may be updated in a single SET clause.
  1311. /
  1312. SELECT PRES_NAME
  1313. FROM BIOGRAPHY
  1314. WHERE PRES_NAME LIKE '%Carter%'
  1315. /
  1316. REMARK
  1317. \
  1318. To delete an entire row from a table,  use  the  DELETE  command.   In the
  1319. following example, the entry for 'Carter Jimmy' in the BIOGRAPHY  table  is
  1320. deleted.
  1321. /
  1322. DELETE
  1323. FROM BIOGRAPHY
  1324. WHERE PRES_NAME = 'Jimmy Carter'
  1325. /
  1326. REMARK
  1327. \
  1328. If no WHERE clause is specified in a DELETE or UPDATE statment, all rows in
  1329. table are deleted or updated.  The following  statement  deletes  all  rows
  1330. from the BIOGRAPHY table.
  1331. /
  1332. DELETE
  1333. FROM BIOGRAPHY
  1334. /
  1335. SELECT *
  1336. FROM BIOGRAPHY
  1337. /
  1338. REMARK
  1339. \
  1340. To completely remove a table from the database, you must use the DROP TABLE
  1341. command.  Even though we have deleted all the rows in the BIOGRAPHY  table,
  1342. the database still contains an entry for this table.  The following command
  1343. removes the entire table from the database.
  1344. /
  1345. DROP TABLE BIOGRAPHY
  1346. /
  1347. REMARK
  1348. \
  1349. SQLBASE keeps track of information regarding all the tables in the database
  1350. and their associated columns and indices.  This information comprises  what
  1351. is known as the data dictionary.
  1352. /
  1353. PAUSE
  1354. /
  1355. REMARK
  1356. \
  1357. The SQLBASE data dictionary includes the  following  tables:  (a)  a  table
  1358. called SYSTABLES containing a list of  all  the  tables  and  their  column
  1359. count, (b) a table called SYSCOLUMNS containing a list of all  the  columns
  1360. in the database and their associated tables, column numbers and  datatypes,
  1361. (c) a table called SYSINDEXES containing a  list  of  all  indexes  in the
  1362. database and their  associated tables and  (d)  a  table  called  SYSKEYS
  1363. containing a list of all indexes and their associated  columns and  (e)  a
  1364. table called SYSVIEWS containing the text of all the views in the database.
  1365. These tables may be queried using SQL commands just like any  user  defined
  1366. table in the database. Other  dictionary  tables  exist  in  SQLBASE  that
  1367. contain information about users and their security  privileges.   They are
  1368. described later in this tutorial.
  1369. /
  1370. SELECT NAME, COLCOUNT, REMARKS
  1371. FROM SYSTABLES
  1372. /
  1373. SELECT NAME, TBNAME, COLNO, COLTYPE, LENGTH, NULLS, REMARKS
  1374. FROM SYSCOLUMNS
  1375. /
  1376. SELECT NAME, TBNAME, UNIQUERULE, COLCOUNT
  1377. FROM SYSINDEXES
  1378. /
  1379. SELECT IXNAME, COLNAME, COLNO, COLSEQ, ORDERING
  1380. FROM SYSKEYS
  1381. /
  1382. REMARK
  1383. \
  1384. The views that exist in a database are listed in the SYSTABLES
  1385. table and designated as such. They are also contained in the SYSVIEWS table.
  1386. /
  1387. SELECT * FROM SYSTABLES
  1388. WHERE TYPE = 'V'
  1389. /
  1390. SELECT * FROM SYSVIEWS
  1391. /
  1392. REMARK
  1393. \
  1394. Note that the REMARKS column of the SYSTABLES  and  SYSCOLUMNS tables was
  1395. truncated because of not enough screen width.  This column may be  updated
  1396. via the COMMENT ON command.
  1397. /
  1398. COMMENT ON TABLE PRESIDENT
  1399. IS 'Names of U.S. Presidents'
  1400. /
  1401. COMMENT ON COLUMN PRESIDENT.DEATH_AGE
  1402. IS 'Age at which president died'
  1403. /
  1404. SELECT NAME, REMARKS
  1405. FROM SYSTABLES
  1406. WHERE NAME = 'PRESIDENT'
  1407. /
  1408. SELECT TBNAME, NAME, REMARKS
  1409. FROM SYSCOLUMNS
  1410. WHERE TBNAME = 'PRESIDENT'
  1411. AND NAME = 'DEATH_AGE'
  1412. /
  1413. PAUSE
  1414. /
  1415. REMARK
  1416. \
  1417. Data in the dictionary tables (other than the REMARKS columns) is  updated
  1418. by SQLBASE whenever appropriate.  The following sequence  of  SQL  commands
  1419. demonstrates this automatic dictionary maintenance  facility  of  SQLBASE.
  1420. Let us drop the index X_PRES_NAME on PRES_NAME in the PRESIDENT table.
  1421. /
  1422. DROP INDEX X_PRES_NAME
  1423. /
  1424. REMARK
  1425. \
  1426. Now, if we query the SYSINDEXES table, we will see that  no  indices  exist
  1427. for the PRESIDENT table.
  1428. /
  1429. SELECT *
  1430. FROM SYSINDEXES
  1431. WHERE TBNAME = 'PRESIDENT'
  1432. /
  1433. PAUSE
  1434. /
  1435. REMARK
  1436. \
  1437. You can alter the structure of a table by adding or dropping columns. The
  1438. following command adds a column called BIO_DATA  to  the  PRESIDENT  table.
  1439. Perhaps this column could be used to contain the text matter we  previously
  1440. stored in the BIOGRAPHY table.
  1441. /
  1442. ALTER TABLE PRESIDENT
  1443. ADD BIO_DATA LONG VARCHAR
  1444. /
  1445. REMARK
  1446. \
  1447. Note that the data dictionary will automatically  reflect  this  additional
  1448. column.
  1449. /
  1450. SELECT TBNAME, NAME
  1451. FROM SYSCOLUMNS
  1452. WHERE TBNAME = 'PRESIDENT'
  1453. /
  1454. REMARK
  1455. \
  1456. More than one column can be added with a single ALTER TABLE statement.
  1457. /
  1458. ALTER TABLE PRESIDENT
  1459. ADD NEWCOL1 VARCHAR(30),
  1460.     NEWCOL2 INTEGER
  1461. /
  1462. REMARK
  1463. \
  1464. Columns can be added to any of the dictionary tables too.   Thus  if  you'd
  1465. like to store information in the data dictionary that is not maintained  by
  1466. SQLBASE, you may do so by adding the appropriate columns  to  the  relevant
  1467. dictionary table.  The following command adds the 'RECORD_COUNT' column  to
  1468. the SYSTABLES table.  It could be used to store the total record  count  of
  1469. the specified table.
  1470. /
  1471. ALTER TABLE SYSTABLES
  1472. ADD RECORD_COUNT INTEGER
  1473. /
  1474. REMARK
  1475. \
  1476. Columns can be dropped as easily.  Note however, that when  this  is  done,
  1477. the column numbering of the remaining columns is not changed.  This ensures
  1478. that application programs that utilize the column numbering feature of the
  1479. data dictionary are not suddenly rendered incompatible.  The maximum column
  1480. numbers that can be utilized for a table is 250.
  1481. /
  1482. ALTER TABLE PRESIDENT
  1483. DROP NEWCOL1, NEWCOL2
  1484. /
  1485. REMARK
  1486. \
  1487. You can drop user defined columns from a dictionary table but  not  any  of
  1488. the system defined columns.
  1489. /
  1490. ALTER TABLE SYSTABLES
  1491. DROP RECORD_COUNT
  1492. /
  1493. ALTER TABLE SYSTABLES
  1494. DROP NAME
  1495. /
  1496. REMARK
  1497. \
  1498. The ALTER TABLE command can also be used to rename a  table,  to  rename  a
  1499. column or to increase the length of a character column.  The following are
  1500. examples of these capabilities.
  1501. /
  1502. ALTER TABLE PRES_MARRIAGE
  1503. RENAME TABLE MARRIAGE
  1504. /
  1505. REMARK
  1506. \
  1507. The above query renamed the PRES_MARRIAGE table to the MARRIAGE table. The
  1508. following statement renames the NR_CHILDREN column to NUM_CHILDREN
  1509. /
  1510. ALTER TABLE MARRIAGE
  1511. RENAME NR_CHILDREN NUM_CHILDREN
  1512. /
  1513. REMARK
  1514. \
  1515. The following statement increases the length of the PRES_NAME column to 40.
  1516. Note that the length may be increased but not decreased.
  1517. /
  1518. ALTER TABLE MARRIAGE
  1519. MODIFY PRES_NAME CHAR(40)
  1520. /
  1521. REMARK
  1522. \
  1523. Now let's take a look at the SYSCOLUMNS table  to  verify  that  the  above
  1524. changes have actually taken place.
  1525. /
  1526. SELECT *
  1527. FROM SYSCOLUMNS
  1528. WHERE TBNAME = 'MARRIAGE'
  1529. /
  1530. REMARK
  1531. \
  1532. Let's go back to the old names.
  1533. /
  1534. ALTER TABLE MARRIAGE
  1535. RENAME TABLE PRES_MARRIAGE
  1536. /
  1537. ALTER TABLE PRES_MARRIAGE
  1538. RENAME NUM_CHILDREN NR_CHILDREN
  1539. /
  1540. PAUSE
  1541. /
  1542. REMARK
  1543. \
  1544. Now let us demonstrate a very key feature of SQLBASE---its  capability for
  1545. transaction level rollback and recovery.  A transaction is any logical unit
  1546. of work.  In SQLBASE terms, it may consist of one or more SQL commands.   A
  1547. transaction starts either when you connect to a database or when you  issue
  1548. the SQL command called COMMIT. Let us issue a COMMIT.
  1549. /
  1550. COMMIT
  1551. /
  1552. REMARK
  1553. \
  1554. At this point, all changes to the database made since we  issued  the  last
  1555. COMMIT have been committed to disk.  In the event the computer goes down or
  1556. you shut it off, SQLBASE guarantees you that the data you have entered  or
  1557. modified since the start of the  session  will not  be  affected  by  this
  1558. shutdown.  Now, let us perform the following transaction.
  1559. /
  1560. UPDATE PRES_MARRIAGE
  1561.   SET SPOUSE_NAME = 'Davis N'
  1562.   WHERE PRES_NAME = 'Carter J E'
  1563. /
  1564. UPDATE PRES_MARRIAGE
  1565.   SET SPOUSE_NAME = 'Smith R'
  1566.   WHERE PRES_NAME = 'Reagan R'
  1567. /
  1568. REMARK
  1569. \
  1570. If you query the database, you will see that it reflects the  changes  made
  1571. above.
  1572. /
  1573. SELECT PRES_NAME, SPOUSE_NAME
  1574. FROM   PRES_MARRIAGE
  1575. WHERE  PRES_NAME = 'Reagan R'
  1576. OR     PRES_NAME = 'Carter J E'
  1577. /
  1578. REMARK
  1579. \
  1580. Note that the names of the spouses of Presidents  Reagan  and  Carter  were
  1581. intersposed.  However, the database at this  point  is  still capable  of
  1582. "rolling back" to the state it was in at the time of the  previous  COMMIT.
  1583. This can be demonstrated by issuing the command ROLLBACK.
  1584. /
  1585. ROLLBACK
  1586. /
  1587. REMARK
  1588. \
  1589. Upon querying the PRES_MARRIAGE table, we will see that  the  spouse  names
  1590. are listed correctly.
  1591. /
  1592. SELECT PRES_NAME, SPOUSE_NAME
  1593. FROM PRES_MARRIAGE
  1594. WHERE PRES_NAME = 'Carter J E'
  1595. OR    PRES_NAME = 'Reagan R'
  1596. /
  1597. REMARK
  1598. \
  1599. In  the  previous  sequence  of  statements,  by  issuing  a  ROLLBACK  we
  1600. effectively undid the SQL commands that updated the SPOUSE_NAME column and
  1601. restored the database to a state as if those  statements  had never  been
  1602. executed.  This occurs automatically in the event of  unanticipated  system
  1603. failure.  A ^C exit from SQLTALK will  also  cause  a  rollback  to  occur.
  1604. However, a normal exit (ie. not a ^C) will cause a COMMIT to be  issued  at
  1605. the end of the SQLTALK session.
  1606. /
  1607. REMARK
  1608. \
  1609. The default mode of SQLTALK is that SQL statments are not  committed  until
  1610. you explicitly issue  the  COMMIT  command.   However,  you  may  set the
  1611. AUTOCOMMIT option on in SQLTALK which will  automatically  cause  each SQL
  1612. statement to be committed after it is executed.
  1613. /
  1614. PAUSE
  1615. /
  1616. REMARK
  1617. \
  1618. Another important advanced feature of SQLBASE is  that it  allows  you  to
  1619. store and retrieve precompiled queries.  In the  following  example,  STORE
  1620. and EXECUTE are commands specific to SQLTALK.  The equivalent C programming
  1621. operations are described in the SQLBASE C Program Interface Guide.
  1622. /
  1623. STORE COMMAND1
  1624. UPDATE PRESIDENT
  1625. SET DEATH_AGE = 100
  1626. WHERE PRES_NAME = :1
  1627. /
  1628. PAUSE
  1629. /
  1630. REMARK
  1631. \
  1632. The above command was stored in the database  as  COMMAND1.It  may  now  be
  1633. executed for a specific value of PRES_NAME.
  1634. /
  1635. EXECUTE COMMAND1
  1636. \
  1637. Washington G
  1638. /
  1639. PAUSE
  1640. /
  1641. REMARK
  1642. \
  1643. Querying the database shows that it has been updated accordingly.
  1644. /
  1645. SELECT PRES_NAME, DEATH_AGE
  1646. FROM PRESIDENT
  1647. WHERE PRES_NAME = 'Washington G'
  1648. /
  1649. REMARK
  1650. \
  1651. The above feature means that SQL commands that are used often in a  program
  1652. may be compiled and stored in a denser representation than  in their  text
  1653. form, and executed with a different bind  variable  each  time.   Executing
  1654. stored commands is faster than compiling  and  executing  them each  time,
  1655. because parsing is done only once, ie. at precompilation time.
  1656. /
  1657. PAUSE
  1658. /
  1659. REMARK
  1660. \
  1661. We will now illustrate the use of multiple cursors within a database. First,
  1662. We'll GRANT CONNECT privleges to cursor #2 called TWO.
  1663. /
  1664. GRANT CONNECT TO TWO IDENTIFIED BY TWO
  1665. /
  1666. GRANT DBA TO TWO
  1667. /
  1668. CONNECT TO DEMO AS TWO/TWO
  1669. /
  1670. PAUSE
  1671. /
  1672. REMARK
  1673. \
  1674. The CONNECT command above (which is a SQLTALK command) created  Cursor  #2
  1675. for the database (Cursor # 1 was created  automatically  at  the  time you
  1676. signed on  to  SQLTALK).   The primary  use  of  multiple  cursors  is  in
  1677. interactive applications where the user needs  to  browse  within  multiple
  1678. tables.  An application built using multiple cursors can have a significant
  1679. performance  advantage over  single  cursor  applications.   For  more  on
  1680. cursors, consult the SQLBASE C Program Interface Guide.
  1681. /
  1682. PAUSE
  1683. /
  1684. USE DEMO SYSADM
  1685. /
  1686. REMARK
  1687. \
  1688. The USE command indicates to SQLBASE that the next SQL statement is to use
  1689. Cursor # n.  The default is Cursor # 1.
  1690. /
  1691. PAUSE
  1692. /
  1693. REMARK
  1694. \
  1695. In the following SQL statement associated with Cursor # 1, we will also use
  1696. the ROWID feature.  ROWID is  SQLBASE  keyword that  provides a  relative
  1697. address for a row in a table.  If the ROWID of a given row is  known,  then
  1698. that row can be modified (or queried) directly by using the  ROWID  in the
  1699. search condition, thus improving performance.
  1700. /
  1701. SELECT ROWID, PRES_NAME, DEATH_AGE
  1702. FROM PRESIDENT
  1703. WHERE DEATH_AGE  > 75
  1704. /
  1705. REMARK
  1706. \
  1707. Typically, in a multi-cursor application, the  results of  one  query are
  1708. browsed in some fashion to determine  if  they need  to  be  modified,  or
  1709. perhaps used as input into another query.  Here, we examine the results  of
  1710. the  previous  query,  and  determine  that  George  Washington's  age  is
  1711. incorrect.  So instead of creating an update statement that  searches the
  1712. database for 'Washington G', we directly perform the update  based  on the
  1713. row_id for his record. We use Cursor # 2 for this update.
  1714. /
  1715. PAUSE
  1716. /
  1717. USE DEMO TWO
  1718. /
  1719. UPDATE PRESIDENT
  1720. SET DEATH_AGE = 67
  1721. WHERE ROWID = :1
  1722. \
  1723. ALAAAAAAABAAAAAAICAFAAAAAAAAAAAAGFAA
  1724. /
  1725. REMARK
  1726. \
  1727. Now, the user may loop back into using Cursor #  1,  browse  through the
  1728. results of the previous query again, then use Cursor # 2  to  perform yet
  1729. another update, go back to Cursor # 1, and so on and so forth.
  1730. /
  1731. PAUSE
  1732. /
  1733. USE DEMO SYSADM
  1734. /
  1735. REMARK
  1736. \
  1737. Querying the database will  show  that George Washinton's  age  has  been
  1738. corrected.
  1739. /
  1740. SELECT PRES_NAME, DEATH_AGE
  1741. FROM PRESIDENT
  1742. WHERE PRES_NAME = 'Washington G'
  1743. /
  1744. REMARK
  1745. \
  1746. DISCONNECT removes the specified cursor  from  the  named  database.   Both
  1747. CONNECT and DISCONNECT are SQLTALK commands.  They should not  be  confused
  1748. with the CONNECT and DISCONNECT  operations  described in  the  C  Program
  1749. Interface Guide.
  1750.  
  1751. This entire demo is being  conducted  on  the  'demo'  database.   You can
  1752. actually initialize more than one database (for example, one  could  be  on
  1753. the hard disk and another could be on the floppy) and then connect  to all
  1754. those databases simultaneously from SQLBASE.   In  a  network  environment,
  1755. this feature is extremely important because it allows multiple application
  1756. nodes to connect to multiple database nodes  without  being  aware  of the
  1757. physical  location  of any  of  the  databases.   For this  single   user
  1758. demonstration, we have stayed with one database.
  1759. /
  1760. DISCONNECT DEMO TWO
  1761. /
  1762. PAUSE
  1763. /
  1764. REMARK
  1765. \
  1766. The following sequence of  commands  illustrates  the use  of  the  FETCH
  1767. BACKWARDS feature of the SQLBASE  C  Program  In  Interface.   Normally,  a
  1768. program will execute a query, and then fetch each row returned by the query
  1769. by issuing a FETCH operation.  The FETCH  BACKWARDS  operation allows the
  1770. programmer to fetch the row previous to the current row.   The combination
  1771. of FETCH and FETCH  BACKWARDS  commands,  when used  in  conjunction  with
  1772. multiple cursors and row id, can allow record at a time  operations  to  be
  1773. performed on the database efficiently and easily.
  1774.  
  1775. First, let's issue a SQL query.
  1776. /
  1777. PAUSE
  1778. /
  1779. SELECT PRES_NAME, BIRTH_DATE
  1780. FROM PRESIDENT
  1781. WHERE BIRTH_DATE > 1-Jan-1850
  1782. /
  1783. REMARK
  1784. \
  1785. Now, let us ask for the same query to be repeated followed by the  last  15
  1786. rows to be displayed in reverse order.
  1787. /
  1788. PAUSE
  1789. /
  1790. BACKWARD 15
  1791. /
  1792. REMARK
  1793. \
  1794. Note that the above command syntax (BACKWARD n) is syntax created specially
  1795. for SQLTALK.  For details on how to  use  the  FETCH  and  FETCH  BACKWARDS
  1796. operations in a C program, refer to the SQLBASE C Program Interface Guide.
  1797. /
  1798. PAUSE
  1799. /
  1800. REMARK
  1801. \
  1802. Features such  as  transaction level  rollback  and  recovery, referential
  1803. integrity, precompiled queries, multiple cursor capability, and  row  level
  1804. access are unique to SQLBASE  among  database  management  systems  on the
  1805. market today.  These are  mainframe features,  and coupled  with SQL
  1806. compatibility, superior performance, and an extraordinarily  compact  size,
  1807. they make SQLBASE an excellent DBMS engine  for  your  micro  or  micro  to
  1808. mainframe application needs.
  1809. /
  1810. PAUSE
  1811. /
  1812. REMARK
  1813. \
  1814. The entire tutorial so far has assumed the existence of a single user. The
  1815. name of this user was SYSADM (assigned when you created the  demo  database
  1816. via the CREATEDB utility).  We shall now review the  security  features  of
  1817. SQLBASE whereby the original creator of  a  database  may  create  multiple
  1818. users with different levels of access authority  to  the  database. The
  1819. original  creator  has what  is  called  SYSADM  authority  ie.   he has
  1820. unrestricted access to all the tables in the database and he may create new
  1821. tables or drop them as he pleases.
  1822.  
  1823. He may also create new users and grant them  various  levels  of  authority
  1824. over the database.  These users, if they have the authority to create new
  1825. tables, may selectively grant access privileges to users for a given table,
  1826. or in some cases, even to individual columns within a table.  All of  these
  1827. capabilities are implemented through a single  SQL  command  called  GRANT.
  1828. The privileges and authority levels may be selectively revoked through the
  1829. REVOKE command.
  1830. /
  1831. PAUSE
  1832. /
  1833. REMARK
  1834. \
  1835. Prior to creating any new users, a SYSADM should change  his  own  password
  1836. since his own original password is known to everyone (SYSADM). A  password
  1837. may be modified by specifying the new password in a GRANT CONNECT command.
  1838. /
  1839. GRANT CONNECT
  1840. TO SYSADM
  1841. IDENTIFIED BY TIGER
  1842. /
  1843. REMARK
  1844. \
  1845. We shall change this back to SYSADM now since the SQLTALK  program  expects
  1846. the password of this 'demo' database to always be SYSADM.
  1847. /
  1848. GRANT CONNECT
  1849. TO SYSADM
  1850. IDENTIFIED BY SYSADM
  1851. /
  1852. REMARK
  1853. \
  1854. The following commands creates a new user called USER1 identified  by the
  1855. password PWD1.
  1856. /
  1857. GRANT CONNECT
  1858. TO USER1
  1859. IDENTIFIED BY PWD1
  1860. /
  1861. REMARK
  1862. \
  1863. More than one user may be created in a single GRANT command.
  1864. /
  1865. GRANT CONNECT
  1866. TO USER2, USER3, USER4
  1867. IDENTIFIED BY PWD2, PWD3, PWD4
  1868. /
  1869. REMARK
  1870. \
  1871. The CONNECT authority simply means that the specified user is a valid  user
  1872. and may connect to the database (in this case DEMO) and access any  tables
  1873. to which he has been granted specific privileges.  Other  authority  levels
  1874. are RESOURCE (a user may create new tables) and DBA (a user  may  have all
  1875. the privileges a SYSADM enjoys except that he may not create new  users  or
  1876. change the authority level of existing users).
  1877.  
  1878. At this point, USER1 has only been granted CONNECT  'authority'.   He  does
  1879. not have specific 'privileges' to any of the tables in the  database. Let
  1880. us connect to the database via the USER1 id and try to select data  from  a
  1881. table.
  1882. /
  1883. CONNECT TO DEMO AS USER1/PWD1
  1884. /
  1885. SELECT *
  1886. FROM PRESIDENT
  1887. /
  1888. REMARK
  1889. \
  1890. We got the response that the table does not exist because the user does not
  1891. have any privileges on the PRESIDENT table, and as far as he is  concerned,
  1892. there is no table by that name.  Let us connect back as SYSADM and grant to
  1893. USER1, the privilege to query the PRESIDENT table.
  1894. /
  1895. USE DEMO SYSADM
  1896. /
  1897. GRANT SELECT
  1898. ON PRESIDENT
  1899. TO USER1
  1900. /
  1901. REMARK
  1902. \
  1903. Now, let us connect back as USER1 and try  to  select  from  the  PRESIDENT
  1904. table.
  1905. /
  1906. USE DEMO USER1
  1907. /
  1908. SELECT *
  1909. FROM SYSADM.PRESIDENT
  1910. /
  1911. REMARK
  1912. \
  1913. Note the use of the prefix SYSADM for the  name  of  the  PRESIDENT  table.
  1914. This is because each table, when being accessed by anybody other  than the
  1915. creator of that table, must have the name of the creator as its  prefix  in
  1916. order to properly identify it. You can always create a synonym for a table
  1917. that enables you to avoid having to type in a prefix for  tables  that are
  1918. not your own.  The following command creates a synonym called MYPRES.
  1919. /
  1920. CREATE SYNONYM MYPRES
  1921. FOR SYSADM.PRESIDENT
  1922. /
  1923. REMARK
  1924. \
  1925. You can now refer to SYSADM.PRESIDENT  as  MYPRES.   Note  however  that  a
  1926. synonym is only valid for the person who created it.  No other user can use
  1927. it (though he could create an identically named synonym).  To find out all
  1928. the synonyms that exist in the database,  you  can  query  the SYSSYNONYMS
  1929. table.
  1930. /
  1931. SELECT NAME, CREATOR, TBNAME, TBCREATOR
  1932. FROM SYSADM.SYSSYNONYMS
  1933. /
  1934. REMARK
  1935. \
  1936. To drop a synonym, issue the DROP SYNONYM command.
  1937. /
  1938. DROP SYNONYM MYPRES
  1939. /
  1940. REMARK
  1941. \
  1942. Now let us connect back as SYSADM to grant further  privileges on  tables.
  1943. More than one privilege or user may be listed in  a  GRANT  command  for  a
  1944. table.
  1945. /
  1946. USE DEMO SYSADM
  1947. /
  1948. GRANT SELECT, INSERT, DELETE
  1949. ON PRESIDENT
  1950. TO USER1, USER2
  1951. /
  1952. REMARK
  1953. \
  1954. Note that  since  USER1  already  had  a  SELECT  privilege  on  PRESIDENT,
  1955. repeating the privilege in another GRANT command had no additional  effect.
  1956. Other privileges that may be granted to a user are UPDATE (an entire  table
  1957. or specified columns within a table), INDEX (create or drop  indices) and
  1958. ALTER (a table by adding, dropping, modifying or renaming its columns).
  1959.  
  1960. The following command grants the UPDATE privilege to USER1 and USER2 on the
  1961. DEATH_AGE and YRS_SERV columns of the PRESIDENT table.
  1962. /
  1963. GRANT UPDATE (DEATH_AGE, YRS_SERV)
  1964. ON PRESIDENT
  1965. TO USER1, USER2
  1966. /
  1967. REMARK
  1968. \
  1969. Rather than listing out all the privileges available for a given table, you
  1970. may use the keyword ALL to  signify  all  privileges. Also,  rather  than
  1971. listing individual users, the keyword PUBLIC may be  used  to  signify all
  1972. users.
  1973. /
  1974. GRANT ALL
  1975. ON ELECTION
  1976. TO PUBLIC
  1977. /
  1978. REMARK
  1979. \
  1980. Note that user privileges may include all SQL statements except CREATE and
  1981. DROP TABLE.  A user granted only the CONNECT authority to  a  database may
  1982. not create or drop tables.  A user must be granted  RESOURCE  authority  to
  1983. CREATE his own tables. Such a user may also DROP tables as  long  as  they
  1984. were created by him.  He automatically has all access  privileges  (SELECT,
  1985. INSERT, DELETE, UPDATE, INDEX and ALTER) on any tables that he creates.
  1986.  
  1987. The following examples illustrate how to grant RESOURCE  authority.   Note
  1988. that since USER3 has already been assigned a password (PWD3)  when  he was
  1989. granted CONNECT authority,  no password  needs  to  be  specified  in the
  1990. following command.
  1991. /
  1992. GRANT RESOURCE
  1993. TO USER3
  1994. /
  1995. REMARK
  1996. \
  1997. Sometimes, the original creator  of  a database  may  wish  to  grant the
  1998. authority to a user to be able to access, modify or drop not only  his own
  1999. tables, but also to be able to do so with all other tables in the database.
  2000. This is known as DBA authority and is very  similar  to  SYSADM  authority.
  2001. The only difference is that DBA authority does no allow a  user  to  create
  2002. new users or to change their existing authority levels.   The  only  person
  2003. who can do that is the original creator of the database  (ie.  with  SYSADM
  2004. authority).
  2005.  
  2006. The following command grants DBA authority to USER4 identified by PWD4.
  2007. /
  2008. GRANT DBA
  2009. TO USER4
  2010. /
  2011. REMARK
  2012. \
  2013. To verify that all of  the  above  authorities and  privileges  have  been
  2014. granted as specified, let us now look at the system  dictionary  tables  to
  2015. see if they accurately reflect the results of the above commands.   We are
  2016. already aware of the SYSTABLES, SYSCOLUMNS and SYSINDEXES tables.  Each  of
  2017. them also has a column called CREATOR that displays the name  of  the  user
  2018. who created a particular table.
  2019. /
  2020. SELECT NAME, CREATOR
  2021. FROM SYSTABLES
  2022. /
  2023. SELECT NAME, TBCREATOR, TBNAME
  2024. FROM SYSCOLUMNS
  2025. /
  2026. SELECT NAME, CREATOR, TBNAME
  2027. FROM SYSINDEXES
  2028. /
  2029. REMARK
  2030. \
  2031. In addition to the above three tables, the system dictionary contains three
  2032. other tables that contain  information about  users  and  their  authority
  2033. levels and  privileges.   These  are  as   SYSUSERAUTH,   SYSTABAUTH and
  2034. SYSCOLAUTH.
  2035.  
  2036. Let us take a look  at the  information  in  the  SYSUSERAUTH table.  It
  2037. contains the name of each user to whom a CONNECT (or higher) authority has
  2038. been granted, his password, and an indication whether he  has  RESOURCE  or
  2039. DBA authority.
  2040. /
  2041. SELECT *
  2042. FROM SYSUSERAUTH
  2043. /
  2044. REMARK
  2045. \
  2046. Now let us take a look at the SYSTABAUTH  and  SYSCOLAUTH  tables  for the
  2047. entries relating to the PRESIDENT and ELECTION tables.
  2048. /
  2049. SELECT *
  2050. FROM SYSTABAUTH
  2051. WHERE TTNAME IN ('PRESIDENT', 'ELECTION')
  2052. /
  2053. SELECT *
  2054. FROM SYSCOLAUTH
  2055. WHERE TNAME IN ('PRESIDENT', 'ELECTION')
  2056. /
  2057. REMARK
  2058. \
  2059. Note that in the SYSTABAUTH table, the UPDATECOLS column contains an * for
  2060. the entry relating to the PRESIDENT table.  This is because  only  some  of
  2061. the columns are specified as updateable by USER1 and USER2.   The  specific
  2062. columns that may be updated are contained in the SYSCOLAUTH table.
  2063. /
  2064. PAUSE
  2065. /
  2066. REMARK
  2067. \
  2068. So far, we have seen examples only of how to grant privileges and authority
  2069. levels to users.  For each GRANT command option, there is  a  corresponding
  2070. REVOKE option. Thus a SYSADM may revoke any of the authority granted to  a
  2071. user (including CONNECT authority), a DBA may revoke any privileges of any
  2072. user of the database (but not change authority levels), and the creator  of
  2073. a table (RESOURCE  authority)  may  revoke  any  privileges  he  previously
  2074. granted to a user.
  2075.  
  2076. The following are examples of revocation of privileges by a SYSADM.
  2077. /
  2078. REVOKE UPDATE
  2079. ON PRESIDENT
  2080. FROM USER2
  2081. /
  2082. REVOKE ALL
  2083. ON ELECTION
  2084. FROM PUBLIC
  2085. /
  2086. REVOKE ALL
  2087. ON PRESIDENT
  2088. FROM USER1
  2089. /
  2090. REVOKE RESOURCE
  2091. FROM USER3
  2092. /
  2093. REVOKE DBA
  2094. FROM USER4
  2095. /
  2096. COMMIT
  2097. /
  2098. REMARK
  2099. \
  2100. As you can see, SQLBASE has a security scheme that is comprehensive and
  2101. easy to administer. It is compatible with the scheme employed by SQL/DS
  2102. and DB 2 though there are some minor differences between SQLBASE and the
  2103. above mentioned mainframe products.
  2104. /
  2105. PAUSE
  2106. /
  2107. REMARK
  2108. \
  2109. Now let us see how you can backup and restore the  tables  in  a  database.
  2110. SQLTALK gives a user the capability to LOAD or UNLOAD tables (or an  entire
  2111. database) to and from a DOS file.  LOAD and UNLOAD also enable a  database
  2112. to be "reorganized"  in  the  event  of  excessive  fragmentation  (SQLBASE
  2113. employs extensive reclamation schemes to ensure optimum use of disk  space
  2114. but there  are always applications  where  periodic  reorganization  will
  2115. improve performance).
  2116. /
  2117. PAUSE
  2118. /
  2119. REMARK
  2120. \
  2121. The following is an example of the use of the  UNLOAD  command.   We  first
  2122. UNLOAD the ELECTION table to a (SQL format) file called file.1
  2123. /
  2124. UNLOAD SQL FILE.1 ELECTION
  2125. /
  2126. REMARK
  2127. \
  2128. The above statement created the file.1 file containing the  definition  of
  2129. the ELECTION table, its data, and all its indices.  If you  examine  file.1
  2130. you will find that it contains commands that can be executed by SQLTALK  to
  2131. recreate the table, its data and its indices.  To verify  this fact, let's
  2132. issue the following commands.
  2133. /
  2134. DROP TABLE ELECTION
  2135. /
  2136. LOAD SQL FILE.1
  2137. /
  2138. SELECT *
  2139. FROM ELECTION
  2140. /
  2141. SELECT *
  2142. FROM SYSINDEXES
  2143. WHERE TBNAME = 'ELECTION'
  2144. /
  2145. REMARK
  2146. \
  2147. Note that the ELECTION table is restored to the database by LOAD exactly as
  2148. it was prior to the UNLOAD operation.  You could also  have  UNLOADed  only
  2149. the data in the ELECTION table.  The following command will do that.
  2150. /
  2151. UNLOAD DATA SQL FILE.2 ELECTION
  2152. /
  2153. REMARK
  2154. \
  2155. The LOAD and UNLOAD commands will also work for more than one  table  at  a
  2156. time or for the entire database.  The following commands  UNLOAD  and  LOAD
  2157. the PRES_MARRIAGE and ELECTION tables. The use of the keyword ALL (in lieu
  2158. of the table name) would have unloaded the entire database.
  2159. /
  2160. UNLOAD SQL FILE.3 ELECTION PRES_MARRIAGE
  2161. /
  2162. REMARK
  2163. \
  2164. LOAD and UNLOAD can also be used to transfer data to and from DIF and ASCII
  2165. format files.  The following commands unload and load DIF  and ASCII  data
  2166. files for the ELECTION table.  The output files may subsequently be used to
  2167. transfer data to popular microcomputer software  products,  most  of  which
  2168. have utilities to accept either of those two file formats.
  2169. /
  2170. UNLOAD DATA DIF FILE.5 ELECTION
  2171. /
  2172. UNLOAD ASCII FILE.6 ELECTION
  2173. /
  2174. DELETE FROM ELECTION
  2175. /
  2176. LOAD DIF FILE.5 ELECTION
  2177. /
  2178. DELETE FROM ELECTION
  2179. /
  2180. LOAD ASCII FILE.6 ELECTION
  2181. /
  2182. REMARK
  2183. \
  2184. This concludes the SQLBASE tutorial.  We hope we have been able to  provide
  2185. you with an appreciation of the power and functionality of the SQL language
  2186. and its scope of implementation in SQLBASE.
  2187.  
  2188. For more information on SQLBASE, please contact:
  2189.  
  2190.         GUPTA TECHNOLOGIES, INC.
  2191.         1040 MARSH RD, SUITE 200
  2192.         MENLO PARK, CA 94025
  2193.         Tel: (415) 321-9500
  2194. /
  2195.  
  2196.  
  2197.  
  2198.