Mimer SQL Database Objects

This chapter provides a general introduction to the basic concepts of Mimer SQL databases and Mimer SQL objects.

Mimer SQL is a relational database system. This means that the information in the database is presented to the user in the form of tables. The tables represent a logical description of the contents of the database which is independent of, and insulates the user from, the physical storage format of the data.

The Mimer SQL database includes the data dictionary which is a set of tables describing the organization of the database and is used primarily by the database management system itself.

The database, although located on a single server, may be accessed from many distinct clients, linked over a network.

Commands are available for managing the connections to different databases, so the actual database being accessed may change during the course of an SQL session.

At any one time, however, the database may be regarded as one single organized collection of information.

System and Private Objects

Mimer SQL database objects can be divided into the following groups:

System Objects

System objects are global to the database. System object names must be unique for each object type since they are global and therefore common to all users.

The system objects in a Mimer SQL database are: databanks, idents, schemas and shadows. A system object is owned by the ident that created it and only the creator of the object can drop it.

Private Objects

Private objects belong to a schema. Private object names are local to a schema, so two different schemas may contain an object with the same name. It is also possible to have objects with the same name in a schema, if they are of different types.

The private objects in a Mimer SQL database are collations, domains, functions, indexes, modules, precompiled statements, procedures, sequences, synonyms, tables, triggers, and views.

Private objects are usually fully identified by their qualified name, which is the name of the schema to which they belong and the name of the object in the following form: schema.object, see Qualified Object Names.

Routines may exist in multiple versions having the same name. See Mimer SQL Programmer's Manual, Parameter Overloading.

Conflicts arising from the use of the same object name in two different schemas are avoided when the qualified name is used. If a private object name is specified without explicit reference to its schema, it is assumed to belong to a schema with the same name as the current ident.

The Data Dictionary

The data dictionary contains information on all the database objects stored in a Mimer SQL database and how they relate to one another.

The data dictionary stores information about:

Databanks, see Databanks

Idents, see Idents

Schemas, see Schemas

Tables and Views, see Tables

Indexes, see Primary Keys and Indexes

Functions and procedures, see Routines – Functions and Procedures

Modules, see Modules

Synonyms, see Synonyms

Triggers, see Triggers

Shadows, see Shadows

Sequences, see Sequences

Collations, see Collations

Domains, see Domains

Precompiled statements, see Precompiled Statements

Access rights and privileges, see Privileges.

Databanks

A databank is the physical file where a collection of tables is stored. A Mimer SQL database can contain any number of databanks.

There are two types of databanks; system databanks and user databanks.

System Databanks

System databanks contain system information used by the database manager. These databanks are defined when the system is created.

The system databanks are:

SYSDB, containing the data dictionary tables

TRANSDB, used for transaction handling

LOGDB, used for transaction logging

SQLDB, used in transaction handling and for temporary storage of internal work tables.

User Databanks

User databanks contain the user tables. These databanks are defined by the user(s) responsible for setting up the database. See Specifying the Location of User Databanks for details concerning path names for user databank files.

The division of tables between different user databanks is a physical file storage issue and does not affect the way the database contents are presented to the user. Except in special situations (such as when creating tables), databanks are completely invisible to the user.

Note:Backup and restore in Mimer SQL can be performed on a per-databank basis rather than on entire database basis. See the System Management Manual, Backing-up and Restoring Data for more information.

Specifying the Location of User Databanks

The location for a user databank file can be specified completely (as an absolute path name) or with some of the path name components omitted (a relative path name).

The default values used for omitted path name components are taken from the path name for the system databank file SYSDB, which is located in the database home directory.

Note:The databank location stored in the Mimer SQL data dictionary is the path name as explicitly specified, i.e. without the addition of default values for any omitted path name components. Such additions are determined and added each time the file is accessed.

Refer to the System Management Manual, The Database Environment for recommendations concerning databank file management and for information on how the path name for a databank file is determined.

Idents

An ident is an authorization-id used to identify users, programs and groups. The different types of idents in a Mimer SQL database are USER, PROGRAM and GROUP idents.

USER Idents

USER idents identify individual users who can connect to a Mimer SQL database.

