Advertisement

SQL intro.pptx

Mar. 26, 2023
Advertisement

More Related Content

Advertisement

SQL intro.pptx

  1. Introduction • Database design • Process of determining the particular tables and columns that comprise a database • One must understand: • Database concepts • Process of normalization
  2. Database Concepts • Entity • Attribute • Relationship • Functional dependence • Primary key
  3. Relational Database • A collection of tables • Tables in KimTay Pet Supplies Database • SALES_REP • CUSTOMER • INVOICES • INVOICE_LINE • ITEM
  4. Entities, Attributes, and Relationships (1 of 4) • Entity (like a noun) • A person, place, thing, or event • Attribute • Property of an entity • Relationship • Association between entities • One-to-many relationship • One sales representative (sales rep) is related to many customers • Implemented by having common columns in two or more tables • REP_ID is a column in the SALES_REP table as well as the CUSTOMER table • Repeating groups • Multiple entries in an individual location
  5. Figure 2-2: Table with Repeating Groups INVOICES INVOICE_NU M INVOICE_D ATE CUST_ID ITEM_ID QUANTI TY QUOTED_PRI CE 14216 11/15/2021 125 CA 75 3 $37.99 14219 11/15/2021 227 AD 72 2 $79.99 NA NA NA DT 12 4 $39.99 14222 11/16/2021 294 LD 14 1 $47.99 14224 11/16/2021 182 KH 81 4 $18.99 14228 11/18/2021 435 FS 42 1 $124.99 NA NA NA PF 19 1 $74.99 14231 11/18/2021 125 UF 39 2 $189.99 14233 11/18/2021 435 KH 81 1 $19.99 NA NA NA QB 92 4 $109.95 NA NA NA WB 49 4 $74.95 14237 11/19/2021 616 LP 73 3 $54.95
  6. Figure 2-3: INVOICES Table without Repeating Groups INVOICES INVOICE_ NUM INVOICE_DAT E CUST_ ID ITEM_ID QUANTITY QUOTED_PRIC E 14216 11/15/2021 125 CA 75 3 $37.99 14219 11/15/2021 227 AD 72 2 $79.99 14219 11/15/2021 227 DT 12 4 $39.99 14222 11/16/2021 294 LD 14 1 $47.99 14224 11/16/2021 182 KH 81 4 $18.99 14228 11/18/2021 435 FS 42 1 $124.99 14228 11/18/2021 435 PF 19 1 $74.99 14231 11/18/2021 125 UF 39 2 $189.99 14233 11/18/2021 435 KH 81 1 $19.99 14233 11/18/2021 435 QB 92 4 $109.95 14233 11/18/2021 435 WB 49 4 $74.95 14237 11/19/2021 616 LP 73 3 $54.95
  7. Entities, Attributes, and Relationships (2 of 4) • Relation is a two-dimensional table • Entries in the table are single-valued • Each column has a distinct name • All values in a column match this name • Each row is distinct • Order of the rows and columns is immaterial
  8. Entities, Attributes, and Relationships (3 of 4) • Use shorthand representation to show tables and columns in a relational database • For each table, write the name of the table and then within parentheses list all of the columns in the table SALES_REP (REP_ID, FIRST_NAME, LAST_NAME, ADDRESS, CITY, STATE, POSTAL, CELL_PHONE, COMMISSION, RATE) CUSTOMER (CUST_ID, FIRST_NAME, LAST_NAME, ADDRESS, CITY, STATE, POSTAL, EMAIL, BALANCE, CREDIT_LIMIT, REP_ID) INVOICES (INVOICE_NUM, INVOICE_DATE, CUST_ID) INVOICE_LINE (INVOICE_NUM, ITEM_ID, QUANTITY, QUOTED_PRICE) ITEM (ITEM_ID, DESCRIPTION, ON_HAND, CATEGORY, LOCATION, PRICE)
  9. Entities, Attributes, and Relationships (4 of 4) • Database management system (DBMS) • Set of programs that allows users to store, manipulate, and retrieve data efficiently • Requires columns with duplicate names to be qualified to avoid confusion • Reference the REP_ID column in the CUSTOMER table as CUSTOMER.REP_ID • Reference the REP_ID column in the SALES_REP table as SALES_REP.REP_ID
  10. Functional Dependence • A column, B, is functionally dependent on another column, A, if a value for A determines a single value for B at a point in time • B is functionally dependent on A • A → B • A functionally determines B • Cannot be determined from sample data • One must know user’s policies
  11. Figure 2-4: SALES_REP Table with a PAY_CLASS Column SALES_REP REP _ ID FIRST _ NAME LAST _ NAME ADDRE SS CIT Y STA TE POST AL CEL L_ PHO NE COMMI SSION PAY_ CLAS S RAT E 05 Susan Garcia 42 Mountai n Ln Cod y WY 8241 4 307- 824- 1245 $12,743 .16 1 0.04 10 Richar d Miller 87 Pikes Dr Ral ston WY 8244 0 307- 406- 4321 $20,872 .11 2 0.06 15 Donna Smith 312 Oak Rd Po well WY 8244 0 307- 982- 8401 $14,912 .92 1 0.04 20 Daniel Jacks on 19 Lookout Dr Elk Butt e WY 8243 3 307- 883- 9481 $0.00 1 0.04
  12. Primary Key • Unique identifier for a table • Column A (or a collection of columns) is the primary key for a table if the following is true: • Property 1 • All columns in the table are functionally dependent on A • Property 2 • No subcollection of the columns in A (assuming that A is a collection of columns and not just a single column) also has Property 1
  13. Primary Key: Shorthand Representation • A table’s primary key can be indicated by underlining the column or collection of columns that comprise the primary key SALES_REP (REP_ID, FIRST_NAME, LAST_NAME, ADDRESS, CITY, STATE, POSTAL, CELL_PHONE, COMMISSION, RATE) CUSTOMER (CUST_ID, FIRST_NAME, LAST_NAME, ADDRESS, CITY, STATE, POSTAL, EMAIL, BALANCE, CREDIT_LIMIT, REP_ID) INVOICES (INVOICE_NUM, INVOICE_DATE, CUST_ID) INVOICE_LINE (INVOICE_NUM, ITEM_ID, QUANTITY, QUOTED_PRICE) ITEM (ITEM_ID, DESCRIPTION, ON_HAND, CATEGORY, LOCATION, PRICE)
  14. Database Design • Based on a set of requirements that the database must support • Requirements are gathered through a process known as systems analysis
  15. Design Method • Read the requirements, identify the entities (objects) involved, and name the entities • Identify the unique identifiers for the entities identified in the previous step • Identify the attributes for all the entities • Identify the functional dependencies that exist among the attributes • Use the functional dependencies to identify the tables by placing each attribute with the attribute or minimum combination of attributes on which it is functionally dependent • Identify any relationships between tables
  16. Database Design Requirements • Requirement for KimTay Pet Supplies involves storing information pertaining to: • Sales reps • Customers • Items • Invoices • Invoice lines • Conditions for invoice lines • There is only one customer per invoice • On a given invoice, there is at most one line item for a given item • The quoted price may differ from the actual price
  17. Database Design Process Example: KimTay Pet Supplies (1 of 2) • Identify the entities, and name them • SALES_REP, CUSTOMER, ITEM, and INVOICES • Review the data, and determine the unique identifier for each entity • REP_ID, CUST_ID, ITEM_ID, and INVOICE_ NUM • Assign appropriate names to the specific attributes in each of the requirements • Identify functional dependencies based on associations of attributes with unique identifiers
  18. Database Design Process Example: KimTay Pet Supplies (2 of 2) • Use the established functional dependencies to create tables for each of the requirements • Examine the tables, and identify common columns among them to arrive at a list of relationships
  19. Diagrams for Database Design • Entity-relationship (E-R) diagram is an illustration in which: • Rectangles represent entities (tables) • Lines represent relationships • Some styles of E-R diagrams use arrows or crow’s foot • Original style of E-R diagrams • Relationships are indicated in diamonds that describe the relationship • The number 1 indicates the one side of the relationship, and the letter “n” represents the many side of the relationship
  20. Figure 2-13: E-R Diagram for the KimTay Pet Supplies Database with Rectangles and Arrows
  21. Figure 2-14: E-R Diagram for the KimTay Pet Supplies Database with Crow’s Foot
  22. Figure 2-15: E-R Diagram for the KimTay Pet Supplies Database with Named Relationships
  23. Objectives • Create and run S Q L commands • Create and activate a database • Create tables • Identify and use data types to define columns in tables • Understand and use nulls • Add rows to tables • View table data • Correct errors in a table • Save S Q L commands to a file • Describe a table’s layout using S Q L
  24. Introduction • Structured Query Language (S Q L) • One of the most popular and widely used languages for retrieving and manipulating database data • Developed in the mid-1970s under the name SEQUEL • Renamed S Q L in 1980 • Used by most database management systems (DBMS)
  25. Creating and Running S Q L Commands • Can be done by using a DBMS that supports S Q L to accomplish tasks • My S Q L 8.0 (My S Q L Community Server 8.0.18) installation • Suggested setup types: Developer default type and full type • Both include the installation of the My S Q L Workbench • My S Q L Workbench contains: • Powerful tools that allow developers and database administrators to visually design, develop, and administer My S Q L databases • An S Q L editor that is used throughout the text to enter commands and view the results of the commands entered • Users should make sure they remember the password selected during installation
  26. Using My S Q L Workbench • Run My S Q L Workbench 8.0 C E app after My S Q L 8.0 is installed • Access the main My S Q L Workbench environment for the listed connection via the server created during installation • By clicking the available connection box listed on the My S Q L Workbench opening window and then submitting the password • Simplify the main window to give more room for the Query pane and Output pane • By hiding Navigation pane and My S Q L Additions pane
  27. Figure 3-1: My S Q L Workbench Opening Window
  28. Figure 3-2: Entering Password for Server
  29. Figure 3-3: Main My S Q L Workbench Environment
  30. Figure 3-4: Outcome of Hiding the Navigator and S Q L Additions Panes
  31. Entering Commands • Commands • Followed by a semicolon • Semicolon ends a statement in S Q L • Entered into the My S Q L Workbench S Q L editor • Microsoft has a specific system of identifying keywords • Executed by selecting Execute Current Statement or Execute (All or Selected) from the Query menu • Displays a result grid between the query pane and output pane ▶ Result grid and details from output pane can be cleared • History of processing commands can be accessed • Cleared by highlighting and deleting them
  32. Figure 3-5: Syntax of the SHOW DATABASES Command
  33. Figure 3-6: Enter SHOW DATABASES Command
  34. Figure 3-7: Execute the Query
  35. Figure 3-8: Outcome of SHOW DATABASES Command
  36. Figure 3-9: To Clear Results of the SHOW DATABASES Command
  37. Creating a Database (1 of 2) • Objects within a database have identifiers, or names, associated with them • DBMS rules for naming identifiers vary • General guidelines for naming identifiers when using a DBMS • The identifier name: • Cannot exceed 30 characters • Must start with a letter • Can contain letters, numbers, and underscores (_) • Cannot contain spaces
  38. Creating a Database (2 of 2) • Steps involved • Enter the command keyword followed by the desired name of database and end with semicolon • Execute the command • Execute the command to list databases • Results in six internal databases and one additional database that was created with the desired name
  39. Changing the Default Database • Known as activating or using the database • Default database must be set to the database one wants to work with • Default database is the one to which all subsequent commands pertain • Activate the default database • By executing the USE command followed by the name of the database • Tables can be added to the database after it has been activated
  40. Figure 3-10: Syntax of the CREATE DATABASE Command
  41. Figure 3-11: Enter SHOW DATABASES Command
  42. Figure 3-12: Outcome of SHOW DATABASES Command after Creating the KimTay Database
  43. Figure 3-13: Syntax of the USE Command
  44. Figure 3-14: Enter USE Command to Activate the KIMTAY Database
  45. Creating a Table (1 of 2) • Describe the layout of the table in the database • Use the CREATE TABLE command followed by: • Table name • Names and data types of the columns in the table • Data types: Define the type of data and the number of characters or digits stored in each column of data
  46. Figure 3-15: CREATE TABLE Command to Create the SALES_REP Table
  47. Creating a Table (2 of 2) • Commands are free format • No rules stating specific words in specific positions • Commands are not case sensitive • Exception: Use the correct case when inserting values into a table
  48. Oracle Figure 3-1: CREATE TABLE Statement to Create SALES_REP Table Using Oracle
  49. S Q L Server Figure 3-1: CREATE TABLE Statement to Create SALES_REP Table Using S Q L Server
  50. Figure 3-17: Outcome of SHOW TABLES Command Listing the Tables in the KIMTAY Database
  51. Correcting Errors in S Q L Commands • Use the same techniques that you might use in a word processor • Make changes and click the Run button to execute the command again
  52. Dropping a Table • Errors in tables can be corrected by dropping (deleting) and starting over • Useful when a table is created before errors are discovered • Command is followed by the table to be dropped and a semicolon • Data in the table will also be deleted
  53. Using Data Types • For each column, the type of data must be defined • Common data types • CHAR(n) • VARCHAR(n) • DATE • DECIMAL(p,q) • INT • SMALLINT
  54. Using Nulls (1 of 2) • A special value to represent a situation when the actual value is not known for a column • Can specify whether to allow nulls in the individual columns • Should not allow nulls for the primary key column • Use the NOT NULL clause in a CREATE TABLE command to indicate columns that cannot contain null values • Default is to allow null values • If a column is defined as NOT NULL, system will reject any attempt to store a null value there
  55. Using Nulls (2 of 2) CREATE TABLE SALES_REP ( REP_ID CHAR (2) PRIMARY KEY, FIRST_NAME CHAR (20) NOT NULL, LAST_NAME CHAR (20) NOT NULL, ADDRESS CHAR (20), CITY CHAR (15), STATE CHAR (2), POSTAL CHAR (5), CELL_PHONE CHAR (12), COMMISSION DECIMAL (7, 2), RATE DECIMAL (3, 2) );
  56. Adding Rows to a Table • INSERT command • Type INSERT INTO followed by the name of the table • Type VALUES followed by specific values in parentheses • Type Values for character columns in single quotation marks • To add new rows, modify the previous INSERT command • Use the same editing techniques as those used to correct errors
  57. Figure 3-22: INSERT Command Executed for the First Record in the SALES_REP Table
  58. Inserting a Row That Contains Nulls • Use a special format of INSERT command to enter a null value in a table • Identify the names of the columns that accept non-null values and then list only the non-null values after the VALUES command
  59. Figure 3-30: INSERT Command Executed for the Fifth Record in the SALES_REP Table Containing Null Values
  60. Viewing Table Data • Use the SELECT command • Displays all the rows and columns in a table • Display all the rows and columns in the SALES_REP table • SELECT * FROM followed by the name of the table • End with a semicolon • In Oracle • Enter the statement followed by slash (/) • In S Q L Server • Enter SELECT statement in the Query Editor window
  61. Correcting Errors in a Table • UPDATE, DELETE, and INSERT • UPDATE command is used to change a value in a table • DELETE is used to delete a row • INSERT is used to add a row
  62. Figure 3-34: Using an UPDATE Command to Change a Value
  63. Figure 3-35: Using a SELECT Command to View a Table
  64. Figure 3-37: Using the DELETE Command to Delete a Row
  65. Saving S Q L Commands (1 of 6) • Allows the use of commands repeatedly without retyping them • Commands are saved in a script file or script • Text file with .s q l extension • Script repository • Available in some database management systems such as Oracle • Specific location on local drive is used to download scripts
  66. Saving S Q L Commands (2 of 6) • To create and save a script in My S Q L • Enter the command, or commands, that would comprise the script in the query pane • On the My S Q L Workbench main menu, click on the File menu and select the Save Script As option • Navigate to a location in the file system where the file has to be saved • Enter a name for the script in the File name • Click the Save button • To view, edit, or run a script in My S Q L • On the My S Q L Workbench main menu, click on the File menu and select Open S Q L Script • Navigate to the location in the file system where the script is saved
  67. Saving S Q L Commands (3 of 6) • Select the script and click Open • Selected script is now in a new tab in the query pane without any change in the name of the script and can now be edited or run by choosing one of the two options below: • Execute by clicking Query on the My S Q L Workbench main menu and selecting the appropriate execute option • In the Run S Q L Script dialog box, select the Default Schema Name from the drop-down menu and click Run ▶ Click on Close to close the Run S Q L Script dialog box after the results are displayed • To delete a script in My S Q L • Navigate to the location in the file system where the script is saved • Delete the file containing the script
  68. Saving S Q L Commands (4 of 6) • To create a script file in Oracle S Q L Developer • Load the Oracle S Q L Developer tool • Enter query statement to be stored and execute it • Click the Save button and then enter a name for the script • To view, edit, or run an existing script in Oracle S Q L Developer • Load Oracle S Q L Developer and double click on Database Connection • Click the Open File button on the toolbar • Navigate to the folder containing the script file and then click the Open button in the Open File dialog box • Make edits and click the Save button to save changes • To run a script, click the Execute button
  69. Saving S Q L Commands (5 of 6) • To create a script file in S Q L Server • Load S Q L Server Management Studio and then click the Connect button in the Connect to Server dialog box • Open the appropriate database and then click the New Query button • Type the command or commands to save in the script • When necessary, click the Execute button to execute the commands saved in the script • Click the Save button and then enter a name for the script
  70. Saving S Q L Commands (6 of 6) • To view, edit, or run an existing script in S Q L server • Load S Q L Server Management Studio and then click the Connect button in the Connect to Server dialog box • Open the appropriate database and then click the New Query button • Click the Open File button on the toolbar • Navigate to the folder containing the script file and then click the Open button in the Open File dialog box • Click the Save button to save any changes made • Click the Execute button to run the script
  71. Creating the Remaining Database Tables • Execute appropriate CREATE TABLE and INSERT commands • Save these commands as scripts • Separate multiple commands in a script file with a semicolon
  72. Describing a Table • Involves using the DESCRIBE command • DESC in also accepted in My S Q L and Oracle • SP_COLUMNS in S Q L Server
  73. Figure 3-47: DESCRIBE Command for the SALES_REP Table
  74. Summary (1 of 2) • Use the CREATE TABLE command to create tables • Use the DROP TABLE command to delete a table • CHAR, VARCHAR, DATE, DECIMAL, INT, and SMALLINT are commonly used data types • Null value is used when an actual value for a column is unknown, unavailable, or not applicable • Use the NOT Null clause to identify columns that cannot accept a null value • Use the INSERT command to add rows in a table • Use the SELECT command to view data in a table
  75. Summary (2 of 2) • Use the UPDATE command to change the value in a column • Use the DELETE command to delete a row from a table • S Q L commands in a script file can be saved in My S Q L, Oracle, and S Q L Server • The DESCRIBE command in My S Q L and Oracle can be used to display a table’s structure and layout, and SP_COLUMNS in S Q L Server
Advertisement