哪个列类型用于在包含年度数据行的表中存储year字段

时间:2021-10-09 22:46:17

My Laravel web app uses the schema builder for database portability, so the MySQL-specific YEAR column type is not available.

我的Laravel Web应用程序使用模式构建器实现数据库可移植性,因此特定于MySQL的YEAR列类型不可用。

I want to be able to sort and select rows of data by year (including BETWEEN). What's the best portable column type for storing year values?

我希望能够按年度排序和选择数据行(包括BETWEEN)。存储年份值的最佳便携式柱类型是什么?

3 个解决方案

#1


5  

What's the best portable column type for storing year values?

存储年份值的最佳便携式柱类型是什么?

smallint is a good choice to represent years, and it's ANSI SQL, so will work in most databases. It will last until the year 32767.

smallint是表示年份的好选择,它是ANSI SQL,因此可以在大多数数据库中使用。它将持续到32767年。

Some databases support create domain which basically lets you create your own types. You could create a year domain for other databases. I prefer the simplicity of smallint for year though.

有些数据库支持创建域,基本上可以让你创建自己的类型。您可以为其他数据库创建年份域。虽然我更喜欢小巧的一年。

#2


1  

Could you do something like this:

你能做这样的事情:

public function up()
{
    Schema::create('mytable', function(Blueprint $table)
    {
        $table->increments('id');
        // columns
        $table->timestamps();
    });

    DB::statement('ALTER mytable ADD year YEAR' );

}

#3


1  

I think this should work:

我认为这应该有效:

Schema::table('tablea_name', function(Blueprint $table)
{
    DB::statement('ALTER TABLE table_name ADD year YEAR(4);');
});

Portable solution would be if Laravel provides any way to access this method (in /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php) in upcoming versions:

便携式解决方案是,如果Laravel在即将推出的版本中提供了访问此方法的任何方法(在/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php中):

/**
 * Add a new column to the blueprint.
 *
 * @param  string  $type
 * @param  string  $name
 * @param  array   $parameters
 * @return \Illuminate\Support\Fluent
 */
protected function addColumn($type, $name, array $parameters = array())
{
    $attributes = array_merge(compact('type', 'name'), $parameters);

    $this->columns[] = $column = new Fluent($attributes);

    return $column;
}

#1


5  

What's the best portable column type for storing year values?

存储年份值的最佳便携式柱类型是什么?

smallint is a good choice to represent years, and it's ANSI SQL, so will work in most databases. It will last until the year 32767.

smallint是表示年份的好选择,它是ANSI SQL,因此可以在大多数数据库中使用。它将持续到32767年。

Some databases support create domain which basically lets you create your own types. You could create a year domain for other databases. I prefer the simplicity of smallint for year though.

有些数据库支持创建域,基本上可以让你创建自己的类型。您可以为其他数据库创建年份域。虽然我更喜欢小巧的一年。

#2


1  

Could you do something like this:

你能做这样的事情:

public function up()
{
    Schema::create('mytable', function(Blueprint $table)
    {
        $table->increments('id');
        // columns
        $table->timestamps();
    });

    DB::statement('ALTER mytable ADD year YEAR' );

}

#3


1  

I think this should work:

我认为这应该有效:

Schema::table('tablea_name', function(Blueprint $table)
{
    DB::statement('ALTER TABLE table_name ADD year YEAR(4);');
});

Portable solution would be if Laravel provides any way to access this method (in /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php) in upcoming versions:

便携式解决方案是,如果Laravel在即将推出的版本中提供了访问此方法的任何方法(在/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php中):

/**
 * Add a new column to the blueprint.
 *
 * @param  string  $type
 * @param  string  $name
 * @param  array   $parameters
 * @return \Illuminate\Support\Fluent
 */
protected function addColumn($type, $name, array $parameters = array())
{
    $attributes = array_merge(compact('type', 'name'), $parameters);

    $this->columns[] = $column = new Fluent($attributes);

    return $column;
}