A USER ident's access to the database is usually protected by a password, and is also restricted by the specific privileges granted to the ident. USER idents are generally associated with specific physical individuals who are authorized to use the system.

For a USER ident it is possible to add one or several OS_USER logins which allows the user currently logged in to the operating system to access the Mimer SQL database without providing a password.

For example: if the current operating system user is ALBERT and there is an OS_USER login called ALBERT for an ident in Mimer SQL, ALBERT may start Mimer BSQL (for example) and connect directly to Mimer SQL simply by giving the ident name at the Username: prompt and press <return> at the password: prompt.

If the ident name is the same as the OS_USER login no ident name needs to be given, it is sufficient to press <return> at the username: prompt.

A USER ident may be defined without a password and in that case it is only possible to connect to Mimer SQL by using the OS_USER login. Dropping and adding password and OS_USER logins is done with ALTER IDENT statement.

PROGRAM Idents

PROGRAM idents do not strictly connect to Mimer SQL, but they may be entered from within an application program by using the ENTER statement.

The ENTER statement may only be used by an ident who is already connected to a Mimer SQL database.

An ident is granted the privilege to enter a PROGRAM ident. A PROGRAM ident is set up to have certain privileges and these apply after the ENTER statement has been used.

PROGRAM idents are generally associated with specific functions within the system, rather than with physical individuals.

The LEAVE statement is used to return to the state of privileges and database access that existed before ENTER was used.

GROUP Idents

GROUP idents are collective identities used to define groups of USER and/or PROGRAM idents.

Any privileges granted to or revoked from a GROUP ident automatically apply to all members of the group. Any ident can be a member of as many groups as required, and a group can include any number of members.

GROUP idents provide a facility for organizing the privilege structure in the database system. All idents are automatically members of a logical group which is specified in Mimer SQL statements by using the keyword PUBLIC.

Schemas

A schema defines a local environment within which private database objects can be created. The ident creating the schema has the right to create objects in it and to drop objects from it.

When a USER or PROGRAM ident is created, a schema with the same name is automatically created by default, and the created ident becomes the creator of the schema. This happens unless WITHOUT SCHEMA is specified in the CREATE IDENT statement. For idents who are not supposed to create database objects, it’s good practice to specify WITHOUT SCHEMA.

When a private database object is created, the name for it can be specified in a fully qualified form which identifies the schema in which it is to be created. The names of objects must be unique within the schema to which they belong, according to the rules for the particular object-type.

If an unqualified name is specified for a private database object, a schema name equivalent to the name of the current ident is assumed.

Tables

Data in a relational database is logically organized in tables, which consist of horizontal rows and vertical columns. Columns are identified by a column-name. Each row in a table contains data pertaining to a specific entry in the database. Each field, defined by the intersection of a row and a column, contains a single item of data.

Each row in a table must have the same set of data items (one for each column in the table), but not all the items need to be filled in. A column can have a default value defined (either as part of the column specification itself or by using a domain with a default value) and this is stored in a field where an explicit value for the data item has not been specified.

If no default value has been defined for a column, the null value is stored when no data value is supplied (the way the null value is displayed depends on the application – in Mimer BSQL the minus sign is used).

A relational database is built up of several inter-dependent tables which can be joined together. Tables are joined by using related values that appear in one or more columns in each of the tables.

Part of the flexibility of a relational database structure is the ability to add more tables to an existing database. A new table can relate to an existing database structure by having columns with data that relates to the data in columns of the existing tables. No alterations to the existing data structure are required.

All data in a column contains information of one data type. The data type determines which data that can be stored in a column also the maximum length of the data. A data type may either be of fix or varying length. A fix data type will always use the same amount of physical space whereas a varying type only uses as much space as is needed. More information about data types can be found in Data Types in SQL Statements.

Base Tables and Views

The logical representation of data in a Mimer SQL database is stored in tables. This is what the user sees, as distinct from the physical storage format which is transparent to the user.

The tables which store the data are referred to as base tables. Users can directly examine data in the base tables.

In addition, data may be presented in views, which are created from specific parts of one or more base tables. To the user, views may look the same as tables, but operations on views are actually performed on the underlying base tables.

Access privileges on views and their underlying base tables are completely independent of each other, so views provide a mechanism for setting up specific access to tables.

