db
db ๋ง์ด๊ทธ๋ ์ด์
ํ ์ด๋ธ ์คํค๋ง์ ๋ฒ์ ๊ด๋ฆฌ๋ก ํ ์ด๋ธ์ ์๋ก์ด ์ด์ ์ถ๊ฐ,์์ ์ด๋ ฅ์ ๋จ๊ฒจ ๋กค๋ฐฑํ๋ ๋ฑ์ ์์ ์ ์ํํ ์ ์๊ฒ ํด์ค๋ค.
๋ง์ด๊ทธ๋ ์ด์
์ด ํ์ํ ์ด์
๋ชจ๋ ๊ฐ๋ฐ ๋ฐฉ๋ฒ๋ก
ํ ๋ด ๊ฐ๋ฐ์๋ค์ ๊ฐ์ ์คํค๋ง๋ก ๊ฐ๋ฐํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฐํ๊ฒฝ๋ฟ๋ง ์๋๋ผ ์ด์ํ๊ฒฝ์ ์ฝ๊ณ ๋น ๋ฅด๊ฒ ๋ง๋ค์ ์๊ฒ ํด์ค๋ค.
์๊ฐ์ด์ง๋จ์ ๋ฐ๋ผ ์๊ตฌ์ฌํญ์ด ๋ฐ๋๊ณ ๋ชจ๋ธ๋ง์ด ๋ฐ๋๊ฒ ๋๊ธฐ๋ ํ๊ณ , ์ค์๊ฐ ๋ฐ์ํ์๋ ๋น ๋ฅด๊ฒ ๋กค๋ฐฑํด์ผ ํ๋ ์ํฉํ๋ ๊ฒ๊ณผ ๊ฐ์ด ํจ๊ณผ์ ์ผ๋ก ๋์ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ง๋๋ ๋ฐฉ๋ฒ
$ 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