0% found this document useful (0 votes)
68 views

Difference Between Varchar and Varchar2 Data Types?: Salary Decimal (9,2) Constraint Sal - CK Check (Salary 10000)

The document discusses various Oracle database concepts including the difference between varchar and varchar2 data types, views and their advantages, the differences between DECODE and CASE functions, how to use the ANALYZE command to collect statistics and validate objects, creating tables with constraints, indexes and their types, using cursors to retrieve data, differences between functions and procedures, and tips for choosing an advantageous join order to improve query performance.

Uploaded by

bittuankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Difference Between Varchar and Varchar2 Data Types?: Salary Decimal (9,2) Constraint Sal - CK Check (Salary 10000)

The document discusses various Oracle database concepts including the difference between varchar and varchar2 data types, views and their advantages, the differences between DECODE and CASE functions, how to use the ANALYZE command to collect statistics and validate objects, creating tables with constraints, indexes and their types, using cursors to retrieve data, differences between functions and procedures, and tips for choosing an advantageous join order to improve query performance.

Uploaded by

bittuankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Difference between varchar and varchar2 data types?

Varchar can store upto 2000 bytes and varchar2 can store upto 4000 bytes. Varchar will occupy space
for NULL values and Varchar2 will not occupy any space. Both are differed with respect to space.

Coalesce ( ) function can take N number of arguments and as a result it returns the first not null
parameter from the argument list. If in case all the arguments are evaluated as null then this function
returns NULL as a result. 

A view is a logical table based on one or more tables or views.

Views offer the following advantages:

1. Ease of use: A view hides the complexity of the database tables from end users.
Essentially we can think of views as a layer of abstraction on top of the database
tables.
2. Space savings: Views takes very little space to store, since they do not store actual
data.
3. Additional data security: Views can include only certain columns in the table so that
only the non-sensitive columns are included and exposed to the end user. In addition,
some databases allow views to have different security settings, thus hiding sensitive
data from prying eyes.

The Difference Between DECODE and CASE


1. CASE can work with logical operators other than ‘=’
2.DECODE works with expressions that are scalar values only. CASE can work with predicates and
subqueries in searchable form.
3. CASE can work as a PL/SQL construct
Explain about the ANALYZE command in Oracle?

This “Analyze” command is used to perform various functions on index, table, or cluster. The
following list specifies the usage of ANALYZE command in Oracle:

 Analyze command is used to identify migrated and chained rows of the table or a cluster.
 It is used to validate the structure of an object.
 This helps in collecting the statistics about the object used by the user and are then stored on
to the data dictionary.
 It also helps in deleting statistics that are used by an object from the data dictionary.

CREATE TABLE SAMP.EMP


(
EMPNO CHAR(6) NOT NULL CONSTRAINT EMP_PK PRIMARY KEY,
FIRSTNME CHAR(12) NOT NULL,
MIDINIT vARCHAR(12) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
SALARY DECIMAL(9,2) CONSTRAINT SAL_CK CHECK (SALARY >= 10000),
BONUS DECIMAL(9,2),
TAX DECIMAL(9,2),
CONSTRAINT BONUS_CK CHECK (BONUS > TAX)
);

CREATE TABLE CONDOS


(
CONDO_ID INT NOT NULL CONSTRAINT hotels_PK PRIMARY KEY,
CONDO_NAME VARCHAR(40) NOT NULL,
CITY_ID INT CONSTRAINT city_foreign_key
REFERENCES Cities ON DELETE CASCADE ON UPDATE RESTRICT
);

What is an Index? Explain its different types.

A database index is a data structure that provides a quick lookup of data in a column
or columns of a table. It enhances the speed of operations accessing data from a
database table at the cost of additional writes and memory to maintain the index
data structure.

CREATE INDEX index_name /* Create Index */


ON table_name (column_1, column_2);
DROP INDEX index_name; /* Drop Index */

What is Cursor? How to use a Cursor?

A database cursor is a control structure that allows for the traversal of records in a
database. Cursors, in addition, facilitates processing after traversal, such as retrieval,
addition, and deletion of database records. They can be viewed as a pointer to one
row in a set of rows.

Working with SQL Cursor:


1. DECLARE a cursor after any variable declaration. The cursor declaration must always
be associated with a SELECT Statement.
2. Open cursor to initialize the result set. The OPEN statement must be called before
fetching rows from the result set.
3. FETCH statement to retrieve and move to the next row in the result set.
4. Call the CLOSE statement to deactivate the cursor.
5. Finally use the DEALLOCATE statement to delete the cursor definition and release
the associated resources.
DECLARE @name VARCHAR(50) /* Declare All Required Variables */
DECLARE db_cursor CURSOR FOR /* Declare Cursor Name*/
SELECT name
FROM myDB.students
WHERE parent_name IN ('Sara', 'Ansh')
OPEN db_cursor /* Open cursor and Fetch data into @name */
FETCH next
FROM db_cursor
INTO @name
CLOSE db_cursor /* Close the cursor and deallocate the resources */
DEALLOCATE db_cursor
A function has a return type and returns a value.
A procedure does not have a return type. But it returns values using the OUT parameters.

You cannot use a function with Data Manipulation queries. Only Select queries are allowed
in functions.
You can use DML queries such as insert, update, select etc… with procedures.

You cannot call stored procedures from a function


You can call a function from a stored procedure.

You can call a function using a select statement.


You cannot call a procedure using select statements.

A function is compiled and executed every time whenever it is called.

Stored Procedures are pre-compiled objects which are compiled for the first
time and its compiled format is saved, which executes (compiled code)
whenever it is called.

Performance :
Choose an Advantageous Join Order

Join order can have a significant effect on performance. The main objective of
SQL tuning is to avoid performing unnecessary work to access rows that do not
affect the result. This leads to three general rules:

 Avoid a full-table scan if it is more efficient to get the required rows through
an index.

 Avoid using an index that fetches 10,000 rows from the driving table if you
could instead use another index that fetches 100 rows.

 Choose the join order so as to join fewer rows to tables later in the join order

Tables should be arranged from smallest effective number of rows to largest effective
number of rows.

A.2.2 Bypassing the Query Optimizer

Normally optimizer picks the best execution plan, an optimal order of tables to be
joined. In case the optimizer is not producing a good execution plan you can control
the order of execution using the HINTS feature SQL. For more information see the
Oracle Database Lite SQL Reference.

For example, if you want to select the name of each department along with the name
of its manager, you can write the query in one of two ways. In the first example which
follows, the hint /*+ordered*/ says to do the join in the order the tables appear in
the FROM clause.

SELECT /*+ordered*/ d.NAME, e.NAME

FROM DEPT d, EMP e

WHERE d.MGR = e.SS#

Use Untransformed Column Values

Use untransformed column values. For example, use:


WHERE a.order_no = b.order_no

You might also like