MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite Purpose Week 01 SQL DDL classwork exercise will introduce you to structured query language by using data definition language commands to build a database that can house collected data (i.e., physical data model). This exercise should be completed early in Week 01 as a classwork foundation for subsequent exercises and tutorials. Pre-Requisites Prior to attempting this classwork exercise, a student should: a. Read sections 8.1, 8.2, and 8.3 of our text on data definition language commands. b. Successfully install SQLite and DBBrowser for SQLite on your computer. Refer to the Week 01 application installation guide for details. Have access to a text editor like Notepad. c. Review the Getting Started section on the SQLite URL http://www.sqlite.org/cli.html. d. Download the CreateLibDB.sql script file to a directory on your computer where you will be saving SQL scripts for use with SQLite. For ease of use, choose the directory where sqlite3.exe is located. e. Download the CreateLibDB_out.txt output file to same directory as the previous script file. Supported Learning Objectives This learning activity supports the following student learning objectives: Understand how to use structured query language (SQL) with respect to data manipulation language (DML) to quantify relational database data. Learning Activity The following steps will guide you and demonstrate the input and output you should have with the sqlite3 program in executing DDL commands to build physical database tables. Any of the screen shots may be viewed with greater magnification for clarity. 1) Using sqlite3.exe to create database tables: Launching sqlite3.exe will create a window with a command prompt to execute sqlite3 dot commands or SQL commands. 2) Enable foreign key checking in sqlite3 with the following SQL command: PRAGMA foreign_keys = ON; Note: SQL commands are always terminated using a semi-colon, unlike sqlite3 dot commands. MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite 3) Create the following two tables (tblVENDOR and tblPRODUCT) depicted in E-R diagram below, using the CREATE TABLE command 1,1 0,M Supplies CREATE TABLE tblVENDOR ( vcode char(3) constraint vend_vcode_pk primary key, vname varchar(35) ); Note: tblVENDOR is on the 1-side and is considered an independent table, so we create it first. If there is an error, an error will be displayed CREATE TABLE tblPRODUCT ( prodnum char(5) constraint prod_prodnum_pk primary key, prodname varchar (35), prodprice number(7,2), vcode char(3) constraint prod_vcode_fk references tblVENDOR (vcode) ); Note: tblPRODUCT is on the M-side and is considered a dependent table since it has a foreign key link to the primary key in tblVENDOR. We can only create dependent, M-side tables after tables with the primary key link are created. If there is an error, an error will be displayed 4) Check your work with the dot command tables: .tables 5) Enter data into the tables, using the INSERT command: (a) Insert data into the vendor table (always insert into 1-side first): MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite INSERT INTO tblVENDOR VALUES ('111', 'vendor1name'); Press enter (If there are errors, error messages will be listed) Repeat for vendor 2 ('222', 'vendor2name') and vendor 3 ('333', 'vendor3name') (b) Insert data into the product table (insert into dependent, M-side last): INSERT INTO tblPRODUCT VALUES ('AAAAA', 'ProductA', 888.88, '111'); Press enter, prompt should appear with no error INSERT INTO tblPRODUCT VALUES ('BBBBB', 'ProductB', 25.29, '444'); Note: Press enter and sqlite3 should give you an error. Why? tblVENDOR 444 does not exist and the sqlite3 DB engine could not establish a foreign key link for tblPRODUCT ProductB. INSERT INTO tblPRODUCT VALUES ('BBBBB', 'ProductB', 25.29, '222'); Note: Press enter and sqlite3 does not give you an error. Why? tblVENDOR 222 does exist and the sqlite3 DB engine could establish a foreign key link for tblPRODUCT ProductB to tblVENDOR for vcode = ‘222’. Note: When you want to delete all the data from a table, use DELETE FROM tblPRODUCT; 6) Viewing the contents of the two tables tblVENDOR and tblPRODUCT: SELECT * FROM tblVENDOR; MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite SELECT * FROM tblPRODUCT; 7) Discard created tables from temporary database: DROP TABLE tblPRODUCT; DROP TABLE tblVENDOR; (must drop dependent, M-side first) (drop independent, 1-side last) Since we did not tell the sqlite3 program to .open a specific database file, both tables tblVENDOR and tblPRODUCT were created in the temporary database that exists only in RAM memory. When we exit, the sqlite3 program discards the temporary database. The .open sqlite3 dot command creates or opens a persistent file on the computer’s hard disk as the database. If your hard disk file is not in the same directory as sqlite3.exe, then you will need to use a full pathname to locate the disk file in the directory where you saved it. 8) To exit sqlite3, type the dot command quit: .quit 9) Using DBBrowser for SQLite as a graphical user interface (GUI) for sqlite3: Launch DBBrowser for SQLite. Choose File, New In-Memory Database, then click the Cancel button on the resulting Edit Table Definition window. Choose the Edit Pragmas tab and check the Foreign Keys checkbox. Choose the Execute SQL tab. Type SQL commands into the Execute SQL tab. Execute the SQL commands with the execute all icon or execute line icon Reference a blank Execute SQL tab screen shot on the next page. . MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite Enter the SQL Commands from steps 3 to 7 into the Execute SQL tab window. These commands in combination are: CREATE TABLE tblVENDOR ( vcode char(3) constraint vend_vcode_pk primary key, vname varchar(35) ); CREATE TABLE tblPRODUCT ( prodnum char(5) constraint prod_prodnum_pk primary key, prodname varchar (35), prodprice number(7,2), vcode char(3) constraint prod_vcode_fk references tblVENDOR (vcode) ); INSERT INTO tblVENDOR VALUES ('111', 'vendor1name'); INSERT INTO tblVENDOR VALUES ('222', 'vendor2name'); INSERT INTO tblVENDOR VALUES ('333', 'vendor3name'); INSERT INTO tblPRODUCT VALUES ('AAAAA', 'ProductA', 888.88, '111'); INSERT INTO tblPRODUCT VALUES ('BBBBB', 'ProductB', 25.29, '222'); SELECT * FROM tblVENDOR; SELECT * FROM tblPRODUCT; Clicking the Execute all icon will execute each SQL command in order. Upon completion, the two tables are displayed on the right side of the DBBrowser screen under DB Schema. The top portion of the Execute SQL tab (SQL 1) will have the above SQL commands you entered. The middle portion of the Execute SQL tab will have the SELECT results of the last SELECT statement. The lower portion of the Execute SQL tab will have the results of last SQL command executed. The next page displays a screen shot of your expected results. MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite Note: You can view the contents of tables via the Browse Data tab. Note: The Database Structure tab displays the same contents as the DB Schema pane, but allows independent tables to be created or altered as well as unique indexes. To exit DBBrowser for SQLite, choose File and then Exit. The application will prompt you that existing without saving the contents of the temporary database will result in discarding your current data. 10) Creating a persistent DB from a hard disk script file CreateLib.sql: As noted with the previous DBBrowser example, SQL commands can be executed in batch if they are executed in a logical order. To accomplish a successful batch SQL command execution in sqlite3, you will need to place your SQL commands and dot commands in order within a script file. The script file will need to be created in a text editor such as Notepad or WordPad on a Windows computer. Do not use a Word processing application like MS Word to create SQL script files as they can embed control characters into the text. Placing SQL DDL commands and SQLite dot MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite commands in a script file allows a logical review of the commands as well as a persistent source to rebuild the database. CreateLib.sql is not a database. CreateLib.sql is an SQL script file, containing sqlite3 dot commands and SQL commands that create the database Lib.DB. Open CreateLib.sql with a text editor like Notepad or Wordpad to view its contents. The following is a screen shot from Notepad displaying the contents of CreateLibDB.sql. To execute CreateLib.sql in sqlite3 and create the Lib.DB database, you should place the script file in the same directory as sqlite3 or identify the directory path of where it is located. Launch sqlite3.exe. At the command prompt, type in the dot command read CreateLib.sql or read C: \directorypath\CreateLib.sql. Note: C:\directorypath is an alias for your computer’s directory path to CreateLib.sql. .read CreateLib.sql Note: .output DMS_LibSQL_out.txt directs sqlite3 to echo the execution of each dot command and SQL command to a text file named DMS_LibSQL_out.txt in the same directory as sqlite3.exe. Upon successful execution of the commands in the CreateLib.sql script file, you should be able to see the files Lib.DB and DMS_LibSQL_out.txt in the same directory as sqlite3 (unless you inserted paths to place them in another directory on your computer). The database Lib.DB can be accessed using SELECT statements via sqlite3 or DBBrowser for SQLite. The Lib.DB database will also be used in later Tutorials. MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite A copy of DMS_LibSQL_out.txt output file will be created why you run CreateLib.sql. CreateLib_out.txt can be viewed via Notepad and I encourage you to do so. Should your script file not execute properly, then you can use an output file to troubleshoot your SQL commands in the script file. The following table depicts screenshots of expected outcomes. sqlite3 screen shot of SQL script results Notepad screen shot of CreateLib_out.txt Batch SQL script files may also be executed in DBBrowser via the following steps: Launch DBBrowser for SQLite. In DBBrowser, click on the New Database button , enter the database file name Lib.DB, and click Save. If you have already created the Lib.DB database, DBBrowser will prompt you as to whether you wish to overwrite it. Overwrite if needed. Click on Cancel to close the resulting Edit table definition window. In the Execute SQL tab, click on the Open SQL file icon and enter the script file name CreateLibDB.sql. In the Execute SQL tab, a new window named CreateLibDB.sql will be created. In the CreateLib.sql tab window, be sure to remove all the SQLite dot commands in the script file. The SQLite dot commands include: .echo ON .mode list .separator " | " .output CreateLibDB_out.txt .open LIB.db .output stdout .echo OFF MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite With only SQL commands in the CreateLib.sql tab window, a screen shot of your DBBrowser shows: Clicking on the Execute all icon will execute all the SQL commands in CreateLib.sql. The following is a screen shot of the expected results: The Database Structure tab displays the structure of the Lib.DB database. MIS 502 | Week 01 – SQL DDL Classwork Exercise DB Creation with SQLite The Browse Data tab displays contents of a table. Toggle between tables by using the Table dropdown list on the top left. On the top right, the New Record button allows the INSERT of a row of data into the specific table. Also, the Delete Record button allows the DELETE of a row of data from the specific table. Close DBBrowser for SQLite GUI by choosing File and then Exit. DBBrowser will prompt you as to whether you wish to save the changes to the database, so choose Save. Learning Activity Take-aways Using sqlite3 to create a temporary database, create tables within the database, and populate the tables with the INSERT SQL DML command. Using DBBrowser for SQLite to create a temporary database, create tables within the database, and populate tables with data. Differentiation between SQL commands and SQLite dot commands. Using a script file of SQL commands and SQLite dot commands to create a persistent database on the computer hard drive, create tables in the database, and insert data records into the tables. Using a script file of SQL commands and SQLite dot commands for a persistent database in sqlite3. A SQL DDL script file to use as a template for future SQL DDL script file creation. How to create an output file from a SQL command script in sqlite3 and how to use an output file to facilitate trouble shooting of errors in SQL command syntax. Using a script file of SQL commands for a persistent database in DBBrowser for SQLite. The skills demonstrated in this learning activity will be used and enhanced in future graded exercises, tutorials, and group project work. The midterm and final exam will assess the skills demonstrated in this exercise.