Sequence is a database object which is used to generate unique number. The sequence is generated and incremented by an internal oracle routine. Sequence is a sharable object which can be shared by multiple users to generate unique numbers. The same sequence can be used for multiple tables. Sequences have many purposes in database systems but the most common purpose is to generate Primary keys automatically, which must be unique for each row, sequences are created with the create sequence statement. The syntax for creating the sequence is as:-
CREATE SEQUENCE Sequence_name
[INCREMENT BY n]
[ START WITH n]
[ { Maxvalue n/ No Maxvalue} ]
[ { Minvalue n/ No Minvalue} ]
[ { Cycle / Nocycle} ]
[ { Cache n/ Nocache} }
In the syntax: - Sequence_name is the name of sequence
INCREMENT BY n specifies the interval between sequence numbers where n is an integer. The default for this clause is 1 if it is not explicitly specified.
START WITH n: It defines the first numbers that the sequence will generate if this clause is omitted then the sequence starts with 1.
Maxvalue n: It defines the largest number that the sequence will generate. if no maximum value is specified, then the default is no maxvalue which is 10^27 for an ascending sequence and -1 for descending sequence.
Minvalue n. It defines the minimum value that the sequence will generate. If no minimum value is specified then oracle assumes the default nominvalue which is 1 for ascending sequence and -10^26 for descending sequence.
Cycle : It specifies whether the sequence will continue to generate values after reaching its maximum or minimum value. Cycle option can not be used if the sequence is to generate primary key values .Nocycle is the default option which specifies the sequence not to repeat numbers after reaching their maximum or minimum value.
Cache : It defines the size of the block of sequence numbers held in memory. The default is 20.
Once the sequence is created, it can be referenced by using the CURRVAL and NEXTVAL Pseudocolumns. Nextval returns the next available sequence value. It returns a unique value every time is referenced, even for different users. Nextval can be issued only for the sequence before Currval contains a value. As sequence is a database object so you can see it in the user objects data dictionary. The Currval and nextval values can be seen from the dual table.
Modifying a sequence: A sequence can be modified by using the ALTER statement. we can change the increment value, maximum value, minimum value, cycle and cache option. START With value can’t be altered by this if you want to change the START WITH value then firstly you have drop the sequence and then recreate it. Syntax is
Alter Sequence sequence_name
[ Increment by n]
[ { Maxvalue n | No maxbvalue] }
[{ Minvalue n | No minvalue} ]
[ { cycle | No cycle} ]
[ { Cache n | no chache} ]
Information about sequence can be viewed in the USER_SEQUENCES data dictionary.
Gaps in Sequence : - Sequence are independent of the table ,so the same sequence can be used for multiple table .If this is done then each table can contain gaps in sequential numbers. Gaps in sequence can also occur if the system crashes down.
Removing a sequence: You can drop a sequence by using the Drop Sequence statement. Syntax is:-
Drop sequence sequence_name;
Once a sequence is dropped, then it can’t be refereed.
Controlling User Access
Oracle 9i provides you several methods for controlling user access. When you create users you can specify how they are authenticated, as well as set of variety of attributes. You can also modify user accounts to add and change attributes.
A primary way to control user access is through privileges. The privileges that are included in Oracle are object privileges, system privileges and role privileges. Privileges are the right to execute particular SQL statements. The database Administrator (DBA) has the ability to grant user access to the database and its objects. The users require system privileges to gain access to the database and one require object privileges to manipulate the contents of the objects in the database.
Creating users: the CREATE USER statement is used to create a user and optionally to assign additional attributes to that user. At that point the user does not have any privileges. The DBA can then grant the privileges to that user. The ALTER USER statement is used to assign any combination of account attributes to the user account but for this the account must already exist. The DBA grant the privileges to the user. The privileges determine the actions that the user can do with the objects in the database. The username can be upto 30 characters long an it can contain alphanumeric characters as well as dolor ($), Pound (#) and underscore (_) characters. The syntax for creating the user is as:
CREATE USER user
IDENTIFIED by Password;
In the syntax
User is the name of the user to be created. Password specifies that the user must log in with this password. The keywords identified by Password tells the oracle that the account is a database authenticated account.
Creating a user Authenticated by the host:
Host authentication means that you create a user in oracle whose password is validated by the underlying host system. When the user logs onto the host with a correct password, then oracle gives the user access to oracle without providing another password. These externally identified accounts are sometimes called OPS$ accounts as shown in the example:
Create User OPS$ Kashi
Identified externally;
Later, if kashi wants to connect to oracle from a host system command prompt, then he can specify the following to do so and does not need to provide a password.
C:\windows>c:\oracleora81\bin\sqlplus\
Globally Authenticated User Accounts: These types of accounts are most common in large organizations where a single sign on system is use. The keywords Identified Globally As <directory_name> tell oracle that the account user global authentication.
Assigning Attributes to Accounts:
ALTER USER statement is used to assign any combination of account attributes to the user account but for this the account must already exist users can change the password with the ALTER USER statement. The syntax for ALTER USER statement is as
ALTER USER kashi
IDENTIFIED BY Joshi;
User’s account can be altered to assign default and temporary tablespace, profiles, roles and password restrictions.
Using Profile: Profiles can be used to limit the resources that t user’s session can consume. Some of these limiting resources include connect. Some of these limiting resources include connect time, idle time, logical reads per session etc. The default profile allows unlimited resource usage before using unlimited resource usage, the limit. Ora parameter resource limit must be set to TRUE.
Create User Kashi
Identified By Joshi
Profile instruction;
Alter user kashi Profile business;