Recent

Author Topic: tbMy - how to make a field longer?  (Read 658 times)

Nicole

  • Hero Member
  • *****
  • Posts: 972
tbMy - how to make a field longer?
« on: January 02, 2023, 08:15:57 pm »
I have a table, which looks like shown below.
It is embedded in my code and to touch it in the wrong way, will kill a lot of items, I do not want to imagine what all.

There is a field
"BEZEICHNUNG VARCHAR(50) "

I would rather prefer to have it as
100 digits
and
UTF8 or so.

Code: MySQL  [Select][+][-]
  1. CREATE TABLE TBNEWS
  2. (
  3.   DATUM DATE NOT NULL,
  4.   ZEIT TIME,
  5.   CODE_ INTEGER NOT NULL,
  6.   BEZEICHNUNG VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE DE_DE,
  7.   CONSTRAINT PK_TBNEWS PRIMARY KEY (ID)
  8. );
  9.  
  10. ALTER TABLE TBNEWS ADD CONSTRAINT FK_TBNEWS_1
  11.   FOREIGN KEY (DATUM) REFERENCES TBKALENDER (JDATUM);
  12.  ON TBNEWS TO  SYSDBA WITH GRANT OPTION;

And yes, this fields arel filled with stuff already.

Do you see any option to change the length or even the type without data loss and being busy for half a day?
Or is it better to live with the old version?

Thank you for your ideas.

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: tbMy - how to make a field longer?
« Reply #1 on: January 02, 2023, 09:01:41 pm »
I have a table, which looks like shown below.
It is embedded in my code and to touch it in the wrong way, will kill a lot of items, I do not want to imagine what all.

There is a field
"BEZEICHNUNG VARCHAR(50) "

I would rather prefer to have it as
100 digits
and
UTF8 or so.

Code: MySQL  [Select][+][-]
  1. CREATE TABLE TBNEWS
  2. (
  3.   DATUM DATE NOT NULL,
  4.   ZEIT TIME,
  5.   CODE_ INTEGER NOT NULL,
  6.   BEZEICHNUNG VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE DE_DE,
  7.   CONSTRAINT PK_TBNEWS PRIMARY KEY (ID)
  8. );
  9.  
  10. ALTER TABLE TBNEWS ADD CONSTRAINT FK_TBNEWS_1
  11.   FOREIGN KEY (DATUM) REFERENCES TBKALENDER (JDATUM);
  12.  ON TBNEWS TO  SYSDBA WITH GRANT OPTION;

And yes, this fields arel filled with stuff already.

Do you see any option to change the length or even the type without data loss and being busy for half a day?
Or is it better to live with the old version?

Thank you for your ideas.

https://firebirdsql.org/file/documentation/html/en/refdocs/fblangref40/firebird-40-language-reference.html#fblangref40-ddl-tbl-alter
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Nicole

  • Hero Member
  • *****
  • Posts: 972
Re: tbMy - how to make a field longer?
« Reply #2 on: January 03, 2023, 05:38:44 pm »
Thank you for your answer.
Unfortunately this is just what is generally known about how to change a table.

My questions was: Is it wise to do it in my case and if yes how exactly?

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: tbMy - how to make a field longer?
« Reply #3 on: January 03, 2023, 09:02:53 pm »
if you only want to change varchar length, use ALTER COLUMN:
Code: SQL  [Select][+][-]
  1. ALTER TABLE TBNEWS ALTER COLUMN BEZEICHNUNG TYPE VARCHAR(100);
 
while if you want to change the CHARSET as well, you need to add a new column and move the data to it
Code: SQL  [Select][+][-]
  1. ALTER TABLE TBNEWS ADD BEZEICHNUNG_NEW VARCHAR(100) CHARACTER SET UTF8;  
  2. UPDATE TBNEWS SET BEZEICHNUNG_NEW=CAST(BEZEICHNUNG AS VARCHAR(100) CHARACTER SET UTF8);  
  3. ALTER TABLE TBNEWS DROP BEZEICHNUNG;  
  4. ALTER TABLE TBNEWS ALTER COLUMN BEZEICHNUNG_NEW TO BEZEICHNUNG;
 
Best regards / Pozdrawiam
paweld

Nicole

  • Hero Member
  • *****
  • Posts: 972
Re: tbMy - how to make a field longer?
« Reply #4 on: January 05, 2023, 06:08:08 pm »
Thanks Pawel, this looks very helpful!

 

TinyPortal © 2005-2018