Data Modeling AWS SQL and Scripting Training Topics • Getting Started with a Database Project (the Capstone) • Data Modeling Concepts • Steps for Creating an ERD • Getting Started with a Database Project (the Capstone) 2 Create Database AWS SQL and Scripting Training SQLite Create Database • Recall that a database is a ‘container’ that holds related database tables • A database is managed/controlled by a DBMS • SQLite is the database management system that we are using • We need to create a database within SQLite for our Capstone project 4 SQLite Create Database • Syntax in SQLite for creating a database is: DatabaseName.db At the SQLite prompt enter in: test.db is the database name 5 Database is created… or is it? • Note: Even though we have provided a database name, SQLite does not actually create the database. • SQLite will defer creating the database until you actually create something inside of it, such as a table or a view. 6 Using the SQLite test.db 7 Viewing the contents 8 Note on .headers and .mode • First: We’ll cover the formatting commands in a subsequent section. • Next: We’ll discuss the select statement in a subsequent section too. • But, the .headers and .mode commands are used to improve the formatting of the output. 9 Getting Database Schema Information • There are several commands that we can use to obtain information about the contents of your database. • Later, we’ll add tables to our database (using DDL). • Presently, we have one table created based on our prior “create table” command. • We can view the tables by entering in the following command (which will show the schema that contains the ’test’ table) 10 Each table has an index (a key) that can be viewed 11 Viewing the schema (the DDL) for a database • Recall that we (a) created the database and (b) added the test table to the database (using DDL) • We can review the contents of the database schema by entering in the .schema followed by the database name. 12 View the schema of the database 13 SQLite Mater Table Schema items 14 Example: Using all schema items to view the contents of a database 15 Assignment: Create SQLite database • Open SQLite on your computer • Replicate the commands in this section • Verify results 16 SQL Concepts AWS SQL and Scripting Training Video: SQL or SEQUEL? 18 Section Context • DDL – Data Definition Language. << create tables (this section) • DML – Data Manipulation Language << subsequent section on querying the database 19 DDL AWS SQL and Scripting Training Data Definition Language (DDL) • We can either create DDL ‘by hand’ (text) or (for some ER design tools, the DDL can be automatically created from the design) • Keep in mind that a complete Data Dictionary can serve as a basis for creating tables in the database. • There are other DDL commands that will assist you in not only creating the tables, but altering (changing) the table structure. 21 DDL Statements • CREATE – is used to create the database or its objects (like table, index, function, views, store procedure and triggers). • DROP – is used to delete objects from the database. • ALTER-is used to alter the structure of the database. • TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are removed. • COMMENT –is used to add comments to the data dictionary. • RENAME –is used to rename an object existing in the database. 22 Create tables in the database • Page 47 23 DML AWS SQL and Scripting Training A Query, a Statement, and a Clause • A query is a request (a question) that returns information from the database. • An SQL statement is any valid piece of SQL code that is executed by the SQL engine. • A clause is a subsection of a query containing at least one keyword and relevant information needed by the query (clause is a portion of a query). 25 The SELECT statement • Words in ALL CAPS are SQL keywords. • A query can contain multiple clauses, each starting with a keyword. 26 Recap of SQL concepts • A table is a two-dimensional grid of rows and columns that contain data. • Logical and Physical database terms include: • Table (physical) = Entity (logical) • Field (physical) = Attribute (logical) • A database record is a row in a table or multiple rows in a table (with a unique primary key) • Data can exist as a variety of different data types, such as strings of text, numbers, or special characters. • Metadata describes the nature and format of the data, including any minimum/maximum character length or required numbers, letters, or special characters. 27 Recap of SQL concepts • Relational databases can contain many tables. • Each table in a relational database should have a primary key that serves as a unique identifier for a row in a given table. • A foreign key is any column in a table that exists as a primary key in another table. • The relationship between tables and their primary and foreign keys is called a database schema. • A database schema can be shown visually by an ERD (Entity Relationship Diagram), which serves as a blueprint for a database. 28 Recap of SQL concepts • There are a variety of relational database (RDBMS) products including the Oracle database, Microsoft SQL Server, MySQL, IBM DB2 and SQLite. While they may differ in their interface to some degree, the fundamental SQL engine is essentially the same (not including extensions). • The SELECT keyword is the most common SQL command used in SQL queries. • SQL statements can contain multiple clauses that use different SQL keywords. 29 Most common tasks in SQL • Create, retrieve, update and delete data based on the selection criteria (C.R.U.D.) • Organize data in the selected order • Join data from multiple tables 30 Database (db), SQL, and Schema 31 Database (db), NoSQL, Document/Graph/Key Value 32 Video Review Questions (NoSQL) AWS SQL and Scripting Training Video Review Questions 34 Types of SQL statements • Data Definition Language (DDL) consists of SQL commands that are used to define the database schema. It is used to create and modify the structure of database objects. Some DDL commands include CREATE, DROP, and COMMENT (GeeksforGeeks, 2019). • Data Manipulation Language (DML) are SQL commands that deal with data manipulation within the database. Examples of DML commands are DELETE, UPDATE, and INSERT (Technopedia, 2014). • Data Control Language (DCL) are SQL commands that deal with rights and permissions of the database. DCL commands include GRANT, which gives users access privileges, and REVOKE, which eliminates a user’s privileges (GeeksforGeeks, 2019). 35 Recap of SQL Category Description Examples* Data definition language (DDL) A series of commands used to ALTER; COMMENT; CREATE; change the way data is stored in the DROP; RENAME; TRUNCATE database and/or alter its structure. Data manipulation language (DML) A series of commands used to add INSERT; UPDATE; DELETE and/or manipulate data within the existing database schema. Data control language (DCL) Two commands used to allow or GRANT; REVOKE remove access and/or privileges to the database 36 View • A VIEW is a virtual table displaying the results of an SQL statement. • This can contain data from more than one table and runs when it’s accessed so the data that is displayed is current. It’s useful if you want a user to be able to see only certain data without having access to the entire database. 37 Jeopardy: Database • How to Convert ER Diagram to Relational Database: https://www.learndb.com/databases/how-to-convert-er-diagramto-relational-database 39 What does the school counselor do? What can the school counselor help with? Who does the school counselor talk to? Will I see the school counselor? $100 $100 $100 $100 $200 $200 $200 $200 $300 $300 $300 $300 $400 $400 $400 $400 40 References