Change Primary Key with custom identity seed on existing table SQL Server - sql-server

I very new in SQL Server it's about 3 months. Now, I am stuck with a problem.
I need to change the primary key on a table with lots of data, about 10000 rows. Some other tables have relationship with this table (FK), but some tables stand alone. i wanna change the primary key and change the start identity seed with i want.
I have browsing in google but still no luck.
Can someone in here give me solution.
Thanks..

1)To DROP a PRIMARY KEY Constraint
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
2)Alter table with new primary constarint
ALTER TABLE Persons
ADD PRIMARY KEY (pk_PersonID)

Related

Foreign Key constraint on set of rows of a table data sql server

I have a ValidationTable like
and OrderProcessing table like
I want to create foreign key constraint on OrderProcessing table on column OrderType that references first 3 rows of ValidationTable (of ValidationType = "orders"). Is this possible? I referred similar questions but those did not really help.
I don't believe it is possible to link a specific set of rows.
The constraint has to be for the whole column.

Primary key column with Foreign key reference to itself

I came across this T-SQL code in a client's database today:
CREATE TABLE dbo.Dates
(
MarketDate date,
PRIMARY KEY (MarketDate),
FOREIGN KEY (MarketDate) REFERENCES dbo.Dates (MarketDate)
)
It is unclear to me what purpose can be served by allowing the primary key column to have a foreign key relationship back to itself. Naturally I have deleted the relationship.
Would this foreign key have any effect at all? Is there ever a use case which would justify using it? If not then why would SQL Server permit such a relationship.
The only reason I can imagine is that the creator of the table wanted to disallow the use of TRUNCATE TABLE statements against the Dates table.

SQL Server having a column as both PK and FK

I have not done much database design work and I was searching around for some example.
While I know the difference from a primary and foreign key, one thing that caught be off guard was that even if a Table has a primary key and is used as a foreign key in another table, as I was used the GUI SSMS tools, I noticed that I sometime end up having this
PhoneID (PK, int, not null)
While my User table
UserId(PK,FK, int, not null)
BOTH of these tables have these ID's as primary keys in those tables, along with foreign keys in other tables, but why does one of them have "PK,FK" obviously I accidentally created it, but should it be like that?
It is Possible for a Primary key to be a Foreign Key as well at the same time.
But looking at you database design, In your case I dont think it was intentional it was done by mistake. And if it wasnt done by mistake then you need to fix it.
In your dbo.PhoneType Table the column PhoneTypeID needs to be a Primary key only not a Foreign key. My guess is this was done by mistake, you wanted to create a Foreign key in your dbo.Phone table on column PhoneTypeID referencing PhoneTypeID column in dbo.PhoneType table. But somehow you end up create a foreign key constraint on the Primary key column of the dbo.Phontype table.
This Design contradicts constraints.
In simple english : The foreign Key Constraint on your dbo.PhoneType(PhoneTypeID) enforces that you cannot have a PhoneTypeID in dbo.PhoneType table unless it exists in PhoneTypeID column of dbo.Phone table.
On the other hand Foreign Key Constraint on dbo.Phone(PhoneTypeID) enforces that you cannot have a PhoneTypeID in dbo.Phone unless it exists in dbo.PhoneType(PhoneTypeID).
and same is true for the UserID column in dbo.Users table.
Solution
You need to drop the following Constraints to make it work properly.
1) In dbo.PhoneType table Drop Foreign key constraint referencing
PhoneTypeID column in dbo.phone table.
2) In dbo.Users Table drop the Drop Foreign key constraint referencing
UserID column in dbo.phone table.
It's entirely possible, yes. A primary key of a table could also be a foreign key referencing another table.
In your case, I'm not exactly sure what you did. You can check the constraints to see which column the UserId column is referencing.
As an additional note, simply adding a foreign reference to a table does not implicitly make that column a foreign key on another table. For example, just because you add FK_PhoneTypeID to the Phone table, SQL Server does not automatically make PhoneTypeID column in the PhoneType table a FK. In your statements, somewhere, it's possible that you made assignments to other columns, or even to themselves.

Adding column with existing table which have primary key

I need your help...., Please help me
I want to add a new primary key to an existing table which already has 3 columns as composite primary key. But, I do not want to drop 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)
Note:
Hem154 ~ Table Name
indexNo ~ Column Name which will to added
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.
Please help me, how can I do it???
Thanks
Drop all primary keys and add Again all primary keys
ALTER TABLE hem154
DROP PRIMARY KEY,ADD PRIMARY KEY (col1,col2,indexNO);
You can only have one primary key per table. You can either add a second table that maps your new PK to the old PK, or drop the old PK and add the new one. Dropping the PK won't drop the columns, it just stops using them as the composite key. Any tables that were relying on the old key should probably be updated as well to support whatever answer you decide on.
First DROP existing PRIMARY KEY and ADD new composite PRIMARY KEY.
ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1
GO
and then try your code
There is no easy way of doing this. From your question, I understand that you want to add one additional column to the existing Primary Key. If they have existing relationships, then it is not possible to do this, without dropping those FK constraints first. Even if you do that, you will not be able to reestablish those FK constraints again, as you have modified the PK.
If you do not need to reestablish those FK relationships, then you can do this:
Drop all FK relationships
Drop existing PK (data will not be deleted, only the constraint will be dropped)
Add new column
Recreate PK to include the new column
However, if you cannot drop the existing FK constraints, then I am afraid this is not possible
On Microsoft SQL Server Management Studio:
Select/highlight both columns
Right click either
Press "Set primary key"

How do you add a unique primary key field automatically in SQL Server?

I am using SQL Server 2012 and need to add a column with a unique primary key. I am about to load several hundred thousand records BULK and just discovered repetition in the field I was going to use. Have seen SEQUENCE and GUID. Need some guidance on the best choice and how to go about setting this up so that the key field is populated during the bulk load.
When you create your table in which you want to insert information create an IDENTITY column. That will serve as an auto-populating column with a unique number for each record.
Here is a link that might help you.
If you have already created your table just change this query to what suits to your table name and run it in order to add the new column you requested.
ALTER TABLE mytable
ADD COLUMN unique_id IDENTITY (1,1)
Just a slight update on what’s already posted that includes details for adding primary key constraint
alter table database.schema.table_t
add ID_column int identity(1,1)
primary key (ID_column)
If you already set the primary key on this table just go and remove it before you execute this statement.

Resources