SAP HANA Introduction
- SAP HANA - Core Architecture
- SAP HANA - Information Modeler
- SAP HANA - System Monitor
- Studio Administration View
- SAP HANA - Studio
- In-Memory Computing Engine
- SAP HANA - Overview
SAP HANA Modeling
- SAP HANA - Export and Import
- SAP HANA - Information Composer
- SAP HANA - Analytic Privileges
- SAP HANA - Calculation View
- SAP HANA - Analytic View
- SAP HANA - Attribute View
- SAP HANA - Packages
- SAP HANA - Tables
- SAP HANA - Data Warehouse
- SAP HANA - Modeling
SAP HANA Reporting
- SAP HANA - Excel Integration
- SAP HANA - Crystal Reports
- Bi 4.0 Connectivity to HANA Views
- SAP HANA - Reporting View
SAP HANA Security
- SAP HANA - Auditing
- SAP HANA - License Management
- SAP HANA - Authorization methods
- SAP HANA - Authentications
- User Administration & Management
- SAP HANA - Security Overview
SAP HANA Data Replication
- SAP HANA - MDX Provider
- SAP HANA - CTL Method
- SAP HANA - DXC Method
- SAP HANA - Log Based Replication
- SAP HANA - ETL Based Replication
- SAP HANA - Data Replication Overview
SAP HANA Monitoring
- SAP HANA - Log Configuration
- SAP HANA - High Availability
- SAP HANA - Backup & Recovery
- SAP HANA - Persistent Layer
- SAP HANA - Monitoring and Alerting
SAP HANA SQL
- SAP HANA - SQL Script
- SAP HANA - SQL Data Profiling
- SAP HANA - SQL Explain Plans
- SAP HANA - SQL Synonym
- SAP HANA - SQL Triggers
- SAP HANA - SQL Sequences
- SAP HANA - SQL Stored Procedures
- SAP HANA - SQL Expressions
- SAP HANA - SQL Functions
- SAP HANA - SQL Operators
- SAP HANA - Data Types
- SAP HANA - SQL Overview
SAP HANA Useful Resources
- SAP HANA - Discussion
- SAP HANA - Useful Resources
- SAP HANA - Quick Guide
- SAP HANA - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP HANA - SQL Sequences
A sequence is a set of integers 1, 2, 3, that are generated in order on demand. Sequences are frequently used in databases because many apppcations require each row in a table to contain a unique value, and sequences provide an easy way to generate them.
Using AUTO_INCREMENT column
The simplest way in MySQL to use sequences is to define a column as AUTO_INCREMENT and leave rest of the things to MySQL to take care.
Example
Try out the following example. This will create table and after that it will insert few rows in this table where it is not required to give record ID because it is auto-incremented by MySQL.
mysql> CREATE TABLE INSECT -> ( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> PRIMARY KEY (id), -> name VARCHAR(30) NOT NULL, # type of insect -> date DATE NOT NULL, # date collected -> origin VARCHAR(30) NOT NULL # where collected ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO INSECT (id,name,date,origin) VALUES -> (NULL, housefly , 2001-09-10 , kitchen ), -> (NULL, milppede , 2001-09-10 , driveway ), -> (NULL, grasshopper , 2001-09-10 , front yard ); Query OK, 3 rows affected (0.02 sec) Records: 3 Duppcates: 0 Warnings: 0 mysql> SELECT * FROM INSECT ORDER BY id;
+----+-------------+------------+------------+ | id | name | date | origin | +----+-------------+------------+------------+ | 1 | housefly | 2001-09-10 | kitchen | | 2 | milppede | 2001-09-10 | driveway | | 3 | grasshopper | 2001-09-10 | front yard | +----+-------------+------------+------------+ 3 rows in set (0.00 sec)
Obtain AUTO_INCREMENT Values
LAST_INSERT_ID( ) is a SQL function, so you can use it from within any cpent that understands how to issue SQL statements. Otherwise, PERL and PHP scripts provide exclusive functions to retrieve auto-incremented value of last record.
PERL Example
Use the mysql_insertid attribute to obtain the AUTO_INCREMENT value generated by a query. This attribute is accessed through either a database handle or a statement handle, depending on how you issue the query. The following example references it through the database handle −
$dbh->do ("INSERT INTO INSECT (name,date,origin) VALUES( moth , 2001-09-14 , windowsill )"); my $seq = $dbh->{mysql_insertid};
PHP Example
After issuing a query that generates an AUTO_INCREMENT value, retrieve the value by calpng mysql_insert_id( ) −
mysql_query ("INSERT INTO INSECT (name,date,origin) VALUES( moth , 2001-09-14 , windowsill )", $conn_id); $seq = mysql_insert_id ($conn_id);
Renumbering an Existing Sequence
There may be a case when you have deleted many records from a table and you want to re-sequence all the records. This can be done by using a simple trick but you should be very careful to do so if your table is having join, with other table.
If you determine that resequencing an AUTO_INCREMENT column is unavoidable, the way to do it is to drop the column from the table, then add it again. The following example shows how to renumber the id values in the insect table using this technique −
mysql> ALTER TABLE INSECT DROP id; mysql> ALTER TABLE insect -> ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST, -> ADD PRIMARY KEY (id);
Starting a Sequence at a Particular Value
By default, MySQL will start sequence from 1 but you can specify any other number as well at the time of table creation. Following is the example where MySQL will start sequence from 100.
mysql> CREATE TABLE INSECT -> ( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT = 100, -> PRIMARY KEY (id), -> name VARCHAR(30) NOT NULL, # type of insect -> date DATE NOT NULL, # date collected -> origin VARCHAR(30) NOT NULL # where collected );
Alternatively, you can create the table and then set the initial sequence value with ALTER TABLE.
Advertisements