Databases stores the data based on the schema definition. Understand the structure by reading about:
In DbSchema you an add a new table using the table menu button or using
the New Table option in the Layout Pane pop-up menu.
Double-click the table header in diagram to edit.
In the Table Editor you can add, edit, order or remove Columns, Indexes, Foreign Keys or Constraints.
Here can be set only primary keys over one column. To set composite primary keys ( using multiple columns ) please use the second tab 'Pk and Idexes' in the Table Dialog.
To edit a column just double-click it in the layout diagram.
The Primary Keys are in fact unique indexes over mandatory ( not null ) columns. A primary key is an unique identifer for each table record.
You may ask if is possible to create two Primary Keys for one table. No, this is not supported by databases. But you can get the same if you create an unique index ( the columns should be mandatory ).
The referred column has to be a primary key or unique column.
The foreign key enforces that the referencing column can store only values existing in the referred column.
The columns which have a foreign key ( in or out ) are marked with a small arrow on the right side of the column. Clicking it you can add or view the table on the other end of the foreign key.
If the referencing column is NULL, the check will not validate this value. NULLs are allowed in this column only if the column is not mandatory.
Double-clicking any of the foreign key lines in the layout will open the Foreign Key editor. One foreign key must have at least
one pair of columns. The columns has to have the same data type. The foreign key can be defined over two or three pair of columns
( composed foreign key ). Press the small Add button to add further columns.
|Referencing columns (from)||Foreign Key Type||Representation (See Layout Menu - Fk Notation)|
|no||no||1:n (one to many)||dashed line, 3-lines foot|
|no||yes||1:0 or 1 (one to zero or one)||dashed line|
|yes||no||1:many (one to many)||3-lines foot|
|yes||yes||1:1 (one to one)|
Or in Barker Notation ( you can set the notation from the Layout Menu ) :
The many-to-many relationships cannot be implemented in the physical model unless you use an intermediate table.
Sample: To establish a many-to-many relationship between FLIGHTS and PEOPLES we may use an intermediate table PERSON_FLIGHTS. We may use two foreign keys PERSON_FLIGHTS( flightid ) to FLIGHTS( flightid ) and PERSON_FLIGHTS( personid ) to PERSONS( personid ).
For the case one or more records from the primary key ( referred ) column are deleted or updated, different actions can be set for each foreign key:
A Foreign Key can be virtual and won't be implemented in the database. The foreign key is saved in the project file. It can be used in Queries or Data Editor to simulate a real foreign key.
Composite foreign keys are involving two or more columns on each side. In this case in the Foreign Key Editor there will be more columns listed, and each of the column value will match the value in the referred table. The primary key or unique index in the referred table will be defined as well on multiple columns.
Hint: Always use meaning-full names for constraints. For the constraint age > 18 use the name 'CheckAgeOver18'. If a user may try to insert the value 14 in the field age, he will get back in DbSchema or software 'Error: Constraint CheckAgeOver18 failed' which is easy to understand. If you name the constraint like 'Check214' you can imagine what he can understand from 'Error: Constraint Check124 failed'.
Constraints are useful to enforce data integrity, eq. have no incorrect data. Mistakes can occur via human errors when data is entered, computer or software errors, etc. Setting constraints may save lot of troubles in the software.
Foreign Key are also constraints. They are enforced via internal database triggers, so each time a new record is inserted or verified the inserted record is verified against the trigger condition.
Views can be a clean way for the programmers to move their queries inside the database. Instead of keeping complex SELECTS in the application logic, they create views. Their statement will be saved in the database. Than they can do some more efficient testing of the database, by running the checks for the database data and for views at the same time.
In the View Editor you have the chance to edit and test the query view statement. The view columns are automatically generated after testing the view query against the database.
MySql does not have sequences, he uses IDENTITY columns instead. By insert this columns doesn't require any value. They are automatically filled by the database.
Sample: in the table NAMES( ID integer identity, NAME varchar(100)) you can do 'INSERT INTO NAMES( NAME ) VALUES ( 'Lulu')'. The ID will be filled in by the database.
For Oracle you have to create a sequence. DbSchema will execute 'CREATE SEQUENCE MYSEQ' in the database. For the table NAMES you will to than 'INSERT INTO NAMES( ID, NAME ) VALUES ( MYSEQ.nextval, 'Lulu')'
Procedures can do some operations in the database without returning any value. Functions will compute something and return a value. Triggers are fired by INSERT, UPDATE or DELETE operations in the database. Usually you can do COMMIT or ROLLBACK only in procedures. Functions or triggers can't do this, the operation which calls them have to commit or rollback.
Procedure, trigger and functions are written in a database specific language. If you decide to convert the schema from one database to another, you have to re-write them.