How to add the primary key and I have increment the record 1 by 1 - sql

I have to add a auto_Increment primary key column to the table. I used this script to create but its doing fine. but one thing is thtat I have alredy have a primary key in that table. so its throwing an error.
how to remove alredy existing primary and making a new auto_increamtn column has a primary key.
ALTER TABLE ABCD ADD ABCD_Id int NOT NULL IDENTITY (1,1) PRIMARY KEY
thanks

You need to drop the PRIMARY key constraint first, something like
ALTER TABLE DROP CONSTRAINT PK_Table_Col

Related

Switch primary key constraint to a new column

I want to switch the primary key away from my existing identity column
to a different column.
This is my table:
CREATE TABLE dbo.ParkingLot
(
ID int IDENTITY(1,1) PRIMARY KEY,
Address ???,
Status ???,
newID ???
);
I want to remove the primary key on the ID column and instead have newID be the primary key (this is a new column but it is already populated with values).
Drop primary Key constraint and Re-Add a new one
You have to drop the primary key constraint and add a new one
Drop primary key Constraint
-- Return the name of primary key.
SELECT name
FROM sys.key_constraints
WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) =
N'Tablename';
GO
-- Delete the primary key constraint.
ALTER TABLE Production.Tablename
DROP CONSTRAINT PK_Tablename;
GO
Add new primary key Constraint
ALTER TABLE Tablename ADD CONSTRAINT pk_NewPrimary PRIMARY KEY (Newid)
References
Delete Primary Keys
Change primary key column in SQL Server

Cannot find primary key column in table

I added a constraint primary key to my table and the query ran successfully. The dilemma I am facing is I cannot locate the primary key column in my table.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( "Address_of_New_Home","Sq_Ft","Build_Spec", "Realtor_Sale","Can","Actual_Sale_Date")
When I do:
select * from salesdata
It shows all the columns from before and no primary key column (pk_salesdata).
And even more baffling is when:
select pk_salesdata from salesdata
Database shows:
ERROR: column "pk_salesdata" does not exist
I want to add primary key column to the table. I humbly request assistance of databasers.
You create PRIMARY KEY, but...
you create composed primary key from columns "Address_of_New_Home", "Sq_Ft", "Build_Spec", "Realtor_Sale", "Can", "Actual_Sale_Date" - it is not good idea
your primary key have an alias name pk_salesdata, but it's only constraint name
you didn't create new column
If you would like new synthetic primary key column you have to use command:
ALTER TABLE salesdata ADD COLUMN mynewautoincrementid SERIAL PRIMARY KEY;
Primary Kay it's constraint in table when you add a primary key to any column you can select column name to show data
It looks like you've wrapped the columns you want to include in your primary keys in double quotes. That's not how this command works. Drop the quotes re-run the command and see what happens.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( Address_of_New_Home,Sq_Ft,Build_Spec, Realtor_Sale,Can,Actual_Sale_Date)
It might be easier to add a primary index through the SQL GUI.
Here's the MS documentation page for creating a primary key.
https://msdn.microsoft.com/en-us/library/ms189039.aspx
Note: Since your adding a primary key don't expect it to be available as a column.

ALTER COLUMN Command doesn't work SQL Server

i want to add to a primary key in one table a references to the primary key of another table.
my code:
CREATE TABLE[payment]
(ID int Primary key)
CREATE TABLE [tab]
(ID int Primary key references tab2(ID))
Alter Table payment
alter column ID
ADD constraint fk_payment
references tab(ID)
i get the error that the syntax near constraint is wrong, but i don't know what to change
because of the not changeable order of the table Alter table is the only option. to reference from one table to the other doesn't work cause I've references from that table to another one already.
i need two one-to-one-relations from one table to another
If you want to add a FK constraint, just use this code:
ALTER TABLE dbo.payment
ADD CONSTRAINT fk_payment
FOREIGN KEY(ID) REFERENCES dbo.tab(ID)
You don't need to alter the column or table - just add the constraint

Drop one Primary key and add another in oracle

I have a table that I need to drop the primary key which is a compound key and make it a primary key based on a single value.
I dropped the original Primary key:
SQL> alter table depositor
2 drop primary key;
Table altered.
But when I try try add the new back I get an error message.
SQL> alter table depositor
2 add primary key (account_number);
alter table depositor
*
ERROR at line 1:
ORA-02437: cannot validate (ZSMITH.SYS_C0084996) - primary key violated
Was the PK not dropped? Did I not add it back correctly?
On an existing table, you can only create a primary key if the data in that table would really work as a primary key (i.e. all values distinct and not null).

Change Primary Key

I have a table in Oracle which has following schema:
City_ID Name State Country BuildTime Time
When I declared the table my primary key was both City_ID and the BuildTime, but now I want to change the primary key to three columns:
City_ID BuildTime Time
How can I change the primary key?
Assuming that your table name is city and your existing Primary Key is pk_city, you should be able to do the following:
ALTER TABLE city
DROP CONSTRAINT pk_city;
ALTER TABLE city
ADD CONSTRAINT pk_city PRIMARY KEY (city_id, buildtime, time);
Make sure that there are no records where time is NULL, otherwise you won't be able to re-create the constraint.
You will need to drop and re-create the primary key like this:
alter table my_table drop constraint my_pk;
alter table my_table add constraint my_pk primary key (city_id, buildtime, time);
However, if there are other tables with foreign keys that reference this primary key, then you will need to drop those first, do the above, and then re-create the foreign keys with the new column list.
An alternative syntax to drop the existing primary key (e.g. if you don't know the constraint name):
alter table my_table drop primary key;
Sometimes when we do these steps:
alter table my_table drop constraint my_pk;
alter table my_table add constraint my_pk primary key (city_id, buildtime, time);
The last statement fails with
ORA-00955 "name is already used by an existing object"
Oracle usually creates an unique index with the same name my_pk. In such a case you can drop the unique index or rename it based on whether the constraint is still relevant.
You can combine the dropping of primary key constraint and unique index into a single sql statement:
alter table my_table drop constraint my_pk drop index;
check this:
ORA-00955 "name is already used by an existing object"