Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management System 1 (ITED123A)

Similar presentations


Presentation on theme: "Database Management System 1 (ITED123A)"— Presentation transcript:

1 Database Management System 1 (ITED123A)
Introduction to SQL Database Management System 1 (ITED123A)

2 Objectives Define terms Interpret history and role of SQL
Discuss SQL:1999 and SQL:2008 standards Identify the different types of SQL commands Explore the SQL Developer and SQL *Plus environment

3 SQL Overview Structured Query Language
The standard for relational database management systems (RDBMS) RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables

4 History of SQL 1970–E. F. Codd develops relational database concept
–System R with Sequel (later SQL) created at IBM Research Lab 1979–Oracle markets first relational DB with SQL 1981 – SQL/DS first available RDBMS system on DOS/VSE Others followed: INGRES (1981), IDM (1982), DG/SGL (1984), Sybase (1986) 1986–ANSI SQL standard released 1989, 1992, 1999, 2003, 2006, 2008–Major ANSI standard updates Current–SQL is supported by most major database vendors

5 Purpose of SQL Standard
Specify syntax/semantics for data definition and manipulation Define data structures and basic operations Enable portability of database definition and application modules Specify minimal (level 1) and complete (level 2) standards Allow for later growth/enhancement to standard (referential integrity, transaction management, user-defined functions, extended join operations, national character sets)

6 Benefits of a Standardized Relational Language
Reduced training costs Productivity Application portability Application longevity Reduced dependence on a single vendor Cross-system communication

7 SQL Environment Catalog Schema Data Definition Language (DDL)
A set of schemas that constitute the description of a database Schema The structure that contains descriptions of objects created by a user (base tables, views, constraints) Data Definition Language (DDL) Commands that define a database, including creating, altering, and dropping tables and establishing constraints Data Manipulation Language (DML) Commands that maintain and query a database Data Control Language (DCL) Commands that control a database, including administering privileges and committing data

8 SQL Statements SELECT INSERT UPDATE DELETE MERGE CREATE
ALTER DROP RENAME TRUNCATE COMMENT GRANT REVOKE COMMIT ROLLBACK SAVEPOINT Data manipulation language (DML) Data definition language (DDL) SQL Statements SQL statements supported by Oracle comply with industry standards. Oracle Corporation ensures future compliance with evolving standards by actively involving key personnel in SQL standards committees. The industry-accepted committees are ANSI and International Standards Organization (ISO). Both ANSI and ISO have accepted SQL as the standard language for relational databases. Data control language (DCL) Transaction control Oracle Database 11g: SQL Fundamentals I I - 8

9 Figure 6-1 A simplified schematic of a typical SQL environment, as described by the SQL: 2008 standard 9

10 SQL Data Types

11 DDL, DML, DCL, and the database development process
Figure 6-4 DDL, DML, DCL, and the database development process 11

12 Oracle Database 11g Manageability High availability Performance
Security Information integration Oracle Database 11g Organizations need to support multiple terabytes of information for users who demand fast and secure access to business applications round-the-clock. The database systems must be reliable and must be able to recover quickly in the event of any kind of failure. Oracle Database 11g is designed along the following feature areas to help organizations manage infrastructure grids easily and deliver high-quality service: Manageability: By using some of the change assurance, management automation, and fault diagnostics features, the database administrators (DBAs) can increase their productivity, reduce costs, minimize errors, and maximize quality of service. Some of the useful features that promote better management are Database Replay facility, the SQL Performance Analyzer, and the Automatic SQL Tuning facility. High availability: By using the high availability features, you can reduce the risk of down time and data loss. These features improves online operations and enable faster database upgrades. Oracle Database 11g: SQL Fundamentals I I - 12

13 Development Environments for SQL
There are two development environments for this course: Primary tool is Oracle SQL Developer SQL*Plus command line interface may also be used SQL *Plus SQL Developer Development Environments for SQL SQL Developer This course has been developed using Oracle SQL Developer as the tool for running the SQL statements discussed in the examples in the slide and the practices. SQL Developer version 1.2 is shipped with Oracle Database 11g, and is the default tool for this class. In addition, SQL Developer version is also available on the classroom machine, and may be installed for use. At the time of publication for this course, version was the latest release of SQL Developer. SQL*Plus The SQL*Plus environment may also be used to run all SQL commands covered in this course. Note See Appendix E for information on using SQL Developer, including simple instructions on installing version See Appendix D for information on using SQL*Plus. Oracle Database 11g: SQL Fundamentals I I - 13

