Alter a table column with auto increment by 1 in derby - sql

I have created a table in derby Netbeans and now i realize that i need to make a column as auto incremented by 1 which is a primary key. How can i do so? I tried the following code but was in vain.
ALTER TABLE ISSUERECIPT ALTER IRCODE SET INCREMENT BY 1;
Do i need to create the table once again or can it be possible some other way?

I have found an alternate solution, i dropped the column from the database (thanks vels4j) added the column once again from the netbeans derby UI as shown below:

To alter the column to be auto-generated, the code is
ALTER TABLE ISSUERECIPT ALTER IRCODE SET INCREMENT BY 1;
BUT the column must already be defined with the IDENTITY attribute (as written in this documentation).
In most cases (assuming that you too), the primary key column is not set as IDENTITY. Therefore, you may intend to alter the column to IDENTITY, but that is impossible.
The only way is to drop the table and create it again, as written here.

ALTER TABLE ISSUERECIPT ADD IRCODE INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
I guess could do the things for you

Check this
ALTER TABLE ISSUERECIPT
ALTER IRCODE INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
If your table is empty, Try this
ALTER TABLE DROP PRIMARY KEY your_primaryKeyContrainName ;
ALTER TABLE ISSUERECIPT DROP COLUMN IRCODE ;
ALTER TABLE ISSUERECIPT ADD COLUMN
IRCODE PRIMARY KEY INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
See Also : Derby ALTER TABLE Syntax

ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

The ALTER TABLE statement cannot add an IDENTITY column to a table
If your table is empty or is not in production. drop table and create again, example:
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(CUSTOMER_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY
1),
FIRSTNAME VARCHAR(100) NOT NULL,VARCHAR(100),
PREFERRED_ID INTEGER,
CONSTRAINT primary_key PRIMARY KEY (CUSTOMER_ID)
);

Try this :
alter table ISSUERECIPT modify column IRCODE int(4) auto_increment

Recreate the table again see example below:
CREATE TABLE students
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(24) NOT NULL,
address VARCHAR(1024),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;

Related

Postgres Alter ID Start With?

I have an PostgreSQL SQL for creating table. Sometimes i need to TRUNCATE this table. but after this ID primary key, don't start with initial or desired starting value. my question is that how can i restart primary key (id) to desired starting value.
CREATE TABLE IF NOT EXISTS "binance_rules" (
"id" int8 GENERATED ALWAYS AS IDENTITY (START WITH 100100100000),
"THPARS_DATI" timestamp DEFAULT NULL,
"THPARS_LIST" varchar(10) check ("THPARS_LIST" in ('yes','no')) DEFAULT NULL,
"THPARS_PRIO" INT DEFAULT NULL,
PRIMARY KEY ("id")
);
any sql example will be appreciated.
You can alter the table column to restart the counting:
alter table binance_rules alter column id restart;
Here is a db<>fiddle.

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

How to Write a Script to add extra columns for my primary key

I have the following table in SQL Server 2008 R2
Now I need to write a script to add a new column cusomerVLANID as part of the primary key, so that the three columns becomes the primary key, is there a way to write such script.
Second thing I want to write a script to remove the Allow Null, check box from the CustomerVLANID columns ?
Thanks
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY ([ID], [CustomerName], [CustomerVLANSID])
Run this statement separately to set up the NOT NULL constraint:
ALTER TABLE <Table_Name>
ALTER COLUMN [CustomerVLANSID] INT NOT NULL
alter table TABLE1
alter column [CustomerVLANID] int not null
I hope this helps,
-Thomas
RosSQL.blogspot.com

Postgres: generate IDs automatically

Objective: Have postgres generate ids automatically
CREATE TABLE user_privilege (
id bigint NOT NULL,
name character varying(255) NOT NULL,
version integer
);
CREATE TABLE
INSERT INTO user_privilege (name, version) values ('XYZ', 1);
ERROR: null value in column "id" violates not-null constraint
ALTER TABLE user_privilege ALTER COLUMN id SET DEFAULT nextval('user_privilege_id_seq'::regclass);
ERROR: relation "user_privilege_id_seq" does not exist
Thanks!
EDIT:
I want to keep my id as bigint as all other tables have id as bigint.
You need to use either SERIAL or BIGSERIAL, not BIGINT.
CREATE TABLE user_privilege (
id BIGSERIAL NOT NULL,
It's not clear whether your table has a PRIMARY KEY or UNIQUE constraint. But it should.
You have to create the sequence at first:
CREATE SEQUENCE user_privilege_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
and after you can use it:
ALTER TABLE ONLY user_privilege ALTER COLUMN id SET DEFAULT nextval('user_privilege_id_seq'::regclass);
Here is the create sequence documentation

SQL Server : set primary key without dropping table and content [duplicate]

This question already has answers here:
SQL Server add auto increment primary key to existing table
(16 answers)
Add primary key column in SQL table
(3 answers)
Closed 9 years ago.
Is it possible to set the primary key and auto increment on a SQL Server table without dropping and recreating the table, and losing all it's data?
Yes of course! You just add a new column, and it an INT IDENTITY and add a primary key constraint to it:
ALTER TABLE dbo.YourTable
ADD ID INT IDENTITY(1,1) NOT NULL
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable PRIMARY KEY (ID)
If there is an existing primary key, you must first drop it:
IF EXISTS (SELECT * FROM sys.key_constraints
WHERE type = 'PK' AND parent_object_id = OBJECT_ID('MyTable')
AND Name = 'PK_MyTable')
ALTER TABLE MyTable DROP CONSTRAINT PK_MyTable
If you are adding a column to be used as a primary key, then you can simply add it:
ALTER TABLE MyTable ADD MyKey INT IDENTITY
Then, you can set this column as your table's primary key:
ALTER TABLE MyTable ADD CONSTRAINT PK_MyTable PRIMARY KEY(MyKey)