add a new column to a table in sql with default value code example

Example 1: add a new column to existing table in a migration

// for Laravel 5+
php artisan make:migration add_email_to_users_table --table=users

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('email');
    });
}

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('email');
    });
}

php artisan migrate

Example 2: add column table sql default value

ALTER TABLE users
ADD visit_number INT DEFAULT 0;

Example 3: alter table add column with default value

ALTER TABLE Protocols
ADD ProtocolTypeID int NOT NULL DEFAULT(1)

Tags:

Sql Example