14 Creating a Database Connection
1 Database Connection Window Connections tabbed page 2 3 Click to test Creating a Database Connection (continued) To create a database connection, perform the following steps: 1. On the Connections tabbed page, right-click Connections and select New Connection. 2. In the New/Select Database Connection window, enter the connection name. Enter the username and password of the schema that you want to connect to. 1. From the Role drop-down box, you can select either default or SYSDBA (you choose SYSDBA for the sys user or any user with database administrator privileges). 2. You can select the connection type as: - Basic: In this type, enter hostname and SID for the database you want to connect to. Port is already set to Or you can also choose to enter the Service name directly if you use a remote database connection. - TNS: You can select any one of the database aliases imported from the tnsnames.ora file. - Advanced: You can define a custom Java Database Connectivity (JDBC) URL to connect to the database. 3. Click Test to ensure that the connection has been set correctly. 4. Click Connect. Oracle Database 11g: SQL Fundamentals II C - 14

15 Browsing Database Objects
Use the Connections Navigator to: Browse through many objects in a database schema Review the definitions of objects at a glance Browsing Database Objects After you create a database connection, you can use the Connections Navigator to browse through many objects in a database schema including Tables, Views, Indexes, Packages, Procedures, Triggers, and Types. SQL Developer uses the left side for navigation to find and select objects, and the right side to display information about the selected objects. You can customize many aspects of the appearance of SQL Developer by setting preferences. You can see the definition of the objects broken into tabs of information that is pulled out of the data dictionary. For example, if you select a table in the Navigator, the details about columns, constraints, grants, statistics, triggers, and so on are displayed on an easy-to-read tabbed page. If you want to see the definition of the EMPLOYEES table as shown in the slide, perform the following steps: 1. Expand the Connections node in the Connections Navigator. 2. Expand Tables. 3. Click EMPLOYEES. By default, the Columns tab is selected. It shows the column description of the table. Using the Data tab, you can view the table data and also enter new rows, update data, and commit these changes to the database. Oracle Database 11g: SQL Fundamentals II C - 15

16 Using the SQL Worksheet
Use the SQL Worksheet to enter and execute SQL, PL/SQL, and SQL *Plus statements. Specify any actions that can be processed by the database connection associated with the worksheet. Using the SQL Worksheet When you connect to a database, a SQL Worksheet window for that connection automatically opens. You can use the SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus statements. The SQL Worksheet supports SQL*Plus statements to a certain extent. SQL*Plus statements that are not supported by the SQL Worksheet are ignored and not passed to the database. You can specify actions that can be processed by the database connection associated with the worksheet, such as: Creating a table Inserting data Creating and editing a trigger Selecting data from a table Saving the selected data to a file You can display a SQL Worksheet by using one of the following: Select Tools > SQL Worksheet. Click the Open SQL Worksheet icon. Click the Open SQL Worksheet icon. Select SQL Worksheet from the Tools menu, or Oracle Database 11g: SQL Fundamentals II C - 16

17 Using the SQL Worksheet
2 4 6 8 Execute statement Run script Commit Rollback Cancel SQL History Execute explain plan Autotrace Clear 1 3 5 7 9 Using the SQL Worksheet (continued) You may want to use the shortcut keys or icons to perform certain tasks such as executing a SQL statement, running a script, and viewing the history of SQL statements that you have executed. You can use the SQL Worksheet toolbar that contains icons to perform the following tasks: 1. Execute Statement: Executes the statement where the cursor is located in the Enter SQL Statement box. You can use bind variables in the SQL statements, but not substitution variables. 2. Run Script: Executes all statements in the Enter SQL Statement box by using the Script Runner. You can use substitution variables in the SQL statements, but not bind variables. 3. Commit: Writes any changes to the database and ends the transaction 4. Rollback: Discards any changes to the database, without writing them to the database, and ends the transaction 5. Cancel: Stops the execution of any statements currently being executed 6. SQL History: Displays a dialog box with information about SQL statements that you have executed 7. Execute Explain Plan: Generates the execution plan, which you can see by clicking the Explain tab 8. Autotrace: Generates trace information for the statement 9. Clear: Erases the statement or statements in the Enter SQL Statement box Oracle Database 11g: SQL Fundamentals II C - 17

