Hi developers!
As you know InterSystems IRIS besides globals, object, document and XML data-models also support relational where SQL is expected as a language to deal with the data.
And as in other relational DBMS InterSystems IRIS has its own dialect.
I start this post to support an SQL cheatsheet and invite you to share your favorites - I'll update the content upon incoming comments.
Here we go!
List all the tables in database:
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
SQLSQL
Credit to @Dmitry Maslennikov
Create table with current date and current time as defaults:
CREATE TABLE CUSTOMER
(
ID INTEGER PRIMARY KEY NOT NULL,
NAME VARCHAR(100) NOT NULL,
PASSWORD VARCHAR(20) NOT NULL,
LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
REGISTRATION_DATE DATE DEFAULT CURRENT_DATE NOT NULL
);
SQLSQL
notice functions CURRENT_TIMESTAMP and CURRENT_DATE are without parenthesis.
Create stored procedure/function that uses ObjectScript inside:
CREATE OR REPLACE FUNCTION IRIS_VERSION()
RETURNS VARCHAR
LANGUAGE OBJECTSCRIPT
{
Return $ZV
}
SQLSQL
Call Stored procedure/function:
select IRIS_VERSION() as "Version"
SQLSQL
Call internal functions.
Return IRIS Version:
Select $version
SQLSQL
Return current namespace:
Select $namespace
SQLSQL
Run SQL from file (credit @Raj Singh ):
DO $SYSTEM.SQL.Schema.ImportDDL("c:\InterSystems\mysqlcode.txt",,"IRIS")
ObjectScriptObjectScript
Run SQL from file using python terminal(credit @Dmitry Maslennikov):
irissqlcli iris://_SYSTEM:SYS@localhost:1972/USER < c:\InterSystems\mysqlcode.txt
Shell SessionShell Session
Open SQL shell in terminal(credit @Chad Severtson):
USER>:sql
Open SQL shell in webterminal (credit @Nikita Savchenko ):
SAMPLES > /sql SAMPLES:SQL > select top 5 * from Cinema.Film
ID | Category | Description | Length | PlayingNow | Rating | TicketsSold | Title |
1 | 1 | A post-modern excursion into family dynamics and Thai cuisine. | 130 | 1 | PG-13 | 47000 | Her Spicy Brothers |
2 | 1 | A gripping true story of honor and discovery | 121 | 1 | R | 50000 | Einstein's Geisha |
3 | 1 | A Jungian analysis of pirates and honor | 101 | 1 | PG | 5000 | A Kung Fu Hangman |
4 | 1 | A charming diorama about sibling rivalry | 124 | 1 | G | 7000 | Holy Cooking |
5 | 2 | An exciting diorama of struggle in Silicon Valley | 100 | 1 | PG | 48000 |
The Low Calorie Guide to the Internet |
SAMPLES: SQL > /sql SAMPLES > write "COS!" cos!
Add yours?
nice! Thanks ... I didn't know you could do things like Select $zversion :)
here it s in practice for those interested:
Try ":sql" instead of
Holy cow @Chad Severtson, that's really cool! Do you know when that was added / where I can find more information? I don't see it in the docs on SQL Shell (https://docs.intersystems.com/iris20221/csp/docbook/Doc.View.cls?KEY=GSQ...).
Also, what is the ":" shorthand for and where else can it be used?
:py for embedded python
@Dmitry Maslennikov - thank you! Docs reference? Do you know what the ":" syntax is?
USER>:? :<number> Recall command # <number> :? Display help :py Do $system.Python.Shell() :mdx Do $system.DeepSee.Shell() :sql Do $system.SQL.Shell() :tsql Do $system.SQL.TSQLShell() :alias Create/display aliases :clear Clear history buffer :history Display command history :unalias Remove aliases
HTH
wow. Didn't know we have that! And aliases can be setup and transferred too? E.g. like a package (IPM)?
from docs:
If you are using a UNIX® or Linux system, you can provide a list of alias definitions which the Terminal will set automatically at the start of every session. Define these aliases (one per line) in a file named .iris_init in the home directory.
never tried this. I rarely work on *X
Another very cool aspect of the feature :) So it looks like this is just a built-in alias which ships with InterSystems IRIS.
Creating Aliases for Commonly Used Commands
holy cow #2 :-) so much precious info on this thread!
seriously! it's amazing how much knowledge there is to be gleaned within this community :)
Hey @Ben Spead,
You can find out more about the alias capability of iris session here: Using the Terminal Interactively | Using the Terminal | InterSystems IRIS Data Platform 2022.3
It's quite useful. You can even provide parameter substitution with $1, $2, etc.
Thank you @Jeffrey Drumm !!!
It has been around for a few years.
Personally, I hesitate to use the alias capability lest I find myself in an environment without them!
For those that posted "I never knew that! Wow!" about the aliases: didn't you read this 2 years ago?
😞
is there no quicker way to list all database tables? Like the postgresql \t?
in irissqlcli, you can use \dt or .tables
Would be nice to have
Run SQL from a file:
DO $SYSTEM.SQL.Schema.ImportDDL("c:\InterSystems\mysqlcode.txt",,"IRIS")
or
or
You can also save the current query
Nice work @Evgeny Shvarov those typical things you want to do quickly and never remember and have to read the docs :-)
Thanks, Luca!
Just a note...
LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
This field will only define the LAST_UPDATED field at INSERT, not for an UPDATE. You probably want something like:
LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL,
This will cause LAST_UPDATED to be set at INSERT and UPDATE to the current timestamp to microsend precision.