Relational Database Tutorial - .Relational Database Tutorial Contents Create First Tables ...

download Relational Database Tutorial -  .Relational Database Tutorial Contents Create First Tables ...

of 20

  • date post

    31-Jan-2018
  • Category

    Documents

  • view

    246
  • download

    0

Embed Size (px)

Transcript of Relational Database Tutorial - .Relational Database Tutorial Contents Create First Tables ...

  • Relational Database Tutorial

    Contents Create First Tables ................................................................................................................................... 2

    Foreign Keys ........................................................................................................................................ 7

    Indexes ................................................................................................................................................ 9

    Generate Random Data ......................................................................................................................... 11

    Relational Data Browse ......................................................................................................................... 11

    DbSchema Project File ........................................................................................................................... 13

    Schema Synchronization ....................................................................................................................... 15

    DbSchema Layouts ................................................................................................................................ 16

    Visual Query Builder .............................................................................................................................. 17

    The Query Editor ................................................................................................................................... 18

    Forms and Reports ................................................................................................................................ 19

    Technical Support .................................................................................................................................. 20

    End ......................................................................................................................................................... 20

    In this tutorial you will get an introduction into relational databases. Among the tutorial we will use

    the DbSchema designer tool. Download, install and open DbSchema from

    http://www.dbschema.com. DbSchema can be used on trial base 15 days for free.

    http://www.dbschema.com/

  • Create First Tables

    Start DbSchema and you should get this screen. Choose Connect to Database.

    We are going to connect to a H2 database. We will use this because H2 requires no other software to

    be installed. For MySql, MariaDb, Postgresql, etc., the database software should be installed as well.

    If you wish to do this please check in DbSchema Help, in the Databases and How to Connect there

    are some tips, they will help to install the database software end enable network connections so you

    can connect DbSchema to the database.

    Choose this

    H2 connection to file database

    Press new to create a new database in a folder

  • In the connection dialog choose first H2 database. This will prompt to download the driver. Choose

    Download from DbSchema.

    Press Create New to create an empty database. In the next dialog you can choose or create an

    empty folder.

    Press Ok in the connection dialog.

    Next step is to choose a schema you will work on. A schema is similar with a group of the tables, so

    different applications may use the same database but different schemes. Information_schema is an

    internal H2 schema, with tables used by H2 itself.

    Press ok and we should get a screen like bellow. The database is empty, no table is created. Create a

    table by right-clicking the layout (the large free space) and Create table.

    Create new folder

  • In our tutorial we are going to create two tables: Customers and Employees. Each table will have few

    columns.

    Customers

    Customerid a number, to identify each customer. For example a customer Targot will have

    the customerid 100, and each time we reffer the customerid 100 we know we talk about this

    customer. Using this Ids has the advantage that we can easy rename the customer and all

    other tables which point to it wont be changed.

    Name some text

    Description - text

    Employees

    EmployeeId similar with customerid, a number to identify each employee

    Firstname text

    Lastname test

    Birthdate date

    Companyid the id of the company he or she works for

    In the table dialog enter the first table name, than add one column at a time by pressing the add

    button.

    Right-click the layout and choose Create table.

  • When you will enter the table name as well as the column names, you will notice the text is

    automatically converted to upper-cases. This because the database is using by default uppercases for

    table and column names, and DbSchema convert them to show them as they are looking in the

    database.

    Lets add the first column CUSTOMERID. Enter the name, choose the data type Integer which means

    number. The checkbox Primary Key should be on, which means this column will unique identify any

    record from this table. Primary keys must always be specified. This means all records should have a

    value in this field and therefore the mandatory field is checked and cannot be un-checked.

    Enter the table name

    Add columns one by one

  • Similar we add the name and description as varchar (this is text) with maximal number of characters

    allowed 100 for name and 500 for description. The name should be mandatory in order to make sure

    no company is missing the name.

    After adding all columns the table will look like here:

    Or like this:

    Primary key and x for mandatory symbols

    Press this button to see the data type

  • Similar create the second table EMPLOYEES.

    Foreign Keys

    To prevent having employees with CUSTOMERID values which does not exists in the customer table,

    we are going to create a foreign key. The foreign key is a constraint, which means it is a logical

    verification of the data. This verification takes place each time a new record is inserted in the table or

    the data is modified.

    To create the foreign key, drag and drop the CUSTOMERID from EMPLOYEES over the CUSTOMERID

    in CUSTOMERS. We want that each EMPLOYEE.CUSTOMERID exists in the CUSTOMERS.CUSTOMERID.

    The foreign key dialog will open:

    Drag & drop column

  • The foreign key requires the referring table (employees) and referred table (customers) plus the

    columns for each of them. We can set an action to be taken if a customer is deleted. No action

    means nothing will be done. If we will try to drop a customer which has employees we will get back

    an error message. We can set on delete cascade and all employees belonging to a customer will be

    deleted when the company is deleted.

    Virtual means the foreign key is created only in DbSchema. In this case no data verification will be

    done in the database. The virtual foreign keys may be used only for design purposes or for relational

    data browse which will be explained later.

    A foreign key always require an index to exist on the referred columns (CUSTOMERID in customers

    table). If this column is a primary key will automatically get an index. Read more about indexes in the

    index chapter.

    About the foreign key impact on performance

    Is a common mistake to skip creating foreign keys because they slow down the database. This is not

    true. The foreign keys are a validation of the data which is done only when inserting or deleting new

  • rows. Usually the ID fields dont get modified, so the foreign key wont be triggered. The foreign key

    validation wont be done when updating other columns than the foreign key columns.

    Foreign keys are useful as they will prevent having bad data in the database. This is very important!

    Foreign keys are healthy!

    Indexes

    Database indexes are used to fasten searching of data by a given criteria. Consider a query SELECT *

    FROM EMPLOYEES WHERE FIRSNAME=JOHN. Without indexes the database will scan the complete

    data before finding the employees with this firstname. This may take time if the table contains many

    records. Having an index on FIRSTNAME will fasten the search.

    Indexes work similar with a book keyword index, which can be usually found in the end of a book. An

    index is a list of values in a column with a reference to the position where the values can be found in

    the table.

    Indexes duplicate the column data in a separate structure and hold for each distinct value a list of

    pointers to the table records. The index is synchronized by the database server each time the column

    data is modified, inserted or deleted.

    Lets create an index on EMPLOYEES.FIRSTNAME. Right-click the FIRSTNAME column and choose Add

    Index.

  • An index can be normal, which means any value can be stored inside. Unique indexes do not allow

    duplicate values. When a value is inserted in the table field and that value exists in another record for

    the same field, the insert operation will throw an error and the operation is aborted.

    Primary Key indexes are the same as unique indexes plus the condition that the column must be

    mandatory (does not allow missing or empty values). A table can have at most one primary key, but if

    you need more you can create unique indexes and set the columns to b