Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Database schema introduction

From MoodleDocs

The Moodle database has around 200 tables, and can be quite daunting at first sight. The good news is that you don't have to understand it all at once. For example, there are eight tables called forum_somthing. If you are interested in the forum module, then obviously you need to understand these tables, and the places they link into core tables. But if you are not interested in the forum module, you can forget about them. The same is true of each activity module.

Once you take out the tables for each activity module in this way, and similarly take out the tables belonging to the enrolment plugins, question types, etc. You are left with about 50 core tables. But the good news is that even here they break down into groups that mostly you can understand together, or ignore. This page lists the core database tables in these groups. Later, it would be good to add more detailed documentation explaining some of these groups.

The database structure is defined, edited and upgraded using the XMLDB system.

Automatically generated documentation

The install.xml files that define Moodle's database structure (are supposed to) contain comments on each table and database field, to document their purpose.

From Moodle 2.0 onwards, you can view this documentation in HTML format by going to Administration -> Development -> XMLDB editor and then clicking on the [Doc] links.

Configuration

config
config_plugins

Users and their profiles

user
user_info_category
user_info_field
user_info_data
user_lastaccess
this is separated from the user table for performance reasons
user_preferences
user_private_key

The roles and capabilities system

role
defines a role, its name, etc. Other parts of the role definition are stored in the role_capabilities and role_context_levels tables.
capabilites
the various permissions that can be granted.
context
a context is a scope in Moodle, for example the whole system, a course, a particular activity. The type is given by contextlevel, and depending on context level, instanceid points to one of a number of different tables.
role_allow_assign
which roles can assign which other roles
role_allow_override
which roles can override which other roles
role_allow_switch
which roles can switch to which other roles (Moodle 2.0 onwards.)
role_assignments
which users are assigned which roles in which contexts
role_capabilities
the permission for each capability in either a role definition (if contextid points to the system context) or a role override (if contextid points to some other context)
role_context_levels
the context levels at which each role can be assigned. (Moodle 2.0 onwards)
role_names
used to implement the feature where roles can be given different names in different courses (or, more gererally, contexts)
role_sortorder
is not actually used anywhere in the code!

The following diagram shows the tables with their columns and the relationships between them, and other tables in Moodle.

RolesDatabase.png

Courses and their organisation into categories

course
course_categories
course_display
course_meta
course_request

Activities and their arrangement within courses

modules
course_allowed_modules
course_modules
course_sections

See also the tables belonging to particular activity modules. For example forum_*, quiz_*, etc.

Groups and groupings

groups
groups_members
groupings
groupings_groups

The logging system

log
log_display

Blocks system

block
block_instance
block_rss_client
block_pinned
block_search_documents

Some particular blocks also have their own database tables. For example block_rss_client.

Events

event
events_handlers
events_queue
events_queue_handlers

Backup and restore

backup_config
backup_courses
backup_files
backup_ids
backup_log

Statistics

stats_daily
stats_monthly
stats_user_daily
stats_user_monthly
stats_user_weekly
stats_weekly

Tags

tag_*

Gradebook

grade_*

Question bank and question engine

question
question_answers
question_attempts
question_categories
question_sessions
question_states

See also the tables for individual question types, which all start question_.

Messaging system

message_*

Moodle Network

mnet_*

Caching

cache_*

Miscellaneous

scale
scale_history
sessions2
timezone
post

Activity modules

  • Assignment
  • Chat
  • Choice
  • Data
  • Feedback
  • Forum
  • Glossary
  • Hotpot
  • Label
  • Lesson
  • Quiz
  • Resource
  • SCORM
  • Survey
  • Wiki

Authentication plugins

  • ...

Blocks

  • ...

Enrolment plugins

  • ...

Question types

See also