DECODE is a function in Oracle and is used to provide if-then-else type of logic to SQL.
It is
not available in MySQL or SQL Server. The syntax for DECODE is:
SELECT DECODE ( "column_name", "search_value_1", "result_1",
["search_value_n", "result_n"],
{"default_result"} );
"search_value" is the value to search for, and "result" is the value that is displayed.
For example, assume we have the following Store_Information table,
Table Store_Information
Store_Name
Sales Txn_Date
Los Angeles
1500 Jan-05-1999
San Diego
250 Jan-07-1999
San Francisco
300 Jan-08-1999
Boston
700 Jan-08-1999
if we want to display 'LA' for 'Los Angeles', 'SF' for 'San Francisco', 'SD' for 'San Diego', and
'Others' for all other cities, we would issue the following SQL,
SELECT DECODE (Store_Name,
'Los Angeles', 'LA',
'San Francisco', 'SF',
'San Diego', 'SD',
'Others') Area, Sales, Txn_Date
FROM Store_Information;
"Area" is the name given to the column with the DECODE statement.
Result:
Area Sales Txn_Date
LA
1500 Jan-05-1999
SD
250 Jan-07-1999
SF
300 Jan-08-1999
Others 700 Jan-08-1999
To achieve what DECODE does in MySQL and SQL Server, we would use the CASE function.
Sometimes we want to have the database create a numerical primary key value as we continue to
add rows of data. In MySQL, this is done by specifying AUTO_INCREMENT to the primary
key field.
The syntax for AUTO_INCREMENT is as follows:
CREATE TABLE TABLE_NAME
(PRIMARY_KEY_COLUMN INT NOT NULL AUTO_INCREMENT
...
PRIMARY KEY (PRIMARY_KEY_COLUMN));
For example, let's say we want to create a table that consists of a primary key, last name, and
first name. We use the following SQL:
CREATE TABLE USER_TABLE
(Userid int NOT NULL AUTO_INCREMENT,
Last_Name varchar(50),
First_Name varchar(50),
PRIMARY KEY (Userid));
Upon creation, there is no data in this table.
We insert the first value:
INSERT INTO USER_TABLE VALUES ('Perry', 'Jonathan');
Now the table has the following values:
Table USER_TABLE
Userid Last_Name First_Name
1
Perry
Jonathan
Now we insert the second value:
INSERT INTO USER_TABLE VALUES ('Davis', 'Nancy');
Now the table has the following values:
Table USER_TABLE
Userid Last_Name First_Name
1
Perry
Jonathan
Davis
Nancy
By default, AUTO_INCREMENT starts with 1 and increases by 1. To change the default
starting value, you can use the ALTER TABLE command as follows:
ALTER TABLE TABLE_NAME AUTO_INCREMENT = [New Number];
where [New Number] is the starting value you want to use.
The AUTO INCREMENT interval value is controlled by the MySQL Server variable
auto_increment_increment and applies globally. To change this to a number different from the
default of 1, use the following command in MySQL:
mysql> SET @@auto_increment_increment = [interval number];
where [interval number] is the interval value you want to use. So, if we want to set the interval to
be 5, we would issue the following command:
mysql> SET @@auto_increment_increment = 5;
IDENTITY is a SQL Server-specific command, and its purpose is to allow SQL Server to
automatically insert numerical primary key values to a table as new data is inserted. This is
similar to the AUTO INCREMENT command in MySQL.
The syntax for IDENTITY is as follows:
CREATE TABLE TABLE_NAME
(PRIMARY_KEY_COLUMN INT PRIMARY KEY IDENTITY ( [Initial_Value],
[Interval] ),
...);
where [Initial_Value] is the first value of the primary key, and [Interval] is the interval between
two consecutive identity values. If no [Initial_Value] or [Interval] is specified, the default for
both is 1. In other words, the first row would be 1, and subsequent rows would get a value that is
1 larger than the previous row.
For example, let's say we want to create a table that consists of a primary key, last name, and
first name. We use the following SQL:
CREATE TABLE USER_TABLE
(Userid int PRIMARY KEY IDENTITY(2,1),
Last_Name nvarchar(50),
First_Name nvarchar(50));
Upon creation, the table is empty.
We will insert the first value:
INSERT INTO USER_TABLE VALUES ('Washington', 'George');
Now the table has the following values:
Table USER_TABLE
Userid Last_Name First_Name
2
Washington George
userid is 2 because we had specified the initial value to be 2.
Next we insert the second value:
INSERT INTO USER_TABLE VALUES ('Jefferson', 'Thomas');
Now the table has the following values:
Table USER_TABLE
Userid Last_Name First_Name
2
Washington George
Jefferson
Thomas
userid for the second row is 3 because it is 1 larger than the previous userid, which is 2.
SQL > Advanced SQL > SEQUENCE And NEXTVAL
Oracle uses the concept of SEQUENCE to create numerical primary key values as we add rows
of data into a table. In Oracle, we first need to create the sequence. The syntax for creating a
sequence is:
CREATE SEQUENCE SEQUENCE_NAME
[START WITH {Initial_Value}]
[INCREMENT BY {interval}];
{Initial_Value} is the starting value of the sequence, and {interval} is the interval between
consecutive sequence numbers. Both [START WITH] and [INCREMENT BY] are optional
fields. If they are not specified, the default {Initial_Value} and {interval} are both 1.
Next, assume we have a table as follows:
Table USER_TABLE
Userid
integer
Last_Name varchar(50)
First_Name varchar(50)
and we want to use the following sequence to generate the userid:
CREATE SEQUENCE SEQ_USER START WITH 5 INCREMENT BY 5;
We will specify that we want to use the sequence and the NEXTVAL function in the INSERT
INTO statement as follows:
INSERT INTO USER_TABLE VALUES (SEQ_USER.NEXTVAL, 'Washington',
'George');
INSERT INTO USER_TABLE VALUES (SEQ_USER.NEXTVAL, 'Jefferson', 'Thomas');
Now the table has the following two rows:
Table USER_TABLE
Userid Last_Name First_Name
5
Washington George
10
Jefferson
Thomas
It is worth noting that a sequence is independent of a table. In other words, a sequence can be
used to generate primary key values for multiple tables, and the sequence continues even if it is
being applied to a different table. So, let's say for example we have a second table, Table
NEW_USERS, which has the same structure as table USER_TABLE, and we issue the
following SQL command after executing the two SQL commands above:
INSERT INTO NEW_USER VALUES (SEQ_USER.NEXTVAL, 'Adams', 'John');
Table NEW_USER will have the following row:
Table NEW_USER
Userid Last_Name First_Name
15
Adams
John
userid is 15 because that is the next value after 10.