{"id":19,"date":"2022-03-04T04:11:10","date_gmt":"2022-03-04T04:11:10","guid":{"rendered":"http:\/\/localhost\/blog\/?p=19"},"modified":"2023-04-24T09:09:27","modified_gmt":"2023-04-24T02:09:27","slug":"create-magento-2-database-table","status":"publish","type":"post","link":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/","title":{"rendered":"Create Magento 2  database table"},"content":{"rendered":"<div class=\"magerubik-quote\">\n<p>From Magento 2.3, we get acquainted with the method of creating a database called <span class=\"code\">declarative schema<\/span>. In this article, we will use it to create Magento 2 database table for our module. To better understand the structure of db_schema.xml file, I suggest you start looking at the Magento Catalog in the path <span class=\"code\">vendor\\magento\\module-catalog\\etc\\db_schema.xml<\/span>.<\/p>\n<\/div>\n<p><strong>Let go!<\/strong> First we create db_schema.xml file put at the path <span class=\"code\">app\\code\\Magerubik\\Simple\\etc\\db_schema.xml<\/span> with below content:<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<p>In the declarative schema method, we will use two files <span class=\"code\">db_schema.xml<\/span> and <span class=\"code\">db_schema_whitelist.json<\/span> in the same folder to create and update the database.<\/p>\n<ul class=\"hkk-list\">\n\t<li>In there<\/li>\n\t<li>\u2013 <span class=\"code\">&#8220;db_schema.xml&#8221;<\/span>: create or modify database table.<\/li>\n\t<li>\u2013 <span class=\"code\">&#8220;db_schema_whitelist.json&#8221;<\/span>: save modified history.<\/li>\n<\/ul>\n<p class=\"simple-note\"><strong>So, we need to run the below command to save modified history after changing file db_schema.xml.<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:db-declaration:generate-whitelist --module-name=Magerubik_Simple<\/pre>\n\n\n\n<p class=\"h3\"><strong>OK check it working with our module<\/strong>:<\/p>\n<h2 class=\"h3\"><strong>1. Run upgrade command to check database was being created<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:db-declaration:generate-whitelist --module-name=Magerubik_Simple\nphp bin\/magento setup:upgrade\nphp bin\/magento setup:static-content:deploy -f<\/pre>\n\n\n\n<p>Go to CPanel check your database if see the below screenshot everything is ok.<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/cdn2.magerubik.com\/media\/module\/magento-create-table.jpg\" alt=\"Magento create table\"\/><\/figure>\t\n<p>Then go to your module folder check file <span class=\"code\">db_schema_whitelist.json<\/span>. It should have content like below at this time.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"vendor_message\": {\n        \"column\": {\n            \"messages_id\": true,\n            \"user_id\": true,\n            \"title\": true,\n            \"description\": true,\n            \"status\": true,\n            \"created_at\": true,\n            \"updated_at\": true\n        },\n        \"index\": {\n            \"VENDOR_MESSAGE_USER_ID\": true\n        },\n        \"constraint\": {\n            \"PRIMARY\": true\n        }\n    }\n}<\/pre>\n\n\n\n<h2 class=\"h3\"><strong>2. Perform modify the database table<\/strong><\/h2>\n<p class=\"h3\">Add a column to table<\/p>\n<p>We add line <span class=\"code\"><column xsi:type=\"timestamp\" name=\"readed_date\" on_update=\"false\" nullable=\"true\" comment=\"Readed Date\"\/><\/span> to <span class=\"code\">db_schema.xml<\/span> like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"readed_date\" on_update=\"false\" nullable=\"true\" comment=\"Readed Date\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<p>Then run command generate whitelist and upgrade to check it working.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:db-declaration:generate-whitelist --module-name=Magerubik_Simple\nphp bin\/magento setup:upgrade<\/pre>\n\n\n\n<p>The <span class=\"code\">db_schema_whitelist.json<\/span> file should have content like below at this time<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"vendor_message\": {\n        \"column\": {\n            \"messages_id\": true,\n            \"user_id\": true,\n            \"title\": true,\n            \"description\": true,\n            \"status\": true,\n            \"created_at\": true,\n            \"updated_at\": true,\n            \"readed_date\": true\n        },\n        \"index\": {\n            \"VENDOR_MESSAGE_USER_ID\": true\n        },\n        \"constraint\": {\n            \"PRIMARY\": true\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"h3\">remove a column from a table<\/p>\n<p>we will delete the added line then run the upgrade command.<\/p>\n<p>Also you can remove column from other module by redeclare it with the <span class=\"code\">disabled<\/span> attribute set to <span class=\"code\">true<\/span>.<\/p>\n<p class=\"simple-note\">You possible to remove a column only if it exists in the <span class=\"code\">db_schema_whitelist.json<\/span> file.<\/p>\n<p>The <span class=\"code\">db_schema.xml<\/span> file Should like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"title\" nullable=\"false\" length=\"255\" comment=\"Title\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n    &lt;\/table>\n\t&lt;table name=\"catalog_product_entity_varchar\" resource=\"default\" engine=\"innodb\" comment=\"Catalog Product Varchar Attribute Backend Table\">\n        &lt;column xsi:type=\"varchar\" name=\"value\" nullable=\"true\" length=\"255\" disabled=\"true\" comment=\"Value\"\/>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<p>It ok, if you can see the below screenshot:<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/cdn2.magerubik.com\/media\/module\/magento-remove-column.jpg\" alt=\"Magento Remove Column\"\/><\/figure>\n<p class=\"h3\">Change the column &#8220;type&#8221;:<\/p>\n<p>You can modify the attribute except &#8220;name&#8221; then run the upgrade command to check it.<\/p>\n<p class=\"h3\">Change the column &#8220;name&#8221;:<\/p>\n<p>Change the column \u201cname\u201d consists of two processes: delete the original column declaration and create a new one then migrate data from the original column declaration to the new column with the method <span class=\"code\">onCreate=&#8221;migrateDataFrom(old_name)&#8221;<\/span><\/p>\n<p>Change <span class=\"code\">db_schema.xml<\/span> file Should like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"username\" nullable=\"false\" length=\"255\" onCreate=\"migrateDataFrom(title)\" comment=\"Username\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<p>Then run command generate whitelist and upgrade to check it working. It ok, if you can see the below screenshot:<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/cdn2.magerubik.com\/media\/module\/magento-rename-column.jpg\" alt=\"Magento Rename Column\"\/><\/figure>\n<p class=\"h3\">Add an index:<\/p>\n<p>See on our example<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n\t&lt;column name=\"user_id\"\/>\n&lt;\/index><\/pre>\n\n\n\n<ul class=\"hkk-list\">\n\t<li>In there<\/li>\n\t<li>\u2013 <span class=\"code\">\u201creferenceId\u201d<\/span> must be unique<\/li>\n\t<li>\u2013 <span class=\"code\">\u201cindexType\u201d<\/span> The value must be btree, fulltext, or hash<\/li>\n<\/ul>\n<p class=\"h3\">Create a foreign key:<\/p>\n<p>We use <span class=\"code\">\u201cconstraint\u201d<\/span> node to create a foreign key<\/p>\n<p>Change <span class=\"code\">db_schema.xml<\/span> file Should like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"username\" nullable=\"false\" length=\"255\" onCreate=\"migrateDataFrom(title)\" comment=\"Username\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n\t\t&lt;constraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" \n\t\t\ttable=\"vendor_message\" column=\"user_id\" \n\t\t\treferenceTable=\"customer_entity\" referenceColumn=\"entity_id\" onDelete=\"CASCADE\"\/>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<p>Then run command generate whitelist and upgrade to check it working.<\/p>\n<p class=\"h3\">Remove foreign key:<\/p>\n<p>You can easy Remove foreign key by delete constraint node<\/p>\n<p>Also you can remove foreign key from other module by redeclare it with the <span class=\"code\">disabled<\/span> attribute set to true. like below<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;table name=\"catalog_product_entity_varchar\">\n\t&lt;constraint disabled=\"true\" xsi:type=\"foreign\" referenceId=\"CAT_PRD_ENTT_VCHR_ATTR_ID_EAV_ATTR_ATTR_ID\"\n\t\ttable=\"catalog_product_entity_varchar\" column=\"attribute_id\" referenceTable=\"eav_attribute\"\n\t\treferenceColumn=\"attribute_id\" onDelete=\"CASCADE\"\/>\n&lt;\/table><\/pre>\n\n\n\n<p class=\"simple-note\">You possible to remove a foreign key only if it exists in the <span class=\"code\">db_schema_whitelist.json<\/span> file.<\/p>\n<p class=\"h3\">Create a table:<\/p>\n<p>We use <span class=\"code\">&#8220;table&#8221;<\/span> node to create a table<\/p>\n<p class=\"simple-note\">Remember to generate whitelist and upgrade to apply the change.<\/p>\n<p class=\"h3\">Remove a table:<\/p>\n<p>You can easy Remove table by delete it from the <span class=\"code\">db_schema.xml<\/span> file.<\/p>\n<p class=\"h3\">Rename a table:<\/p>\n<p>Change the table &#8220;name&#8221; consists of two processes: delete the original table declaration and create a new one then migrate data from the original table declaration to the new table with the method <span class=\"code\">onCreate=&#8221;migrateDataFromAnotherTable(old_name)&#8221;<\/span><\/p>\n<p>You can check it with below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n\t&lt;table name=\"new_vendor_message\" resource=\"default\" engine=\"innodb\" comment=\"vendor message Table\" onCreate=\"migrateDataFromAnotherTable(vendor_message)\">\n        &lt;column xsi:type=\"int\" name=\"messages_id\" padding=\"10\" unsigned=\"true\" nullable=\"false\" identity=\"true\" comment=\"Messages ID\"\/>\n        &lt;column xsi:type=\"smallint\" name=\"user_id\" padding=\"5\" unsigned=\"true\" nullable=\"false\" identity=\"false\" default=\"0\" comment=\"User ID\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"username\" nullable=\"false\" length=\"255\" onCreate=\"migrateDataFrom(title)\" comment=\"Username\"\/>\n        &lt;column xsi:type=\"mediumtext\" name=\"description\" nullable=\"true\" comment=\"Description\"\/>\n        &lt;column xsi:type=\"varchar\" name=\"status\" nullable=\"false\" length=\"20\" default=\"not read yet\" comment=\"Status\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"created_at\" on_update=\"false\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Creation Time\"\/>\n        &lt;column xsi:type=\"timestamp\" name=\"updated_at\" on_update=\"true\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Update Time\"\/>\n        &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n            &lt;column name=\"messages_id\"\/>\n        &lt;\/constraint>\n        &lt;index referenceId=\"VENDOR_MESSAGE_USER_ID\" indexType=\"btree\">\n            &lt;column name=\"user_id\"\/>\n        &lt;\/index>\n\t\t&lt;constraint xsi:type=\"foreign\" referenceId=\"FL_ALLOWED_SEVERITIES\" \n\t\t\ttable=\"new_vendor_message\" column=\"user_id\" \n\t\t\treferenceTable=\"customer_entity\" referenceColumn=\"entity_id\" onDelete=\"CASCADE\"\/>\n    &lt;\/table>\n&lt;\/schema><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\">\n\t<p>so we have grasped the declarative schema structure and create Magento 2 database table. In the <a href=\"https:\/\/magerubik.com\/blog\/how-to-use-magento-2-ui-component\" title=\"How to use Magento 2 UI component\">next posts<\/a> we will learn how to use Magento 2 UI component. <a href=\"https:\/\/magerubik.com\/contact\" title=\"Contact Magerubik\">Contact us<\/a> if you face any problems during the installation process.<\/p>\n<\/blockquote>\n<p class=\"simple-note\">You can download the demo code for this entire series from <a href=\"https:\/\/github.com\/magerubik\/module-simple\" title=\"demo code on github\" rel=\"nofollow noopener\">GitHub<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From Magento 2.3, we get acquainted with the method of creating a database called declarative schema. In this article, we<\/p>\n","protected":false},"author":1,"featured_media":166,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2-extension-tutorials","category-magento-2-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Magento 2 database table | MageRubik<\/title>\n<meta name=\"description\" content=\"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Magento 2 database table | MageRubik\" \/>\n<meta property=\"og:description\" content=\"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\" \/>\n<meta property=\"og:site_name\" content=\"MageRubik\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/magerubik\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/magerubik\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-04T04:11:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-24T02:09:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"445\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Hilary howard\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/magerubik\" \/>\n<meta name=\"twitter:site\" content=\"@magerubik\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hilary howard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\"},\"author\":{\"name\":\"Hilary howard\",\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8\"},\"headline\":\"Create Magento 2 database table\",\"datePublished\":\"2022-03-04T04:11:10+00:00\",\"dateModified\":\"2023-04-24T02:09:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\"},\"wordCount\":642,\"publisher\":{\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8\"},\"image\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg\",\"articleSection\":[\"Magento 2 Extension Tutorials\",\"Magento 2 Tutorials\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\",\"url\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\",\"name\":\"Create Magento 2 database table | MageRubik\",\"isPartOf\":{\"@id\":\"https:\/\/magerubik.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg\",\"datePublished\":\"2022-03-04T04:11:10+00:00\",\"dateModified\":\"2023-04-24T02:09:27+00:00\",\"description\":\"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.\",\"breadcrumb\":{\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage\",\"url\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg\",\"contentUrl\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg\",\"width\":800,\"height\":445,\"caption\":\"Magento create database\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Magerubik blog site\",\"item\":\"https:\/\/magerubik.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Magento 2 database table\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/magerubik.com\/blog\/#website\",\"url\":\"https:\/\/magerubik.com\/blog\/\",\"name\":\"Magerubik\",\"description\":\"MageRubik blog site\",\"publisher\":{\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8\"},\"alternateName\":\"Magento 2 Extension\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/magerubik.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8\",\"name\":\"Hilary howard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/magerubik-logo.png\",\"contentUrl\":\"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/magerubik-logo.png\",\"width\":265,\"height\":90,\"caption\":\"Hilary howard\"},\"logo\":{\"@id\":\"https:\/\/magerubik.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/localhost\/blog\",\"https:\/\/www.facebook.com\/magerubik\",\"https:\/\/www.instagram.com\/magerubik\",\"https:\/\/www.linkedin.com\/magerubik\",\"https:\/\/www.pinterest.com\/magerubik\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/magerubik\",\"https:\/\/www.myspace.com\/magerubik\",\"https:\/\/www.youtube.com\/magerubik\",\"https:\/\/www.soundcloud.com\/magerubik\",\"https:\/\/www.tumblr.com\/magerubik\",\"https:\/\/www.wikipedia.com\/magerubik\"],\"url\":\"https:\/\/magerubik.com\/blog\/author\/hilary-howard\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Magento 2 database table | MageRubik","description":"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/","og_locale":"en_US","og_type":"article","og_title":"Create Magento 2 database table | MageRubik","og_description":"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.","og_url":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/","og_site_name":"MageRubik","article_publisher":"https:\/\/www.facebook.com\/magerubik","article_author":"https:\/\/www.facebook.com\/magerubik","article_published_time":"2022-03-04T04:11:10+00:00","article_modified_time":"2023-04-24T02:09:27+00:00","og_image":[{"width":800,"height":445,"url":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg","type":"image\/jpeg"}],"author":"Hilary howard","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/magerubik","twitter_site":"@magerubik","twitter_misc":{"Written by":"Hilary howard","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#article","isPartOf":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/"},"author":{"name":"Hilary howard","@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8"},"headline":"Create Magento 2 database table","datePublished":"2022-03-04T04:11:10+00:00","dateModified":"2023-04-24T02:09:27+00:00","mainEntityOfPage":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/"},"wordCount":642,"publisher":{"@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8"},"image":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage"},"thumbnailUrl":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg","articleSection":["Magento 2 Extension Tutorials","Magento 2 Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/","url":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/","name":"Create Magento 2 database table | MageRubik","isPartOf":{"@id":"https:\/\/magerubik.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage"},"image":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage"},"thumbnailUrl":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg","datePublished":"2022-03-04T04:11:10+00:00","dateModified":"2023-04-24T02:09:27+00:00","description":"Create Magento 2 database - From Magento 2.3, we get acquainted with the method of creating a database called declarative schema.","breadcrumb":{"@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#primaryimage","url":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg","contentUrl":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/Magento_create_database.jpg","width":800,"height":445,"caption":"Magento create database"},{"@type":"BreadcrumbList","@id":"https:\/\/magerubik.com\/blog\/create-magento-2-database-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Magerubik blog site","item":"https:\/\/magerubik.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create Magento 2 database table"}]},{"@type":"WebSite","@id":"https:\/\/magerubik.com\/blog\/#website","url":"https:\/\/magerubik.com\/blog\/","name":"Magerubik","description":"MageRubik blog site","publisher":{"@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8"},"alternateName":"Magento 2 Extension","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/magerubik.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/dad797dc557c925c92436706db1359d8","name":"Hilary howard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/magerubik-logo.png","contentUrl":"https:\/\/magerubik.com\/blog\/wp-content\/uploads\/2022\/03\/magerubik-logo.png","width":265,"height":90,"caption":"Hilary howard"},"logo":{"@id":"https:\/\/magerubik.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/localhost\/blog","https:\/\/www.facebook.com\/magerubik","https:\/\/www.instagram.com\/magerubik","https:\/\/www.linkedin.com\/magerubik","https:\/\/www.pinterest.com\/magerubik","https:\/\/twitter.com\/https:\/\/www.twitter.com\/magerubik","https:\/\/www.myspace.com\/magerubik","https:\/\/www.youtube.com\/magerubik","https:\/\/www.soundcloud.com\/magerubik","https:\/\/www.tumblr.com\/magerubik","https:\/\/www.wikipedia.com\/magerubik"],"url":"https:\/\/magerubik.com\/blog\/author\/hilary-howard\/"}]}},"_links":{"self":[{"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":32,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":627,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions\/627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magerubik.com\/blog\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}