在Laravel迁移中确定唯一,主要还是增量 [英] Deciding between unique, primary, or increments in Laravel Migrations

查看:81
本文介绍了在Laravel迁移中确定唯一,主要还是增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,其中的主要唯一元素称为CRN,它表示课程注册号",它是一个整数值.

I have a table of of data where the primary unique element is called a CRN, which denotes "course registration number", and it's an integer value.

此表中大约有4000个课程,每个课程都有一个唯一的CRN.

In this table there are around 4000 classes, and each class has a unique CRN.

我的问题是,在我的迁移模式构建器中,我应该将CRN设置为唯一,主要还是增量?

My question is, in my schema builder for migrations, should this I set the CRN as a unique, a primary, or an increments?

根据我的用法,唯一是最有意义的.

According to my usage, a unique makes the most sense.

CRN确实是唯一的,并且永远不会改变.我有一个脚本可以更新与此CRN对应的其他列,并且将CRN设置为唯一将阻止创建同一CRN的其他行.

The CRN is indeed unique, and never changes. I have a script that updates the other columns that corresponds to this CRN, and having CRN as unique will prevent additional rows of the same CRN created.

我没有设置任何唯一/主/增量约束的情况下遇到的错误是,当我重新运行更新注册号的脚本时(例如,对于CRN),会生成新行来代替正在更新.因此,现在我具有相同CRN的两行数据.

A bug I ran into without setting any of these constraints of unique/primary/increments is, when I re-ran my script that updates the enrollment numbers, for example, for a CRN, a new row is generated instead of it being updated. So now I have two rows of data of the same CRN.

推荐答案

如果您的密钥CRN是代理密钥,则应使其唯一,主键和增量,因为它是代理密钥并且与对象没有任何意义.这是自然键,那么您不必使其递增就可以使其唯一.

İf your key, CRN, is surrogate key then you should make it unique ,primary and increment beacuse it is surrogate key and has no meaning with your object.If it is natural key then you dont have to make it incremented just make it unique.

如果它是代理,则用$table->increments('id')创建它,这将使它成为主键,唯一键并递增.如果它是自然键

If it is surrogate then create it with $table->increments('id') and this will make it primary,unique and incremented.If it is natural key

$table->string('crn')->unique();
$table->primary('crn');

而且我认为您的'crn'似乎是自然键.

And i think your 'crn' seems natural key.

这篇关于在Laravel迁移中确定唯一,主要还是增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