DECODE For SQL Sample Coding
DECODE For SQL Sample Coding
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
Perry
Jonathan
Davis
Nancy
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:
Washington George
Washington George
Jefferson
Thomas
userid for the second row is 3 because it is 1 larger than the previous userid, which is 2.
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