Auto Incrementing-&>Updating Values In Two Related Tables :Help!!!
Nov 11, 2005
hi there,
i am new to sql server database.i am doing small projects right
now using asp.net and sql to create webpages (very basic webpages)
My problem is:
Problem :
i have two tables .....table 1 and table 2.
Table 1 has following fields: studentid,student name,student address.
Table 2 has following fields:studentid and course .
table1 student id is the primary key refrencing table 2 student id.
Now i delete a record in table 1 which will in turn also get deleted in
table 2 . so for eg if i have three records 1 ,2 and 3 ....then i
delete 2 in table 1 ...i will have 1 and 3 in both table 1 and table
2....now i want 3 to become 2 in both table 1 and table 2...so that i
dont have empty space between two student id's 1 and 3. so this is my
problem....if any one can help me out with suggestions please
do.
I need some help for designing the IDs / Primary keys for some master tables in my database. Following are master tables. Client_Master, Buyer_Master & Seller_Master; I want to set Client_Id, Buyer_Id & Seller_Id as their respective primary keys and they should have following properties
Client_Id :- a) should be auto-incrementing value, b) unique & c)should be of the format – CLXXXXXX, where “CL” {Constant start characters} & “X” {any number 0-9} Similarly:: Buyer_Id :- BYXXXXXX Seller_Id :- SLXXXXXX
We are implementing the database in MS-SQL 2005 & MySQL
Can anyone help me find a solution to this, especially in MS-SQL.
ID – Auto IncrementingNumber_of_Records – IntAccount_Number – Varchar (Format 1234)Account_Number_Instance – Varchar (Format e.g. 1234-01)Other_Field1Other_Field2…etc.
When the Account_Number was initially inserted into the table, a Stored procedure (not written be me) was used to concatenate the Account_Number and Number_of_Records. As a result the Account_Number_Instance became, e.g. 1234-01, etc.
From the ASPX form we retrieve each of the fields above, and based on the Number_of_Records an appropriate number of records were inserted for the Account_Number.
For Example:
Account_Number = 12345Number_of_Records = 4
The result inserted into the table were as follows:
ID # of Records Acct_Number New_Num Acct_Number_Instance …
1 4 12345 12345-01
2 4 12345 12345-02
3 4 12345 12345-03
4 4 12345 12345-04
Now, I would like to UPDATE this table, and based on the original Number_of_ Records (4) I want to ADD, for Account_Number (1234) add an appropriate Number_of_Instances, beginning with the next incremental number and adding what the New Number of Records input is…
ID # of Records Acct_Number New_Num Acct_Number_Instance …
1 4 12345 12345-01
2 4 12345 12345-02
3 4 12345 12345-03
4 4 12345 12345-04
5 12345 3 12345-05
6 12345 3 12345-06
7 12345 3 12345-07
I do realize that the next time I have to update this Account_Number I am also going to have to somehow ADD only one of the #_of_Records and New_Num for the Acct_Number instances to obtain the next starting number, which in the example above would be (8)…
Any and all suggestions on how to accomplish this would be greatly appreciated!
I have a database table. I need to set a particular data field to allow for Auto Incrementing. Basically I need to set up the initial integer value to start at. Then to specify the amount to increment by. What I have done so far is to right click on the field name in SQL Server 2005. Then, on the pop-up menu I select the menu option for properties. When the properties are displayed I see some fields listed in dark grey but I am not able to edit these fields. In other words, if I try to type text in this field, nothing happens. The fields I am talking about are: Identity Increment Identity Seed Is Identity Does anyone know what the problem is? What do I have to do to edit these fields.
Here is a simplified version of my problem: I am inserting data into a table using a stored procedure. The table has an identity column that increments with each insert. When I use erroneous data in the other fields the insert fails….no surprises there! But when the next insert occurs with valid data I find that my identity field has increased even with those inserts that failed, so my sequence has jumped a few numbers. How do I get the identity inserts to roll back if the rest of the data in a row doesn’t insert successfully? Marcha x
I am porting a database from Informix to SQL Server 2000. Several fields in the informix database are specified as SERIAL, which means that they auto-increment auto-magically if you insert a 0 into them. Well...I was looking for an equivalent in SQL Server 2000, and thought I had found it in the IDENTITY column concept.
With further investigation into IDENTITY columns, however, I discovered that only one column per table can be given the IDENTITY property. Why is this? Is there a way around that? If not, is there another way to achieve this feature in SQL Server 2000? If not, any suggestions on how I can do it?
My Identity value column is not autoincrementing, its seed is set to 1, and increment set to 1. yet when i click the auto generated 'Add New Record' button in the BindingNavigator control set (in a form where it is populated with databound text boxes), this Identity column is not auto incremented?
And if i try to write a value to this column, i get the error telling me this column cannot be written to. Yet if i do not write to it, then i get "This column does not allow nulls"
To load my data i am using:
Code Snippet 'clear dataset Me.DsQuote.Clear() 'fill the datagrid with data from tblQuoteID Me.TblQuoteIDTableAdapter.Fill(Me.DsQuote.tblQuoteID)
and to save data im using:
Code Snippet Me.Validate() 'end data edit Me.TblQuoteIDBindingSource.EndEdit() 'update the dataset with table data Me.TblQuoteIDTableAdapter.Update(DsQuote.tblQuoteID)
anything im doing wrong? this data is being viewed in a datagrid btw.
Hi, I have an auto incrementing int column setup which serves as my unique primary key. Just wondering what happens when the auto increment reaches the limit? Will it recycle numbers from the begining (who's rows have obviously been deleted by this stage)?
Hi All, I need to set up a kind of identity insert that gives an output in the format: 00001, 00002, 00003 etc. Is there a formatting option for this sort of output using normal identity insert features or do I need to write a function to insert these values (perhaps as text) each time a new record is created? Sorry if this is really simple but it's only my 4th day in this job! Marcha
We have upgraded our SQL 2008 server to 2012 last month. i've noted since then that many tables that have auto increment primary key bigint field increases by 1 like it should, then for some reason it jumps up by 1000 or even 10000? i have seed adn auto increment set to 1 but doesnt effect it. Is there anything that could be causing the next value to jump that much?
If i were to replicate a database with another where in both the databases have an auto incrementing field. What could be the problems that's goin to arise out of it. How to deal with such a scenario...?????
Along with the data that I am pulling back from query, I also need to provide an ImportID column with the following requirements:
YYMMDDGP0001, YYMMDDGP0002, YYMMDDGP0003, and so on. The 0001, 0002, and 0003 part could get big into the hundreds of thousands.
I have the YYMMDDGP part down with the following expression:
SELECT CONVERT(VARCHAR(6), GETDATE(), 12) + 'GP' AS [ImportID]
Now I need to get the Auto Incrementing number part of this. I have been researching this trying SELECT INTO a temp table using the Identity Function and declaring different variables all with no luck.
I need to auto increment an alphanumeric field in a table.
The process is as follows:
1. First position is static letter C for contact or static letter A for account - field contact_id 2. The following 6 positions are numeric - example of the string would be C004658 3. When new contact is entered, it should look up the highest numeric value (in this case 004658) and increment it by one - resulting in C004659
Hi,I'm in the process of implementing a multi-user system containing anadjacency list (tree structure). I'm using a TIMESTAMP field on eachrecord in the adjacency list in order to tell when a node has been changedsince the last read. Sometimes though, it is useful to flag a "parent" (orall ancestors or a node) as being changed if any of its children have. Isthere any way I can force an update to the parent TIMESTAMP field withoutactually modifying any of the other fields in the record? Something likethis (assuming I have a field called [Timestamp]):UPDATE Adjacency SET [Timestamp] = [Timestamp] + 1 WHERE ID = @_In_IDNow, will the timestamp be incremented here by 1, or will SQL server getconfused as in theory it must "update the timestamp after updating thetimestamp" for this record?Thanks,Robin
I am looking for suggestions on how to accomplish the following task with the least amount of hand keying that I can get away with.
I have a main table named Office and 4 other tables that have the Office table ID field value in them. I have been given a new set of ID values that need to replace the values that are currently in the Office table and then update the ID field in the other 4 tables.
I have only thought of 2 solution and I don’t like either one. 1 Add a new column to the Office table and key in the new ID’s then go through the pages that reference the old ID field and change the SQL queries to use the new field.
2 Change the value of the current Office ID field to the new value. Search the other 4 tables for the old value and then update them to the new value.
Hello EveryOne,I have a program which insert the Data into table Fine. But the problem is that whenever expecption is raised the auto increament id is increases..i want that user enter the record and without error the auto increament id is 1 and when second user enter the record on table and exception raised and next time he entered again then his record auto increament id is 3 why? But record is not entered only the expecption is raised.How to resolve this problem to make cosistency in record of other tables.? Help me
Can you describe the best (or your preferred) method for updating data held in a related table using Visual Studio 2005 and SQL Server. For example; if you had a stock control system with the product names and current stock levels in one table and all stock movements in and out held in another table, what is the best, fastest, safest and most reliable method of inserting a stock movement and then updating the current stock level? I have tried a couple of different methods but would really appreciate a wider range of opinions. Thanks
I would like to create an application that displays the status of my machines almost real-time (Run, Down, or idle etc). And I'm wondering what is the common method to accomplish this? Do you tell the application to keep going out and getting the status of the machines at a specific interval? or Do you have a way for the application to just 'listen' if a status update is available for the specific machines? If so, how? I only have a 'Read' access to the database. Also, other applications update the database (and I don't have access to those applications.) My application will be in VB.NET. Thanks for any leads.
I have a textbox that is receiving a date in the format mm/dd/yyyy. The field in the database is VarChar size 10. I purposely don't want to use the DateTime data type for various reasons. When I extract the text from that textbox: Example:4/11/2005 I do my INSERT. When I look in the database at that field, it shows up like:April 11, 2005 12 The database's field is VarChar! How/why is it doing automatic DateTime formatting?
Is it possible to get SQL Server to automatically update the table/field names in your stored procedures when changing those table/field names in the database?
Hi Gnite everyone, i once again need help with a T-SQL syntax for Auto Correction for insert and update when client enters the wrong format, i;e, creating a SSN Data type and a User Defined Procedure that auto corrects input format i;e, user inserts into table authors of the pubs DB 525-477845 column au_id that executes auto correction so user doesn't have to put in dashes for SSN format. Please help me with this syntax .
On the web form I have textboxes for the Purchase No., date and part description, and a drop down list for the machine name. How do I insert them into the different tables?
I've just start learning ASP.NET and I am using Web Matrix for this. The examples I've seen so far only shows how to insert into a single table.
hi this is probably a dumb question with an easy answer but does anyone know how to do this...
2 tables, one is a supplier list (id, supplier_name) and the other is an order list (id, order_name, supplier_id... etc)
basically i need to join the 2 tables together (id=supplier_id) but i need to display the results without the supplier_id and the supplier table id, only the supplier name?
do i have select only the specific fields I want? or can I exclude the supplier_id and the id from the supplier table?
Hi all. how could i join unrelated tables? for example: table1: employee dcno name address telno 1 sarah philippines 111 2 john india 222 3 joy usa 333
table2: hospital hospname location owner hosp1 loc1 billgates hosp2 loc2 ann hosp3 loc3 love hosp4 loc4 vic
I have 2 tables with this design: one has ArticleID as primary key and multiple other fields and one has GroupID as primary key and multiple other fields. Each article can belong in multiple groups so I created a new table called articleGroups with only 2 fields: ArticleID and GroupID to show the groups associated with each article. There is a relation between this table and each of the main 2 tables. My question is, in the articleGroup table, does it make any sense to create an Index on ArticleID, GroupID or both? Since the group is needed for each article the Groups will always be queried everytime the article is queried. So, I am not sure if an index is needed?