db

db λ§ˆμ΄κ·Έλ ˆμ΄μ…˜

ν…Œμ΄λΈ” μŠ€ν‚€λ§ˆμ˜ λ²„μ „κ΄€λ¦¬λ‘œ ν…Œμ΄λΈ”μ— μƒˆλ‘œμš΄ 열을 μΆ”κ°€,μˆ˜μ • 이λ ₯을 남겨 λ‘€λ°±ν•˜λŠ” λ“±μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 있게 ν•΄μ€€λ‹€.

λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ΄ ν•„μš”ν•œ 이유

  1. λͺ¨λ˜ 개발 방법둠

    νŒ€ λ‚΄ κ°œλ°œμžλ“€μ€ 같은 μŠ€ν‚€λ§ˆλ‘œ κ°œλ°œν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— κ°œλ°œν™˜κ²½λΏλ§Œ μ•„λ‹ˆλΌ μš΄μ˜ν™˜κ²½μ„ 쉽고 λΉ λ₯΄κ²Œ λ§Œλ“€μˆ˜ 있게 ν•΄μ€€λ‹€.

  2. μ‹œκ°„μ΄μ§€λ‚¨μ— 따라 μš”κ΅¬μ‚¬ν•­μ΄ λ°”λ€Œκ³  λͺ¨λΈλ§μ΄ λ°”λ€Œκ²Œ λ˜κΈ°λ„ ν•˜κ³ , μ‹€μˆ˜κ°€ λ°œμƒν–ˆμ„λ•Œ λΉ λ₯΄κ²Œ λ‘€λ°±ν•΄μ•Ό ν•˜λŠ” μƒν™©ν•˜λŠ” 것과 같이 효과적으둜 λŒ€μ‘ν•  수 있기 λ•Œλ¬Έμ΄λ‹€.

λ§Œλ“œλŠ” 방법

$ php artisan make:migration create_posts_table --create=posts
$ php artisan make:migration create_authors_table --create=authors

생성을 ν•˜λ©΄ database/migrations디렉토리 ν•˜μœ„μ— μƒμ„±λ˜κ³  κ΄€λ‘€μ μœΌλ‘œ μŠ€λ„€μ΄ν¬ ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•΄μ„œ create_,make_,add_,drop_,change_λ“±μœΌλ‘œ μ‹œμž‘ν•˜κ³  _table둜 λλ‚œλ‹€.

μ˜΅μ…˜μœΌλ‘œ --createμ˜΅μ…˜μ„ μ£Όλ©΄ upκ³Ό down이 create와 drop으둜 생성이 되고, --tableμ˜΅μ…˜μ„ μ£Όλ©΄ up(),down()λ©”μ„œλ“œκ°€ table()λ©”μ„œλ“œλ‘œ μžλ™μƒμ„±λœλ‹€.

createμ˜΅μ…˜

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

table μ˜΅μ…˜

class CreateExampleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('example', function (Blueprint $table) {
            //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('example', function (Blueprint $table) {
            //
        });
    }
}

class CreateAuthorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email',255);
            $table->string('password',60);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('authors');
    }
}

up()λ©”μ„œλ“œλŠ” λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ„ μ‹€ν–‰ν•˜λŠ” λ©”μ„œλ“œμ΄κ³  down()은 둀백을 μœ„ν•œ λ©”μ„œλ“œμ΄λ‹€. up()λ‚΄λΆ€μ˜ increments()λŠ” μžλ™μ¦κ°€ κΈ°λ³Έ ν‚€ μ»¬λŸΌμ„ λ§Œλ“€κ³ , timestamp()λŠ” created_atκ³Ό updated_at μ»¬λŸΌμ„ λ§Œλ“ λ‹€.

create()λ©”μ„œλ“œμ˜ λ‘λ²ˆμ§Έ λ§€κ°œλ³€μˆ˜λŠ” 콜백 ν•¨μˆ˜μ΄κ³  이 ν•¨μˆ˜μ˜μ˜ λ§€κ°œλ³€μˆ˜λ‘œ BlueprintλŠ” νƒ€μž… 힌트둜써, ν•΄λ‹Ή 클래슀의 μΈμŠ€ν„΄μŠ€μ—¬μ•Όν•œλ‹€κ³  κ°•μ œν•˜λŠ” 것이닀.

  • Schema::create() : ν…Œμ΄λΈ”μ„ 생성

  • Schema::drop() : ν…Œμ΄λΈ”μ„ μ‚­μ œ

  • Schema::table() : ν…Œμ΄λΈ”μ„ 생성/μ‚­μ œλ₯Ό μ œμ™Έν•œ λ‚˜λ¨Έμ§€ μŠ€ν‚€λ§ˆ κ΄€λ ¨ μž‘μ—…λ“€

μ»¬λŸΌνƒ€μž…μ— λŒ€μ‘λ˜λŠ” λ©”μ„œλ“œ μ’…λ₯˜

  • boolean(), dateTime(), enum(), integer()λ“±κ³Ό 같은 μ»¬λŸΌνƒ€μž… λ©”μ„œλ“œ

  • timestamps(), softDeletes()λ“±μ˜ λ„μš°λ―Έ λ©”μ„œλ“œ

  • nullable(), default(), unsinged()λ“±μ˜ μž₯식 λ©”μ„œλ“œ

  • unique(), index()λ“±μ˜ 인덱슀 λ©”μ„œλ“œ

  • dropColumn()

이외에도 κ³΅μ‹λ¬Έμ„œμ„ 보면 λ§Žμ€ λ©”μ„œλ“œκ°€ μ‘΄μž¬ν•œλ‹€.

μ‹€ν–‰

$ php artisan migrate

λ‘€λ°±

$ php artisan migrate:rollback

μ΄ˆκΈ°ν™”

$ php artisan migrate:refresh

μž‘μ„±λ˜λŠ” SQLλ¬Έ 보기

$ php artisan migrate --pretend

Last updated