Wednesday, April 13, 2022

[FIXED] How to change column length of existing table in SQL?

Issue

I have one SQL table which will contain (TEXT/VARCHAR) columns of some length.

How can I change the existing column length without dropping or hardcode existing database table.

2020_02_13_065846_create_users_documents_table.php

public function up()
    {
        Schema::create('user_documents', function (Blueprint $table) {

            $table->increments('id');
            $table->string('upload_url', 200)->nullable();
            $table->string('user_name', 50)->nullable();
        });

Now I want to change the length of column user_name to 200 by creating a new table.


Solution

you just have to modify you columns:

The change method allows you to modify the type and attributes of existing columns. For example, you may wish to increase the size of a string column

make a new Migration, set this statement:

Schema::table('users', function ($table) {
    $table->string('upload_url', 500)->change();
 $table->string('user_name', 500)->change();
});


Answered By - OMR
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

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