Database Flashcards
(114 cards)
What are migrations used for?
Defining the application’s database schema and making it sharable
Which facade is typically used for database agnostic support for creating and manipulating tables across all of Laravel’s supported database systems?
Schema
Create a migration using Artisan!
php artisan make:migration create_examples_table
How do you run all of your outstanding migrations?
php artisan migrate
Use Artisan to see the SQL statements, that would be executed by the migrations, without actually running them!
php artisan migrate –pretend
How can you stop multiple servers from trying to migrate the same database at the same time, if your application is running across multiple servers?
php artisan migrate –isolated
What do you need to do to roll back the latest migration operation?
php artisan migrate:rollback
Roll back the last 3 migrations!
php artisan migrate:rollback –step=3
Use Artisan to see the SQL statements, that would be executed by a rollback, without actually rolling back!
php artisan migrate:rollback –pretend
Which command rolls back all of your applications migrations?
php artisan migrate:reset
How can you roll back and remigrate your database in one command?
php artisan migrate:refresh
Drop all tables from your database and then migrate and seed it in one command!
php artisan migrate:fresh –seed
Which method of the Schema facade should you use to make a new database table?
Schema::create
Create a database table, which includes an id, 2 strings and time stamps!
Schema::create('example', function (Blueprint $table) { $table->id(); $table->string('example1'); $table->string('example2'); $table->timestamps(); });
How can you check if a database includes a table / column / index?
Schema::hasTable('exampletable') Schema::hasColumn('exampletable', 'examplecolumn') Schema::hasIndex('exampletable', ['examplecolumn'], 'exampleindex')
Create a table that uses the “InnoDB” engine!
Schema::create('example', function (Blueprint $table) { $table->engine('InnoDB'); // ... });
Which method do you need to use to perform a schema operation on a database connection that is not your application’s default connection?
Schema::connection('sqlite')->create...
(just using sqlite as an example)
Specify the charset encoding as “utf8mb4” for a database during its creation!
Schema::create('example', function (Blueprint $table) { $table->charset('utf8mb4'); // ... });
What is the main characteristic, that a temporary table has?
A table, that was created with the temporary method, drops all tables once the connection is closed
How can you add a comment to a table within the Schema::create?
$table->comment('Business calculations');
Add the integer “score” to an already existing table!
Schema::table('example', function (Blueprint $table) { $table->integer('score'); });
How can you rename a table in an existing database?
Schema::rename($from, $to);
How can you drop an existing table?
Schema::drop('example');
alternativelySchema::dropIfExists('example');
What does the boolean() method do?
The boolean method creates a BOOLEAN equivalent column:
$table->boolean('confirmed');