=========
::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.
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.
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)
);
==>>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
Ex:-
CREATE TABLE customers (
cid number GENERATED BY DEFAULT AS IDENTITY,
cname varchar(10)
);
==>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.
Ex:-
CREATE TABLE customers (
cid number GENERATED BY DEFAULT ON NULL AS IDENTITY,
cname varchar(10)
);
=============>>>>>>>
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
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
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)
6. Rowid Datatypes
ROWID
UROWID(size)
NUMERIC(precision, scale)
DECIMAL(precision, scale)
DEC(precision, scale)
INTEGER
INT
SMALLINT
FLOAT(size)
DOUBLE PRECISION
REAL
===================================================================================
=============
Types of SQL Statements:
=======================