laravel database migration
Data migration files are stored in database/migrations Features: Version rollback is possible, which is convenient for team development
Create table from data migration file 1 Create a database Configure the .env file to select the database
copyDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=ches DB_USERNAME=root DB_PASSWORD=root
2. Create a database migration file table
php artisan make:migration create_table_goods -- create=goods
..................................................................................... ....Operation description--create surface

The above operation will only generate the migration file. At this time, the database does not generate a data table c. We need to edit the migration file and set the corresponding fields

Using php artisan migrate

successfully generated table
Database migration file has up/down methods in this file
copyup()://add/modify $table->increments('id');//auto increment, primary key int $table->string('name');//varchar() $tanle->float('price');//dounble(); $tanble->timestamps();//update time/creation time
Note: If the error length is not enough during the generation process, you need to set the default length of characters in app/providers/AppServiceProwvider.php
copyuse Illuminate\Support\Facades\Schema; Schema::defaultStringLength(191);//Set default string length:
Add fields to the already created data table 1. Create a migration file
copyphp artisan make:migration add_quantity_to_c --table=c//Create migration file
Edit, insert or delete fields on migration files up() method $table->integer('field') down() method $table->dropColumn('field'); up adds several fields, down relatively deletes the corresponding fields, which is convenient for rollback operations 2. Update the table php artisan migrate
go back php artisan migrate: common commands rollback() rolls back to the most recent database operation reset(): fall back to the initial state before all migrations (initialization)
refresh(): go back to the initial state, and then execute all migration files (restart (back->migrating)
fresh(): delete the data table, execute all migration files again (5.5) (delete (drop->migrating))
install() resets and reruns all migrations The migrations table needs to be deleted before reset - reset the migrations table The reset is the migratic table not the migration file force(): force the latest migration file
php artisan migrate:rollback --step=5; rollback to the last 5 migrations Database Connection & Table Options If you want to link to other databases use connection(database)->..... example:
copySchema::connection('database(connection name)')->create('users',function(Blueprint $table)){ //$table->increments() }
Database Structure Generator Common Commands
$table->engine = 'InnoDB' specifies the table engine ->charset = 'utf8' specifies the default character set of the data table ->collation = 'utf8_general_ci' specifies the default collation of the data table ->temporary() creates a temporary table
Rename data table Schema::table('from','to')
delete data table
copySchema::drop('table') Schema::dropIfExists('table')
Database Builder Common Field Types
copy$table->char('name', 4); $table->string('name', 100);=varchar $table->date('created_at'); $table->dateTime('created_at'); $table->double('column', 8, 2); Double precision floating point number with two decimal places $table->float('amount', 7, 2); single precision floating point number $table->enum('level', ['easy', 'hard']); $table->increments('id');Increment primary key $table->integer('votes');plastic $table->tinyInteger('votes'); $table->text('description'); equivalent to TEXT $table->time('sunrise');
Database Builder Common Field Modifications
copy->first()field first ->after('column') after other fields ->charset('utf8') specified character set ->collation('..') specify collation ->comment('') field annotation ->default('') Defaults ->nullable()empty ->unsigned()unsigned
The difference between signed and unsigned
Unsigned: The data is 0 or a positive number Memory ratio: -127~127
Signed data can be negative memory ratio: 0-255
Modify fields
1. Import doctrine/dbal
copycomposer require doctrine/dbal
Update field properties
copy$table->string('name',50)->nullable()->change()
rename field
copy$table->renameColumn('from','to')
delete field
copydropCloumn('field name') dropCloumn(['field name','field 2'])
foreign key constraints
copy$table->foreign('current table from table fields')->references('Reference table main table fields')->on('Primary table') ->onDelete('cascade')//Cascade delete ->update('cascade')//Cascading updates
Default foreign key name
copydata table name_foreign key field_foreign
delete foreign key
dropForeign('foreign key name')
Turn foreign key constraints on/off
copySchema::enableForeignKeyConstraints() Schema::disableForeignKeyConstraints()
create index
copy$table->string('name')->unique();//unique index
Create index after defining fields
copy$table->unique('name');
Passing an array to create a composite/composite index
$table->index(['id','time'],rname) rname can optionally specify the index name
Available index commands
copy$table->primary('id') //add primary key $table->primary(['id','cid'])// composite primary key $table->unique('email') //Create a unique index $table->index('name') //normal index $tale->spatialIndex('diz') //Add Spatial Index
index length setting
boot method settings of app\Providers\AppServiceProvider.php
copySchema::defaultStringLength(191);
drop index
copy$table->dropPrimary('user_id_primary') $table->dropUnique('') $table->dropIndex('geo_state_index'); $table->dropSpatialIndex('geo_location_spatialindex');