Redefining An Auto-generated Primary Key Constraint Name
Jan 9, 2007
Hey there :)
I'm using Ruby on Rails to create a migration of a legacy database on
an MS SQL Server. I'd like to name my constraints myself, such as
'pk_authors', but in the cases where a table has an auto incremented
IDENTITY(1,1) id field, Rails takes over and adds the primary key
itself when creating the table.
This is fine, except then the constraint gets a name like
'PK__authors_384934' which is not very intuitive or easily remembered
;)
I'd like to just throw in an 'ALTER TABLE authors...' statement just
after that table has been created, but I'm not sure how to go about
renaming the auto generated constraint since the name it gets is
partially random. Is there any way to indirectly refer to the
constraint like 'RENAME PRIMARY KEY CONSTRAINT ON authors TO
pk_authors' or something like that, so that I can rename the
constraint?
Thanks in advance,
Daniel Buus :)
View 9 Replies
ADVERTISEMENT
Feb 7, 2007
hi guys,
just a question regarding database design
i have a table with an auto-generated primary key but the problem is this:
say i have 4 records,so logically they'll be numbered 1 to 4.so the problem is whenever i delete all records and add new ones,the numbering will start from 5 and not 1 again.
how do i remedy this?
thanx
View 5 Replies
View Related
Feb 7, 2007
Hello,
I've a problem with auto-generated key. In my parent table, some lines have been deleted. I would like to add new lines with the same content in order to restore my database, but I cannot decide on the id value which is an auto-generated key. Is there a solution to set an auto-generated key with a specific value ?
Thank you in advance,
regards,
mathmax
View 3 Replies
View Related
Feb 3, 2008
Hello everyone,i have a web form to take user details.as soon as the submit button is clicked the form is submitting and the new row is being added into the sqldatabase.its fine ..but in the newly added row, i want the user id(primary key and auto generating) to display in different manner.in mytable column(userid),instead of userid auto displaying 1,2,3,4 ...i want it to display in this way.these are all primary keys of rows added.todaydate-username-1 eg(2.2.2008-jack-1)todaydate-usernaem-2 eg(2.2.2008-zak-2)todaydate-usernaem-3 eg(2.3.2008-leme-2)any idea how to achieve this.?thanks.jack.
View 9 Replies
View Related
Oct 22, 2006
I downloaded the AdventureWorks OLTP Schema and am trying to use some of the design concepts that are used in this example but I don€™t quite understand some things.
First there is a column named rowguid, at the moment I assume that this is a GUID. If this is wrong most likely my next question will make no sense.
Second if that is indeed a guid all the tables have another id; example would be Employee table would have employeeid and rowguid, why do this? My best guess is that you would use the guid to easily insert (linked/sync) data into multiple tables without needing to retrieve the id from the table that was first input into. My scenario would be inserting an employee; to insert an employee you need to create a row in the employee, contact and address tables and for ease you would use the guid to link these rows. But if this is the case wouldn€™t there be sync issues that would arise?
If my guess is totally off please correct me also if anyone can direct me to some good resources that cover database design.
The schema can be downloaded at http://www.microsoft.com/downloads/details.aspx?familyid=0F6E0BCF-A1B5-4760-8D79-67970F93D5FF&displaylang=en#filelist
Thanks for any help.
View 1 Replies
View Related
Aug 31, 2014
can i create Custom Auto-Generated Sequences with SQL Server like DD-0001,DD-0002,DD-0003...... to DD-000........
View 1 Replies
View Related
Dec 29, 2014
My question: Is it okay to drop all the auto generated column statistics? (for the following scenario)
- I am cleaning up unnecessary objects (tables, unused indexes, overlapping statistics etc) from databases and found out there are more than 1400 auto generated column statistics on one database (lets call it A).
- Database A was used to be our reporting database but from last several years we are using database B for reporting. DB A has all the historical data while DB B only has valid records.
- We are updating all the column statistics with full scan nightly on database A and it is talking almost 2.5 hours to do that. Now I want to drop all the "unnecessary" statistics those were created when DB A was reporting database and they are no longer in use. There is no way to know the creation date of the column statistics that I know of. Statistics "last update date" is of no use because of our nightly job. So I was thinking of dropping all the auto generated column statistics and let the sql server create as it needs from now.
View 0 Replies
View Related
Sep 12, 2007
Ok, let's say that I do a following inline INSERT statement.....
INSERT INTO Car ( carMake, carModel ) VALUES ( 'Honda', 'S2000' )
When I do this, a carId int value will automatically be generated because it is the Car table's PK and is an int identity. How do I obtain this value so I can immediately use it in let's say, the following example.
INSERT INTO Person ( carId ) VALUES ( @carId ) ..... where @carId is the value I need
Maybe you would say I should do something like.....
SELECT carId FROM Car WHERE carMake = 'Honda' AND carModel = 'S2000'
However, let's assume that the carMake and the carModel fields are NOT unique. I'm sure there's an easy way to extract the new identity value (maybe even in the same query) as the INSERT, but I just don't know how.
Thanks.
View 10 Replies
View Related
Nov 21, 2014
We have implemented a very small reporting database which has a main table that started off small and has now grown to around half a million rows. Initially, there were no indexes on the table apart from a clustered index, but as the data has grown, performance has dropped and so we have added a number of indexes. This has resolved the performance issues.
Before creating the indexes SQL Server had auto created a number of statistic objects (_WA_Sys_000... etc). After creating the indexes, new statistic objects where created for the new indexes. In some cases, there are duplicate statistics (auto and index) for the same columns.Should I go through and drop the duplicate auto statistics? Will having duplicates cause issues at all?
View 2 Replies
View Related
Mar 28, 2008
Dear all,
I would like to use SSIS to populate product information from a remote database to a local one.
There are Product table (which contains the basic information) and ProductImage table(which contains the product images in binary).
I would like to use a join query
SELECT A.name, A.price, B.image from Product A INNER JOIN ProductImage B WHERE A.ProductImageID = B.ProductImageID
to return all the product information from the remote database.
When populating the local database, the local ProductImage table will be populated first and then the local Product table will be populated with the ProductName, ProductPrice return from the query together with the newly genetated ProductImageID afer insert a record into the local productimage table.
The ProductImageID column is set as the identity column in the ProductImage table.
I am very new to SSIS. Is there a way to handle this scenario? What components do I need to use?
Thanks
View 4 Replies
View Related
Aug 13, 2007
Hi,
I have recently been looking at a database and wondered if anyone can tell me what the advantages are supporting a unique collumn, which can essentially be seen as the primary key, with an identity seed integer primary key.
For example:
id [unique integer auto incremented primary key - not null],
ClientCode [unique index varchar - not null],
name [varchar null],
surname [varchar null]
isn't it just better to use ClientCode as the primary key straight of because when one references the above table, it can be done easier with the ClientCode since you dont have to do a lookup on the ClientCode everytime.
Regards
Mike
View 7 Replies
View Related
Mar 23, 2005
Hi,
I am trying to insert into a table. I got the above error message then I deleted all the data in the table. But I am still getting this error.
Whats wrong?
View 2 Replies
View Related
Oct 30, 2001
Hello,
I am getting the following message when I am trying to insert into my table. Request is the primary keys and the Bus_Req_Id and Test_Case_Id are both foreign keys. The data that is being inserted into these fields are not unique.
This is the exact error message:
Violation of PRIMARY KEY constraint 'PK_REQUEST_BUS_REQ_TST'. Cannot insert duplicate key in object 'REQUEST_BUS_REQ_TST'.
The statement has been terminated.
I am trying to insert 700 - 900 rows based on certain criteria. If I delete the keys the inserts will work.
Any help would be most greatful.
Thank you in advance
Anne
View 1 Replies
View Related
Sep 21, 2005
I know what this error means is that you can not insert duplicate primary keys in the table but the thing is I am checking the rows if they do not exist then insert otherwise don't do any thing these are the lines I am writing in my strored procedure can someone please let me know what I am doing wrong here.
If not exists
(Select * From GGP WHERE
FFECTIVE_DATE =@v_EFFECTIVE_DATE AND
ASSET_ID= @v_ASSET_ID AND
ASSET_TYPE = @v_ASSET_TYPE AND
Value = @v_Value AND
hour = @v_Hour)
INSERT INTO GGP
(ASSET_ID,ASSET_TYPE,Hour,Value,EFFECTIVE_DATE) values(@v_ASSET_ID,@v_ASSET_TYPE,@v_Hour,@v_Value, @v_EFFECTIVE_DATE)
The exact error is
Violation of PRIMARY KEY constraint 'PK_SP_GGP'. Cannot insert duplicate key in object
Thanks in advance.
View 7 Replies
View Related
Apr 10, 2006
When I try to insert data into a table (let's just call it MyTable for this post), I suddenly get the following error.
Violation of PRIMARY KEY constraint 'PK_MyTable'. Cannot insert duplicate key in object 'MyTable'.
My table does have a primary key field named 'id', which is an auto-incrementing BIGINT. The last record I successfully inserted received 14 in the id field, so I'm assuming the database is trying to assign 15 to the next. Unfortunately, there is already a record with an id of 15; the next available id is 21.
Is there a way to avoid these primary key collisions?
View 5 Replies
View Related
Mar 21, 2007
Hi All,
I have a table that has 3 columns, two of them make a composite primary key. The table is populated with data. What I need to do is to add a third column to a composite primary key. I have tried to do that with the following command:
alter table databasesize
add constraint pk_dbsize primary key (dbid)
But I get the error message:
Table 'databasesize' already has a primary key defined on it.
How can I do this?
View 2 Replies
View Related
Feb 12, 2004
I have an odd problem on something that used to work fine.
I have an SP that inserts a record into a table (Contract) with two keyed fields.
The keys are as follows:
ContractID and SeqID (Sequence)
These two keys make the records unique.
Ex:
ContractID SeqID
12345 1
12345 2
12345 3
etc....
Several weeks of using this procedure have been fine. Suddenly I started getting this error:
Violation of PRIMARY KEY constraint 'PK_contract'. Cannot insert duplicate key in object 'Contract'.
The statement has been terminated.
I verified that the values do not violate the constraints. In fact, I can type the exact information into the table directly without a problem.
Has anybody experienced this before?
Any help would be apprciated!
Here is the code in the SP;
CREATE PROCEDURE bcipNewContractSeq @ContractID Char(10 )AS
DECLARE @MaxSeqID int
DECLARE @NewSeqID int
SELECT @MaxSeqID = Max(SeqID) from Contract_Live..Contract WHERE ContractID = @ContractID
SET @NewSeqID = @MaxSeqID + 1
--Copy Contract info for new seq with new seqid- record has default start and end dates
INSERT INTO [Contract_Live].[dbo].[Contract] ([ContractID], [seqID], [Status], [ContractName])
SELECT @ContractID, @NewSeqID, 'In Process', ContractName
FROM [Contract_Live].[dbo].[Contract]
WHERE [Contract_Live].[dbo].[Contract] .ContractID = @ContractID
GO
View 11 Replies
View Related
Mar 3, 2006
I'm trying to insert records into a table and getting the below error and not sure how to resolve it:
Server: Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_propertyItem'. Cannot insert duplicate key in object 'propertyItem'.
The statement has been terminated.
View 2 Replies
View Related
Feb 15, 2008
Violation of PRIMARY KEY constraint 'PK_tbl_others. Cannot insert duplicate key in object 'tbl_others.
The statement has been terminated.-- What does it mean i cant load the data to tbl_others
View 2 Replies
View Related
Jul 20, 2005
Hi allWould there be a easy way to find the column name(s) which constitutea Primary constraint for a table through navigating the systemcatalogs.I found that the PK Constraint object in syscontraints is showing thecolid = 0.TIANorman
View 2 Replies
View Related
Apr 2, 2007
When replication merge, is taking place between two servers on one particular table I keep getting violation of primary key constraint, Does anyone out there have a fix for this?
View 1 Replies
View Related
Jan 4, 2008
Hi,
I've a set of source tables on one server and a set of destination tables on another.
When I move data from one server to next, I get an error saying "Violation of Primary Key Constraint". This happens for only one table among the 6 tables that I have. I dont understand why it throws me this error. Though theres an error for this table, the data from source table moves into this destination table.
Am I missing something?
Thanks,
Subha Fernando
View 7 Replies
View Related
Jan 4, 2008
Hi,
I've a set of source tables on one server and a set of destination tables on another. I'm moving this using a data flow task.
When I move data from one server to next, I get an error saying "Violation of Primary Key Constraint". This happens for only one table among the 6 tables that I have. I dont understand why it throws me this error. Though theres an error for this table, the data from source table moves into this destination table.
Am I missing something?
Thanks,
Subha Fernando
View 5 Replies
View Related
Jun 26, 2007
How do I make a Primary Key in a table automatically increment as a new row of data is entered? do i have to do it with a trigger?? if so what is the code behind it....what I have is a Applicant table, which holds the ApplicantID (<<<needs auto incremented), FirstName, LastName, Address, City, State, Zip, and PhoneNumber. I enter all the other information through visual web developer, and what I need to do when the hit submit on the form with the data i want to automatically set an id right there. any suggestions will help!
View 3 Replies
View Related
Dec 21, 2007
Hi all,I have a table where I have my ProdPK set up as Primary key, turned on "Is Identity" and set the Identity increment to 1. But each time I add a new item, the number incremented by 2... I have couple of other tables and they are all fine, just this particular table increased twice as it should. I check the setting against other tables and everything seems to be the same. By the way, this is adding the data to the table inside MS SQL Server Management Studio manually. I haven't done anything in the ASP.NET page yet. Thank you very much,Kenny.
View 5 Replies
View Related
Apr 13, 2008
Dear all,
I am using SQL express 2005 in visual studio 2008. I would like to use Form View to insert new record in the table. There is a column call "id". I think it is too stupid to let user enter a unique identifier instead of generating by the system.
I know how to generate a random integer number in a normal TextBox. However, when I edit the InsertItemTemplate, I cannot modified the value of that textbox. For example, i couldn't use "TextBox1.Text = temp;" here. How can I achieve to change this TextBox's value by the system that I would like to generate a random interger. Thanks a lot.
Regards,
Eric Chan
View 4 Replies
View Related
Dec 7, 2004
How do I make my Primary Key Auto Increment from Enterprise Manager?
Thanks
View 1 Replies
View Related
Jan 25, 2006
I discovered the cause:
in SQL Server Enterprise Manager, open up db tree, right click the table, go - 'design' and set 'Identity' to yes.
View 1 Replies
View Related
Jun 8, 2007
I can't for the life of me find out how to auto increment the primary key like you can in access. I want to use userIds that will auto increment as new users are added. Can you guys help me out at all?
As of now I used this code as an example. I'm using SQL server 2005
CREATE TABLE Customer
(SID integer PRIMARY KEY,
Last_Name varchar(30),
First_Name varchar(30));
View 5 Replies
View Related
Jan 26, 2007
I want a table to have an int auto primary key, that will fill in deleted rows.
EG
1
2
4
5
8
The next primary key should be 3 (NOT 9)
This table changes often, so just adding 1 to the last number used, will fast run out of numbers.
The table is accessed by many users, so it can not be manually generated.
View 5 Replies
View Related
Mar 6, 2008
I have two tables: table 1 and table 2. The primary key of table is composite key of two collumns and table 2 is the child of parent table 1. Is it possible to create / define a foreign key constraint using a composite primary key?
View 10 Replies
View Related
Apr 18, 2007
We are using web application, our server log this type error. some can help me out how to slove this type of error
4/16/2007 2:44:26 PM DECIMonitoring.DocTracking.Db:insertState [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of PRIMARY KEY constraint 'idx_edcstate_p_cl_01'. Cannot insert duplicate key in object 'tbl_edc_state'. tech.deci.com:5555
4/16/2007 2:44:26 PM DECIMonitoring.DocTracking.Db:insertState [ADA.1.316] Cannot execute the SQL statement "INSERT INTO db_webmethods_support_edc.dbo.tbl_edc_state(stat_s tate_id, state_item_id, state_start_ts) VALUES (?, ?, ?)". "
(23000/2627) [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of PRIMARY KEY constraint 'idx_edcstate_p_cl_01'. Cannot insert duplicate key in object 'tbl_edc_state'.
(HY000/3621) [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]The statement has been terminated."
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of PRIMARY KEY constraint 'idx_edcstate_p_cl_01'. Cannot insert duplicate key in object 'tbl_edc_state'. tech.deci.com:5555
4/16/2007 2:44:26 PM DECIMonitoring.DocTracking.Db:insertState [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of PRIMARY KEY constraint 'idx_edcstate_p_cl_01'. Cannot insert duplicate key in object 'tbl_edc_state'. tech.deci.com:5555
View 3 Replies
View Related
Apr 17, 2014
Got the following error when the backup was run
Executing the query "BACKUP DATABASE [msdb] TO DISK = N'd:Sql backups..." failed with the following error: "Violation of PRIMARY KEY constraint 'PK__backupse__21F79AAB7WERB85D3'. Cannot insert duplicate key in object 'dbo.backupset'. The duplicate key value is (16771).Could not insert a backup or restore history/detail record in the msdb database. This may indicate a problem with the msdb database. The backup/restore operation was still successful.
12 percent processed.
21 percent processed.
31 percent processed.
41 percent processed.
[code]...
The statement has been terminated.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
View 4 Replies
View Related