<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>How to Use CSQL (CUBRID SQL Interpreter)</title>
        <link>http://www.cubrid.org/?mid=how_to_use_csql_utilities</link>
        <description>How to Use CSQL (CUBRID SQL Interpreter)</description>
        <language>en</language>
        <pubDate>Tue, 06 Apr 2010 16:19:42 -0800</pubDate>
        <lastBuildDate>Wed, 09 Mar 2011 11:02:55 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>How to Use CSQL (CUBRID SQL Interpreter)</title>
            <dc:creator>admin</dc:creator>
            <link>http://www.cubrid.org/how_to_use_csql_utilities</link>
            <guid isPermaLink="true">http://www.cubrid.org/how_to_use_csql_utilities</guid>
                                    <description><![CDATA[<h1>How to Use CSQL (CUBRID SQL Interpreter)</h1>

<div class="contents-table"></div>

<h2>What is CSQL?</h2>

<h3>Definition of CSQL</h3>

<p>To execute a SQL query in CUBRID, there are three tools:</p>

<ul>
	<li>CSQL Command Line SQL Interpreter</li>
	<li><a href="/cubrid_manager">CUBRID Manager</a>, Graphical User Interface based database administration tool.</li>
	<li><a href="/cubrid_query_browser">CUBRID Query Browser</a>, Graphical User Interface based database administration tool.</li>
</ul>

<i>The CSQL Interpreter is the command line-based SQL Interpreter.</i> It allows you to create a SQL query, retrieve execution result, and save the SQL query and its result into a file for later use. In addition, it can print the execution result or display it page by page on a screen. Thus:

<ul>
	<li>Supports all terminal environments without X Windows</li>
	<li>Can be used to enter, edit, and execute all CUBRID commands</li>
	<li>Outputs the result to a file or screen</li>
</ul>

<h2>Connecting to a Database</h2>

<p>The CSQL Interpreter allows to connect to both local and remote database hosts.</p>

<h3>Connecting to a local database</h3>

<p>The syntax used to connect to a local host in the command line using the CSQL Interpreter is shown below:</p>

<div class="code">
<div editor_component="code_highlighter" code_type="plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql [options list] database_name
</div>
</div>

<p>In other words, you can connect to the <b>demodb</b> database using CSQL by entering the followings in the shell prompt. (The <b>demodb</b> database is the default demo database installed automatically with CUBRID.)</p>
<div class="code">
<div editor_component="code_highlighter" code_type="plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
> csql demodb
</div>
</div>

<h3>Connecting to a remote database</h3>

<p>To connect to a remote database using the CSQL Interpreter, you need to enter the remote database information in the <b>databases.txt</b> configuration file located in the <b>databases</b> directory under the CUBRID installation directory (the environment variable which stores the location is <b>$CUBRID_DATABASES</b>).</p>

<div class="code">
<div editor_component="code_highlighter" code_type="plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
vi $CUBRID_DATABASES/databases.txt
# local db information
demodb /home/cubrid/CUBRID/CUBRID_DATABASES/demodb DBHOST /home/cubrid/CUBRID/CUBRID_DATABASES/demodb
# remote db information
testdb /home/cubrid/CUBRID/CUBRID_DATABASES/testdb 192.168.1.1 /home/cubrid/CUBRID/CUBRID_DATABASES/testdb
</div>
</div>

<p class="important"><b>Remember</b> that the CUBRID version of a local database and that of a remote database must be the same.</p>

<p>The syntax used to connect to a remote host in the command line using the CSQL Interpreter is shown below, where <b>testdb</b> is the name of the remote database you indicated in the <b>databases.txt</b> file:</p>

<div class="code">
<div editor_component="code_highlighter" code_type="plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
prompt csql -C testdb
</div>
</div>

<h2>Command-line options</h2>

<p>The CSQL Interpreter allows to specify the following options:</p>

<table class="blackcap rowbg">
<thead>
<tr>
<th width="180">Option</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<th>-S</th>
<td>Executes the CSQL Interpreter in a standalone mode.</td></tr>
<tr>
<th>-C</th>
<td>Executes the CSQL Interpreter in a client/server mode.</td></tr>
<tr>
<th>-i input_file</th>
<td>Executes the CSQL Interpreter in a batch mode. The input_file parameter is the file name where SQL statements have been saved.</td></tr>
<tr>
<th>-o output_file</th>
<td>Saves a result of the statement execution in the specified output_file without displaying it on the screen.</td></tr>
<tr>
<th>-u user_name</th>
<td>Specifies the user that tries to access the database. The default value is public.</td></tr>
<tr>
<th>-p password</th>
<td>Specifies the password of the user who wants to access the database (if any).</td></tr>
<tr>
<th>-s</th>
<td>Executes multiple SQL statements saved consecutively in a file one by one. Multiple SQL statements must be separated by semicolons (;).</td></tr>
<tr>
<th>-c "CSQL commands"</th>
<td>Executes SQL statements directly from the prompt. To use this option, enclose the SQL statement to execute in double quotes.</td></tr>
<tr>
<th>-l</th>
<td>Displays the results of the SQL statement in a line format instead of a column. By default, the results will be displayed in a column format.</td></tr>
<tr>
<th>-e</th>
<td>Continues the query execution even when an error occurs.</td></tr>
<tr>
<th>--no-auto-commit</th>
<td>Sets the auto-commit mode of the CSQL Interpreter to OFF.</td></tr>
<tr>
<th>--no-pager</th>
<td>Displays the results of the query performed by the CSQL Interpreter at once instead of page-by-page.</td></tr></tbody></table>

<p>You can see the option list when you enter 'csql' in the shell prompt as shown below:</p>

<div class="code">
<div editor_component="code_highlighter" code_type="plain" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
C:\Users\me> csql
A database-name is missing.
interactive SQL utility, version R3.1
usage: csql.exe [OPTION] database-name

valid options:
  -S, --SA-mode                standalone mode execution
  -C, --CS-mode                client-server mode execution
  -u, --user=ARG               alternate user name
  -p, --pasword=ARG            password string, give "" for none
  -e, --error-continue         don't exit on statement error
  -i, --input-file=ARG         input-file-name
  -o, --output-file=ARG        output-file-name
  -s, --single-line            single line oriented execution
  -c, --command=ARG            CSQL-commands
  -l, --line-output            display each value in a line
  -r, --read-only              read-only mode
      --no-auto-commit         disable auto commit mode execution
      --no-pager               do not use pager

For addtional information, see http://www.cubrid.org
</div>
</div>

<h3>Options Usage Examples</h3>

<h4>Standalone mode -S</h4>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql -S demodb
</div></div>

<p>The above command means a database administrator wants to connect to the <b>demodb</b> database in a standalone mode. As a result no one except the current user will be allowed to connect to this database.</p>

<h4>Line format -l</h4>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql -S -l demodb
</div></div>

<p>The <b>-l</b> option will tell CSQL to output the result of the SQL statement in a line format as shown below. If the <b>-l</b> option is omitted, the results will be displayed in a column format.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql select * from code
csql ;x

=== Result of SELECT Command in Line 1===

00001s_name: 'X'
f_name: 'Mixed'
00002s_name: 'W'
f_name: 'Woman'
00003s_name: 'M'
f_name: 'Man'
00004s_name: 'B'
f_name: 'Bronze'
00005s_name: 'S'
f_name: 'Silver'
00006s_name: 'G'
f_name: 'Gold'


6 rows selected.

Current transaction has been committed.

1 command(s) successfully processed.
csql
</div></div>

<h4>Execute SQL statement in the shell -c</h4>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
prompt csql -S -c "select * from code" demodb
</div></div>

<p>The <b>-c</b> option will allow you to execute a SQL statement in the shell without connecting to the CSQL Interpreter.</p>

<p class="important">Enter a SQL statement in double quotes when executing it directly in the shell.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql -S -c "select * from code" demodb

=== Result of SELECT Command in Line 1 ===

s_name f_name
============================================
'X' 'Mixed'
'W' 'Woman'
'M' 'Man'
'B' 'Bronze'
'S' 'Silver'
'G' 'Gold'
</div></div>

<h4>Force SQL statement execution -e</h4>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
prompt csql -S -e -i infile demodb
</div></div>

<p>If you use the <b>-e</b> option, the next SQL statement is executed even when a semantic error or a runtime error occurs during the current SQL statement execution.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
vi infile

select * from test_tb;
create table test_tb (n int);
insert into test_tb(m) values (10);
insert into test_tb(n) values (5);
select * from test_tb;

prompt csql -e -i infile demodb
In line 1, column 1,

ERROR: Unknown class "test_tb".


In line 3, column 23,

ERROR: m is not defined.


=== Result of SELECT Command in Line 5 ===

n
=============
5
</div></div>

<h2>CSQL Session Commands</h2>

<h3>Command Syntax</h3>

<p>In addition to SQL statements, CSQL Interpreter provides special commands allowing you to control the Interpreter. These commands are called <b>session commands</b>. All the session commands must start with a semicolon (<b>;</b>). Enter the '<b>;help</b>' command in the CSQL Interpreter to display a list of the session commands available. Note that only the uppercase letters of each session command are required to make the CSQL Interpreter recognize it and not case sensitive.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql ;help === Help: Session Command Summary ===
All session commands should be prefixed by `;' and only blanks/tabs
can precede the prefix. Capitalized characters represent the minimum
abbreviation that you need to enter to execute the specified command.
;REad [ file-name] - read a file into command buffer.
;Write [ file-name] - (over)write command buffer into a file.
;APpend [ file-name] - append command buffer into a file.
;PRINT - print command buffer.
;SHELL - invoke shell.
;CD - change current working directory.
;EXit - exit program.
;CLear - clear command buffer.
;EDIT - invoke system editor with command buffer.
;List - display the content of command buffer.
;RUn - execute sql in command buffer.
;Xrun - execute sql in command buffer,
and clear the command buffer.
;COmmit - commit the current transaction.
;ROllback - roll back the current transaction.
;AUtocommit [ON|OFF] - enable/disable auto commit mode.
;SHELL_Cmd [shell-cmd] - set default shell, editor, print and pager
;EDITOR_Cmd [editor-cmd] command to new one, or display the current
;PRINT_Cmd [print-cmd] one, respectively.
;PAger_cmd [pager-cmd]
;DATE - display the local time, date.
;DATAbase - display the name of database being accessed.
;SChema class-name - display schema information of a class.
;SYntax [sql-cmd-name] - display syntax of a command.
;TRigger [`*'|trigger-name] - display trigger definition.
;Get system_parameter - get the value of a system parameter.
;SEt system_parameter=value - set the value of a system parameter.
;Info command- display internal information.
;TIme [ON/OFF] - enable/disable to display the query
execution time.
;HISTORYList - display list of the executed queries.
;HISTORYRead history_num- read entry on the history number into command buffer
;HElp - display this help message.
csql
</div></div>

<h3>Commands</h3>

<table class="blackcap rowbg">
<thead>
<tr>
<th width="180">Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span style="color: blue;">;RE </span>ad file name</td>
<td>Reads the specified file into a buffer.</td></tr>
<tr>
<td><span style="color: blue;">;W </span>rite filename</td>
<td>Saves the contents of the buffer in the specified file name.</td></tr>
<tr>
<td><span style="color: blue;">;A </span>ppend filename</td>
<td>Appends the contents of the buffer into the specified file.</td></tr>
<tr>
<td><span style="color: blue;">;L </span>ist</td>
<td>Displays the contents of the buffer</td></tr>
<tr>
<td><span style="color: blue;">;P </span>rint</td>
<td>Prints the contents of the buffer.</td></tr>
<tr>
<td><span style="color: blue;">;CL </span>ear</td>
<td>Deletes the contents of the buffer.</td></tr>
<tr>
<td><span style="color: blue;">;CD </span>[dir_path]</td>
<td>Changes the current working directory to the specified directory.</td></tr>
<tr>
<td><span style="color: blue;">;SHELL </span></td>
<td>Exits to the shell prompt.</td></tr>
<tr>
<td><span style="color: blue;">;EX </span>it</td>
<td>Exits CSQL.</td></tr>
<tr>
<td><span style="color: blue;">;EDIT </span></td>
<td>Edits the contents of the buffer by using vi.</td></tr>
<tr>
<td><span style="color: blue;">;RU </span>n</td>
<td>Executes the SQL statement in the buffer. However, the contents of the buffer are not changed.</td></tr>
<tr>
<td><span style="color: blue;">;X </span>run</td>
<td>Executes the SQL statement in the buffer and also deletes the contents of the buffer.</td></tr>
<tr>
<td><span style="color: blue;">;CO </span>mmit</td>
<td>Commits current transaction to database.</td></tr>
<tr>
<td><span style="color: blue;">;RO </span>llback</td>
<td>Rolls back transactions up to now.</td></tr>
<tr>
<td><span style="color: blue;">;AU </span>tocommit [ON|OFF]</td>
<td>Displays current Auto Commit mode. Auto Commit mode can be changed according to value of [ON|OFF].</td></tr>
<tr>
<td><span style="color: blue;">;DATA </span>base</td>
<td>Displays database name that is being accessed.</td></tr>
<tr>
<td><span style="color: blue;">;SHELL_C </span>md [shell_cmd]</td>
<td>Displays current default shell. If you want to set up a new one, see "Setting up the SQL/X processor environment" manual.</td></tr>
<tr>
<td><span style="color: blue;">;EDITOR_C </span>md [print_cmd]</td>
<td>Sets up the editor that is used in ;edit command.</td></tr>
<tr>
<td><span style="color: blue;">;PRINT_C </span>md [print_cmd]</td>
<td>Displays current default printer. If you want to set up a new printer as a default, see "Setting up the SQL/X processor environment".</td></tr>
<tr>
<td><span style="color: blue;">;PA </span>ger_cmd [pager_cmd]</td>
<td>Displays current default page. If you want to set up a new one, see "Setting up the SQL/X processor environment" manual.</td></tr>
<tr>
<td><span style="color: blue;">;SC </span>hema [table_name]</td>
<td>Shows schema in the database. That is, the created table and its structure are displayed.</td></tr>
<tr>
<td><span style="color: blue;">;TR </span>igger ['*'|trigger_name]</td>
<td>Displays the created trigger in the current database.</td></tr>
<tr>
<td><span style="color: blue;">;SY </span>ntax [csql_cmd_name]</td>
<td>Displays the grammar of SQL statement.</td></tr>
<tr>
<td><span style="color: blue;">;G </span>et system_parameter</td>
<td>Checks the defined parameter value.</td></tr>
<tr>
<td><span style="color: blue;">;SE </span>t system_parameter=value</td>
<td>Sets a parameter value. (However, you can set only client parameter value, not server parameters.)</td></tr>
<tr>
<td><span style="color: blue;">;I </span>nfo command</td>
<td>Displays the status of schema, trigger, deferred, workspace, lock, and stats.</td></tr>
<tr>
<td><span style="color: blue;">;TI </span>me [ON|OFF]</td>
<td>Outputs query execution time.</td></tr>
<tr>
<td><span style="color: blue;">;HISTORYL </span>ist</td>
<td>This command displays the list that contains previously executed commands (input) and their history numbers.</td></tr>
<tr>
<td><span style="color: blue;">;HISTORYR </span>ead history_num</td>
<td>Moves input with the specified history number into the buffer.</td></tr>
<tr>
<td><span style="color: blue;">;HE </span>lp</td>
<td>Displays all of the CSQL session commands.</td></tr>
</tbody></table>

<h3>Basic Examples of Session Commands</h3>

<h4>Default Screen</h4>

<p>When the CSQL is executed, the following information is displayed on the screen.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
CUBRID SQL Interpreter


Type `;help' for help messages.

csql
</div></div>

<h4>Examples</h4>

<p>You can use the read command, which reads an input file into a buffer, to check the contents of the input file. The list command displays the contents of a buffer and the edit command modifies the contents of a buffer.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql <span style="color: blue;">;re </span>infile

The file has been read into the command buffer.
csql <span style="color: blue;">;li </span>

1 select * from test_tb;
2 create table test_tb (n int);
3 insert into test_tb(m) values (10);
4 insert into test_tb(n) values (5);
5 select * from test_tb;

csql
</div></div>

<p>First, create a table with the schema creation command. Just type the new schema information of the new table, once completed, type <b>;x</b> and hit enter to execute the SQL statement you have just written. This will execute the SQL statement in the buffer and also deletes the contents of the buffer.</p>

<p>Then, use '<b>;sc table_name</b>' to check the details of the newly created table.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql create table student
csql (
csql name varchar(10),
csql gender char(3),
csql phone varchar(20),
csql addr string
csql )
csql ;x

Current transaction has been committed.

1 command(s) successfully processed.

csql <span style="color: blue;">;sc </span>student

=== Help: Schema of a Class ===


Class Name

student

Attributes

name CHARACTER VARYING(10)
gender CHARACTER(3)
phone CHARACTER VARYING(20)
addr CHARACTER VARYING(1073741823)


Current transaction has been committed.
csql
</div></div>

<p>Now insert the data. Type the SQL statement as shown below and execute it by using the <b>;r</b> run command. Then search for the data.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql insert into student (name, gender, phone, addr) values ('Gildong Hong', 'F', '111-2222', 'Seoul Gangnamgu Nonhyundong')
csql <span style="color: blue;">;ru </span>

1 rows inserted.

Current transaction has been committed.

1 command(s) successfully processed.
csql <span style="color: blue;">;cl </span>
csql select * from student
csql <span style="color: blue;">;ru </span>

=== Result of SELECT Command in Line 1 ===

name gender phone addr

===========================================================================
Gildong Hong' 'F ' '111-2222' 'Seoul Gangnamgu Nonhyundong'


1 rows selected.

Current transaction has been committed.

1 command(s) successfully processed.
csql
</div></div>

<p>If the <b>-noac</b> (no auto-commit) option was used when starting CSQL, all the transactions till now, e.g. entering, deleting, or changing schema or data, must be reflected on the database using the <b>;co</b> (commit) or <b>;ro</b> (rollback) command before termination. If you exit without reflecting the transactions on the database, the system will ask whether you want to reflect it or not.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql <span style="color: blue;">-no-auto-commit </span>demodb
csql insert into student(name,gender,phone,addr) values('Hani','F','333-9999',
csql 'Seoul Youngdeungpogu Yeoidodong')
csql ;ru

1 rows inserted.

1 command(s) successfully processed.
csql ;co

Current transaction has been committed.
csql ;exit
</div></div>

<h2>Display Query Plan</h2>

<h3>Optimization level</h3>

<p>Query plan shows which index is used in a query and what type of join is used. A user can see the query plan by changing optimization level before query execution.</p>

<div class="code">
<div editor_component="code_highlighter" code_type="bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false">
csql set optimization level 257
csql ;x

Current transaction has been committed.

1 command(s) successfully processed.
csql select name from student where gender='F'
csql ;x

<span style="color: blue;">Query plan: </span>

<span style="color: blue;">Sequential scan(student student) </span>br
=== Result of SELECT Command in Line 1 ===

name
======================
Gildong Hong'
Hani'


2 rows selected.

Current transaction has been committed.

1 command(s) successfully processed.
</div></div>

<h2>See also</h2>

<ul>
	<li><a href="http://blog.cubrid.org/cubrid-story/csql-interpreter/">CSQL - Command Line SQL Interpreter</a> - Blog article</li>
	<li><a href="/tutorials">CUBRID Tutorials</a></li>
</ul>]]></description>
                        <pubDate>Tue, 06 Apr 2010 15:20:32 -0800</pubDate>
                                </item>
            </channel>
</rss>
