Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter column length in Schema builder?

I have two fields i need to increment the character limit on. I're read through the documentation and to my surprise i found no option for it. Is it possible to do? If not, how should i go about solving this?

I could drop the column and re-create it with the correct properties, but i don't want to loose any data in the database.

like image 256
qwerty Avatar asked Dec 10 '12 17:12

qwerty


People also ask

How do I change the column length?

In generic terms, you use the ALTER TABLE command followed by the table name, then the MODIFY command followed by the column name and new type and size. Here is an example: ALTER TABLE tablename MODIFY columnname VARCHAR(20) ; The maximum width of the column is determined by the number in parentheses.

How do I change the length of an existing column in SQL?

In this case, you need to use ALTER TABLE statement to increase column size. ALTER TABLE table_name MODIFY column_name varchar(new_length); In the above command, you need to specify table_name whose column you want to modify, column_name of column whose length you want to change, and new_length, new size number.

How do I increase the size of an existing column in SQL Server?

SQL - Modify Column Data Type and SizeALTER TABLE Employee ALTER COLUMN FirstName VARCHAR(50); The following will change the size in the Oracle database. ALTER TABLE Employee MODIFY (FirstName VARCHAR2(50)); The following will change the size in the PostgreSQL database.

How can I change varchar size in SQL Server?

You can use the ALTER table command to change the length of a varchar column. You can increase the length of a varchar column to a maximum size of 64,000.


2 Answers

For Laravel 4

DB::update('ALTER TABLE `my_table` MODIFY `my_column` VARCHAR(<new length>)');
like image 86
ademers Avatar answered Oct 07 '22 21:10

ademers


Use Raw Queries:

/**
 * Make changes to the database.
 *
 * @return void
 */
public function up()
{
  DB::query('ALTER TABLE mytable MODIFY mycolumn VARCHAR(new-length)');
}

/**
 * Revert the changes to the database.
 *
 * @return void
 */
public function down()
{
  DB::query('ALTER TABL mytable MODIFY mycolumn VARCHAR(old-length)');
}

Replace mytable, mycolumn, new-length and old-length.

For Laravel 5+ replace DB::query with DB::statement.

like image 31
NARKOZ Avatar answered Oct 07 '22 20:10

NARKOZ