The essential difference between a table and a view is underlined by the action of the DROP command, which removes objects from the database. If a table is dropped, all data in the table is lost from the database and can only be recovered by redefining the table and re-entering the data. If a view is dropped, however, the table or tables on which the view is defined remain in the database, and no data is lost. Data may, however, become inaccessible to a user who was allowed to access the view but who is not permitted to access the underlying base table(s).

Note:Since views are logical representations of tables, all operations requested on a view are actually performed on the underlying base table, so care must be taken when granting access privileges on views.

Such privileges may include the right to insert, update and delete information. As an example, deleting a row from a view will remove the entire row from the underlying base table and this may include table columns the user of the view had no privilege to access.

Views may be created to simplify presentation of data to the user by including only some of the base table columns in the view or only by including selected rows from the base table. Views of this kind are called restriction views.

Views may also be created to combine information from several tables (join views). Join views can be used to present data in more natural or useful combinations than the base tables themselves provide (the optimal design of the base tables will have been governed by rules of relational database modeling). Join views may also contain restriction conditions.

Primary Keys and Indexes

Rows in a base table are uniquely identified by the value of the primary key defined for the table. The primary key for a table is composed of the values of one or more columns. A table cannot contain two rows with the same primary key value. (If the primary key contains more than one column, the key value is the combined value of all the columns in the key. Individual columns in the key may contain duplicate values as long as the whole key value is unique).

Other columns may also be defined as UNIQUE. A unique column is also a key, because it may not contain duplicate values, and need not necessarily be part of the primary key.

The columns of the primary key may not contain null values (this is one of the requirements of a strictly relational database).

Primary keys and unique columns are automatically indexed to facilitate effective information retrieval.

Other columns or combinations of columns may be defined as a secondary index to improve performance in data retrieval. Secondary indexes are defined on a table after it has been created (using the CREATE INDEX statement).

A secondary index may be useful when, for example, a search is regularly performed on a non-keyed column in a table with many rows, then defining an index on the column may speed up the search. The search result is not affected by the index but the speed of the search is optimized.

It should be noted, however, that indexes create an overhead for update, delete and insert operations because the index must also be updated.

An index can be used in select statements as an ordinary table, but explicit write operations on indexes are not allowed. There is no guarantee that the presence of an index will actually improve performance because the decision to use it or not is made by the internal query optimization process.

SQL queries are automatically optimized when they are internally prepared for execution. The optimization process determines the most effective way to execute the query and in some cases optimal query execution may not actually involve using an index.

Stored Procedures

In Mimer SQL you can define functions, procedures and modules, collectively known as stored procedures.

Mimer SQL stored procedures enable you to define and use powerful functionality through the creation and execution of routines. By using stored procedures, you can move application logic from the client to the server, thereby reducing network traffic.

Stored procedures are stored in the data dictionary and you can invoke them when needed.

For a complete and detailed discussion of stored procedures, see Mimer SQL Programmer's Manual, Mimer SQL Stored Procedures.

Mimer SQL stored procedures are based on the ISO standard for Persistent Stored Modules (PSM).

Routines – Functions and Procedures

The term routine is a collective term for functions and procedures. Functions are distinguished from procedures in that they return a single value and the parameters of a function are used for input only. A function is invoked by using it where a value expression would normally be used.

Mimer SQL supports standard procedures and also result set procedures, which are procedures capable of returning the row value(s) of a result set.

Standard procedures are invoked directly by using the CALL statement and can pass values back to the calling environment through the procedure parameters.

In embedded SQL, result set procedures are invoked by declaring a cursor which includes the procedure call specification and by then using the FETCH statement to execute the procedure and return the row(s) of the result set.

In interactive SQL, a result set procedure is invoked by using the CALL statement directly and the result set values are presented in the same way as for a SELECT returning more than one row.

The creator of a routine must hold the appropriate access rights on any database objects referenced from within the routine. These access rights must remain as longs as the routine exists.

Routine names, like those of other private objects in the database, are qualified with the name of the schema to which they belong.

Modules

A module is simply a collection of routines. All the routines in a module are created when the module is created and belong to the same schema.

