Postgres Alter ID Start With? - sql

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.

Related

Upgrading H2 database gives constraint not found exception

I am trying to upgrade my H2 dependency which I use on my testcases from 1.4.200 to 2.1.212 but it gives a constraint not found exception when I try to do so. The SQL is like this:
CREATE TABLE itineraries
(
id SERIAL,
itinerary_id VARCHAR(36) NOT NULL,
user_id VARCHAR(36) NOT NULL,
created_at TIMESTAMP,
version INTEGER DEFAULT 1,
update_timestamp BIGINT,
CONSTRAINT itineraries_pkey PRIMARY KEY (itinerary_id, user_id),
CONSTRAINT itineraries_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (user_id)
);
CREATE UNIQUE INDEX itineraries_id_key ON itineraries (id);
CREATE TABLE subscriptions
(
subscription_id VARCHAR(36) PRIMARY KEY NOT NULL,
type VARCHAR(10) NOT NULL,
status VARCHAR(20),
last_updated TIMESTAMP NOT NULL,
itinerary_id VARCHAR(36) NOT NULL,
itinerary_db_id BIGINT,
CONSTRAINT subscriptions_it_itinerary_id_fkey FOREIGN KEY (itinerary_db_id) REFERENCES itineraries (id)
);
Which gives the following error:
Cause: org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "PRIMARY KEY | UNIQUE (ID)" not found; SQL statement:
What needs to be changed about the SQL? Since to me it seems like the unique index is created before the create table query.
Unique indexes and unique constraints are different things.
You need to create a constraint and you don't need an index in H2, because unique constraints in H2 create indexes automatically.
CREATE TABLE itineraries
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY CONSTRAINT itineraries_id_key UNIQUE,
…
I changed PostgreSQL-compatibility SERIAL to standard-compliant BIGINT GENERATED BY DEFAULT AS IDENTITY, because SERIAL is a legacy data type and it creates an INTEGER column, but it is referenced by subscriptions.itinerary_db_id with BIGINT data type, it isn't strictly required, but normally you should use the same data type for both columns, you can choose BIGINT, INTEGER or some other numeric type.
Usually it is more reasonable to create a primary key constraint for id column and a unique constraint for (itinerary_id, user_id), but you may have own reasons for such schema.
Also VARCHAR(36) looks like a some data type for UUID values, H2 has more efficient specialized UUID data type for this purpose.

Create an auto incrementing alpha numeric primary key in SQL Server Management Studio

I have a Student table in SQL Server database which is as follows:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want the Id property to be alpha-numeric and auto-increment itself for a new entry. I want Id to be S<number> and then S<number+1> and so on.
I tried to solve this problem as a two-step process:
(i) I first tried to make the Id an auto-incrementing property by doing this:
Then I pressed "Update":
And then I updated again and it led me to this table:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I do not think Id is an auto-incrementing value yet. How can I make it both auto-incrementing and alpha-numeric from the following interface:
It seems that you don't really want a fully auto-incrementing alphanumeric column A001,A002...B001, you just want a regular integer column with a prefix of S. For this you can use a simple computed column
ALTER TABLE Student
ADD MyId AS CONCAT('S', Id);

Cannot define PRIMARY KEY constraint on nullable column in table 'tp_ladder'

I'm trying to run the following SQL statement
--dropping customer TABLE--
DROP TABLE tp_ladder;
--creating ladder TABLE
CREATE TABLE tp_ladder (
ladder_id INTEGER,
ladder_type VARCHAR(50),
ladder_name VARCHAR(50) NOT NULL,
ladder_discount DECIMAL(3,2),
ladder_price DECIMAL(8,2) NOT NULL,
ladder_weight DECIMAL(5,2),
ladder_height DECIMAL(5,2),
ladder_rating DECIMAL(10,2),
warehouse_id INTEGER
);
--creating primary key for ladder table
ALTER TABLE tp_ladder
ADD CONSTRAINT tp_ladder_ladder_id
PRIMARY KEY(ladder_id);
However I receive the error message:
Cannot define PRIMARY KEY constraint on nullable column in table 'tp_ladder'
Any advice?
The error is quite clear, but why it is an error is not obvious.
Other databases (such as MySQL and Postgres), do allow you to do what you want -- adding a primary key on a column that is not explicitly declared as NOT NULL. After all, PRIMARY KEY imposes a NOT NULL constraint as well. This is surprising, especially on an empty table. So, the error is not obvious.
Further, SQL Server stores the null flags for all columns, even those that are declared NOT NULL. So, even if there were data, then the data would not need to change (assuming there are no NULL values). Not all databases store NULL flags the same way.
If you have defined the table, you can modify the column using:
ALTER TABLE tp_ladder ALTER COLUMN ladder_id INT NOT NULL
This will allow you then add the primary key constraint.
But, I recommend doing it in-line when you create the table:
ladder_id INT PRIMARY KEY
Note that when defined in the CREATE TABLE, the NOT NULL is not needed (it is actually redundant).

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

Alter a table column with auto increment by 1 in derby

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)
) ;