Oracle11g SQL Lab Guide

March 4, 2018 | Author: Amit Sharma | Category: Sql, Table (Database), Databases, Computer Programming, Information Retrieval
Share Embed Donate


Short Description

Download Oracle11g SQL Lab Guide...

Description

Oracle 11g SQL Lab Guide Part I This is another document from our learn Oracle SQL query series. We are committed to provide the BEST learning materials to the beginners. In the series, this document assists new bees to learn SQL Queries. The document teaches steps by steps way to write simple to complex SQL queries. The document consists of many examples to ease your learning. Join our professional training program to learn from experts.

Provided By: BISP http://bispsolutions.wordpress.com learnsqlquery.wordpress.com

learnsqlquery.wordpress.com

Created By: Abhishek Kumar Srivastava Reviewed By: Amit Sharma Team BISP

|

SQL Lab Guide

1

Table of Content……………………………………………………………………..2-7 (A)

Introduction…………………………………………………………………………………..9-14 Oracle 11g Database……………………………………………………………………………….9 Data Models…………………………………………………………………………………………….9 Entity Relationship Model………………………………………………………………………10 Relational Database………………………………………………………………………………..10 Using SQL Commands……………………………………………………………………………..10 SQL Development Environment………………………………………………………………11 Oracle SQL Developer……………………………………………………………………………..12-14 Creating Database Connection………………………………………………………………..12-14

1. Data Retrieving by using SELECT statement………………………….15-22 Creating Table…………………………………………………………………………………………15 Insert Data into Table……………………………………………………………………………..15 Using SELECT statement for All Columns…………………………………………………15-16 Using SELECT statement for Selecting Specific Columns………………………….16 Way of writing of SQL Statement……………………………………………………………..16-17 Executing SQL Statements……………………………………………………………………….17 Column Names in a Table………………………………………………………………………...18 Using Arithmetic Expressions in SQL……………………………………………………….18-19 Operators Precedence……………………………………………………………………………..19-20 Column Aliases…………………………………………………………………………………………20 Defining and Using Concatenation Operator……………………………………………21 Defining and Using Literal Character Strings…………………………………………...21 learnsqlquery.wordpress.com

|

SQL Lab Guide

2

Displaying Structure of Table…………………………………………………………………..22

2. Restricting and Sorting Data…………………………………………………23-29 Using WHERE Clause……………………………………………………………………………….23 Character Strings and Dates……………………………………………………………………23 Defining and Using Comparison Operator………………………………………………24-25 Using BETWEEN Operator as for Range Condition………………………………....25-26 Using LIKE Operator for Pattern Matching……………………………………………..26 Wildcard Characters……………………………………………………………………………….27 Using NULL Conditions……………………………………………………………………………27 Defining conditions using Logical Operators………………………………………….28-29 Using AND Operator………………………………………………………………………………..28 Using OR Operator…………………………………………………………………………………..28 Using NOT Operator…………………………………………………………………………………29

3. Using Single-Row Functions to Customize Output………………….30-34 SQL Functions………………………………………………………………………………………….30 Single-Row Functions………………………………………………………………………………30 Character Functions…………………………………………………………………………………30 Using Case Conversion Function………………………………………………………………31-32 Using Character Multiplication Functions………………………………………………..32 Number Functions……………………………………………………………………………………32-33 •

Using ROUND Function………………………………………………………………………..32



Using TRUNC Function…………………………………………………………………………33



Using MOD Function…………………………………………………………………………….33

Working with DATE Function…………………………………………………………………..33-34 learnsqlquery.wordpress.com

|

SQL Lab Guide

3



SYSDATE……………………………………………………………………………………………..34

4. Using Conversion Functions and Conditional Expressions……..35-73 Conversion Function………………………………………………………………………………...35 •

Implicit………………………………………………………………………………………………..35

Using TO_CHAR Function with DATE…………………………………………………………35 Using Nesting Function…………………………………………………………………………….36-37 Using NVL Function………………………………………………………………………………….36 Using NVL2 Function………………………………………………………………………………..36 Using NULLIF Function…………………………………………………………………………….36 Using COALESCE Function………………………………………………………………………..36 Conditional Expressions…………………………………………………………………………..37 •

CASE Expression………………………………………………………………………………….37



DECODE Function………………………………………………………………………………..37