EXECUTE rights on the routines contained in a module are held on a per-routine basis, not on the module.

If a module is dropped, all the routines contained in the module are dropped.

Under certain circumstances a routine may be dropped because of the cascade effect of dropping some other database object. If such a routine is contained in a module, it is implicitly removed from the module and dropped. The other routines contained in the module remain unaffected.

In general, care should be taken when using DROP or REVOKE in connection with routines, modules or objects referenced from within routines because the cascade effects can often affect many other objects.

For more information, see Mimer SQL User's Manual, Dropping Objects from the Database, and the Mimer SQL User's Manual, Revoking Privileges.

Synonyms

A synonym is an alternative name for a table, view or another synonym. Synonyms can be created or dropped at any time.

Using synonyms can be a convenient way to address tables that are contained in another schema.

For example, if a view customer_details is contained in the schema mimer_store, the full name of the view is mimer_store.customer_details.

This view may be referenced from another schema mimer_store_book by its fully qualified name as given above. Alternatively, a synonym may be created for the view in schema mimer_store_book, e.g. cust_details. Then the name cust_details can simply be used to refer to the view.

Shadows

Mimer SQL Shadowing is a separate product you can use to create and maintain one or more copies of a databank on different disks. Shadowing provides extra protection from the consequences of disk crashes, etc. Shadowing requires a separate license.

Read more in the System Management Manual, Mimer SQL Shadowing.

Triggers

A trigger defines a number of procedural SQL statements that are executed whenever a specified data manipulation statement is executed on the table or view on which the trigger has been created.

There are two types of triggers, row triggers and statement triggers. A row trigger is executed once for each row that is modified by a data manipulation operation. A statement trigger is invoked once for a data manipulation operation.

The trigger can be set up to execute AFTER, BEFORE or INSTEAD OF the data manipulation statement. Trigger execution can also be made conditional on a search condition specified as part of the trigger.

Read more in the Mimer SQL Programmer's Manual, Triggers.

User-Defined Types and Methods

With user-defined types, it is possible to create new data types that can be used in table definitions and stored procedures.The data type used in a user-defined type definition may be a predefined data type or another user-defined type.

It is possible to define methods for a user-defined type. Methods are very similar to functions, they have only in parameters and return a single value. There are three different types of methods, constructor, instance and static methods.

Constructor methods are used to create new instances of a user-defined type. An instance method can only be used with an instance of a user-defined type. A static method is similar to a function, the only difference is how they are invoked. Both instance and constructor methods have an implicit parameter named SELF which represents an instance of a user-defined type.

It is possible to alter a user-defined type by adding or dropping methods.

An ident can use a user-defined type created by another ident, if the user has been granted usage privilege on the user-defined type. Likewise, in order to be able to use a method the user must have been granted execute privilege on the method.

Sequences

A sequence is a database object that provides a series of integer values.

A sequence has a start value, an increment step value and a minimum value and a maximum value defined when it is created (by using the CREATE SEQUENCE statement).

A sequence can be specified as having a certain data type which will determine the span of possible values for the sequence. The possible data types are SMALLINT, INTEGER and BIGINT.

A sequence with CYCLE option will generate its series of values repeatedly.

A sequence with NO CYCLE becomes exhausted when the end value has been used, and can not be used any more. (An exhausted sequence can be reset using the ALTER SEQUENCE statement.)

A sequence is created with an undefined value initially.

To generate the next value in the integer series of a sequence the NEXT VALUE function is used, see NEXT VALUE. When this expression is used for the first time after the sequence has been created, it establishes the initial value for the sequence. Subsequent uses will establish the next value in the series of integer values of the sequence as the current value of the sequence.

It is also possible to get the current value of a sequence by using the CURRENT VALUE function, see CURRENT VALUE. This function can not be used until the initial value has been established for the sequence (by using NEXT VALUE for the first time).

If a sequence is dropped with the CASCADE option in effect, column defaults referencing the sequence will be removed, but the columns will still exist. Similarly domain defaults referencing the sequence will be removed, but the domains will still exist. Other objects referencing the sequence will be dropped.

Precompiled Statements

