4.数据库设置及建立

1.数据库的配置文件在config/database.php路径中,可以看到sqlite,mysql,pgsql,sqlsrv的连线范例。

可预先设置默认的数据库连接

'default' => env('DB_CONNECTION', 'mysql')

2. .env 环境变量文件

env()函数会按照读取当时主机环境去设置环境,本地开发环境local,上线的环境会设置为production。

每个主机环境不同,设置也不同,所有.env文件不会再版本控制中。每个主机都必须建立自己的.env文件,而env()函数会读取.env文件中的设置

APP_ENV=production.Lavaral会针对production环境有不同的安全性提示。像数据库结构修改的比较危险操作,在执行时都会有额外提示信息。

env()函数第一个参数是.env文件文件中的键值,如果没有设置,则会使用第二个参数值当做默认值。

3.数据库迁移(Migration)

3.1 Migration文件放在datebase/migrations文件夹下。

3.2 Migration文件有up()、down()两个函数。up()函数指的是执行此次数据库修改时要执行的程序,而down()函数指的是在复原up()函数时所做的数据库修改。两个函数时相对应的。当up()函数时建表,那么down()函数就是删表。当up()函数时新增字段,那么down()函数就是删除字段。以此类推。

3.3 数据表字段设置

在修改数据库时,所有修改都会写在Schema::的静态方法中。如:

