Geeks Talk

Prepare for your Next Interview




how to create sequence in MYSQL

This is a discussion on how to create sequence in MYSQL within the MY SQL forums, part of the Databases category; please send me the syntax for creating the sequence in mysql and how that sequence be linked to different fields of different tables....


Go Back   Geeks Talk > Databases > MY SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-11-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 73 Times in 62 Posts
psuresh1982 will become famous soon enough
how to create sequence in MYSQL

please send me the syntax for creating the sequence in mysql and how that sequence be linked to different fields of different tables.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-16-2007
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 426
Thanks: 17
Thanked 53 Times in 53 Posts
susarlasireesha is on a distinguished road
Re: how to create sequence in MYSQL

In Oracle sequences are often used to maintain a unique series of numbers for an ID field. Sequences are independant of any table and can be used to keep a value unique across a number of tables. In fact they are not even restricted to use in tables.

MySQL doesn't currently support sequences. However it does have an auto increment value which can be applied to a primary key of a table. This is done during the table creation.

mysql> create table seq_test (id int primary key auto_increment ,name
varchar(30));
Query OK, 0 rows affected (0.06 sec)

As the name suggests the value is assigned automatically, this is in contrast to Oracle where we have to call the nextval function of the sequence to return the value when we need it.

So if we perform an insert against the table but do not specifiy a value for the auto_increment column it's assigned automatically.

mysql> insert into seq_test (name) values ('Dave');
Query OK, 1 row affected ( 0.03 sec)
mysql> select * from seq_test;

However we can override the assignment using a value of our choosing.

mysql> insert into seq_test (id,name) values (100,'John');
Query OK, 1 row affected (0.05 sec)
mysql> select * from seq_test;


The sequence will then start from this new higher point.

mysql> insert into seq_test (name) values ('Penny');
Query OK, 1 row affected (0.02 sec)
mysql> select * from seq_test;

We can insert records with values lower than the current highest value but these will be subject to the normal rules for a primary key (no duplicates) and will not effect the next number assigned automatically.

If all of the records are deleted from a table the sequence is not reset.

mysql> delete from seq_test;
Query OK, 3 rows affected (0.01 sec)
mysql> insert into seq_test (name) values ('garry');
Query OK, 1 row affected (0.03 sec)
mysql> select * from seq_test
To do this you need to truncate the table like so.

mysql> truncate table seq_test;
Query OK, 1 row affected (0.03 sec)
mysql> insert into seq_test (name) values ('garry');
Query OK, 1 row affected (0.03 sec)
mysql> select * from seq_test

Alternatively you can reset the sequence using an alter table command.

mysql> alter table seq_test auto_increment = 100;
Query OK, 1 row affected (0.27 sec)Records: 1 Duplicates: 0 Warnings: 0
This method could also be used to assign a higher number to start the sequence rather than starting with 1 by calling the alter table straight after the table creation.

So in summary auto_increment offers a great way of assigning a unique value automatically to a table. However what it doesn't allow when compared with an Oracle Sequence is different increment values, ability to use across a number of tables and the option to assign in a reverse order.

I hope This will help u............
Reply With Quote
Reply

  Geeks Talk > Databases > MY SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Online Reference - MYSQL joel MY SQL 3 05-20-2008 10:47 AM
j2ee with mysql... psuresh1982 MY SQL 0 07-11-2007 09:57 AM
Why projects are not done using MySql dharmagee MY SQL 4 05-14-2007 12:31 AM
Unable to connect MySQL to ALOV map JobHelper MY SQL 0 01-03-2007 04:37 PM
Difference between MYSQL and Oracle janelyn Oracle 0 07-17-2006 04:35 PM


All times are GMT -4. The time now is 05:56 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved