Creating a database table via MySQL Workbench with SQL scripts 

This section will show you how to create the tables that we discussed earlier in the section via scripts: 

  1. Open MySQL Workbench.
  2. Connect to your local instance or a dev/test instance. Don't perform any of these steps on a live production server
  3. Click the New SQL script button:

  1. Copy or type the following script into the script window and click the run button:
CREATE TABLE yourschema.`teams3` (
`teamkey` smallint NOT NULL AUTO_INCREMENT,
`teamid` char(3) NOT NULL,
`yearid` year(4) NOT NULL,
`leagueid` char(2) NOT NULL,
`teamrank` tinyint NOT NULL,
PRIMARY KEY (`teamkey`),
UNIQUE KEY `teamkey_UNIQUE` (`teamkey`),
KEY `teamid_yearid_leagueid_UNIQUE` (`teamid`,`yearid`,`leagueid`),
CONSTRAINT `check_teamrank` CHECK (((`teamrank` >= 0) and (`teamrank` <= 12))),
CONSTRAINT `check_year` CHECK (((`yearid` >= 1871) and (`yearid` <= 2155))));
  1. Once the teams table is successfully created, run the following script to create the managers table:
CREATE TABLE yourschema.`managers` (
`managerkey` smallint NOT NULL AUTO_INCREMENT,
`playerid` varchar(9) NOT NULL,
`yearid` year(4) NOT NULL,
`teamid` char(3) NOT NULL,
`games` tinyint NOT NULL,
`wins` tinyint NOT NULL,
`losses` tinyint NOT NULL,
`alsoplayer` char(1) NOT NULL,
PRIMARY KEY (`managerkey`),
UNIQUE KEY `managerkey_UNIQUE` (`managerkey`),
UNIQUE KEY `playerid_yearid_teamid_UNIQUE` (`playerid`,`yearid`,`teamid`),
KEY `FK_teamid_idx` (`teamid`),
CONSTRAINT `FK_teamid` FOREIGN KEY (`teamid`) REFERENCES `teams` (`teamid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `check_alsoplayer` CHECK ((`alsoplayer` in (_utf8mb4'Y',_utf8mb4'N'))),
CONSTRAINT `check_games` CHECK (((`games` >= 0) and (`games` <= 165))),
CONSTRAINT `check_losses` CHECK (((`losses` >= 0) and (`losses` <= 165))),
CONSTRAINT `check_manager_year` CHECK (((`yearid` >= 1871) and (`yearid` <= 2155))),
CONSTRAINT `check_wins` CHECK (((`wins` >= 0) and (`wins` <= 165))));

Now, you have the teams and the managers tables set up as per the specifications outlined in the previous section. 

Creating databases and tables is slightly different in each of the other RDMSes. The interfaces are different in Oracle, PostgreSQL, and SQL Server, and the SQL used to create databases and tables could also be slightly different. 
..................Content has been hidden....................

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