Anda di halaman 1dari 7

SEQUENCE:

=========
::Creates an Oracle sequence that can be used to automatically generate sequential
numbers during database operations.
CREATE SEQUENCE [schema.]sequence_name
[INCREMENT BY integer]
[START WITH integer]
[MAXVALUE integer | NOMAXVALUE]
[MINVALUE integer | NOMINVALUE]
[CYCLE | NOCYCLE]
[CACHE integer | NOCACHE]
[ORDER | NOORDER]

Ex:-
CREATE SEQUENCE Emp_Seq
INCREMENT BY 1
START WITH 1
MAXVALUE 1.0E28
MINVALUE 1
NOCYCLE
CACHE 20
NOORDER

INCREMENT BY
Specifies the increment between sequence numbers and can be positive or negative
(but not 0). The default is 1.

START WITH
Specifies the first sequence number to be generated. The default is the MINVALUE
for ascending sequences and MAXVALUE for descending sequences.

MAXVALUE
Specifies the largest value the sequence number can reach. The default is
NOMAXVALUE, which means the maximum value is 1027.

MINVALUE
Specifies the smallest value the sequence number can reach. The default is
NOMINVALUE, which means the minimum value is 1.

CYCLE
Specifies that when sequence numbers reach MAXVALUE they will begin again at
MINVALUE.

NOCYCLE
Specifies that after reaching the maximum value no additional sequence numbers will
be generated. This is the default.

CACHE
Specifies how many sequence numbers Oracle will pregenerate and keep in memory.
Note that when the database is shut down, unused sequence numbers stored in the
cache will be lost. The default is 20.

NOCACHE
Specifies that no sequence numbers are pregenerated to memory.

ORDER
Specifies that sequence numbers are guaranteed to be issued in order of request.
NOORDER
Specifies that sequence numbers are not guaranteed to be generated in the order of
request. This is the default.

Oracle does not support auto-increment fields in the same way that SQL Server does.

Instead, Oracle uses a sequence generator, which is a database object that is used
to generate a sequence of unique values for a primary key column, but is not
related to the table containing the column.
As a result, a sequence generator can generate unique values for more than one
table.
The SQL command CREATE SEQUENCE is used to create a new sequence.
The increment, start value, maximum value, cycling, and caching can be specified
when creating the sequence.

Oracle stores the definition of sequences for a database in a single data


dictionary table in the SYSTEM table namespace.
As a result, all sequence definitions are always available.

A sequence is referenced in SQL statements using the NEXTVAL and CURRVAL keywords.
NEXTVAL generates and returns the next sequence number while CURRVAL can be used to
refer to that value as needed.

::Changes the characteristics of an Oracle sequence, including range, number of


sequence numbers cached in memory, and whether sequential order is preserved.
ALTER SEQUENCE [schema.]sequence_name
[INCREMENT BY integer]
[MAXVALUE integer | NOMAXVALUE]
[MINVALUE integer | NOMINVALUE]
[CYCLE | NOCYCLE]
[CACHE integer | NOCACHE]
[ORDER | NOORDER]

::Removes a sequence from the database.


DROP SEQUENCE [schema.]sequence_name

The DROP SEQUENCE and CREATE SEQUENCE commands can be issued sequentially to
restart a sequence at a lower number.
However, all GRANTs to the sequence will also have to be recreated.

ALWAYS AS IDENTITY:
===================
We are using ALWAYS to force the use of IDENTITY. If we try to insert value into
the
IDENTITY column it will throw error saying cannot insert into a generated always
identity column.

Ex:-
CREATE TABLE customers (
cid number GENERATED ALWAYS AS IDENTITY,
cname varchar(10)
);

INSERT INTO customers(cname) VALUES ('Ram');

==>>We are using ALWAYS to force the use of IDENTITY. If we try to insert value
into
the IDENTITY column it will throw error saying cannot insert into a generated
always identity column.
INSERT INTO customers(cname) VALUES (50,'Ram'); ==> Error

