The master key is provided to fill the line

Reporting tool reporting function is one of the indispensable feature, which allows users to perform data maintenance operations on the page. In particular, the line type reporting, users can not only modify the data, but also can insert / add new data or delete existing data. Thus, when updating the data recording line to fill the page, the primary key field will involve insert and delete operations, and for different types of primary keys How should we update settings? There is what we need to pay attention to it? We will safeguard employee information table below as an example to explore the master key set of related content.

Employee table attachment structure:

First prepare a table reporting line to produce results as shown below:

(The line completing the tutorial see table quickly create data-yourself maintenance, a move to get CRUD )

Source script settings:

Focus coming ~~ whereabouts script different types of primary keys should be how to configure it? We break one by one:

1. Single primary key

The primary key for the Employee table empno, then the script update function is configured to:

A1.update@k(employee:employee_old,employee,empno,ename,salary,sex;empno)

Disposed behind the primary key empno semicolon, reports do update process, the main key will empno employee_old comparative data and employee object, then the difference data to update the table employee.

The full version of the script content as shown below:

2. Multi-master key

       In general, physical table provided only one primary key field can be distinguished from the data recording area is set two or more primary key field in special cases, such as the aforementioned employee table we can distinguish a data recording by empno field, can also ename + sex two fields to lock the only one qualifying record, then how should we script it set the primary key?

Very simple, just use a comma to separate multiple primary key fields can be:

A1.update@k(employee:employee_old,employee,empno,ename,salary,sex;ename,sex)

完整版脚本内容如下图所示:

3.自增主键

自增主键可以提升查询效率、节省磁盘空间,在填报中也有广泛应用。以 mysql 设置为例:可以通过第三方工具(如 Navicat)或者 sql 语句设置某个主键字段的数据自动增长。

       在数据库端很容易配置主键字段的自动增长,那么在润乾报表中如何实现自动增长字段的数据更新呢?很简单,只需要添加一个 @1 选项就搞定了。

A1.update@1k(employee:employee_old,employee,empno,ename,salary,sex;empno)

其中,@1 选项表示第一个字段是自增字段,没有对应更新值表达式

注意:这里是数字 1 而不是字母 l

完整版脚本内容如下图所示:

4.“动态”主键

       “动态”主键我们理解成通常说的 UUID,表示主键的值是一个不重复的随机数,这种方式比自动增长方式更安全、重复率更低。

        我们以 mysql 数据库为例,将随机生成的四位数字作为主键 empno 的数据,并将数据更新入库。在润乾报表中只需要多一步 run 操作给主键字段赋值,剩下的和常规更新配置一样。

       =employee.run(if(empno==null,~.empno=A1.query(“select round(round(rand(),4)*10000) as xuhao”).xuhao))  // 当 employee 对象中的主键 empno 字段为空的时候,我们将随机生成的四位数字赋值给主键字段

(这里为什么条件是判断为空呢?因为一般自增主键或者 UUID 主键都不允许编辑或者直接设置用户不可见,所以此时新增的记录中主键字段为空。)

完整版脚本内容如下图所示:

至此,我们已经将几种常见主键设置方式探讨完毕,现在回过头来看填报数据处理中的主键设置,是不是有种“wow~~amazing~”的感觉?

如果您在填报学习过程中有任何疑问,欢迎留言,我们一起来盘它!

Guess you like

Origin www.cnblogs.com/shiGuangShiYi/p/12110772.html