A precompiled statement is a named query that can be executed by using this name. The query must be a DML statement, i.e. DELETE, INSERT, SELECT or UPDATE, or a SET or CALL statement. When the statement is created a compiled version of the query is stored in the data dictionary. Precompiled statements are primarily intended for use in mobile and embedded environments in which no SQL compiler is available due to limited memory resources.

Mimer SQL Character Sets

For character data, Mimer SQL uses the character set ISO 8859-1, also known as the Latin1 character set. By default, character data is sorted in the numerical order of its code according to the ISO8BIT collation.

For national character data, Mimer SQL uses the Unicode character set, which is a universal character set, see https://www.unicode.org for more information. National character data is sorted according to the UCS_BASIC collation. UCS_BASIC is a collation in which the ordering is determined entirely by the Unicode scalar values of the characters in the strings being sorted.

See the Mimer SQL Reference Manual, Character Sets for more information.

Collations

As stated in the previous section, character and national character data is sorted according to specific collations.

A collation, also known as a collating sequence, is a database object containing a set of rules that determines how character strings are compared, searched and alphabetically sorted. The rules in the collation determine whether one character string is less than, equal to or greater than another. A collation also determines how case-sensitivity and accents are handled.

You can specify a collation for ordering characters when you create or alter a table or create a domain.

If you have specified a collation for a column, the collation is used implicitly in SQL statements.

You only need to explicitly use a collate clause in SQL statements if you want to override the default collation or the collation you specified when creating or altering the column or creating the domain.

For more information, see Mimer SQL User's Manual, Collations.

Data Integrity

A vital aspect of a Mimer SQL database is data integrity. Data integrity means that the data in the database is complete and consistent both at its creation and at all times during use.

Mimer SQL has built-in facilities to ensure the data integrity in the database:

Primary keys and unique keys

Foreign keys (also referred to as referential integrity)

Domains

Check constraints in table definitions

Check options in view definitions

These features should be used whenever possible to protect the integrity of the database, guaranteeing that incorrect or inconsistent data is not entered into it. By applying data integrity constraints through the database management system, the responsibility of ensuring the data integrity of the database is moved from the users of the database to the database designer.

Primary Keys and Unique Keys

Rows in a base table are uniquely identified by the value of the primary key defined for the table. The primary key for a table is composed of the values of one or more columns. A table cannot contain two rows with the same primary key value. If the primary key contains more than one column, the key value is the combined value of all the columns in the key. Individual columns in the key may contain duplicate values as long as the whole key value is unique.

Apart from a primary key constraint its also possible to add one or more unique constraints. The primary key constraint and the unique constraint are similar, but treat null values in different ways. A null value can never be stored in a primary key column, but a unique constraint column can contain null values.

The definition of the primary key is also a definition of the most effective access path for the table.

Foreign Keys – Referential Integrity

A foreign key is one or more columns in a table defined as cross-referencing the primary key or a unique key of a table.

Data entered into the foreign key must either exist in the key that it cross-references or be null. This maintains referential integrity in the database, ensuring that a table can only contain data that already exists in the selected key of the referenced table.

As a consequence of this, a key value that is cross-referenced by a foreign key of another table must not be removed from the table to which it belongs by an update or delete operation if this ultimately violates the referential constraint.

The DELETE rule defined for the referential constraint provides a mechanism for adjusting the values in a foreign key in a way that may permit a cross-referenced key value to effectively be removed.

Similarly, the UPDATE rule defined for the referential constraint provides a mechanism for adjusting the values in a foreign key in a way that may permit a cross-referenced key value to effectively be updated.

Note:The referential integrity constraints are effectively checked at the end of an INSERT, DELETE or UPDATE statement, or at COMMIT depending on whether the constraint is declared as IMMEDIATE or DEFERRED.

Foreign key relationships are defined when a table is created using the CREATE TABLE statement and can be added to an existing table by using the ALTER TABLE statement.

The cross-referenced table must exist prior to the declaration of foreign keys on that table, unless the cross-referenced and referencing tables are the same.

If foreign key relationships are defined for tables in a CREATE SCHEMA statement, it is possible to reference a table that will not be created until later in the CREATE SCHEMA statement.

Note:Both the table containing the foreign key and the cross-referenced table must be stored in a databank with either the TRANSACTION or LOG option.

