PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] how change data type already registered in the database

 July 20, 2022     integer, java, postgresql, types     No comments   

Issue

I'm working now on a system that defines a PK (primary key) of the table as int, this PK has a sequence with a rule:

"YearYearMonthMonth" + 6 numbers.

ex: 2105000001

But the year of "22" (2022) was not predicted, or, if I were to adjust this pattern of the sequence to the current year, it would give an error, as it exceeds the max value. of an int.

Doubt: the system is huge, in java, PostgreSQL database, what would be the pros and cons to switch to Long? Would it bring database performance impacts something like that? Would all IDs already saved in the bank as int automatically change to Long? Sorry, but I don't know the safest way to solve this.. thank you


Solution

You can change the data type without losing any values using ALTER TABLE:

 alter table the_table
     alter the_column type bigint;

Note that this will lock the table and its content will be re-rewritten. So you will have to stop any application accessing that table.

You also need to alter the types of all columns referencing that table.



Answered By - a_horse_with_no_name
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing