473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

i want a certain field to be sequentially increased

Hi,
I want to setup one of the fields in a table so it increments
sequentially(int data type). i.e the first record should be record 1
and the second one should be 2 and so on. This field will also be the
key field. I am new to SQL and don't know how to do this.
I am using SQL server 2000.
Thanks for the help in advance.

-S
Jul 20 '05 #1
9 1547
>> I want to setup one of the fields [sic] in a table so it increments
sequentially(INTEGER data type). i.e the first record [sic] should be
record 1 [sic] and the second one should be 2 and so on. This field
[sic]will also be the key field [sic]. I am new to SQL and don't know
how to do this. <<

Very new. Rows are not records; columns are not fields; keys are not
physical record numbers. You missed the most basic concept of the
RDBMS model. A key is a subset of the attributes (columns) which are
unique and not null for each entity (row) in the table. It is never a
"magic number for everything" generated by the computer which has no
meaning in the reality you are trying to model.

You actually have to think and work hard to design a database. Get a
book on data modeling and read it.
Jul 20 '05 #2
Take a look at IDENTITY (Property) in "SQL Server Books Online".

"Sumanth Suri" <su*********@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...
Hi,
I want to setup one of the fields in a table so it increments
sequentially(int data type). i.e the first record should be record 1
and the second one should be 2 and so on. This field will also be the
key field. I am new to SQL and don't know how to do this.
I am using SQL server 2000.
Thanks for the help in advance.

-S

Jul 20 '05 #3
thanks for the 40. I hear SQL FOR SMARTIES is pretty good. Any thoughts??

jo*******@northface.edu (--CELKO--) wrote in message news:<a2**************************@posting.google. com>...
I want to setup one of the fields [sic] in a table so it increments

sequentially(INTEGER data type). i.e the first record [sic] should be
record 1 [sic] and the second one should be 2 and so on. This field
[sic]will also be the key field [sic]. I am new to SQL and don't know
how to do this. <<

Very new. Rows are not records; columns are not fields; keys are not
physical record numbers. You missed the most basic concept of the
RDBMS model. A key is a subset of the attributes (columns) which are
unique and not null for each entity (row) in the table. It is never a
"magic number for everything" generated by the computer which has no
meaning in the reality you are trying to model.

You actually have to think and work hard to design a database. Get a
book on data modeling and read it.

Jul 20 '05 #4
>> thanks for the 40. I hear SQL FOR SMARTIES is pretty good. Any
thoughts?? <<

I like it a lot -- especially when the royalty check arrives :)
--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Dont listen to Celko - his is too pr0 :p

go with the identity - but beware some of its behaviour, and protect
it carefully, it is not terribly robust and can totally shaft your
data relationships if you use it and let it get damaged.


"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message news:<vq************@corp.supernews.com>...
Take a look at IDENTITY (Property) in "SQL Server Books Online".

"Sumanth Suri" <su*********@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...
Hi,
I want to setup one of the fields in a table so it increments
sequentially(int data type). i.e the first record should be record 1
and the second one should be 2 and so on. This field will also be the
key field. I am new to SQL and don't know how to do this.
I am using SQL server 2000.
Thanks for the help in advance.

-S

Jul 20 '05 #6
Wang,

I do agree with you. These are all features that help programmers to develop
more efficient applications. It all depends on how good you are, if you are
a terrible programmer then it doesn't matter whether you use IDENTITY or
not, you will end up with a piece of crap anyway. A professionals is someone
who can take advantage of these features and use them wisely to help
developing their creative ideas without making a mess.

Shervin

"WangKhar" <Wa******@yahoo.com> wrote in message
news:bb**************************@posting.google.c om...
Dont listen to Celko - his is too pr0 :p

go with the identity - but beware some of its behaviour, and protect
it carefully, it is not terribly robust and can totally shaft your
data relationships if you use it and let it get damaged.


"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message

news:<vq************@corp.supernews.com>...
Take a look at IDENTITY (Property) in "SQL Server Books Online".

