SQL Alter Statement for Primary Key Column - sql

I am having a problem making the column I am adding NOT NULL using the SQL ALTER statement. I am fairly novice with SQL so any guidance would be great. I am using SQL Sever 2008 and I am receiving an error stating that the column cannot be added to the table because it does not allow nulls and does not specify a default definition. I already have data in the table and I am just looking to add an incremental primary key.
This is the SQL I am using to generate the column
ALTER TABLE EPUpdates.GenInfo_OpType3
ADD KeyOpType Integer NOT NULL
This is the SQL I am using to make it a Primary Key/ Identity Column
ALTER TABLE EPUpdates.GenInfo_OpType3
ADD PRIMARY KEY(KeyOpType)

try to add a default value to your column, but if you want to make it as a primary key, the values must be different for each row, so you have to update these value after creating the new column and before create your primary index.
ALTER TABLE EPUpdates.GenInfo_OpType3
ADD KeyOpType Integer NOT NULL Default 0

If your trying to to add an incremental primary key then try this
ALTER TABLE EPUpdates.GenInfo_OpType3
ADD KeyOpType INT NOT NULL IDENTITY (1,1) PRIMARY KEY

Related

Interbase how to add guid field to existing table

I have an old existing interbase table and I want to add a primary key field to and populate it. Is there any way to do it all in the SQL statement (like SQL server). Example:
ALTER TABLE IBUSERS ADD IBUSERSPK VARCHAR(32) default (newid()) NOT NULL
As far as I can tell in interbase newid function does not exist unless I am missing something.
I am using IBExpert and also have IBConsole.
Or am I stuck with populating this field in code after it gets created?
Thanks.
It appears interbase has no easy way to SQL populate a guid field. Therefore the solution is to create your own UDF function in the database to create a random guid. It then becomes a three step process:
Add the field to the table
ALTER TABLE IBUSERS ADD IBUSERSPK VARCHAR(32) NOT NULL
Populate the new field using the UDF:
UPDATE IBUSERS SET IBUSERSPK = GETGUID()
Add primary key constraint if it is such:
ALTER TABLE IBUSERS ADD CONSTRAINT PK_IBUSERS PRIMARY KEY (IBUSERSPK)
The nice thing about this is that then this function can be used anytime/anywhere in the database.

Adding column with primary key in existing table

I am trying to add primary key to newly added column in existing table name Product_Details.
New Column added: Product_Detail_ID (int and not null)
I am trying add primary key to Product_Detail_ID (please note: there are no other primary or foreign key assigned to this table)
I am trying with this query but getting error.
ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)
GO
Error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Product\_Details' and the index name 'pk\_Product\_Detils'. The duplicate key value is (0).
Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.
If you want SQL Server to automatically provide values for the new column, make it an identity.
ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID
GO
ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null
GO
ALTER TABLE Product_Details
add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
GO
In mysql, I was able to achieve with following query
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key
Add Primary Key to First Position
ALTER TABLE table_name
ADD column_name INT PRIMARY KEY AUTO_INCREMENT FIRST;
Reference: Stack Overflow | Tech On The Net
You are getting the error because you have existing data that does not fullfill the constraint.
There are 2 ways to fix it:
clean up the existing data before adding the constraint
add the constraint with the "WITH NOCHECK" option, this will stop sql server checking existing data, only new data will be checked
ALTER TABLE Jaya
ADD CONSTRAINT no primary key(No);
here Jaya is table name,
no is column name,
ADD CONSTRAINT is we giving the primary key keyword
If you want to add a new column say deptId to the existing table say department then you can do it using the below code.
ALTER TABLE department ADD COLUMN deptID INT;
it will create your new column named deptID.
now if you want to add constraint also along with new column while creating it then you can do it using the below code. In the below code I am adding primary key as a constraint. you can add another constraint also instead of primary key like foreign key, default etc.
ALTER TABLE department ADD COLUMN deptID INT NOT NULL ADD CONSTRAINT PRIMARY KEY(deptID);
k. friend
command:
sql> alter table tablename add primary key(col_name);
ex: alter table pk_Product_Detils add primary key(Product_Detail_ID);

How to add a PK to an existing table in DB2?

I am facing one problem. I have a table already create in DB2.
CREATE TABLE "DDL12"
(
"D4_1" decimal(10,0),
"D4_2" decimal(10,0),
);
I am trying to create a PK on this table as :-
ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");
But while running the command, I am getting the error saying D4_1 is NULLABLE.
Now, how can I create a PK on this table?
Thanks
Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now.
So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above.
You can change a column to not NULL like this:
ALTER TABLE "DDL12"
MODIFY "D4_1" decimal(10,0) NOT NULL;

How do I add a auto_increment primary key in SQL Server database?

I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment.
I'm working with a Microsoft SQL Server database. I understand that it can't be done in a single command but every command I try keeps returning syntax errors.
edit ---------------
I have created the primary key and even set it as not null. However, I can't set up the auto_increment.
I've tried:
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
I'm using NVARCHAR because it wouldn't let me set NOT NULL under int
It can be done in a single command. You need to set the IDENTITY property for "auto number":
ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY
More precisely, to set a named table level constraint:
ALTER TABLE MyTable
ADD MytableID int NOT NULL IDENTITY (1,1),
CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)
See ALTER TABLE and IDENTITY on MSDN
If the table already contains data and you want to change one of the columns to identity:
First create a new table that has the same columns and specify the primary key-kolumn:
create table TempTable
(
Id int not null identity(1, 1) primary key
--, Other columns...
)
Then copy all rows from the original table to the new table using a standard insert-statement.
Then drop the original table.
And finally rename TempTable to whatever you want using sp_rename:
http://msdn.microsoft.com/en-us/library/ms188351.aspx
You can also perform this action via SQL Server Management Studio.
Right click on your selected table -> Modify
Right click on the field you want to set as PK --> Set Primary Key
Under Column Properties set "Identity Specification" to Yes, then specify the starting value and increment value.
Then in the future if you want to be able to just script this kind of thing out you can right click on the table you just modified and select
"SCRIPT TABLE AS" --> CREATE TO
so that you can see for yourself the correct syntax to perform this action.
If you have the column it's very easy.
Using the designer, you could set the column as an identity (1,1): right click on the table → design → in part left (right click) → properties → in identity columns, select #column.
Properties:
Identity column:
In SQL Server 2008:
Right click on table
Go to design
Select numeric datatype
Add Name to the new column
Make Identity Specification to 'YES'
you can try this...
ALTER TABLE Your_Table
ADD table_ID int NOT NULL PRIMARY KEY auto_increment;

Table without a identity column but this column can't have repeated values

I'm creating a table that needs to have 2 columns. The first column can't be repeated. The thing is, I will insert the value of the first column. How do I create this column?
SQLServer 2005
Make the first column the primary key of the table.
Set the column as a primary key. I doesn't have to be an identity column to has a PK.
Create it the same way you would any other column: create table sometable (column1 varchar(10), column2 varchar(20)) or whatever.
Do you mean: How can you get the database to force it to be unique? Either declare it to be the primary key, or create a unique index on the column.
Perhaps you're thinking that a primary key must be auto-generated? There's no such rule. Whether you invent the value yourself or use an autonumber feature has nothing to do with whether a field can be a primary key.
Why not just put a unique constraint on it:
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
UNIQUE(<column_name>)