How Can I Specify A Name When Adding A Primary Key?
Aug 30, 2001Im using
ALTER TABLE mytable ADD PRIMARY KEY(mycolumn)
I want to use a specific name like PK_mycolumn, how the heck is this done?
thanks!
Im using
ALTER TABLE mytable ADD PRIMARY KEY(mycolumn)
I want to use a specific name like PK_mycolumn, how the heck is this done?
thanks!
Hi all,
Can anyone suggest me on Adding primary key to a table which has already a primary key.
Thanks,
Jeyam
I want to add new primary key into existing table which already has a primary key. But,I do not want to remove the old primary key, since there are many records and the old primary key also have relationship with other table
When I am using this query:
alter table hem154
add indexNO uniqueidentifier default newid()
alter table hem154
add CONSTRAINT pk_hem154_indexNo PRIMARY KEY (PK_indexNO)
go
Note:
Hem154 ~ Table name
indexNo ~ Column Name
I get this runtime error:
Msg 1779, Level 16, State 0, Line 1
Table 'hem154' already has a primary key defined on it.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Hi guys,I followed the ASP.net official tutorial to create a DAL & Business Logic Layer (http://www.asp.net/learn/dataaccess/tutorial02cs.aspx). I have a table with a int ID field. I wish to write a function to add a new entry into this table but have the ID field auto-increment.The ID field is set as the Identity Column and has a Identity Increment & Seed of "1". If I manually go to the table and insert a new record leaving the ID value null it automatically increments. But if I create a C# function to add a new entry I get an error saying that the ID field can't be Null. Is there any way to use the Update method as shown on line 14 below to add a new entry but with it automatically incrementing? I did create a function called InsertDevice that simply inserts the other fields using a SQL INSERT and it auto-increments fine, just wondering if there is a way to do it using the DataTable and the Update method? Thanks for any help!!! 1 public bool AddDevice(string make, string model)
2 {
3 //cannot have the same device entered twice!
4 if (Adapter.FillDeviceCountByMakeModel(make, model) == 1)
5 return false;
6
7 RepositoryDataSet.DevicesDataTable devices = new RepositoryDataSet.DevicesDataTable();
8 RepositoryDataSet.DevicesRow device = devices.NewDevicesRow();
9
10 device.make = make;
11 device.model = model;
12
13 devices.AddDevicesRow(device); << Error thrown Here!
14 int rows_affected = Adapter.Update(devices);
15
16 return rows_affected == 1;
17 }
I have come up with one scenarios where I have three table like Product, Services and Subscription. I have to create one table say Bundle where I can have some of the product id , service id and Subscription id , i.e. a bundle may contains sum prduct , services and subscription . How I can design these relations ?
View 3 Replies View RelatedI have a table of raw data where each column can be null. The thought was to create an identity key (1,1) and set as primary for each row. (name/ address / zip/country/joindate/spending) with surrogate key: "pkid".However other queries will not use this primary key. So for instance they may count the # of folks at a zip, select all names, addresses etc. The queries may order by join date, or select all the people that joined on a specific date.No other code would logically use the primary key (surrogate primary id key), therefore would it still have any performance benefits? at this time the table would have no clustured or nonclustured indexes or keys. I'm curious if there are millions of records.
View 7 Replies View RelatedI need to add a child table that will tell us who the participants counselor is, what I did was I did a Make Table query based off the primary key of the Parent table and made that the link (foreign key) for the People_tbl and the Counselor_tbl, so if the counselor changes then the user adds the record to the counselor tbl and then puts in the Effective date. The problem is that when I run a report it doesn't show the present counselor always shows the old counselor?
Code:
SELECT Student_ind.StudentFirstName, Student_ind.StudentLastName, Student_ind.[Student ID], People_tbl.[Family ID], People_tbl.FirstName,
People_tbl.LastName, People_tbl.[Parent ID]
FROM People_tbl RIGHT OUTER JOIN
Student_ind ON People_tbl.[Family ID] = Student_ind.[Family ID]
WHERE (People_tbl.LastName = @Enter_LastName) AND (People_tbl.FirstName = @Enter_FirstName)
Is there any way or option to get the all columns of dataset added to table when we add a table in data region. It will take lot of time to add one by one and also there are chances to add one column ore than once.
View 7 Replies View RelatedUma writes "Hi Dear,
I have A Table , Which Primary key consists of 6 columns.
total Number of Columns in the table are 16. Now i Want to Convert my Composite Primary key into simple primary key.there are already 2200 records in the table and no referential integrity (foriegn key ) exist.
may i convert Composite Primary key into simple primary key in thr table like this.
Thanks,
Uma"
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
We have a table, which has one clustered index and one non clustered index(primary key). I want to drop the existing clustered index and make the primary key as clustered. Is there any easy way to do that. Will Drop_Existing support on this matter?
View 2 Replies View RelatedWhat is the syntax for adding a column where you are adding a year to a date in a date format? For example adding a column displaying a year after the participation date in date format?
View 1 Replies View RelatedHi all
I have the following table
CREATE TABLE [dbo].[property_instance] (
[property_instance_id] [int] IDENTITY (1, 1) NOT NULL ,
[application_id] [int] NOT NULL ,
[owner_id] [nvarchar] (100) NOT NULL ,
[property_id] [int] NOT NULL ,
[owner_type_id] [int] NOT NULL ,
[property_value] [ntext] NOT NULL ,
[date_created] [datetime] NOT NULL ,
[date_modified] [datetime] NULL
)
I have created an 'artificial' primary key, property_instance_id. The 'true' primary key is application_id, owner_id, property_id and owner_type_id
In this specific instance
- property_instance_id will never be a foreign key into another table
- queries will generally use application_id, owner_id, property_id and owner_type_id in the WHERE clause when searching for a particular row
- Once inserted, none of the application_id, owner_id, property_id or owner_type_id columns will ever be modified
I generally like to create artificial primary keys whenever the primary key would otherwise consist of more than 2 columns.
What do people think the advantages and disadvantages of each technique are? Do you recommend I go with the existing model, or should I remove the artificial primary key column and just go with a 4 column primary key for this table?
Thanks Matt
Hello all,I'm taking over a project from another developer and i've run into a bit of a problem. This developer had a bad habit of not using primary keys when designing various databases used by his programs. So now i've got approx 1000 tables all of which do not have primary keys assigned. Does anyone know of a tsql script that i can run that will loop through each table and add a primary key field?Thanks in advance?Richard M.
View 2 Replies View RelatedI have a Department Table.
Can any one tell me its Primary Key.
I have the order
AutoNumber, D + AutoNumber, Code,
Can you help me regarding this.
Because some people never like to use AutoNumber.
That's why I am confused.
Hi..
I'm going to build database of university, but I have problem with primaru key,
This is the situation:
there are many faculities and each one has many departments,
each department has many courses,
each course has many sections..
The problem:
I want to make those fields in the same table and make the primary key generate from other fields,
(i.e)
I want the faculity be integer from 4 digit "Example the first faculity start with 1000 the second 2000 and so on" and the the department of each faculity will generate its value from the faculity number+interger number from 3digit "Example the department of the first faculity start with 1100 and the second on will be 1200 and so on "
the same thing will repeate for courses and sections so the sectionsID will be the primary key.
Do you know hoew this idea can be implement by SQL server 2005?
Please help me as soon as possible.
A column will be Primary Key. Others are B and C. I want A will contain B and C. I mean B data is X, C data is Y, A will be XY. How can i do this? Can i set in MSSQL or need ASP.NET?
View 1 Replies View RelatedHeya,
I'm trying to setup a Primary Key on a SQL 6.5 database.
Is there a way to do this? When I hit advanced, it asks for me to select a field for the primary key, but it doesnt list fields to selct from, and I cant type it in.
Thanks for your help,
Hi All,
Using DTS i have imported the data from sybase to MS SQL server and all the data and tables were imported correctly.But the primary keys are not marked why is it like this?
This is not a one time job and this is meant to be for the customers also.I cannot ask the customers to mark the primary keys themselves. Is there a way to get the keys also.While doing DTS I have marked all the options correctly.
Please help.
I am setting up some tables where I used to have an identity column as the primary key. I changed it so the primary key is not a char field length of 20.
Is there going to be a big performance hit for this? I didn't like the identity field because every time I referenced a table I had to do a join to get the name of object.
EG:
-- Old way
tbProductionLabour
ID (pk)| Descr | fkCostCode
----------------------
1 | REBAR | 1J
tbTemplateLabour
fkTemplateID | fkLabourID | Manpower | Hours
--------------------------------------------
1 | 1 | 1 | 0.15
-- New way
tbProductionLabour
Labour | fkCostCode
---------------------
REBAR | 1J
tbTemplateLabour
fkTemplateID | fkLabour | Manpower | Hours
-------------------------------------------
1 | REBAR | 1 | 0.15
This is a very basic example, but you get the idea of what I am referring to.
Any thoughts?
Mike
I need to create my own primary key, how do I go about doing that?? In the database I am working in usually has a primary key that looks like this VL0008
the V is for Vendors, thats basically their number. Some of these Vendors need to be licensed and some dont, the ones that are not licensed dont get a number but I am to use that as the Primary/Index key I need to create one for those particual vendors. How can I go about doing that??? I was wanting to make it TL888 something like that.
i'm having problem to get th primary key from d database....
for your information i'm using java to get the primary key....
this is my code...
rs = stt.executeQuery("sp_columns "+table_db+";");
while(rs.next())
{
out.write(""+rs.getString("COLUMN_NAME"));
out.write(", "+rs.getString("TYPE_NAME"));
out.write(", "+rs.getString("IS_NULLABLE"));
}
rs = stt.executeQuery("sp_foreignkeys @table_name = N'table_db';");
but the problem is....
i get this error message...could anyone tell me what's the problem....
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Could not
find server 'table_db' in sysservers. Execute sp_addlinkedserver to add th
e server to sysservers.
how do i solve this problem....
thanx to anyone who can help me...... :D
Please help:
I am creating a table called Bonus:
ProductHeading1
ProductHeading2 (could be null)
ProductHeading3(could be null)
Bonus
Datefrom
DateTo
.... what would be the primary key?! I know it would be DateTo and sumfing...... Since Heading2 and Heading3 could be null, they cannot be PK... and heading1 cannot be a PK because the following three DIFFERENT options could have the same heading1
Option 1) heading1 = "X" heading2 = Null heading3 = Null
Option 2) heading1 = "X" heading2 = "Y" heading3 = Null
Option 3) heading1 = "X" heading2 = "Y" heading3 = "Z"
... but I need a PK to make sure a bonus is not entered twice... I considered added an Id, but them how do I assign a id?! what would i make the id equal to???
Thanks....
Hi all,
Is it possible to have a primary key for SQL or Oracle or jet to have an alpanumeric beginning?
for example
1st District as a primary key
The statement is:
SELECT itemid FROM MASecurity WHERE userid=%d
Thanks,
Jj :)
What 's the way to know
the name of the column that is
the primary key of a table
In a recent course on database programming using Microsoft Access 2002. I noticed that the text entitiled New Perspectives Microsoft Access 2002 stated that a primary key could only be used once per table. But If I am not mistaken could one use the select key to select more than one primary key within a table.
View 8 Replies View RelatedHi guys,
Is there a method in sql server 2005 to format the primary key so it can be alphanumberic?
Thanks.
Violation of PRIMARY KEY constraint 'PK_Dunning_TBL'. Cannot insert duplicate key in object 'Dunning_TBL'.
The statement has been terminated.
(0 row(s) affected)
Msg 2627, Level 14, State 1, Procedure GenerateFiles_FST_SP, Line 220
Violation of PRIMARY KEY constraint 'PK_Exceptions_TBL'. Cannot insert duplicate key in object 'Exceptions_TBL'.
The statement has been terminated.
i got this error how can i resolve this?
Hi,
I am designing a database for containing the info for the employees in the institution. My dilemma:
-Should I use an sql autonumber primary key?
or
-should I merge lastName+FirstName+middleName into one field and use this string as the primary key?
thank you
I'm new to these forums, and I'm not a database developer, per se, so please forgive me if I make any newb'ish comments.
I have a lookup table called tblCars, that has two columns, cars_id and cars_title. Typically what I do with tables like this is I make cars_id an autonumber, and cars_title the primary key.
The cars_title would contain unique data such as Ford, Chevy, Toyota, etc, which is why I like to make it the primary key (ie - guarantee that it remains unique and no duplicates are ever placed in it).
I would then create an index on cars_id so that I could use it in foreign key constraints.
However, I'm being told by a number of people that it is incorrect to make cars_title the primary key, and that the autonumber field should be the primary key. Yet I am having trouble arriving at a real good reason as to why this is the correct way to do it. I like the warm-fuzzies that I get knowing that no one can accidentally insert a duplicate car title into tblCars because of the primary key constraint.
Thanks in advance for any thoughts or insights on this.
I've noticed that some of my tables have primary keys that are not referenced by a foreign key in another table, is this indicative of bad design?
Jill
Hi..
I'm going to build database of university, but I have problem with primaru key,
This is the situation:
there are many faculities and each one has many departments,
each department has many courses,
each course has many sections..
The problem:
I want to make those fields in the same table and make the primary key generate from other fields,
(i.e)
I want the faculity be integer from 4 digit "Example the first faculity start with 1000 the second 2000 and so on" and the the department of each faculity will generate its value from the faculity number+interger number from 3digit "Example the department of the first faculity start with 1100 and the second on will be 1200 and so on "
the same thing will repeate for courses and sections so the sectionsID will be the primary key.
Do you know hoew this idea can be implement by SQL server 2005?
Please help me as soon as possible.
Hi experts, I would like to ask how to set 2 primary keys in one table?
View 6 Replies View Related