Code cracker
Code cracker

Reputation: 396

How to change column length of existing table in SQL?

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.

Upvotes: 1

Views: 1899

Answers (2)

OMR
OMR

Reputation: 12188

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

Upvotes: 1

N69S
N69S

Reputation: 17206

  1. Development solution

you correct it in that file and refresh your migration with php artisan migrate:fresh but you lose all the data in your database

  1. Production solution

Make a new migration with the alter statement.

public function up()
{
    \DB::statement('ALTER TABLE user_documents ALTER COLUMN upload_url VARCHAR (200)');
}

Upvotes: 1

Related Questions