18 Using the SQL Worksheet
Use the SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus statements. Specify any actions that can be processed by the database connection associated with the worksheet. Enter SQL statements. Using the SQL Worksheet (continued) When you connect to a database, a SQL Worksheet window for that connection automatically opens. You can use the SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus statements. All SQL and PL/SQL commands are supported as they are passed directly from the SQL Worksheet to the Oracle database. SQL*Plus commands used in the SQL Developer have to be interpreted by the SQL Worksheet before being passed to the database. The SQL Worksheet currently supports a number of SQL*Plus commands. Commands not supported by the SQL Worksheet are ignored and are not sent to the Oracle database. Through the SQL Worksheet, you can execute SQL statements and some of the SQL*Plus commands. You can display a SQL Worksheet by using any of the following two options: Select Tools > SQL Worksheet. Click the Open SQL Worksheet icon. Results are shown here. Oracle Database 11g: SQL Fundamentals II C - 18

19 Executing SQL Statements
Use the Enter SQL Statement box to enter single or multiple SQL statements. Use the Enter SQL Statement box to enter single or multiple SQL statements. Executing SQL Statements In the SQL Worksheet, you can use the Enter SQL Statement box to enter single or multiple SQL statements. For a single statement, the semicolon at the end is optional. When you enter the statement, the SQL keywords are automatically highlighted. To execute a SQL statement, ensure that your cursor is within the statement and click the Execute Statement icon. Alternatively, you can press the F9 key. To execute multiple SQL statements and see the results, click the Run Script icon. Alternatively, you can press the F5 key. In the example in the slide, because there are multiple SQL statements, the first statement is terminated with a semicolon. The cursor is in the first statement, and therefore, when the statement is executed, results corresponding to the first statement are displayed in the Results box. View the results on the Script Output tabbed page. Oracle Database 11g: SQL Fundamentals II C - 19

20 Saving SQL Scripts Oracle Database 11g: SQL Fundamentals II C - 20
Enter a file name and identify a location to save the file, and click Save. Click the Save icon to save your SQL statement to a file. The contents of the saved file are visible and editable in your SQL Worksheet window. Saving SQL Scripts You can save your SQL statements from the SQL Worksheet into a text file. To save the contents of the Enter SQL Statement box, follow these steps: 1. Click the Save icon or use the File > Save menu item. 2. In the Windows Save dialog box, enter a file name and the location where you want the file saved. 3. Click Save. After you save the contents to a file, the Enter SQL Statement window displays a tabbed page of your file contents. You can have multiple files open at the same time. Each file displays as a tabbed page. Script Pathing You can select a default path to look for scripts and to save scripts. Under Tools > Preferences > Database > Worksheet Parameters, enter a value in the “Select default path to look for scripts” field. Oracle Database 11g: SQL Fundamentals II C - 20

21 Executing SQL Statements
Use the Enter SQL Statement box to enter single or multiple SQL statements. F9 F5 F5 F9 Executing SQL Statements The example in the slide shows the difference in output for the same query when the [F9] key or Execute Statement is used versus the output when [F5] or Run Script is used. Oracle Database 11g: SQL Fundamentals II C - 21

22 Formatting the SQL Code
Before formatting Formatting the SQL Code You may want to beautify the indentation, spacing, capitalization, and line separation of the SQL code. SQL Developer has a feature for formatting SQL code. To format the SQL code, right-click in the statement area, and select Format SQL. In the example in the slide, before formatting, the SQL code has the keywords not capitalized and the statement not properly indented. After formatting, the SQL code is beautified with the keywords capitalized and the statement properly indented. After formatting Oracle Database 11g: SQL Fundamentals II C - 22

