Vous êtes sur la page 1sur 3

Marc Angelou D.

Lopez

ICT 2A1

CREATE TABLE INDEXES AND SYNONYMS


SQL CREATE INDEX Statement

The CREATE INDEX statement is used to create indexes in tables.

Indexes are used to retrieve data from the database very fast. The users cannot
see the indexes, they are just used to speed up searches/queries.

What is an INDEX?

 A Schema object that can speed up the retrieval of rows by using a


POINTER (isles in a grocery store)
 If you do not have an index on the column you’re selecting,then a full
table scan occurs
 UNIQUE INDEX – Automatically created when you define a column in a
table to have a PRIMARY OR A UNIQUE KEY constraint.
 Non- Unique index – An index that a user can create to speed up to
access to the rows, For example,to optimize joins, you can create an
index on the FOREIGN KEY Column,Which speeds up the search to match
rows to the PRIMARY KEY Column.

Example of INDEX

 When to create an index


o The column contains a wide range of values
o A column contains a large number of null values
o One or more columns are frequently used together in WHERE
clause or a join condition
o The table is large and most queries are expected to retrieve less
than 2-4% of the rows
 When not to create an index
o The table is small
o The columns are not often used as a condition in the query
o Most queries are expected to retrieve more than 2-4% of the rows
in the table
o The table is updated frequently -DML required index updates
o The indexed columns are referenced as part of an expression
EXAMPLE OF AN INDEX

 CREATE INDEX index_name ON table_name (Column…,column);


 CREATE INDEX d_cds_name_email_idx ON d_clients (Last_name,email);
 DROP INDEX d_cds_name_email_idx;

Composite Index

 Created on multiple columns in a table


 Columns can be in any order
 Columns need to be adjacent
 Speeds up retrieval when: Where reference all columns or leading portion
of columns
 Null values not included in composite index

Confirming indexes

 SELECT
o Ic.index_name,ic.column_name,ic.column_position
col_pos,ix.uniqueness
FROM user _indexes ix, user_ind_columns ic
WHERE ic.Index_name = ix.index_name AND ic.table_name –
‘EMPLOYEES’;

Function based indexes

 CREATE INDEX upper_last_name_idx ON employees


(UPPER(Last_name));
 SELECT* FROM employees WHERE UPPER (last_name) = ‘king’;

Removing an index

 You can not modify indexes


 To change them you must delete and create again
 To DROP an index you must be the owner or have DROP ANY INDEX
privilege.
 If you drop a table it automatically drops constrains and indexes,but
views and sequences remain.
What is synonym?

A synonym is an alternative name for objects such as tables, views, sequences, stored
procedures, and other database objects. You generally use synonyms when you are
granting access to an object from another schema and you don't want the users to have to
worry about knowing which schema owns the object.

Example of synonym

 Create [PUBLIC]SYNONYM synonym_name FOR OBJECT;


 CREATE SYNONYM emp FOR ussc_bhs_sql01_s02.employees;
o PUBLIC: creates a synonym accessible to all users (we don’t have
the privilege to use PUBLIC in APEX)
o Synonym_name : is the name of the synonym to be created
o Object : identifies the object for which the synonym is created
o A private synonym name must be distinct from all other objects
owned by the same user

Remove synonym

o DROP [PUBLIC] SYNONYM name_of_synonym;


o DROP SYNONYM dj_titles;
o Guidelines:
 Object cannot be contained in a package
 A private synonym name must be distinct from all other objects
owned by the same user.

Vous aimerez peut-être aussi