473,461 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Primary Key question

144 100+
hi,

is it possible to have an auto increment primary key like 'SP001' then 'SP002'. so the next time i insert new row to the table, i don't have to specify the value for the primary key as it will be 'SP003'.

if it's not possible, what could be a good workaround for this. any advice is appreciated.

Thank you,
SC
Nov 14 '08 #1
6 2015
r035198x
13,262 8TB
Three possibilities.
1) Simply store the IDs as integers auto incrementing but prepend the prefix when presenting the data to the user in the presentation layer.
2.) Use another column called say, reference number which you populate with the required format. (I don't like this approach).
3.) Do a query on the table to find that the last number was and then generate the next key yourself.

I'd simply use 1.
Nov 14 '08 #2
mwasif
802 Expert 512MB
Use an AUTO_INCREMENT column with ZEROFILL attribute. This will create the numbers like 001, 002 and so on. Then you can concatenate this number with SP when displaying the data.

[@r035198x - Didn't refresh the page before posting reply]
Nov 14 '08 #3
r035198x
13,262 8TB
..

[@r035198x - Didn't refresh the page before posting reply]
No problem. I do it all the time.
Nov 14 '08 #4
thesti
144 100+
hi,

thank you for your replies. i think i'll use 1. i never thought that. thanks
Nov 14 '08 #5
thesti
144 100+
hello,

one more question, when i create the table and set the primary key field with zerofill, there seems to be 9 leading zeroes. while actually i only need a 3 digits fixed length int. should the manipulation to get the last three digit be done in the application code by using substr for example?

Thank you.
Nov 15 '08 #6
Atli
5,058 Expert 4TB
You can specify the max *length* of the integer by adding a (n) to the INT keyword.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE mytbl ( MyID Int(3) Zerofill Primary Key )
Nov 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Westcoast Sheri | last post by:
Which will be a faster lookup of an item by, "color" in the following mySQL tables: "unique key," "primary key," or just plain "key"?? CREATE TABLE myTable ( number int(11) NOT NULL default '0',...
1
by: Anthony Robinson | last post by:
I have a question with regads to placement of data files/indexes on multiple filegroups. Here is the current scenario: I have a database comprised of two filegroups - PRIMARY and INDEX. The...
17
by: Philip Yale | last post by:
I'm probably going to get shot down with thousands of reasons for this, but I've never really heard or read a convincing explanation, so here goes ... Clustered indexes are more efficient at...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
0
by: sfh | last post by:
Greetings all, I have a question concerning primary key types. In the past, I have always created tables with a primary key as an "int" such as: CREATE TABLE color_id ( color_id int(10)...
1
by: sfh | last post by:
Greetings all, (I had posted this in MS SQL on accident, my apologies :( ) I have a question concerning primary key types. In the past, I have always created tables with a primary key as...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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 project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.