Schema::create('users', function (Blueprint $table) {}//建表

Schema::table('test', function (Blueprint $table) {});//对已有的表操作



Schema::create('users', function (Blueprint $table) {

            $table->increments('id');

            $table->string('email',150);

            $table->string('password',60);

            //-A 管理者

            //-G 普通用户

            $table->string('type',1)->default('G');

            $table->string('nickname',50);

//            $table->dateTime('created_at');

//            $table->dateTime('updated_at');

            //时间戳记,或用上面的方法自行建立时间字段

            $table->timestamps();

            $table->unique('email','user_email_uk');

        });


3.3.1 字符串类型

分类 方法 mysql类型 使用说明

字符串01 char(列名, 长度 = null) char 定长字符串

字符串02 string(列名, 长度 = null) varchar 长度可变字符串

字符串03 text(列名) text 支持2^16-1个字节,相当于2.1万个汉字

字符串04 mediumText(列名) mediutext 支持2^24-1个字节,相当于560万个汉字

字符串05 longText(列名) longtext 支持2^32-1个字节,相当于14亿个汉字

实现01: rememberToken() varchar(100) string(‘remember_token’, 100)->nullable()

实现02: uuid(列名) char(36)

实现03: ipAddress(列名) varchar(45)

实现04: macAddress(列名) varchar(17)

说明1: string长度指的是字符长度,不是字节,最大支持2^8-1=255个字符,1个数字,字母,汉字都是算一个字符,string(‘name’,25)表示可以储存25个汉字

说明2: text长度指的是字节,

说明3: char适合等长密码,邮编等,默认为…Builder::$defaultStringLength,这个值是可以在服务提供商中修改的

说明4: text与string有些不一样,在utf8编码下,一个汉字相当于3个字节,最大支持21845个汉字,多一个字就会报错

3.3.2 整数类型

分类 方法 mysql类型 使用说明

整数01 参数(列名, 自增长 = false, 无符号 = false)

tinyIntegert 和 unsignedTinyInteger tinyint 占1个字节,长度2^8

整数02 integer 和 unsignedInteger int 占2个字节,长度2^16

整数03 smallInteger 和 unsignedSmallInteger smallint 占3个字节,长度2^24

整数04 mediumInteger 和 unsignedMediumInteger mediumint 占4个字节,长度 2^32

整数05 bigInteger 和 unsignedBigInteger bigint 占8个字节,长度2^63

自增长列01 increments(列名) unsignedInteger(列名, true)

自增长列02 tinyIncrements(列名) unsignedTinyInteger(列名, true)

自增长列03 smallIncrements(列名) unsignedSmallInteger(列名, true)

自增长列04 mediumIncrements(列名) unsignedMediumInteger(列名, true)

自增长列05 bigIncrements(列名) unsignedBigInteger(列名, true)

真假 boolean($column) tinyint(1)

说明1: 整数类型都指定不了长度,也指定不了zerofill,不过这没有关系,因为整数实际存储的长度与指定的长度没有关系,只是显示的长度而已

3.3.3 小数类型

分类 方法 mysql类型 使用说明

单精度浮点数 float(column,column,total = 8, $places = 2) 弃用 单精度必须要指定M和D

双精度浮点数 double(column,column,total = null, $places = null) double(M,D) 或double

正负定点数 decimal(column,column,total = 8, $places = 2)

unsignedDecimal decimal(M,D) 精度和标度必须指定,默认为(8,2)

说明1: float是单精度浮点数,double是双精度浮点数,decimal是定点数

说明2: float占4个字节,double占8个字节

说明3: 这3个类型也可以理解为小数

说明4: 浮点数存在误差问题,定点数精确,对货币等对精度敏感的数据,应该用定点数表示或存储;

说明5: 编程中,如果用到浮点数,要特别注意误差问题,并尽量避免做浮点数比较;

说明6: FLOAT(M,D),DOUBLE(M,D)。表示共M位数,小数点前面为M-D位,小数点后面为D位

说明7: decimal在mysql内存是以字符串存储的

3.3.4 日期时间

分类 方法 mysql类型 使用说明

日期 date($column) date

时间 time($column) time

时间 timeTz($column) time

日期时间 dateTime(column,column,precision = 0) datetime(精度)

datetime

日期时间 dateTimeTz(column,column,precision = 0) datetime

时间戳 timestamp(column,column,precision = 0) timestamp 用使用useCurrent使用当前时间

时间戳 timestampTz(column,column,precision = 0) timestamp

时间戳实现 timestamps(precision=0)...||timestamp(′createdat′,precision=0)...||timestamp(′createdat′,precision)->nullable();

timestamp(‘updated_at’, $precision)->nullable();

时间戳实现 timestampsTz(precision=0)...||timestampTz(′createdat′,precision=0)...||timestampTz(′createdat′,precision)->nullable();

timestampTz(‘updated_at’, $precision)->nullable();

时间戳实现 nullableTimestamps($precision = 0) 是timestamps的别名

时间戳实现 softDeletes(列名 = ‘deleted_at’,精度 = 0)

时间戳实现 softDeletesTz($precision = 0)

说明01: datetime不支持取默认值now(),版本5.6之后的可以默认值CURRENT_TIMESTAMP,它会取当前时间,并转为2017-11-01 23:25:45类的格式,但这种情况不能指定精度,另处写法是new \Illuminate\Database\Query\Expression(‘CURRENT_TIMESTAMP’)才能使用Mysql中的常量

说明02: timestamp(‘列名’,精度),如果指定了精度就不能使用修饰->useCurrent(),框架源代码此处是一个bug,等修正, 只有不指定精度时才可能使用useCurrent

说明03: datetime(3)意思是保留3为毫秒数,timestamp(‘login’,3);的结果如2017-11-02 02:08:34.864,也是保留3位毫秒

说明04: TZ是系统时区的意思,但实际使用中使用非Tz格式就行

说明05: 未提供year的方法,可能是year的范围太小的原因舍弃了,可以使用date获取

日期类型 存储空间 日期格式 日期范围

datetime 8 bytes YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 至 9999-12-31 23:59:59

timestamp 4 bytes YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:01 至 2038

date 3 bytes YYYY-MM-DD 1000-01-01 至 9999-12-31

year 1 bytes YYYY 1901 至 2155

3.3.5 其它

分类 方法 mysql类型 使用说明

枚举 enum($column, [‘市场部’,’设计部’,’总裁办’]) 生成enum(‘市场部’,’设计部’,’总裁办’)

json($column) json 这是5.7.7后才有的功能

jsonb($column) json 这是5.7.7后才有的功能

二进制 binary($column) blob



3.4使用artisan命令建立Migration文件

//建立创建数据表文件

php artisan make:migration create_users_table --create=users

//建立新增数据表字段文件

php artisan make:migration add_sex_to_users_table --table=users

执行命令后,除了自己设计的数据表外,额外多了一个migrations数据表,这个数据表是Laravel管理现在的Migration版本状况的,其中,batch字段指的是Migration的版本号,此字段为整数且会持续增加。


3.5执行Migration文件

php artisan migrate  建立所有Migration

php artisan migrate:rollback,恢复上次版本的所有修改

php artisan migrate:reset ,重设所有Migration版本,并清空所有数据。

php artisan migrate:refresh ,重设所有Migration版本并立即建立,此命令会清空所有数据,并重新建立所有数据库表。

APP_ENV=production时,使用migrate:reset,migrate:refresh时,会再次询问是否要执行命令。

你可能感兴趣的:(4.数据库设置及建立)