| Prev | User's Guide V8.5 (revision 2) | Next |
This section explains the tasks that you perform with SQL DM. The tasks are divided into the following categories:
What to do first? If you are new to SQL DM, begin with the general tasks. If you are new to SQL programming, begin with the tasks for Query Builder. If you are experienced with SQL programming, begin with the tasks for Query Pane.
General tasks orient you to the overall use of SQL DM.
Query Pane tasks help you use Query Pane.
Execution and results tasks apply to the running of SQL statements and working with results on the Grid Results tab and the Text Results tab.
Query Builder Diagram tasks orient you to the use of Query Builder Diagram and Query Builder Grid.
To start SQL DM as a stand-alone application
This step assumes that SQL DM was installed as part of a Pervasive.SQL default installation. A vendor application that uses an embedded database engine may require that you start SQL DM with different menu commands. (The GUI executable is named sqldmgr.exe.)
SQL DM starts by providing a dialog in which you choose a database engine and database name to connect to. By default, the local database engine (on the same computer as SQL DM) is specified.
Note
SQL DM opens a separate instance for each database. This means that you can open multiple instances of SQL DM for the same database. Changes saved in one instance are reflected in another instance only if you run a query (or queries) in the other instance. For example, suppose you start SQL DM twice (called instance A and B) for database MyDBase. If you change data in instance A and save the changes, you need to run a SELECT statement in instance B to see those changes.
SQL DM starts and runs a Select * From table_name statement, where table_name is the name of the table icon. The results are placed into the Grid Results tab.
Note
SQL DM opens a separate instance for each table. This means that you can open multiple instances of SQL DM from PCC for the same table. Changes saved in one instance are reflected in another instance only if you run a query (or queries) in the other instance. For example, suppose you open SQL DM twice (called instance A and B) for table MyTable. If you change data in the grid in instance A and save the changes, you need to run a SELECT statement in instance B to see those changes.
To identify machine and database currently open
The title bar shows the name of the open database and the name of the machine on which the database resides.
For example, the following image shows that DEMODATA is the open database and DOCLAB2 is the machine on which DEMODATA resides.
If the title bar does not show the name of a database and the name of a machine, then no database is open.
The Login to database dialog appears.
If the desired database has Pervasive.SQL security enabled, type the user name and password required for the database in the User and Password fields, respectively. By default, the user name is Master. If the database does not have security enabled, you may leave the User and Password fields blank.
To create an SQL query or script
By default, when you start SQL DM, Query Pane is ready for you type in SQL statements. Statements that you build with Query Builder also appear in Query Pane. Note that Query Builder clears the contents of Query Pane, so you will lose any statements typed in unless you have saved them as a script. A script is one or more SQL statements saved as an ASCII file. See To save an SQL query as a script .
.If execution stops because of an error, the statement indicator positions to the statement that was being run when the interruption occurred. Also, the Text Results tab lists the statement that was last run. Knowing the last statement run can help you troubleshoot the problem. See To continue running SQL statements that were stopped .
Note that the title bar no longer shows the name of an open database or the name of a machine on which the database resides. In addition, the commands and icons for running a query become inactive (grayed out). You can run a query only if a database is open.
To change data with Grid Results grid
The Grid Results grid must be active before you can change values in cells. See To set Grid Results to allow changes to database data .
Note
If you have a table without primary keys that contains records with duplicate data for a column, you will be unable to update the duplicate data on the Grid Results tab. SQL DM cannot tell one record from another because of the duplicate data. This problem does not occur if your table contains primary keys because they prevent duplicate data. (You may use an UPDATE statement to change duplicate data.)
After the grid has been populated by a SELECT statement, you may directly update the table by making changes to the values in the active grid. You must have the proper table permissions to affect the database data.
An icon that indicates a changed value appears in the leftmost column of the row:
To save an SQL query as a script
If Query Pane contains statements that have not been saved, you are prompted to save them. Click Yes to save the existing statements, specify a file name and location, then click Save. Click No to discard the existing statements.
An SQL query file is an ASCII file of one or more SQL statements. By default, an SQL query files saved from SQL DM has a file extension of "sql."
SQL DM places the contents of the open script into Query Pane. Note that if you change the script then click File4Save SQL Query or click
, the changes overwrite the script file. If you want to save the changes to a different location or as a different script name, click File4Save SQL Query As.
If Query Pane contains statements that have not been saved, you are prompted to save them. Click Yes to save the existing statements, specify a file name and location, then click Save. Click No to discard the existing statements.
To move the cursor within Query Pane without running any SQL statements
The statement indicator, the yellow arrow
in the left margin of Query Pane, indicates the statement in which the cursor is located.A statement delimiter is used to separate statements. See To set SQL statement separator .
To move the cursor within Query Pane and run the SQL statement to which it moves
The statement indicator, the yellow arrow
in the left margin of Query Pane, indicates the statement in which the cursor is located.A statement delimiter is used to separate statements. See To set SQL statement separator .
SELECT * FROM Person#
SELECT * FROM Faculty#
DELETE FROM Person WHERE Person.ID=104321686#
SELECT * FROM Class#
To set SQL statement separator
Two or more SQL statements in Query Pane must be separated by a delimiter. By default, SQL DM sets the delimiter to the pound sign (#). You may change the delimiter to the semicolon (;) if you choose.
Note
You must use the pound sign as the statement delimiter in Query Pane if you create stored procedures. Stored procedures use the semicolon as a statement delimiter within the procedure itself.
To set a query time-out period
The query time-out period specifies how many seconds to allow an SQL statement to run before the SRDE cancels the statement. The default is zero seconds, which means that no time-out period is specified. The value you specify is stored in the registry and will also apply the next time you start SQL DM.
The time-out period applies to SELECT, UPDATE, INSERT, and DELETE statements, but not to Data Definition Language (DDL) statements (such as CREATE INDEX, ALTER TABLE, DROP TABLE, and so forth). The SRDE stops processing a query after the specified period, returns SQL_ERROR, and sets SQLSTATE to "S1T00." Because SQL statements are atomic, no records are updated, inserted, or deleted if the SRDE cancels an UPDATE, INSERT, or DELETE statement, respectively.
The allowed range is 0 through 4,294,967,295. Partial seconds (for example, 30.5) and negative numbers are not allowed.
To wrap text within Query Pane
By default, word wrap is turned off, which is indicated by lack of a checkmark beside the command.
To type comments into Query Pane
Single-line comments are indicated by double dashes (--) or double slashes (//). Each comment must be on a separate new line or after the statement separator on an existing line. See To set SQL statement separator .
SQL DM also supports the use of a start/end comment block that can span multiple lines (/* */).
The following example shows valid comments.
SELECT * FROM t2# -- This is valid after the # sign
The following example shows multi-line comments.
SELECT * FROM t1# -- single line comment
/* This is a comment block that spans two lines.
, is still on the line containing the cursor.
To cancel (undo) typing actions in Query Pane
To cancel an Undo action (redo) in Query Pane
The Redo action performs the opposite of the last Undo action. For example, if the last Undo action removed the word "Select" from a statement, Redo restores the word "Select" to the statement.
A dialog appears on which you specify a text string to find.
A dialog appears in which you specify a text string to search for and a text string with which to replace it.
To cut or delete text from Query Pane
Text cut from Query Pane is placed in the clipboard and is available for pasting. Text deleted from Query Pane is not placed in the clipboard.
To print the contents of Query Pane
A dialog appears on which you specify the options for printing.
A dialog appears on which you specify the layout for the printed page.
To toggle insert/overtype mode
Insert mode adds characters between other characters as you type in Query Pane. Existing characters are pushed to the right. Overtype mode replaces characters as you type.
When the Insert/Overtype indicator shows OVR in a dimmed typeface, SQL DM is in insert mode. When OVR is boldface, SQL DM is in overtype mode.
| Insert Mode |
Overtype Mode |
|---|---|
|
|
To set the font for Query Pane
Setting the font affects the font used in the Query Pane, the Grid Results tab, and the Text Results tab.
A database must be open and one or more SQL statements must exist in Query Pane before you can run a statement.
Note
SQL DM automatically uses the Text Results tab for the results of SQL statements that are not SELECT statements. Only SELECT statements use the Grid Results tab.
A database must be open and one or more SQL statements must exist in Query Pane before you can run all statements. Two or more SQL statements in Query Pane must be delimited. See To set SQL statement separator .
To stop running SQL statements
The statement indicator positions to the statement that was being run when the interruption occurred. The indicator is the yellow arrow
.
If you stop the running of SQL statements, or if execution stops because of an error, the Text Results tab lists the statement that was last run. Knowing the last statement run can help you troubleshoot problems.
To continue running SQL statements that were stopped
Execution of the SQL statements continues with the statement that contains the statement indicator. Note that you may move to a statement after execution stops. For example, suppose execution stops at statement 50 in a script that contains 100 statements. You could click on statement 60 and continue execution from there.
Unless you move the statement indicator, execution continues with the next statement following the one that last ran.
To set the font for grid results or text results
Setting the font affects the font used in the Query Pane, the Grid Results tab, and the Text Results tab.
After the grid has been populated by a SELECT statement, you may directly update your database data by changing the values in an active grid. You must have the proper table permissions to affect the database data. The grid must be active before you can change values in cells. See To set Grid Results to allow changes to database data .
To set Grid Results to allow changes to database data
A checkmark beside the Active Grid command indicates that the grid is active. In addition, an indicator column appears as the leftmost column in the grid:
To set default behavior for Grid Results tab
A checkmark beside the option indicates that the grid will be active each time SQL DM is started.
To delete a row(s) from Grid Results tab
Note that the delete action has no undo. The grid must be active to allow deletion of rows. See To set Grid Results to allow changes to database data .
You may also select and delete multiple rows. To select multiple rows, press and hold down the Shift or Control key, then click the indicator column for the desired rows. Once the desired rows are selected, right-click and click Delete Row(s).
The indicator column is the leftmost column in the grid:
Once a row(s) is selected, you may also press the Delete key to delete the row(s).
Note that you cannot delete a row that contains invalid values in one or more grid columns. For example, assume that you want to delete a record from the "Person" table provided with the sample database DEMODATA.
You run a SELECT * FROM person statement in Query Pane. You click on an ID cell in the grid and remove the ID value with the Backspace key. You then decide that you want to delete the entire record. The Delete Row(s) command does not work because the ID column contains an invalid value (it is blank). The ID column in the "Person" table does not allow NULLs. You would first need to add a valid value into the ID cell, then delete the entire record.
To copy data from Grid Results tab to clipboard
Copied data is placed in the clipboard in a tab delimited format.
The copied data can be pasted into another application, such as Microsoft Excel. For example, in Excel, you would click on a cell, right-click, then click Paste.
The Text Results tab shows in a text format the result of running SQL statements. You cannot change the data values in the database by changing the text.
To clear results from Text Results tab
To select text on Text Results tab
To copy text from Text Results tab
To cut or delete text from Text Results tab
Text must first be selected before you can cut or delete it. Text cut is placed in the clipboard and is available for pasting. Text deleted is not placed in the clipboard.
To show or hide Query Builder Diagram
A database must be open before you can show or hide Query Builder Diagram.
These actions function as a toggle. That is, if Query Builder Diagram is hidden, the action shows it, and vice versa. See also Behavior When Query Builder Diagram Contains No Tables .
To show or hide Query Builder Grid
A database must be open before you can show or hide Query Builder Grid.
These actions function as a toggle. That is, if Query Builder Grid is hidden, the action shows it, and vice versa. See also Behavior of Query Builder Grid When Query Builder Diagram Contains No Tables .
To set query type for Query Builder
This task requires that Query Builder Diagram or Query Builder Grid be active (showing).
To build a SELECT statement with Query Builder
This task serves as a mini-tutorial on how to build an SQL statement with Query Builder. This task addresses building a SELECT statement, but many of the actions also apply to building an UPDATE, INSERT, or a DELETE statement. For this reason, we suggest that you complete this task before attempting to build one of the other statement types.
This task presents the steps to accomplish the following:
The database used for this task is the sample database provided with Pervasive.SQL, named DEMODATA. DEMODATA is located, by default, in the folder PVSW\demodata.
This task assumes the following:
Your first goal is to obtain the ID number, first name, and last name of all persons in your database with a first name of "James" or "Lisa" and whose last name begins with an "N," "O," "P," "Q," "R," "S," or "T." Once you have the correct records, you need to sort them by ascending ID number.
The Add Table dialog, the Query Builder Diagram, and the Query Builder Grid appear.
The SQL statement is started in Query Pane as soon as you add a table to Query Builder Diagram. SQL DM should show something similar to the following:
If you see a different type of grid, then you need to set Query Builder to build a SELECT statement. The next two steps show you how. Complete both steps even if you see the same type of grid.
Notice that the columns appear in the grid and that the SQL statement in Query Pane continues to be built.
Note
You must indicate to Query Pane that you are finished typing into a grid cell. You do this by moving the cursor from the cell once you have finished typing. For example, after you type "Last Name," move the cursor from that cell with the Tab key, cursor arrows, or mouse.
Notice that Query Builder automatically adds an equals sign and single quotes around 'James' because "James" is a string data type. Non-string data types are not enclosed by quotes.
SQL DM should show something similar to the following:
At this point, run the SQL statement to see what results are returned.
The statement returns 24 records where first name equals "James" or "Lisa." But your goal also requires the records where the last name begins with an "N," "O," "P," "Q," "R," "S," or "T." You obtain the last name information by using a logical AND.
Also notice that the Aliases you specified appear as the column headings on the Grid Results tab.
Optional: If you want to view the results as text, click Tools4Query4Run Current SQL Statement in Text or click
.
Click the Grid Result tab after you finish viewing the text results.
Notice that the grid does not contain an AND column. You create a logical AND by adding the same database column to the grid and specifying the additional criteria.
SQL DM should show something similar to the following:
Notice that the SQL statement now contains the AND clauses for last name.
This time, the statement returns 10 records, but the records are not sorted. Their order is the sequence in which they were added to the database.
You are almost finished with your first goal! All you have remaining is to sort the records by the person ID numbers.
The ORDER by clause is added to the SQL statement. Notice that the ascending symbol is added to the Person table for the ID column.
The statement returns 10 records but now they are sorted in ascending sequence by ID number.
You have now completed your first goal for this task. Your next goal is to determine from your database the average amount owed by the students. You determine this with an aggregate query that uses a Group By clause. The "Person" table does not contain the data you need so you are finished with it.
SQL DM should show something similar to the following:
The Group By column appears on the grid, the Group By symbol is added to the "Billing" table for the Amount_Owed column, and the SQL statement now contains a Group By clause.
Query Pane now contains a query that determines the average amount owned by students.
Notice that the Group By part of the clause went away when an aggregate function (AVG) was specified for the column. If you also want to group by the column, position the mouse pointer in the empty cell below "Billing"."Amount_Owed," then press and hold down the left mouse button. From the list, click "Billing"."Amount_Owed." The Group by column shows GROUP BY and the statement in Query Pane contains a GROUP BY clause.
In other words, you need one line in Query Builder Grid for each aggregate function or Group By clause that you want in the SQL statement.
The average amount owed by students is $2,894.26.
Notice that the column heading for the output is EXPR_1. What if you wanted the heading to read "Average Amount Owed." What would you change on Query Builder Grid to produce this result? You would type "Average Amount Owed" as an Alias for "Billing"."Amount_Owed." Do that and run the statement again to see if the heading changes.
You have now completed your second goal for this task. Your last goal is to find which persons in your database are also on the faculty, and determine the department in which the faculty member teaches and the salary of the faculty member.
Because you now have some experience using Query Builder, the steps for this goal are more generic and contain fewer images of the GUI. If you are unsure how to complete a step, review the previous steps in this task.
SQL DM should show something similar to the following:
You are now ready to join the two tables with the ID column. The join allows you to determine which faculty members are also in the Person table. The following steps show you how to join the columns by dragging the column from one table to the corresponding column in the other column.
A join line appears between the two tables:
Notice that the JOIN syntax is not used in the SQL statement. Instead, a WHERE clause is added:
The statement returns 95 records. Your manager arrives and reviews the results. Your manager tells you to remove the salary information because it is not needed. How do you do this?
This action removes the "X" from the option and also removes the Salary part from the selection list in the SQL statement.
To build an UPDATE statement with Query Builder
This task assumes the following:
Your goal for this task is to change the time at which Psychology 101 begins to 10:30 am and reduce its maximum class size to 175. Before you make the changes, however, look at the information for this class.
SQL DM should show something similar to the following:
This is the record you want to change. Notice that its ID is 173.
Notice that the
symbol appears in the table window instead of the
symbol that appears for a SELECT statement.
You could build your statement without the class name because the class ID uniquely identifies the record. For clarity to someone reading the SQL statement, though, include the class name.
SQL DM should show something similar to the following:
The Text Results tab contains the following result:
SQL statement (script) has executed successfully. 1 row(s) were affected.Now look at the data in the "Class" table again to verify that you changed the record.
Notice that the maximum class size is now 175 and the start time is 10:30 am.
To build an INSERT statement with Query Builder
This task assumes the following:
Your goal for this task is to add a new philosophy class to the database. Before you make the change, however, look at the information for the existing courses.
SQL DM should show something similar to the following:
SQL DM should show something similar to the following:
Note that when you perform an INSERT, you must provide a value for all columns that cannot be null. For the Course table, "Course"."Name" and "Course"."Dept_Name" must contain values. For practice, however, this task has you provide values for all four columns.
The Text Results tab contains the following result:
SQL statement (script) has executed successfully. 1 row(s) were affected.Now look at the data in the "Course" table again to verify that you inserted the record.
The philosophy class you inserted is now part of the table.
To build a DELETE statement with Query Builder
This task assumes the following:
Your goal for this task is to remove from the database classroom 100 in the Boerner Building. Before you make the changes, however, look at the information for the existing classrooms.
Notice that room 100 is one of the rooms listed in the Boerner Building.
Notice in the table window that none of the columns are selectable for a DELETE statement.You cannot delete individual columns so check boxes do not appear in the table window. Instead of selecting columns, you specify the criteria in Query Builder Grid for the rows you want to delete, as explained next.
SQL DM should show something similar to the following:
Why do you need both the building name and the room number? The "Room" table contains several records for the Boerner Building. If your only deletion criteria was building name, you would delete records you need to keep. Similarly, "Room" contains records for room 100 in other buildings, so you would not want to delete all rooms with room number 100.
SQL DM should show something similar to the following:
The Text Results tab contains the following result:
SQL statement (script) has executed successfully. 1 row(s) were affected.Now look at the data in the "Room" table again to verify that you deleted the record.
Notice that room 100 is no longer listed as one of the classrooms for the Boerner Building.
To add a table to Query Builder Diagram
This task requires that Query Builder Diagram be active (showing).
See also Behavior When Query Builder Diagram Contains No Tables .
To remove a table from Query Builder Diagram
This task requires that Query Builder Diagram be active (showing).
To join tables in Query Builder Diagram
See steps 32 through 34 in To build a SELECT statement with Query Builder for an example of joining tables.
To insert a row into Query Builder Grid
This task requires that Query Builder Grid be active (showing).
The inserted row is placed above the row that contained the cursor.
To clear the contents of a row(s) on Query Builder Grid
This task requires that Query Builder Grid be active (showing).
The clear action removes the values from all of the grid cells for that row, but leaves the empty row in the grid. The SQL statement in Query Pane is rebuilt.
You may also select and clear multiple rows. To select multiple rows, press and hold down the Shift or Control key, then click the indicator column for the desired rows. Once the desired rows are selected, right-click and click Clear Row(s).
The indicator column is the leftmost column in the grid:
To delete a row(s) from Query Builder Grid
This task requires that Query Builder Grid be active (showing).
The delete action removes the row in the grid. The SQL statement in Query Pane is rebuilt.
You may also select and delete multiple rows. To select multiple rows, press and hold down the Shift or Control key, then click the indicator column for the desired rows. Once the desired rows are selected, right-click and click Delete Row(s) (or press the Delete key).
The indicator column is the leftmost column in the grid:
