You are on page 1of 11
sos2016 Cookbook/UsingK Ke Wiki Cookbook/UsingKdb From Kx Wiki Contents = 1 Using kdb+ 1.1 How do kdb+ tables differ from SQL relations ? 1.2 How do I create a table? 1.3 Can I define the type of the columns when I create a table? 1.4 Can | give the column values when creating a table? 1.5 How do I define a table with a primary key field? 1.6 Can the primary key consist of more than one field? 1.7 Can I specify foreign keys for a table? 1.8 How do insert a record into a table? 1.9 Can I insert multiple records into a table? 1.10 What is an upsert? 1.11 Can a column contain non-scalar values 1.12 How do I find out how many rows are in a table? 1.13 How can access the it" row in a table? 1.14 When accessing the i row in a table, what happens if the index is invalid? 1.15 How can J access the first/last n rows in a table? 1.16 How do I get the rows in a table as a set (no duplicates)? 1.17 What is the syntax of select in kdb+? 1.18 How do I aggregate column values? 1.19 How can I add a column with the row number to a table? 1.20 How do I delete rows from a table’? 1.21 How do I update values in a table? 1.22 How do 1 replace null values by something else’? 1,23 What are parameterized queries 1.24 Does kdb+ use stored procedures? 1.25 Can I write a query using SQL syntax against q tables? 1.26 How do I write a table to disk? 1.27 How do I read a table from disk? 1.28 How do I export a table to a CSV file? 1.29 How do I import a CSV file into a table? 1.30 What if the CSV file contains data but no column names? 1.31 How do I export a table to a text file? 1.32 How do I access a table from an MDB file via ODBC? 1,33 Can I execute kdb as a shebang script? Using kdb+ @ In this document, the terms ‘tuple’, ‘row’, and ‘record! are used interchangeably, And similarly for ‘column’ and ‘field’ ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos wm sos2016 Cookbook/UsingK Ke Wiki How do kdb+ tables differ from SQL relations ? Relations in SQL are sets. That is, there are no duplicate rows and rows are not ordered. It is possible to define a cursor on a result set and then manipulate the cursor rows in order, but that can't be done in ANSI SQL. kdb+ tables are ordered and may contain duplicates. The order allows a class of very useful aggregates that are unavailable to the relational database programmer without the cumbersome and poorly performing temporal extensions, For instance, there is the concept of the first row and last row of kdb+ table. The functions first and last give open and close prices in financial calculations. For instance, in q you can where trade is a kdb+ table. How do I create a table? Following the SQL convention, tables are created by naming the items and initializing the items as empty lists. For example, the following expression creates a table with four columns (stock, price, amount and time), and assigns it to trade. ‘ade:({] stock:(); price:(); anount:(); téme:()) Each of the table columns is defined to be an empty list, denoted as the parentheses pair () in kdb+. @ In this table, the datatype of the columns is not defined. The first record that is inserted into the table determines the datatype of each column, Subsequents inserts may only insert values of this type, or else a type error is raised, @ ‘This creates a table in memory. You can read here how to store tables on disk. @ Tables can also be created functionally with q primitive functions. Details are available in the Kab Database and Language Primer (http://www.kx.com/q/d/primer htm) . Can I define the type of the columns when I create a table? Yes. A type descriptor can be specified for each column: } price: Float$(); amount int$(); tine: tines()) Types are discussed in more detail in the [/trac/browser/kx/kdb~/d/a/q].htm?format=raw Q language reference manual], Can I give the column values when creating a table? ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos ant 1282016 Cookbooks Ki Wik Yes, you can give values to initialize the items. By default, item names are taken from corresponding noun names, a)stock! ibe bac usb @)price:121.3 5.76 8.19 G)arount:1080 5e0 808 : @)tine:29:03:06.000 69:03:23.080 @9:04:€1. 600 a) trade: ([]steck;price;ant:anount;tine) a)trade Stock price ant tine ibm 321.2 1008 @9:03:06.000 us) 8.19 890 99:04:03,000 How do I define a table with a primary key field? Just write the field definition inside the square brackets. For instance, this is a simple table of financial markets and their addresses (such as the NYSE, the LSE, etc). Can the primary key consist of more than one field? Yes. For instance, if market names are not unique, the country name can be part of the primary key. F qymarket: ({nane:synbol$(); country: symbolS()] address: ()) E @ ‘An alternative to using multiple columns as a primary key is to add to the table a column of unique values (¢.g. integers). Can I specify foreign keys for a table? ‘Yes. Foreign keys in SQL provide referential integrity. Namely, an attempt to insert a foreign key value that is not in the primary key will fail. This is also true in q. Imagine that we want to record in our trades table the market (NYSE, LSE, ete) where each trade has been done. The primary key of the markets table is a foreign key in the trades table. @)insert [trade] (ibn; 122.5; See; €9:24:59:00a) ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos ant sos2016 Cookbook/UsingK Ke Wiki and 0; 09:04:59:000)] Can I insert multiple records into a table? Yes. This is called a bulk insert. The second argument to insert in the previous question is a q list. It can also be a table having the same column names as the first argument, For instance: )"trage insert trade What is an upsert? If the table is not keyed, the above is equivalent to an insert. table and an insert otherwise. An alternative syntax for upsert is to use the operator , 2.5; 50; 09:04:59:008) @ In the second alternative the table name (trade) is not preceded by a backtick. upsert can also take a table in argument. : trade Can a column contain non-scalar values? Yes. able:([stock:()] price:()) )insert[ table; (intel; enlist (223.2; 220.4; 131.8))] i a | 223.2 120.4 131 CQ otice the use of entast How do I find out how many rows are in a table? Use the function count. ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos ant sos2016 Cookbook/UsingK Ke Wiki How can access the i" row in a table? This is complex in SQL, but easy in g. The keyword i represents the row index, which can be used in queries )select fron | Stock price anount tine : ibm 122.5 50 09:04:59,000 An alternative is to use indexing de[37] tock | ~ ibe price | 122.5 wrount | 50 ‘ime | 9:04:59.000 @ ‘The result of the first expression (using select) is a table with a single row, while the result of indexing on a table is a dictionary. @ Indexing cannot be used on a keyed table, as the following example demonstrates. tabs ((stocki()) prices) insert{ sab; (ibm; 309.5)] x00 It's also easy to access, say, the second to last row. When accessing the i row in a table, what happens if the index is invalid? If we use select, the result in a table with no rows. Fq)select from trade where tock price anount tine 300000 E ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos sit sos2016 Cookbook/UsingK Ke Wiki If we use indexing, the result is a dictionary containing null values. a)trade 300000 stock | market] “markets price | en ‘rount| @ eine | ote How can I access the first/last n rows in a table? One way is to use the take operator (#). For instance, stock market price anount tine 1 1se 130.3029 45 09:34:29,000 © gives the last 3 rows. Note that the take operator treats a list as circular if the number of elements to take is longer than the list: Paya ss An alternative is to use sublist. This takes only as many rows as are available. For instance: @)count trade a)count 3 sublist trade @)count 38 sublist trade we How do I get the rows in a table as a set (no duplicates)? Use the funetion distinct. ‘And the following gives the number of distinet rows F acount distinct trade What is the syntax of select in kdb+? ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos ant sos2016 Cookbook/UsingK Ke Wiki select expressions have the following general form. Select [colunns] [by columns] fron table [ahere cond The result of a select expression is a table. For instance: a)select stock, anount from trade stock amount ibm 580 In their simplest form, select expressions extract subtables. However, it is also possible for them to compute new columns or rename existing ones. a)select stock,newanount anount2® fron trade where price>1a@ stock newanou How do I aggregate column values? In SQL: select stock, sun(anount) as total fron trade group by stock a)select total:sum anount by stock from trade stock} total | 1550 1 95 @ ‘The column stock is a key in the result table. How can I add a column with the row number to a table? The keyword 4 represents the row index. a)select romo:i, stock from trade rowno stock ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos amt so2s2016 Cookbook/UsingK Ke Wiki @ delete returns a table, but does not modify the trade table in place. The that, An alternative that updates the table in place is the following: ment accomplishes a)delete fron “trade where stock= ibe How do I update values in a table? @ update returns a table, but does not modify the underlying table. The assigment accomplishes that. Or more simply: update anount+42 From “trade where stock ibs @ update modifies the table in place like delete deletes in place if a symbol is given as tablename. Also note the default column names, for further information see http://kx.com/q/d/a/kdb+.htm. How do I replace null values by something else? Use the fill operator (*). For instance, the following replaces all nulls in column ‘amount’ by zeroes. What are parameterized queries? Select, update and delete expressions can be evaluated in defined functions. For instance: @)myquery:{Tebl; ant] select stock, tine ron tol where amount > ant) a)myquery[trade; 108] stock tine ibm 99:04:59,600 @ Column names cannot be parameters of a query. Use functional form queries in such cases. Does kdb+ use stored procedures? Any user-defined function (of the right type) can be used in a query. For instance: ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos ant sos2016 aD) 482) a)select stock, * amount fron trade stock amount Can I write a query using SQL syntax against q tables? Yes. kdb+ implements a translation layer from SQL to q. The syntax is to prepend s) to the SQL query. For instance: @ Only a subset of SQL is supported. How do I write a table to disk? @)"#filenane set trade Or altematively a)save "trade You can also specify a directory: -/filenane set How do I read a table from disk? Or alternatively, How do I export a table to a CSV file? ‘trades a)save How do I import a CSV file into a table? Assume a file data.csv with columns of type int, string and int. ea, 481 16,444,579, 22,011.77 ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos at sos2016 Cookbook/UsingK Ke Wiki Then, the following expression does the trick: @ hea 482 ye afl 573 2 oil 77 What if the CSV file contains data but no column names? For instance: @,nea, 16,484,579, 20,011,7 We can read the columns like this: “a)cols: CISI%;",") 8: eatavesv a)cols hea aft of @ Column names must not be the null symbol (°). How do I export a table to a text file? a)save "trade. txt How do I access a table from an MDB file via ODBC? From windows, load odbe.k into your kdb+ session, and then load the MDB file, @ w32/edoc.k ane «od Use .odbc.eval to evaluate SQL commands via ODBC. ¥ q).odbe.eval[h;"select * Fron aa ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos san sos2016 Cookbook/UsingK Ke Wiki Can I execute kdb+ as a shebang script? Yes. Since kdb+2.4, kdb> ignores the first line if it begins with #!. E.g. F nore /test.a fifusrsblnrene 9 23 iW |S chmos 4x ./test.g E 5 s/testa Kos 3.1 2013.11.28 Copyright (C) 1993-2013 kx Systers say See http://en. wikipedia.org/wiki/Shebang_(Unix) Retrieved from "http://code.kx.com/wiki/Cookbook/UsingKdb" = This page was last modified on 17 December 2013, at 19:47. = Content is available under terms and conditions. ip llcode x com/mediawikiindex pop ?ile=Cockbosk/UsingKebsrinisblo=yos sn

You might also like