Domains

Each column in a table holds data of a single data type and length, specified when the column is created or altered. The data type may be specified explicitly, e.g. CHARACTER(20) or INTEGER, or through the use of domains, which can give more precise control over the data that will be accepted in the column.

A domain definition consists of a data type with optional check conditions and an optional default value. Data which falls outside the constraints defined by the check conditions is not accepted in a column which is defined using the domain. If a variable or parameter in a stored routine is defined as a domain with a check constraint, it is not possible to assign the parameter or variable a value which is allowed by the check constraint.

A column defined using a domain for which a default value is defined will automatically receive that value if row data is entered without a value being explicitly specified.

A variable in a stored routine or trigger declared using a domain for which a default value is defined will automatically receive that value unless an explicit default clause is present in the declaration.

In order for an ident to create a table containing columns whose data type is defined through the use of a domain, the ident must first have been granted USAGE rights on it, see the Mimer SQL User's Manual, Granting Privileges.

Check Constraints

Check constraints may be specified in table and domain definitions to make sure that the values in a table row conform to certain conditions. See the Mimer SQL User's Manual, Check Constraints for more information.

Check Options in View Definitions

You can maintain view integrity by including a check option in the view definition. This causes data entered through the view to be checked against the view definition. If the data conflicts with the conditions in the view definition, it is rejected.

Privileges

Privileges control users' access to database objects and the operations they can perform in the database.

USER and PROGRAM idents are usually protected by a password, which must be given together with the correct ident name in order for a user to gain access to the database or to enter a program ident. (Alternatively, an OS_USER login can be used to login without providing a password.)

Passwords are stored in encrypted form in the data dictionary and cannot be read by any ident, including the system administrator. A password may only be changed by the ident to which it belongs or by the creator of the ident.

A set of access and privileges define the operations each ident is permitted to perform. There are three classes of privileges in a Mimer SQL database:

system privileges

object privileges

access privileges.

System Privileges

System privileges control the right to perform backup and restore operations, the right to execute the UPDATE STATISTICS statement as well as the right to create new databanks, idents, schemas and to manage shadows.

System privileges are granted to the system administrator when the system is installed and may be granted by the administrator to other idents in the database. As a general rule, system privileges should be granted to a restricted group of users.

Note:An ident who is given the privilege to create new idents is also able to create new schemas.

Object Privileges

Object privileges control membership in group idents, the right to invoke functions and procedures, the right to enter program idents, the right to create new tables or sequences in a specified databank and the right to use a domain or sequence.

The creator of an object is automatically granted full privileges on that object.

Thus the creator of:

a group is automatically a member of the group

a function or procedure may execute the routine

a program ident may enter the program ident

a schema may create objects in and drop objects from the schema

a databank may create tables and sequences in the databank

a table has all access rights on the table

a domain may use that domain

a sequence may use that sequence.

The creator of an object generally has the right to grant any of these privileges to other users. In the case of functions and procedures, this actually depends on the creator's access rights on objects referenced from within the routine.

Access Privileges

Access privileges define access to the contents of the database, i.e. the rights to retrieve data from tables or views, delete data, insert new rows, update data and to refer to table columns as foreign key references.

About Privileges

Granted privileges can be regarded as instances of grantor/privilege stored for an ident. An ident will hold more than one instance of a privilege if different grantors grant it.

A privilege will be held as long as at least one instance of that privilege is stored for the ident. All privileges may be granted with the WITH GRANT OPTION which means that the receiver has, in turn, the right to grant the privilege to other idents. An ident will hold a privilege with the WITH GRANT OPTION as long as at least one of the instances stored for the ident was granted with this option.

If the same grantor grants a privilege to an ident more than once, this will not result in more than one instance of the privilege being recorded for the ident. If a particular grantor grants a privilege without the WITH GRANT OPTION and subsequently grants the privilege again with the WITH GRANT OPTION, the WITH GRANT OPTION will be added to the existing instance of the privilege.

Each instance of a privilege held by an ident is revoked separately by the appropriate grantor. It is possible to revoke the WITH GRANT OPTION without revoking the associated privilege completely. See the Mimer SQL User's Manual, Defining Privileges for more information.