Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hope to find assistance. I am using ASP.Net C#. The database was created inside Visual Studio using SQL Server 2008 and I made some tables. Its primary key auto increments, but some tables I am not using the auto increment property. I want to make increment using code.

If I have 3 tables(account, manager, project) for example, when a user 1 create account the account_id will auto increment and when select account type manager the manager_id I want to increment by code. User 1 with account_id 1 and manager_id 1 able to create multiple projects.

and this is part of my database:

SQL
create table  account (
account_id   int(8) PRIMARY KEY  AUTO_INCREMENT,
username char(12) not null ,
email varchar (50) not null,
password varchar (20) not null
account type int (5)
);

create table  profile (
profile_id int(8) PRIMARY KEY  AUTO_INCREMENT,
first_name varchar (20),
last_name varchar(20),
gender char(10),
country char (15),
phone_number int(15),
profile_description varchar (100),
account_id int (8),
manager_id int (8),
team_id int(8),
client_id int(8),

Constraint profile_fk foreign key (account_id) references account (account_id),
Constraint profile_fk2 foreign key (manager_id) references manager (manager_id),
Constraint profile_fk3 foreign key (team_id) references team (team_id ),
Constraint profile_fk4 foreign key (client_id) references client (client_id),

);

create table  manager  (
manager_id int(8) PRIMARY KEY  ,

project_id int(8),

Constraint manager_fk1 foreign key (project_id ) references project (project_id )
);

create table  project (
project_id int(8)PRIMARY KEY  AUTO_INCREMENT,
 project_name char (15),
 project_description varchar(100),

);
Posted
v4
Comments
[no name] 14-Mar-13 10:32am    
So take the auto increment off the column in the table that you do not want to auto increment and then write some code that does the incrementing for you.

There is no question in here anywhere, not even a description of a problem.....
CHill60 14-Mar-13 10:34am    
I would really advise you not to do it that way unless you are sure that the application you are running will be the only instance running against that database. You will still be able to have manager_id 1 able to create multiple projects ... it all depends on how you handle your foreign keys

1 solution

Do it in a stored procedure.
These are atomic operations, so there can't be any interruption in a multiuser environment causing a number to be under- or over- incremented.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900