23 Using Snippets Snippets are code fragments that may be just syntax or examples. When you place your cursor here, it shows the Snippets window. From the drop-down list, you can select the functions category that you want. Using Snippets You may want to use certain code fragments when you use the SQL Worksheet or create or edit a PL/SQL function or procedure. SQL Developer has the feature called Snippets. Snippets are code fragments such as SQL functions, Optimizer hints, and miscellaneous PL/SQL programming techniques. You can drag snippets into the Editor window. To display Snippets, select View > Snippets. The Snippets window is displayed at the right side. You can use the drop-down list to select a group. A Snippets button is placed in the right window margin, so that you can display the Snippets window if it becomes hidden. Oracle Database 11g: SQL Fundamentals II C - 23

24 Logging In to SQL*Plus 1 2 sqlplus [username[/password[@database]]]
How you invoke SQL*Plus depends on which type of operating system or Windows environment you are running. To log in from a Windows environment: 1. Select Start > Programs > Oracle > Application Development > SQL*Plus. 2. Enter the username, password, and database name. To log in from a command-line environment: 1. Log on to your machine. 2. Enter the sqlplus command shown in the slide. In the syntax: username Your database username password Your database password (Your password is visible if you enter it here.) @database The database connect string Note: To ensure the integrity of your password, do not enter it at the operating system prompt. Instead, enter only your username. Enter your password at the password prompt. 2 Oracle Database 11g: SQL Fundamentals II D - 24

25 Displaying the Table Structure
Use the SQL*Plus DESCRIBE command to display the structure of a table: DESC[RIBE] tablename Displaying the Table Structure In SQL*Plus, you can display the structure of a table using the DESCRIBE command. The result of the command is a display of column names and data types as well as an indication if a column must contain data. In the syntax: tablename The name of any existing table, view, or synonym that is accessible to the user To describe the DEPARTMENTS table, use this command: SQL> DESCRIBE DEPARTMENTS Name Null? Type DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) Oracle Database 11g: SQL Fundamentals II D - 25

26 Displaying the Table Structure
DESCRIBE departments Name Null? Type DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) Displaying the Table Structure (continued) The example in the slide displays the information about the structure of the DEPARTMENTS table. In the result: Null?: Specifies whether a column must contain data (NOT NULL indicates that a column must contain data.) Type: Displays the data type for a column Oracle Database 11g: SQL Fundamentals II D - 26

27 SQL*Plus File Commands
SAVE filename GET filename START filename @ filename EDIT filename SPOOL filename EXIT SQL*Plus File Commands SQL statements communicate with the Oracle server. SQL*Plus commands control the environment, format query results, and manage files. You can use the commands described in the following table: Oracle Database 11g: SQL Fundamentals II D - 27

28 Using the SAVE, START, and EDIT Commands
LIST 1 SELECT last_name, manager_id, department_id 2* FROM employees SAVE my_query Created file my_query START my_query LAST_NAME MANAGER_ID DEPARTMENT_ID King Kochhar ... 107 rows selected. Using the SAVE, START, and EDIT Commands SAVE Use the SAVE command to store the current contents of the buffer in a file. In this way, you can store frequently used scripts for use in the future. START Use the START command to run a script in SQL*Plus. You can also, alternatively, use the to run a script. @my_query Oracle Database 11g: SQL Fundamentals II D - 28

29 Using the SAVE, START, and EDIT Commands
EDIT my_query Using the SAVE, START, and EDIT Commands (continued) EDIT Use the EDIT command to edit an existing script. This opens an editor with the script file in it. When you have made the changes, quit the editor to return to the SQL*Plus command line. Note: The “/” is a delimiter that signifies the end of the statement. When encountered in a file, SQL*Plus runs the statement prior to this delimiter. The delimiter must be the first character of a new line immediately following the statement. Oracle Database 11g: SQL Fundamentals II D - 29

30 Summary In this lesson, you should have learned the following:
History of SQL SQL statements Browse, create, and edit database objects Execute, edit, and save SQL statements

31 References Hoffer, J., Ramesh, V., Topi, H. (2013). Modern Database Management 11th Edition, Prentice Hall. Singh, P., Pottle, B. (2009). Oracle Database 11g: SQL Fundamentals I, Oracle.


Download ppt "Database Management System 1 (ITED123A)"

Similar presentations


Ads by Google