<<

Create and Populate a with MySQL Workbench Dr. Demuynck Table of Content

• Slide 3: • An annotated of the MySQL script environment • Code to create a temporary database, use the database and create a single table • Slide 4: Populate a table with an INSERT statement • Remaining slides: Populate a table using the grid view - annotated • Slide 5: Access the grid and enter • Slide 6: MySQL generates INSERT code • Slide 7: Finalize the process • Slide 8: View and understand the result • Feedback? Please email feedback to [email protected] New SQL Open and SQL Save SQL Execute SQL Execute SQL script Create Initial FILE button FILE button FILE button script button under button Dababase and a Single Table

As soon as you create a new Code to create a database database or a new table, the and a table in the database in schemas window is updated. a new SQL script file. You may need to use the refresh Note the following: before it becomes visible. • Can create a DB only once • The USE temp statement that sets temp as the current database • Can create a table only once • The text in grey are comments. Ignored by SQL, but important to us In this window you find feedback • For creating the first on your code: if it worked, or if database and tables, there was an error. The share please use the SQL create table creation was successful. table statement Populate a Table with Data Using INSERT

Code to populate a table with INSERT statement. Note the following: • This statement creates a single record/tuple in the table. • You need such an INSERT statement for each record you need in the table. • If you try to insert a record with a duplicate , if gives an error.

Error generated when attempting to insert a record with a duplicate primary key. Enter Records with Grid Data Entry

• Steps: 1. Hover over the table (in this case share), and click on table grid next to the table name. The grid view opens. 2. Click on the cell in the left-most to start data entry. 3. You can enter as many rows as you want. 4. NOTE: IMPORTANT: typing records in the grid does NOT automatically save the data to the table: extra steps are required (next slides)

Start data entry Table grid button Populate Table with Data in Grid

Data entered in the grid, an • Steps: example 1. Four new records were entered in the grid. 2. To save those data to the share table, click the Apply button on the bottom right of the grid. 3. In the next pop-up window, you will see the SQL INSERT code generated for the data you typed in the grid. Click the Apply button to save the data to the share table. 4. MySQL executes the code, and your data are now in the table (next slide) Click the Apply button to execute the generated SQL codes Finalize It

• Steps 1. In the next pop-up window, finalize the execution of the SQL statements: click Finish 2. MySQL will automatically generate a SELECT statement in a new tab to show all the data in the table. 3. You can execute the statement and verify all your tuples are present. 4. You can repeat the process as often as needed to add more data as needed

Click the Finish button to complete the process. Check the Results …

Check out the generated SQL SELECT code, in a new tab

Check out the new data in the results grid

Check out the feedback on execution: 5 rows returned

Now you are ready to do a lot of things: • Add more data • Write new SELECT queries, following the examples in your book or creating your own • Save your own SELECT queries in an SQL file and save it. • Add your name and notes on what queries do as comments