BY DEFAULT AS IDENTITY:
-----------------------
Create IDENTITY column with BY DEFAULT option to insert values into IDENTITY column

by referencing it in insert statement.

Ex:-
CREATE TABLE customers (
cid number GENERATED BY DEFAULT AS IDENTITY,
cname varchar(10)
);

INSERT INTO customers(cname) VALUES ('Happy');

==>If you are specifying identity column in insert statment then it will insert
whatever
value we given if not then it will insert automatically incremented value into the
IDENTITY column.

INSERT INTO customers(cname) VALUES ('Deepak');


INSERT INTO customers(cname) VALUES (100, 'Avinash');
INSERT INTO customers(cname) VALUES ('Sonu');

==>But we cannot insert NULL value it will throw error


INSERT INTO customers(cname) VALUES ('Arvind');
INSERT INTO customers(cname) VALUES (null,'Avinash'); --> Error

BY DEFAULT ON NULL AS IDENTITY:


===============================
Use BY DEFAULT ON NULL option to create identity column to use auto incremented
value
if you specily NULL in identity column in insert statement.

Ex:-
CREATE TABLE customers (
cid number GENERATED BY DEFAULT ON NULL AS IDENTITY,
cname varchar(10)
);

INSERT INTO customers(cname) VALUES ('Deepak');


INSERT INTO customers(cname) VALUES (100, 'Avinash');
INSERT INTO customers(cname) VALUES ('Sonu');
INSERT INTO customers(cname) VALUES (null,'Avinash');

=============>>>>>>>
Additionally we can change the initial value of identity column and interval
between the auto generated values by using following options. The default initial
value and interval values for auto increment identity columns equals to 1.
START WITH initial_value
INCREMENT BY interval_value

CREATE TABLE customers (


cid number GENERATED BY DEFAULT ON NULL AS IDENTITY START WITH 100,
cname varchar(10)
);

CREATE TABLE customers (


cid number GENERATED BY DEFAULT ON NULL AS IDENTITY START WITH 100 INCREMENT BY 2,
cname varchar(10)
);

Limitations of IDENTITY Coulmn:


===============>>>>>>>>>>>>>>>>>>
1.> We can add only one IDENTITY column per table.
2.> We can add identity_clause only on Numeric datatype columns not on User-defined
data types.
3.> We cannot add DEFAULT clause in column defincation if we use identity_clause.
4.> We can create one table from another by adding a AS SELECT statement at the end
of the CREATE TABLE
for example �create table new_emp AS SELECT * from emp�; New table wont
inherit IDENTITY property on column.
5.> When you add an identity_clause, then NOT NULL constraint and NOT DEFERRABLE
constraint are added by default (implicitly specified).
If you add an inline constraint that conflicts with NOT NULL and NOT
DEFERRABLE, then an error will occur.

create table customers(


cid int GENERATED BY DEFAULT AS IDENTITY primary key,
cname varchar(10),
email varchar(15),
phone long,
city varchar(10),
bal float);

create table customers(


cid int primary key,
cname varchar(10),
email varchar(15),
phone varchar(10),
city varchar(10),
bal binary_double);

MySQL:
======
create table customers(
cid int primary key,
cname char(10),
email char(15),
phone long,
city char(10),
bal double);
Oracle Built in Datatypes
=========================
1. Character Datatypes
CHAR (size ) size: Byte/Char
VARCHAR2 (size ) size: Byte/Char
NCHAR (size) size: Byte
NVARCHAR2 (size) size: Byte

2. Number Datatypes
NUMBER(precision, scale)
FLOAT(precision)
BINARY_FLOAT
BINARY_DOUBLE

3. Long and Raw Datatypes


LONG
LONG RAW
RAW (size)