"Sumanth Suri" <su*********@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...
Hi,
I want to setup one of the fields in a table so it increments
sequentially(int data type). i.e the first record should be record 1
and the second one should be 2 and so on. This field will also be the
key field. I am new to SQL and don't know how to do this.
I am using SQL server 2000.
Thanks for the help in advance.

-S

Jul 20 '05 #7
how would you protect the behavior of IDENTITY?
Shervin and wangkhar thanks for the help.
Celko, Don't expect any royalty from this beginner!!

Wa******@yahoo.com (WangKhar) wrote in message news:<bb**************************@posting.google. com>...
Dont listen to Celko - his is too pr0 :p

go with the identity - but beware some of its behaviour, and protect
it carefully, it is not terribly robust and can totally shaft your
data relationships if you use it and let it get damaged.


"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message news:<vq************@corp.supernews.com>...
Take a look at IDENTITY (Property) in "SQL Server Books Online".

"Sumanth Suri" <su*********@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...
Hi,
I want to setup one of the fields in a table so it increments
sequentially(int data type). i.e the first record should be record 1
and the second one should be 2 and so on. This field will also be the
key field. I am new to SQL and don't know how to do this.
I am using SQL server 2000.
Thanks for the help in advance.

-S

Jul 20 '05 #8
>> Celko, Don't expect any royalty from this beginner! <<

What I used to get from beginners was consulting gigs, to clean up the
mess they made when their company has problems. Those jobs pay MUCH
better than royalties :)

Using IDENTITY as a primary key is a sign that there is no data model,
only an imitation of a sequential file system. Since this "magic,
all-purpose, one-size-fits-all" pseudo-identifier exists only as a
result of the physical state of a particular piece of hardware at a
particular time as read by the current release of a particular database
product, how do you verify that an entity has such a number in the
reality you are modeling?

You will see newbies who design tables like this:

CREATE Drivers
(driver_id IDENTITY (1,1) NOT NULL PRIMARY KEY,
ssn CHAR(9) NOT NULL REFERENCES Personnel(ssn),
vin CHAR(17) NOT NULL REFERENCES Motorpool(vin));

Now input data and submit the same row a thousand times, a million
times. Your data integrity is trashed. The natural key was this:

CREATE Drivers
(ssn CHAR(9) NOT NULL REFERENCES Personnel(ssn),
vin CHAR(17) NOT NULL REFERENCES Motorpool(vin),
PRIMARY KEY (ssn, vin));

If you want to enforce a rule that a car can have one driver:

CREATE Drivers
(ssn CHAR(9) NOT NULL REFERENCES Personnel(ssn),
vin CHAR(17) NOT NULL PRIMARY KEY
REFERENCES Motorpool(vin));

Now you are REALLY thinking about relations and keys instead of 1950's
sequential record numbering. Go further and add DRI:

CREATE Drivers
(ssn CHAR(9) NOT NULL
REFERENCES Personnel(ssn)
ON DELETE CASACADE
ON UPDATE CASACADE,
vin CHAR(17) NOT NULL PRIMARY KEY
REFERENCES Motorpool(vin)
ON DELETE CASACADE
ON UPDATE CASACADE,
PRIMARY KEY (ssn, vin));

Adding an IDENTITY column to either of these tables as a candidate key
would be dangerously redundant; one query uses the IDENTITY and another
uses the real key, and like a man with two watches, you are never sure
what time it is.

Researching the CHECK constraints you need for a VIN or SSN will take a
few days -- but newbies only want "quick and magic answers that solve
all the problems" and do not bother doing the real work.

Finally, an appeal to authority, with a quote from Dr. Codd: "..Database
users may cause the system to generate or delete a surrogate, but they
have no control over its value, nor is its value ever displayed to them
..."(Dr. Codd in ACM TODS, pp 409-410) and Codd, E. (1979), Extending
the database relational model to capture more meaning. ACM Transactions
on Database Systems, 4(4). pp. 397-434.

