Table creation: Difference between revisions

J
m (Never use {{header|SQLite}}, because this wrongly sets semantic property ((implemented in language::SQLite)), when SQLite is not a language.)
(J)
Line 4:
See also:
* [[Table Creation - Address]]
 
=={{header|J}}==
 
If we define a <code>table</code> as a named collection of columns, and we define a <code>type</code> as a mechanism for the representation of some kind of data, then:
 
<lang j>stocks=: |: ,: ;:'date trans symbol qty price'
insertStock=: 3 :'0#stocks=: stocks,.y'
insertStock@".;._2]0 :0
'2006-01-05'; 'BUY'; 'RHAT'; 100; 35.14
'2006-03-28'; 'BUY'; 'IBM'; 1000; 45.00
'2006-04-05'; 'BUY'; 'MSOFT'; 1000; 72.00
'2006-04-06'; 'SELL'; 'IBM'; 500; 53.00
)</lang>
 
declares a table and some data within that table.
 
And, here's an example of sorting:
 
<lang j>cols=: cols=: [:; {."1@[ <@i.`(<@i.@#@[)@.(=&(<,'*')@]"1 0) cutopen@]
sortBy=: [ /: a: |:@,. [ }.@{~ cols
from=: cols~ {"0 _ ]
select=: |:
 
select '*' from stocks sortBy 'price'
┌─────┬──────┬─────┬────┬──────────┐
│trans│symbol│price│qty │date │
├─────┼──────┼─────┼────┼──────────┤
│BUY │RHAT │35.14│100 │2006-01-05│
├─────┼──────┼─────┼────┼──────────┤
│BUY │IBM │45 │1000│2006-03-28│
├─────┼──────┼─────┼────┼──────────┤
│BUY │MSOFT │72 │1000│2006-04-05│
├─────┼──────┼─────┼────┼──────────┤
│SELL │IBM │53 │500 │2006-04-06│
└─────┴──────┴─────┴────┴──────────┘</lang>
 
Note that this particular example is both overly general in some senses (for example, named column handling has features not demonstrated here) and overly specific in others (for example, I did not implement sort in descending order).
 
Also, a properly tuned system would likely use different code (for example, you could get better performance if you put an entire column into a box instead of introducing a new box for each element in a column).
 
=={{header|Oz}}==
6,962

edits