5. Reporting aggregated Data using Group Functions………………38-42 Group Functions……………………………………………………………………………………..38 Using AVG and SUM functions…………………………………………………………………38 Using MAX and MIN functions…………………………………………………………………39 Using the COUNT function……………………………………………………………………….39 Using the DISTINCT keyword…………………………………………………………………..40 Group Functions and NULL Values…………………………………………………………..40 Creating Groups of Data…………………………………………………………………………..40-41 Using Group By clause on multiple columns……………………………………………41 Using Having Clause……………………………………………………………………….………..41 Nesting Group Functions………………………………………………………………………….42 learnsqlquery.wordpress.com

|

SQL Lab Guide

4

6. Displaying Data from Multiple Tables…………………………………….43-45 Types of JOIN…………………………………………………………………………………………….43 •

Natural Joins……………………………………………………………………………………….43-44 Natural Join clause…………………………………………………………………………43 USING clause………………………………………………………………………………….44 ON clause……………………………………………………………………………………….44 Non-equijoins……………………………………………………………………………………...44



Outer Joins…………………………………………………………………………………………..45 Left Outer……………………………………………………………………………………….45 Right Outer Join……………………………………………………………………………..45 Full Outer Join………………………………………………………………………………..45



Cross Joins…………………………………………………………………………………………..45

7. Using Sub queries to solve queries…………………………………………46-48 Sub-query…………………………………………………………………………………………………46 Single-row sub-query……………………………………………………………………………….46-47 Using Group Functions in a sub-query………………………………………………………47 Using HAVING clause with sub-queries……………………………………………………..47 Multiple-row sub-query…………………………………………………………………………….48 IN Operator……………………………………………………………………………………..48 ANY Operator………………………………………………………………………………….48 ALL operator…………………………………………………………………………………...48

8. Using the SET Operators………………………………………………………..49-52 learnsqlquery.wordpress.com

|

SQL Lab Guide

5

SET Operators………………………………………………………………………………………….49 Oracle server and Set operators………………………………………………………………49 Using the UNION Operator……………………………………………………………………….49-50 Using the UNION ALL Operator………………………………………………………………...50 Using INTERSECT Operator………………………………………………………………………51 Using MINUS Operator……………………………………………………………………………..51 Using ORDER BY clause in set operations………………………………………………...51-52

9. Data Manipulation………………………………………………………………….53-59 Data Manipulation Language……………………………………………………………………53 Adding New Row in a table……………………………………………………………………….53 Inserting rows with null values………………………………………………………………...53 Inserting special values…………………………………………………………………………….54 Inserting specific Date Values……………………………………………………………………54 Creating a Script………………………………………………………………………………………..54 Copying Rows from one table to another……………………………………………………55 Updating data in a table………………………………………………………………………………55 Updating rows based on another table……………………………………………………….55 Removing rows from a table………………………………………………………………………..56 TRUNCATE Statement………………………………………………………………………………….56 Database Transaction………………………………………………………………………………….57 Implicit Transaction Processing…………………………………………………………………..57 Explicit Transaction Processing……………………………………………………………………58 State of data before COMMIT or ROLLBACK………………………………………………….58 Using COMMIT statement…………………………………………………………………………58 learnsqlquery.wordpress.com

|

SQL Lab Guide

6

Using ROLLBACK statement……………………………………………………………………..59 Statement-Level Rollback………………………………………………………………………..59

10. Using DDL statements to create and manage tables………………60-63 Database Objects……………………………………………………………………………………..60 Naming Rules…………………………………………………………………………………………..60 CREATE Table Statement………………………………………………………………………….60-61 Data Types……………………………………………………………………………………………….61-62 Constraints……………………………………………………………………………………………….62 Using NOT NULL Constraint……………………………………………………………………...63 Using UNIQUE Constraint………………………………………………………………………….63

11. Creating Other Schema Objects………………………………………………64-68 Database Objects……………………………………………………………………………………..64 Views……………………………………………………………………………………………………….64 Simple and Complex views……………………………………………………………………….64 Creating View…………………………………………………………………………………………..64-65 Retrieving data from a View…………………………………………………………………….65 Modifying a View……………………………………………………………………………………...65-66 Creating a Complex View………………………………………………………………………….66-67 Rules for performing DML operations on a View………………………………………67 Using WITH CHECK OPTION clause……………………………………………………………67 Denying DML Operations…………………………………………………………………………..68 Removing a View……………………………………………………………………………………….68 Sequence……………………………………………………………………………………………………68

Introduction learnsqlquery.wordpress.com

|

SQL Lab Guide

7