This means that a surrogate ought to act like an index; created by the
user, managed by the system and NEVER seen by a user. That means never
used in queries, DRI or anything else that a user does.

Codd also wrote the following:

"There are three difficulties in employing user-controlled keys as
permanent surrogates for entities.

(1) The actual values of user-controlled keys are determined by users
and must therefore be subject to change by them (e.g. if two companies
merge, the two employee databases might be combined with the result that
some or all of the serial numbers might be changed.).

(2) Two relations may have user-controlled keys defined on distinct
domains (e.g. one uses social security, while the other uses employee
serial numbers) and yet the entities denoted are the same.

(3) It may be necessary to carry information about an entity either
before it has been assigned a user-controlled key value or after it has
ceased to have one (e.g. and applicant for a job and a retiree).

These difficulties have the important consequence that an equi-join on
common key values may not yield the same result as a join on common
entities. A solution - proposed in part [4] and more fully in [14] - is
to introduce entity domains which contain system-assigned surrogates.
Database users may cause the system to generate or delete a surrogate,
but they have no control over its value, nor is its value ever displayed
to them....." (Codd in ACM TODS, pp 409-410).

References

Codd, E. (1979), Extending the database relational model to capture more
meaning. ACM Transactions on Database Systems, 4(4). pp. 397-434

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #9
It's not always consecutive. So if it's important to you just forget
IDENTITY.
Identity is a means just like many other things in SQL, if you chose the
right tool for your problem you won't regret it. It's not a silver bullet
but it will help you if you use it in the right place.

Good luck,
Shervin

"Sumanth Suri" <da***************@yahoo.com> wrote in message
news:2e*************************@posting.google.co m...
how would you protect the behavior of IDENTITY?
Shervin and wangkhar thanks for the help.
Celko, Don't expect any royalty from this beginner!!

Wa******@yahoo.com (WangKhar) wrote in message

news:<bb**************************@posting.google. com>...
Dont listen to Celko - his is too pr0 :p

go with the identity - but beware some of its behaviour, and protect
it carefully, it is not terribly robust and can totally shaft your
data relationships if you use it and let it get damaged.


"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message news:<vq************@corp.supernews.com>...
Take a look at IDENTITY (Property) in "SQL Server Books Online".

"Sumanth Suri" <su*********@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...
> Hi,
> I want to setup one of the fields in a table so it increments
> sequentially(int data type). i.e the first record should be record 1
> and the second one should be 2 and so on. This field will also be the > key field. I am new to SQL and don't know how to do this.
> I am using SQL server 2000.
> Thanks for the help in advance.
>
> -S

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wm | last post by:
I just imported a CSV file into mySQL using PHPmyAdmin. With deletions, etc., the numbering is now very "intermittent" on the key field. Is there a way for me to delete all existing autonum key...
5
by: diong | last post by:
i have a field with a list of wrong ID (random) and i wish to replac them with a sequential (auto-increment) value. how to remove the valu and to add in the sequential ID? Im a newbie in ASP! Pls...
0
by: gary b | last post by:
My forehead is sore from beating it against the wall. Can anyone help? I've started drinking again because of this problem! Setup: ContestantTbl = table ('one' side) HorseTbl = table ...
4
by: Mal | last post by:
I have an ACC 2000 database that has a strange behaviour I have a small table, with just a few fields... My report has very simple grouping and sorting, no code bar a NODATA event. I have a...
20
by: Prakash | last post by:
Hi ! I have a field "sub_tran_no" in my form in continuous view. When the user presses a button "Re-Number", I'd like to: 1) Save the current record pointer position 2) Save the current...
3
by: JIM.H. | last post by:
Hello, Let say myTable has two fields: ID and Name. ID is an identity field and increased sequentially by 1 each time a record is entered into table. dr=”MayName”;...
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
2
by: Alex | last post by:
Hello, All. I encouraged some strange behaviour of either IIS, ASP.NET, IE or donn't know what... I have a simple wait.aspx page private void Page_Load(object sender, System.EventArgs e) {...
34
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.