Creating a database table via MySQL Workbench

Creating a database table using MySQL Workbench is relatively straightforward. Let's go through the following steps: 

  1. Open MySQL Workbench.
  2. Connect to your local instance or a dev/test instance by clicking on it in MySQL WorkbenchDon't perform any of these steps on a live production server:

  1. Click the down arrow next to the new schema that you created in the previous section:

  1. Right-click Tables and select Create Table...:

  1. This will bring up a tab to allow you to fill in the details of your table; there will be a lot of blank areas for you to fill in and it will have the default table name of new_table
  2. We will fill in these details with information from the following table: 

Table column name

Data type

Key or constraint

teamkey

SMALLINT(5)

Primary key autoincrementing

yearid

YEAR

Part of unique composite key
Not null constraint 

leagueid

CHAR(2)

Part of unique composite key
Not null constraint 

teamid

CHAR(3)

Part of unique composite key
Not null constraint 

teamrank

TINYINT(2)

Check that constraint is between 0 and 12
Not null constraint 

  1. First, we fill out the Columns tab so that it matches the following screenshot. Before hitting Apply, we need to use a couple more tabs; go through the following steps to see the information that we need for these tabs, along with the screenshots showing you the relevant information:

  1. Click on the Indexes tab so that we can add the Unique index on the teamid, leagueid, and yearid columns. Refer to the following screenshot for details:

  1. Click on Apply.
  2. Review the script to apply.
  3. Click on Apply.
  4. Click on Finish.
  5. Click the down arrow on the schema that you created in this chapter and click the down arrow on Tables. You should see the teams table in the list now:

  1. Click the New SQL script button:

  1. Run the following script to add the CHECK constraints:
ALTER TABLE yourschema.teams ADD CONSTRAINT check_year CHECK (yearid >=1871 and yearid <=2155) enforced; 
ALTER TABLE yourschema.teams ADD CONSTRAINT check_teamrank CHECK (teamrank >=0 and teamrank <=12) enforced;
  1. Click the New SQL script button.
  2. Right-click on the teams table and click on Send to SQL Editor and then Create Statement:

This will show you the following script for your table:

You can go through some similar steps to create the managers table, or you can proceed to the section on how to run the scripts to create the tables, since this will also include information on how to create the scripts for both tables. The following screenshots show how to set up the managers table, including the foreign key association for the teams table. 

We will set up the managers table in much the same way as the teams table, but we will add a foreign key reference from the managers table to the teams table. 

The following screenshot shows the setup for the columns in the managers table: 

The following screenshot shows the setup for the indexes in the managers table:

The following screenshot shows the setup for the foreign key reference for the teamid from the managers table to the teams table on teamid. Note that under Foreign Key Options, we are going to set On Update and On Delete to RESTRICT. This means that you won't be allowed to delete a team from the teams table unless you delete or update the corresponding manager record first:

Once the managers table is set up, add the check constraints with the following script: 

ALTER TABLE yourschema.managers 
ADD CONSTRAINT check_year CHECK (yearid >=1871 and yearid <=2155) enforced;

ALTER TABLE yourschema.managers
ADD CONSTRAINT check_games CHECK (games >= 0 and games <= 165) enforced;

ALTER TABLE yourschema.managers
ADD CONSTRAINT check_wins CHECK (wins >=0 and wins <=165) enforced;

ALTER TABLE yourschema.managers
ADD CONSTRAINT check_losses CHECK (losses >=0 and losses <=165) enforced;

ALTER TABLE yourschema.managers
ADD CONSTRAINT check_alsoplayer CHECK (alsoplayer = 'N' or alsoplayer = 'Y') enforced;

The next section will show you how to run the scripts to create both of the tables. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.113.193