AXMLS数据库模式

编程入门 行业动态 更新时间:2024-10-26 22:26:04
本文介绍了AXMLS数据库模式-默认值和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为使用AXMLS格式指定数据库模式的Concrete5开发一个软件包。

I am developing a package for Concrete5 that uses AXMLS format to specify the database schema.

www.concrete5/documentation/how-tos/developers/creating-and -working-with-db-xml-files /

以下是架构:

<?xml version="1.0"?> <schema version="0.3"> <table name="notificationCategory"> <field name="id" type="I"> <!-- integer --> <key/> <autoincrement/> </field> <field name="name" type="C" size="255"> <!-- varchar(255) --> </field> <field name="created" type="T"> <deftimestamp/> </field> <field name="modified" type="T"> </field> <opt> Type=InnoDB </opt> </table> <table name="notificationEntry"> <field name="id" type="I"> <!-- integer --> <key/> <autoincrement/> </field> <field name="name" type="C" size="255"> <!-- varchar(255) --> </field> <field name="cat_id" type="I"> </field> <constraint> ADD CONSTRAINT `cat_id_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `notificationCategory`.(`id`) ON DELETE CASCADE </constraint> <opt> Type=InnoDB </opt> </table> </schema>

我在两件事上挣扎:

  • 外键。以下结果是一个表NotificationEntry,其中未设置外键(使用InnoDB)

  • Foreign keys. The result of the following is a table NotificationEntry where foreign key does not get set (it is using InnoDB)

已创建和已修改字段的默认值。我想创建一个具有当前日期的默认值,而要进行修改,我想具有ON UPDATE CURRENT_TIMESTAMP

Default values for created and modified field. I want created to have a default value of the current date, while for modified I'd like to have ON UPDATE CURRENT_TIMESTAMP

show create table notificationCategory; CREATE TABLE `notificationCategory` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1

这种格式的文档很少,有人使用过这种格式吗? ?

There is very little documentation available for this format, did anyone have success using it?

推荐答案

根据MySQL文档,您不使用 ADD CONSTRAINT 创建表时: dev.mysql/doc/refman/5.6/en/create-table-foreign-keys.html

As per the MySQL documentation, you don't use ADD CONSTRAINT when you're creating the table: dev.mysql/doc/refman/5.6/en/create-table-foreign-keys.html

第二个,所有< constraint> 定义都必须位于ADOdb文档中的< field> 定义内: adodb.sourceforge/docs-datadict.htm

Second, all <constraint> definitions need to be inside the <field> definitions as per ADOdb documentation: adodb.sourceforge/docs-datadict.htm

在字段定义末尾定义的附加约束。

第三,约束定义在其前面需要一个逗号,因为它被连接在 CREATE TABLE 块的末尾,因此如果没有逗号,则生成的MySQL将无效。我不确定是否在任何地方都有记载,但是ADOdb文档页面上的PostgreSQL示例在语句前显示了逗号。

Third, the constraint definition needs a comma in front of it because it is concatenated at the end of the CREATE TABLE block so the generated MySQL would be invalid without the comma. I'm not sure whether this is documented anywhere but the PostgreSQL example on the ADOdb documentation page shows a comma in front of the statement.

因此,您的字段定义和约束应该看起来像这样:

So, your field definition and constraint should look like this:

<field name="catID" type="I"> <constraint> , FOREIGN KEY (catID) REFERENCES NotificationsNotificationCategory(catID) ON DELETE CASCADE </constraint> </field>

还有其他一些建议:

  • 遵循 concrete5核心设置的数据库表和列的命名约定。通过浏览核心数据库表,您可以轻松地弄清楚这些。
    • CamelCaseFormat中的表名(注意首字母大写)。 此规则的唯一例外是BlockType表(从 bt 开始)和AttributeType表(从 at )。
    • 按照核心约定命名您的主键名称。例如。 Collections 表的主键是 cID 和文件表为 fID 。 命名字段时,请尽量避免与核心主键发生冲突,例如。而不是使用$ c $作为类别表的名称,而应像那样使用 catID。
    • 在以下位置也使用camelCaseFormat(注意下方的起始字母)字段名称就像Concrete5中的任何其他表一样。
    • Follow the naming conventions for the database tables and columns set by the concrete5 core. You can easily figure these out by exploring the core database tables.
      • Table names in CamelCaseFormat (note the starting capital letter). Only exceptions to this rule are BlockType tables (starting with bt) and AttributeType tables (starting with at).
      • Name your primary key names following the core conventions. E.g. the primary key for the Collections table is cID and the primary key for the Files table is fID. Try to avoid possible conflicts with the core primary keys when naming your fields, e.g. instead of using "cID" as the name for your category table, use "catID" like you had already suggested.
      • Use camelCaseFormat (note the lower starting letter) also in the field names just like any other table in concrete5.

      遵循这些约定可使代码保持干净并允许已经熟悉Concrete5并可能浏览您的代码的任何将来可能的开发人员阅读。尽管您不同意所有约定,但是最好遵循每个约定,而不是让每个开发人员每次都重新发明轮子。

      Following these conventions keeps your code clean and allows easy reading for any possible future developers that are already familiar with concrete5 and might explore your code. Although you would not agree with all the conventions, it is better to follow them than to let every developer reinvent the wheel every time.

更多推荐

AXMLS数据库模式

本文发布于:2023-10-18 12:25:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   数据库   AXMLS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!