15.2. mSQL SQL
Creates new database elements (or entirely new databases). This statement is used to create tables, indices, and sequences. The CREATE SEQUENCE statement adds a sequence to a table. A sequence is simply a value associated with a table that the mSQL server keeps track of. Most commonly, a sequence is used to generate unique identification numbers for tables. The value of the sequence is incremented every time the sequence value is read. The STEP modifier determines how much the sequence value is increased each time. The VALUE modifier gives the initial value of the sequence. The CREATE INDEX statement defines an index for the table. The mSQL system supports indexes that contain more than one field. You must provide a name for the index, although it need not be meaningful because it is rarely needed by the end user. The CREATE TABLE statement defines the structure of a table within the database. This statement is how all mSQL tables are created. The syntax of the create definition is the name of a field followed by the type of the field, followed by any modifiers (e.g., name char(30) not null). The following datatypes are supported by mSQL:
In addition to the main types, several modifiers can also be used to qualify the type:
ExamplesCREATE TABLE emp_data ( id INT, name CHAR(50) ) # Add a sequence to the table 'checks' with the initial value '1000' and the # default step of 1. CREATE SEQUENCE ON checks VALUE 1000 # Create an index on the table 'music' that covers the fields 'artist', # 'publisher', and 'title'. CREATE INDEX idx1 ON music ( artist, publisher, title )
Deletes rows from a table. When used without a WHERE clause, this will erase the entire table and recreate it as an empty table. With a clause, it will delete the rows that match the condition of the clause. Examples# Erase all of the data (but not the table itself) for the table 'olddata'. DELETE FROM olddata # Erase all records in the 'sales' table where the 'year' field is '1995'. DELETE FROM sales WHERE year=1995
Permanently remove a table, index, or sequence from the mSQL system.
Examples# Delete the tables 'oh_no' DROP TABLE oh_no # Remove the named index 'my_index' DROP INDEX my_index # Erase the sequence defined on the table 'counter'. Another sequence can be # recreated at any time using the 'CREATE SEQUENCE' statement. DROP SEQUENCE FROM counter
Inserts data into a table. This statement inserts the given values into the given columns. Columns in the table that are not given values are set to NULL. If you leave out the list of columns, the number of value given must exactly match the number of columns in the table. Examples# Insert a record into the 'people' table. INSERT INTO people ( name, rank, serial_number ) VALUES ( 'Bob Smith', 'Captain', 12345 )
Retrieves data from a database. The SELECT statement is the primary method of reading data from database tables. If you specify more than one table, mSQL will automatically join the tables so that you can compare values between the tables. If the DISTINCT keyword is present, only one row of data will be output for every group of rows that is identical. The column names can be specified as column or table.column. The longer form is necessary only to disambiguate columns with the same name, but can be used at any time (e.g., SELECT name FROM people; SELECT people.name FROM people). The list of tables to join are specified as Table1, Table2, Table3, ... The tables are joined in the manner that mSQL deems most efficient. Table names can also be aliased (e.g., SELECT t1.name, t2.address FROM long_table_name=t1, longer_table_name=t2). If no clause is provided, SELECT returns all of the data in the selected table(s). The search clause can contain any of the following substatements:
Examples# Find all names in the 'people' table where the 'state' field is 'MI'. SELECT name FROM people WHERE state='MI' # Display all of the data in the 'mytable' table. SELECT * FROM mytable
Alters data within a table. This statement is used to change actual data within a table without altering the table itself. Example# Change the name 'John Deo' to 'John Doe' everywhere in the people table. UPDATE people SET name='John Doe' WHERE name='John Deo' Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|