Jump to content

Table creation: Difference between revisions

m
syntax highlighting fixup automation
(added Perl programming solution)
m (syntax highlighting fixup automation)
Line 11:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">; Helper functions
 
createTable: function [][
Line 49:
print ["getting user with name: JohnDoe =>" findUser "JohnDoe"]
 
close db</langsyntaxhighlight>
 
{{out}}
Line 62:
 
AWK is just a glue language. Simply pipe the creation command into SQLite and capture the output.
<langsyntaxhighlight lang="awk">#!/bin/sh -f
awk '
BEGIN {
Line 69:
exit;
}
'</langsyntaxhighlight>
 
=={{header|C}}==
Line 75:
 
{{libheader|SQLite}}
<syntaxhighlight lang="c">
<lang C>
#include <sqlite3.h>
#include <stdlib.h>
Line 103:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">import db.*
import util.*
 
Line 132:
statement.execute( "SELECT * FROM Persons ORDER BY LastName" )
print( TextTable.apply(statement.getResultSet()) )
conn.close()</langsyntaxhighlight>
{{out}}
Line 152:
<br>
This uses a key/value store rather than a relational database to create the table.
<langsyntaxhighlight lang="go">package main
 
import (
Line 241:
return nil
})
}</langsyntaxhighlight>
 
{{out}}
Line 257:
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:
 
<langsyntaxhighlight lang="j">stocks=: |: ,: ;:'date trans symbol qty price'
insertStock=: 3 :'0#stocks=: stocks,.y'
insertStock@".;._2]0 :0
Line 264:
'2006-04-05'; 'BUY'; 'MSOFT'; 1000; 72.00
'2006-04-06'; 'SELL'; 'IBM'; 500; 53.00
)</langsyntaxhighlight>
 
declares a table and some data within that table.
Line 270:
And, here's an example of sorting:
 
<langsyntaxhighlight lang="j">cols=: [:; {."1@[ <@i.`(<@i.@#@[)@.(=&(<,'*')@]"1 0) cutopen@]
sortBy=: [ /:"1 2 (<__)|:@,. [ }.@{~ cols
from=: cols~ {"0 _ ]
Line 286:
├──────────┼─────┼──────┼────┼─────┤
│2006-04-05│BUY │MSOFT │1000│72 │
└──────────┴─────┴──────┴────┴─────┘</langsyntaxhighlight>
 
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).
Line 295:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using SQLite
 
conn = SQLite.DB() # in-memory
Line 317:
 
df = SQLite.query(conn, "select * from stocks order by price")
println(df)</langsyntaxhighlight>
 
{{out}}
Line 329:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">Columns = {};
Columns.ID = {};
Columns.FName = {};
Line 384:
--[[ ]]--
 
listTables();</langsyntaxhighlight>
 
{{out}}
Line 403:
We can use text from UTF16LE set. Here we have Greek letters in Memo.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
MODULE SIMPLEBASE {
BASE "ALFA" ' ERASED IF FOUND THE NAME OF "ALFA.MDB"
Line 419:
}
SIMPLEBASE
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Needs["DatabaseLink`"];conn = OpenSQLConnection[JDBC["mysql",
"databases:1234/conn_test"], "Username" -> "test"]
SQLCreateTable[conn, SQLTable["TEST"],If[Length[#] == 0,SQLColumn[StringJoin[#,"COL"],"DataTypeName" -> #],SQLColumn[StringJoin[#[[1]], "COL"], "DataTypeName" -> #[[1]],"DataLength" -> #[[2]]]] & /@ {"TINYINT", "SMALLINT", "INTEGER","BIGINT", "NUMERIC", "DECIMAL", "FLOAT", "REAL", "DOUBLE", "BIT","LONGVARBINARY", "VARBINARY", "BINARY","LONGVARCHAR",{"VARCHAR", 5},{"CHAR", 3},"DATE","TIME","TIMESTAMP","OBJECT"}]</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 430:
Nim standard library provides two modules to work with SQL databases. We use here the high level one for SQLite.
 
<langsyntaxhighlight Nimlang="nim">import db_sqlite
 
let dbc = open(":memory:", "", "", "")
Line 445:
# Data retrieval.
for row in dbc.fastRows(sql"select * from stocks order by price"):
echo row</langsyntaxhighlight>
 
{{out}}
Line 455:
=={{header|Oracle}}==
Great SCOTT! from utlsampl.sql
<langsyntaxhighlight lang="sql">
CREATE TABLE EMP
(EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
Line 465:
COMM NUMBER(7,2),
DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT);
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
Line 473:
{{libheader|Ozsqlite}}
 
<langsyntaxhighlight lang="oz">declare
[Sqlite] = {Module.link ['x-ozlib:/sqlite/Sqlite.ozf']}
 
Line 510:
{Sqlite.close DB}
end
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
The most natural way to store tabular data in GP is in a matrix:
<langsyntaxhighlight lang="parigp">m=matrix(10,3);
m[1,] = ["Barack", "Obama", 20500];
\\ ...</langsyntaxhighlight>
 
=={{header|Perl}}==
{{trans|Julia}}
<langsyntaxhighlight lang="perl"># 20211218 Perl programming solution
 
use strict;
Line 552:
print '=' x 75 , "\n";
 
while ( my @row = $sth->fetchrow_array ) { printf $format, @row }</langsyntaxhighlight>
{{out}}
<pre>
Line 565:
=={{header|Phix}}==
{{libheader|SQLite}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">pSQLite</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">sqlcode</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 582:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sqlite3_exec error: %d [%s]\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqlite_last_exec_err</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(class +Account +Entity)
Line 619:
(if (: active) "Yes" "No")
(: username)
(money (: balance)) ) ) )</langsyntaxhighlight>
Output:
<pre>account_id created active username balance
Line 626:
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">declare 1 table (100),
2 name character (20) varying,
2 address,
Line 635:
2 transaction_date date,
2 sex character (1),
2 suppress_junk_mail bit (1);</langsyntaxhighlight>
 
=={{header|PostgreSQL}}==
Line 641:
Postgres developers, please feel free to add additional data-types you commonly use to this example.
 
<langsyntaxhighlight lang="sql">-- This is a comment
 
CREATE SEQUENCE account_seq start 100;
Line 668:
-- char(#): space padded text field with length of #
-- varchar(#): variable length text field up to #
-- text: not limited</langsyntaxhighlight>
 
=={{header|Python}}==
{{libheader|SQLite}}
The sqlite3 database is a part of the Python standard library. It does not associate type with table columns, any cell can be of any type.
<langsyntaxhighlight lang="python">>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
Line 707:
(u'2006-04-06', u'SELL', u'IBM', 500.0, 53.0)
(u'2006-04-05', u'BUY', u'MSOFT', 1000.0, 72.0)
>>> </langsyntaxhighlight>
 
=={{header|Racket}}==
This is the relevant part of [[Table creation/Postal addresses#Racket]] that creates the DB table:
<langsyntaxhighlight lang="racket">
#lang racket
(require db)
(define postal (sqlite3-connect #:database "/tmp/postal.db" #:mode 'create))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 753:
However, tables (or structures) can be constructed by using stemmed arrays; &nbsp; the index would (should) be
<br>a unique identifier, &nbsp; something akin to a SSN &nbsp;(Social Security Number)&nbsp; or something similar.
<langsyntaxhighlight lang="rexx"> id = 000112222 /*could be a SSN or some other unique ID (or number).*/
 
table.id.!firstname = 'Robert'
Line 764:
table.id.!town = 'Gotham City'
table.id.!state = 'NY'
table.id.!zip = '12345-6789'</langsyntaxhighlight><br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Table creation
 
Line 806:
next
sqlite_close(oSQLite)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 839:
This code is enough to create a PStore (or open an existing PStore).
 
<langsyntaxhighlight lang="ruby">require 'pstore'
db = PStore.new "filename.pstore"</langsyntaxhighlight>
 
The example at [[Table creation/Postal addresses#Ruby]] puts Ruby objects into the PStore.
Line 847:
Run Basic supports all features of SQLite.
This is a sample of a item master
<langsyntaxhighlight lang="runbasic">
#sql execute("
CREATE TABLE item (
Line 886:
CREATE UNIQUE INDEX item_descr ON item( descr, itemNum);
CREATE UNIQUE INDEX item_itemNum ON item(itemNum);"
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
===using SLICK FRM===
<langsyntaxhighlight Scalalang="scala">// Use H2Profile to connect to an H2 database
import slick.jdbc.H2Profile.api._
 
Line 925:
// Create the tables, including primary and foreign keys
(suppliers.schema ++ coffees.schema).create
)}</langsyntaxhighlight>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<langsyntaxhighlight lang="sql pl">
CREATE TABLE dept (
deptno NUMERIC(2)
Line 963:
INSERT INTO emp VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20);
INSERT INTO emp VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,063:
=={{header|Tcl}}==
Tables, as used in relational databases, seem far away conceptually from Tcl. However, the following code demonstrates how a table (implemented as a list of lists, the first being the header line) can be type-checked and rendered:
<langsyntaxhighlight Tcllang="tcl">proc table_update {_tbl row args} {
upvar $_tbl tbl
set heads [lindex $tbl 0]
Line 1,137:
balance 0.0 \
created 2009-05-14
puts [table_format $mytbl]</langsyntaxhighlight>
Output:
<pre>
Line 1,155:
 
In practice, a binary search would be needed to find records quickly by key given that the records are being maintained in sorted order. However, for now we use a sequential search instead.
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Tuple
import "/fmt" for Fmt
import "/sort" for Cmp, Sort
Line 1,262:
table.removeRecord(3)
System.print("\nThe record with an id of 3 will be deleted, leaving:\n")
table.showRecords()</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.