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