Oracle 11g Database:Oracle 11g database is designed for some features, which helps to the organizations to manage everything easily and deliver high-quality services:DBA’s can increase their productivity, reduce costs, minimize errors and maximize the quality of services by using the management automation, fault diagnostics features, etc. We can reduce the risk of down time and data loss by using the high availability features. Here the performance of the database is so improved because of OLTP, secure files etc and it helps to secure the information of organization with the data encryption and masking, and complex auditing capabilities, so it’s protected.

Data Model:A data model is used to organize data. It captures the cardinality and referential integrity rules needed to ensure that the data is of good quality for the users. The main aim of data models is to support the development of information systems by providing the definition and format of data.

Figure- A.1

Entity Relationship Model:In software engineering, an entity-relationship model (ERM) is an abstract and conceptual representation of data. learnsqlquery.wordpress.com

|

SQL Lab Guide

8

Relational Database:A relational database matches data by using common characteristics found within the data set. The resulting groups of data are organized and are much easier for many people to understand. It means it can contain one or many tables.

Figure- A.2 Diagrams created by this process are called entity-relationship diagrams, ER diagrams, or ERDs.

Using SQL Command:SQL is a standard language for accessing databases. It is a computer language aimed to store, manipulate, and query data stored in relational databases. SQL provides statements for variety of tasks, including: •

Querying data



Inserting, updating, and deleting rows in a table



Creating, replacing, altering, and dropping objects



Controlling access to the database and its objects

SQL Statements: Here are the SQL Statements, which we are used for querying with database, as1.

Data Manipulation Language(DML)-

learnsqlquery.wordpress.com

|

SQL Lab Guide

9

Retrieve data from database, enter new rows, Changes in existing rows, and removes the rows which we no need to use from the tables in the database. SELECT, INSERT, UPDATE, DELETE, MERGE. 2.

Data Definition Language(DDL)-

Sets up, changes and removes data structures from tables. CREATE, ALTER, DROP, RENAME, TRUNCATE, COMMENT. 3.

Data Control Language(DCL)-

Provides or removes access rights to Oracle database and structures within it. GRANT, REVOKE. 4.

Transaction Control-

Manages the changes made by DML statements. COMMIT, ROLLBACK, SAVEPOINT.

SQL Development Environment:For this, we used SQL Developer and SQLPLUS.

Oracle SQL Developer and Database Connection: After installing the Oracle 11g on Oracle Enterprise Linux5, login as the Oracle user.

learnsqlquery.wordpress.com

|

SQL Lab Guide

10

Now first, as we have to open the SQLDEVELOPER, so for opening this, open the Terminal and go to the folder where SQLDEVELOPER exists. As shown in the Green box. And run the sqldeveloper shell file. Now execute this command as an oracle user:$ cd /u01/app/oracle/product/11.1.0/db_1/sqldeveloper $ ./sqldeveloper.sh

Figure: A.3 After executing this command, sqldeveloper starts to work properly.

Figure: A.4 Now we have to open the SQLPLUS. So for running this, we have to execute the following commands. So we have to set the ’ORACLE_SID’, ‘ORACLE_BASE’, and ‘ORACLE_HOME’.

learnsqlquery.wordpress.com

|

SQL Lab Guide

11

For setting the ORACLE_HOME, we have to set the path for the db_home as here db_1:$ export ORACLE_SID=orcl $ export ORACLE_BASE=/u01/app/oracle/product/11.1.0/db_1 $ export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1 And then set the Environment Variable as ‘orcl’ as shown in the figure:$ oraenv

Figure: A.5 After the Environment variable, go to the db_home as db_1 here and give the permission to the sqlplus and then go inside the ‘bin’ folder and again give the permission to sqlplus and execute the ‘.sqlplus’ shell file. $ cd /u01/app/oracle/product/11.1.0/db_1 learnsqlquery.wordpress.com

|

SQL Lab Guide

12

$ chmod 777 sqlplus $ cd bin $ chmod 777 sqlplus $. /sqlplus After executing these commands the sqlplus wants the user_name and password before starts. So give the correct user_name and password. Then sqlplus is ready for use that is shown in the green box.

Chapter- 1 1. Creating Table: - For creating table, we have to give the table name and the

columns name and their data type and the size of the data which we can insert. Now execute the following SQL command for creating the table-

learnsqlquery.wordpress.com

|

SQL Lab Guide

13

Figure-1.1 2. Insert Data into Table: - When table will create, and then we have to insert data

into the table, which will be inserting in the following columns that are specified before. So we can insert data by executing the following SQL command-

Figure-1.2 3. Using SELECT Statement for All Columns: -

