Re: Reset sequence to current maximum value of rows

2024-06-14 Thread Alban Hertroys
> company_nbr | company_name > -+- > 1 | Markowitz Herbold PC > 2 | Markowitz Herbold PC > 3 | Markowitz Herbold PC > 4 | Markowitz Herbold PC > 5 | Markowitz Herbold PC >

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread David G. Johnston
On Thu, Jun 13, 2024 at 3:13 PM Rich Shepard wrote: > Yes, I'm sure. Early yesterday I did get duplicate key errors. That's when > I > looked on stackexchange to learn how to reset the sequence's max value to > the value of the number of rows in the table. Not only did my attempt to > add > a sin

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Adrian Klaver
On 6/13/24 15:13, Rich Shepard wrote: On Thu, 13 Jun 2024, Adrian Klaver wrote: Not with: Table "public.companies" [...] Indexes:    "organizations_pkey" PRIMARY KEY, btree (company_nbr) That would throw duplicate key errors. Are you sure that you did not do this on the contacts table a

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, Adrian Klaver wrote: Not with: Table "public.companies" [...] Indexes: "organizations_pkey" PRIMARY KEY, btree (company_nbr) That would throw duplicate key errors. Are you sure that you did not do this on the contacts table as the company FK back to companies

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Adrian Klaver
On 6/13/24 12:57, Rich Shepard wrote: On Thu, 13 Jun 2024, David G. Johnston wrote: Because you specified company_name in the column listing for the things you are inserting values for. So in column position 2 you must have a value than can be inserted into the company_name column. It is utterl

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, David G. Johnston wrote: You need to show your work here. As your PK is a number it cannot have a company name as a value and so this doesn't make sense. David, insert into companies (company_nbr,company_name,industry,status) values (DEFAULT,'new company name','Industry'

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, Jeremy Smith wrote: There's no need to specify the column if it has a default value, but specifying it did not cause the issue that you saw. Jeremy, I did not know this. While the reason for the issue shall remain unknown, it did happen and my update script restored order

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread David G. Johnston
On Thu, Jun 13, 2024 at 10:20 AM Rich Shepard wrote: > Two tables have a sequence for the PK. Over time I manually entered the PK > numbers not being aware of applying DEFAULT to generate the next number. > > I just tried to set one table's PK sequence to the current max(PK) value > using this ex

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread David G. Johnston
On Thu, Jun 13, 2024 at 11:24 AM Rich Shepard wrote: > On Thu, 13 Jun 2024, David G. Johnston wrote: > > > You need to show your work here. As your PK is a number it cannot have a > > company name as a value and so this doesn't make sense. > > insert into companies (company_nbr,company_name,indu

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Jeremy Smith
> Aha! So it's likely that by listing the PK column name in the list of > columns to be inserted was what caused the problem? No need to specify > DEFAULT for it? > There's no need to specify the column if it has a default value, but specifying it did not cause the issue that you saw.

Re: Reset sequence to current maximum value of rows [RESOLVED]

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, David G. Johnston wrote: There is no way, in the absence of a user trigger, that the above insert command changed pre-existing rows. And if you cannot reproduce the behavior you claim to have seen I will continue to just assume you have faulty memory. David, While there m

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread David G. Johnston
On Thu, Jun 13, 2024 at 12:57 PM Rich Shepard wrote: > INSERT into companies (company_nbr,company_name,industry,status) VALUES > (DEFAULT,'A new company name', 'Manufacturing',DEFAULT); > > Yesterday, before learning to use DEFAULT for the company_nbr PK I entered > all rows using company_nbr 234

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, Ron Johnson wrote: No need to do that. Just write: INSERT INTO public.companies (company_name, , industry, status) VALUES ('Berkshire Hathaway', 'Conglomerate', 'Mumble'); The next value of companies_org_nbr_seq will automatically be taken and inserted into the table.

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, David G. Johnston wrote: Because you specified company_name in the column listing for the things you are inserting values for. So in column position 2 you must have a value than can be inserted into the company_name column. It is utterly immaterial how you specified the valu

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, Adrian Klaver wrote: You sure you did not actually do an UPDATE without a WHERE? Adrian, Yep. There was no row to update as I was adding a new company. Regards, Rich

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Adrian Klaver
On 6/13/24 11:38, Rich Shepard wrote: On Thu, 13 Jun 2024, Ron Johnson wrote: Today I saw that I had missed one new company and entered it using DEFAULT for the company_nbr PK. When I looked at that table every company_name that I had added yesterday was changed to the one inserted today. Y

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Ron Johnson
On Thu, Jun 13, 2024 at 2:38 PM Rich Shepard wrote: > On Thu, 13 Jun 2024, Ron Johnson wrote: > > > If the table has a primary key, then the command *should* have failed > with > > a duplicate key error as soon as the first dupe was discovered. > > Ron, > > I had manually set the PKs (column: com

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Ron Johnson
On Thu, Jun 13, 2024 at 1:20 PM Rich Shepard wrote: > Two tables have a sequence for the PK. Over time I manually entered the PK > numbers not being aware of applying DEFAULT to generate the next number. > > I just tried to set one table's PK sequence to the current max(PK) value > using this exp

Re: Reset sequence to current maximum value of rows

2024-06-13 Thread Rich Shepard
On Thu, 13 Jun 2024, Ron Johnson wrote: If the table has a primary key, then the command *should* have failed with a duplicate key error as soon as the first dupe was discovered. Ron, I had manually set the PKs (column: company_nbr) which has a sequence defined for it when I added about 50 ro