4. Datetime Datatypes
DATE
TIMESTAMP(fractional_seconds_precision) WITH LOCAL TIME ZONE
INTERVAL YEAR(year_precision) TO MONTH
INTERVAL DAY(day_precision) TO SECOND(fractional_seconds_precision)

5. Large Object Datatypes


BLOB
CLOB
NCLOB
BFILE

6. Rowid Datatypes
ROWID
UROWID(size)

ANSI Supported Datatypes


========================
CHARACTER VARYING(size)
CHAR VARYING (size)
NCHAR VARYING (size)
VARCHAR(size)
NATIONAL CHARACTER VARYING(size)
NATIONAL CHAR VARYING(size)

NUMERIC(precision, scale)
DECIMAL(precision, scale)
DEC(precision, scale)

INTEGER
INT
SMALLINT

FLOAT(size)
DOUBLE PRECISION
REAL

===================================================================================
=============
Types of SQL Statements:
=======================

1. Data Definition Language (DDL) Statements


Data definition language (DDL) statements let you to perform these tasks:
Create, alter, and drop schema objects- CREATE, ALTER, and DROP
Grant and revoke privileges and roles- GRANT, REVOKE
Analyze information on a table, index, or cluster- ANALYZE
Establish auditing options- AUDIT
Add comments to the data dictionary- COMMENT
=>The CREATE, ALTER, and DROP commands require exclusive access to the specified
object.
For example, an ALTER TABLE statement fails if another user has an open
transaction on the specified table.
=>The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not require exclusive
access to the specified object.
For example, you can analyze a table while other users are updating the table.
=>Oracle Database implicitly commits the current transaction before and after every
DDL statement.
=>The DDL statements are:
ALTER ... (All statements beginning with ALTER)
ANALYZE
ASSOCIATE STATISTICS
AUDIT
COMMENT
CREATE ... (All statements beginning with CREATE)
DISASSOCIATE STATISTICS
DROP ... (All statements beginning with DROP)
FLASHBACK ... (All statements beginning with FLASHBACK)
GRANT
NOAUDIT
PURGE
RENAME
REVOKE
TRUNCATE
UNDROP
=>GRANT and REVOKE is also called Data Control Language(DCL) which mainly deals
with the rights, permissions and other controls of the database system.
GRANT-gives user�s access privileges to database.
REVOKE-withdraw user�s access privileges given by using the GRANT command.
=>DDL statements are supported by PL/SQL with the use of the DBMS_SQL package.

2. Data Manipulation Language (DML) Statements


Data manipulation language (DML) statements access and manipulate data in existing
schema objects. These statements do not implicitly commit the current transaction.
The data manipulation language statements are:
CALL
DELETE
EXPLAIN PLAN
INSERT
LOCK TABLE
MERGE
SELECT
UPDATE
=>The SELECT statement is a limited form of DML statement in that it can only
access data in the database.
It cannot manipulate data in the database, although it can operate on the accessed
data before returning the results of the query.
=>The CALL and EXPLAIN PLAN statements are supported in PL/SQL only when executed
dynamically.
All other DML statements are fully supported in PL/SQL.

3. Transaction Control Statements


Transaction control statements manage changes made by DML statements.
The transaction control statements are:
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
=>All transaction control statements, except certain forms of the COMMIT and
ROLLBACK commands, are supported in PL/SQL.

4. Session Control Statements


Session control statements dynamically manage the properties of a user session. The
session control statements are:
ALTER SESSION
SET ROLE
=>These statements do not implicitly commit the current transaction.
=>PL/SQL does not support session control statements.

5. System Control Statement


The system control statement, dynamically manages the properties of an Oracle
Database instance.
There is only one System control statement:
ALTER SYSTEM
=>This statement does not implicitly commit the current transaction
=>PL/SQL does not support session control statements.

6. Embedded SQL Statements


Embedded SQL statements place DDL, DML, and transaction control statements within a
procedural language program.
Embedded SQL is supported by the Oracle precompilers.

Anda mungkin juga menyukai