SELECT identifies the columns to be displayed. FROM identifies the table containing those Columns.

Figure-1.3 We can display all columns of data in a table by executing the SELECT statement with an asterisk (*). In the above example, the employee table contains two columns name and address. The table contains four rows of data for each column. learnsqlquery.wordpress.com

|

SQL Lab Guide

14

We can also show all data by executing all the column names-

Figure-1.4 4. Selecting some Specific Columns: - We can use the SELECT statement to display

some specific columns only.

Figure-1.5 5. Ways of Writing SQL Statement: -



SQL Statements are not case sensitive.



SQL Statements can be entered on more than one line.



Keywords cannot be Split or abbreviated.



In SQL * Plus, we are required to end each SQL Statement with a Semicolon (;). But in SQL Developer it’s optional, but when we execute multiple SQL Statements then Semicolon is required.

6. Executing SQL Statements: -In SQL Developer, click on the Run Script icon or

press F5 button to run the command in SQL Worksheet. We can also click on Execute Statement or press F9 button to run the command in SQL Worksheet. learnsqlquery.wordpress.com

|

SQL Lab Guide

15

Figure-1.6 In SQL * Plus, terminate the SQL Statement with a semicolon (*) and press ENTER to run the command. 7. Column Names in a Table: - Column name in SQL Developer is always display in

Left-aligned and in Upper case. Column name in SQL * Plus also is always display in Left-aligned and in Upper case.

learnsqlquery.wordpress.com

|

SQL Lab Guide

16

Figure-1.7 8. Using Arithmetic Expressions: -We may need to modify the data and also have to

perform some calculations. All these are possible using the arithmetic expressions. An arithmetic expression contains the Column names, Arithmetic operators and Constant numeric values. These are the arithmetic operators+ (Add), – (Subtract), *(Multiply), / (Divide) w, we have a table and there is a salary column and we have to update this column.

learnsqlquery.wordpress.com

|

SQL Lab Guide

17

Figure-1.8 Now, we will increase the salary of all employees by 500. So execute the following command-

Figure-1.9 9. Operators Precedence: -If in an arithmetic expression, more than one operator is

used then there is some precedence for using the operators. So there are some rules of precedence of the arithmetic operators•

Multiplication and Division operators occur before the Addition and Subtraction operators.



Operators of same priority are evaluated from left to right.

learnsqlquery.wordpress.com

|

SQL Lab Guide

18



Parentheses are used to override the default precedence.

Figure-1.10 n the above example, there used the parentheses and the operators also. 10.

Column Aliases: -By this, we can rename the column headings. There is used AS keyword in between the column name and the alias. There is used Double quotation mark (“”), if alias contains space or Special Characters.

Figure-1.11 In the above example, there used Double quotes for alias name because there is space in between the name. And now we can see the Column name has been changed. 11. Concatenation Operator: -It links Columns or character strings to other columns.

It is represented by two vertical bars (||). learnsqlquery.wordpress.com

|

SQL Lab Guide

19

Figure-1.12 12.

Literal Character String: -A literal is a character, a number, or a date that is included in SELECT statement.

Figure-1.13

Character and Date literals must be enclosed within Single Quote marks (‘‘) whereas Number literals need not be enclosed. Like in above example, ‘from’ is as literal character string.

learnsqlquery.wordpress.com

|

SQL Lab Guide

20

13.

Displaying Structure of Table: -Use the DESC command to display the structure of a table.

Figure-1.14

Chapter- 2 1. Using WHERE Clause: -A WHERE clause contains a condition that must be met and

it directly follows FROM clause. If condition will be true, the row meeting the condition is returned. learnsqlquery.wordpress.com

|

SQL Lab Guide

21

The WHERE clause can compare values in columns, literal, arithmetic expression and functions.

Figure-2.1 In the above example, we used the condition with name and getting the employee_key in result. 2. Character Strings and Dates: - Character strings and Date values are enclosed with

single quotation marks (‘’). Character values are case-sensitive and Date values are Format-sensitive.

Figure-2.2 In the above example, there is condition with character string, so we enclosed it with single quotation marks. Default format for displaying the date is DD-MM-YR. 3. Comparison Operators: -Comparison operators are used in conditions that

compare one expression with another expression or value. They are used with WHERE clause like thisSQL> Select expr from table_name where expr operator value; Here are some operators, which are used for comparing two expressions or valueslearnsqlquery.wordpress.com

|

SQL Lab Guide

22

=

(Equal to),

>

(Greater than),

>=

(Greater than or equal to),

<

(Less than),

, =,
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF