---
title: "26: Improving the schema | SurrealDB University"
description: "Improving the schema. A step in the guided Tour of SurrealDB, with examples you can run as you read."
url: https://surrealdb.com/learn/tour/page-26
---

![Course content preview](https://surrealdb.com/assets/static/town.D95IYf2V.avif)

[Back to Courses](https://surrealdb.com/learn)

Course chapters

[Welcome to the Tour of SurrealDB!](https://surrealdb.com/learn/tour) [1: Connecting to a database](https://surrealdb.com/learn/tour/page-01) [2: Creating records](https://surrealdb.com/learn/tour/page-02) [3: Using the ONLY keyword](https://surrealdb.com/learn/tour/page-03) [4: Non-ASCII record IDs](https://surrealdb.com/learn/tour/page-04) [5: Parentheses, indexing, and fields](https://surrealdb.com/learn/tour/page-05) [6: Deleting records](https://surrealdb.com/learn/tour/page-06) [7: Field order in objects](https://surrealdb.com/learn/tour/page-07) [8: Tips when selecting records](https://surrealdb.com/learn/tour/page-08) [9: Casting and aliases](https://surrealdb.com/learn/tour/page-09) [10: Affixes](https://surrealdb.com/learn/tour/page-10) [11: Updating records](https://surrealdb.com/learn/tour/page-11) [12: Linking records](https://surrealdb.com/learn/tour/page-12) [13: Viewing the database as a whole](https://surrealdb.com/learn/tour/page-13) [14: Schemafull and schemaless](https://surrealdb.com/learn/tour/page-14) [15: Viewing the schema](https://surrealdb.com/learn/tour/page-15) [16: Literal types and assertions](https://surrealdb.com/learn/tour/page-16) [17: INSERT and using JSON](https://surrealdb.com/learn/tour/page-17) [18: FOR loops](https://surrealdb.com/learn/tour/page-18) [19: Basic graph relations](https://surrealdb.com/learn/tour/page-19) [20: Creating graph edges](https://surrealdb.com/learn/tour/page-20) [21: Querying graph relations](https://surrealdb.com/learn/tour/page-21) [22: Graph relations at greater depth](https://surrealdb.com/learn/tour/page-22) [23: Adding and improving data](https://surrealdb.com/learn/tour/page-23) [24: Defining events](https://surrealdb.com/learn/tour/page-24) [25: Defining indexes](https://surrealdb.com/learn/tour/page-25) [26: Improving the schema](https://surrealdb.com/learn/tour/page-26) [27: Visualising graph paths](https://surrealdb.com/learn/tour/page-27) [28: Recursive queries: setup](https://surrealdb.com/learn/tour/page-28) [29: Recursive queries](https://surrealdb.com/learn/tour/page-29) [30: More on recursive queries](https://surrealdb.com/learn/tour/page-30) [31: AI vector search](https://surrealdb.com/learn/tour/page-31) [Done the tour!](https://surrealdb.com/learn/tour/page-32)

# 26: Improving the schema

The Designer view inside SurrealDB Studio is able to show the link between the `town` and `place` tables, but the other regular tables and graph tables are just floating around on top. You can experiment for yourself by pasting [all of our queries so far](https://app.surrealdb.com/mini?query=CREATE+place%3Asurreal_library+SET+name+%3D+%27Surreal+Library%27%2C+address+%3D+%272025+Statement+Street%2C+Riverdale%27%2C+place_type+%3D+%27library%27%2C+floors+%3D+8%3B%0ACREATE+town%3Ariverdale+SET+name+%3D+%27Riverdale%27%2C+population+%3D+75000%2C+libraries+%3D+%5B%0A%09place%3Asurreal_library%0A%5D%3B%0ADEFINE+FIELD+address+ON+place+TYPE+string%3B%0ADEFINE+FIELD+name+ON+place+TYPE+string%3B%0ADEFINE+FIELD+name+ON+town+TYPE+string%3B%0ADEFINE+FIELD+population+ON+town+TYPE+int%3B%0ADEFINE+FIELD+libraries+ON+town+TYPE+option%3Carray%3Crecord%3Cplace%3E%3E%3E%3B%0ADEFINE+FIELD+place_type+ON+place+TYPE+%27building%27+%7C+%27tower%27+%7C+%27store%27+%7C+%27school%27+%7C+%27park%27+%7C+%27library%27%3B%0ADEFINE+FIELD+OVERWRITE+population+ON+town+TYPE+int+ASSERT+%24value+%3E%3D+0%3B%0A%0AFOR+%24person+IN+%5B%0A%09%7B%0A%09%09age%3A+30%2C%0A%09%09name%3A+%27Sara+Bellum%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+25%2C%0A%09%09name%3A+%27Lydia+Wyndham%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+45%2C%0A%09%09name%3A+%27Samm+Schwartz%27%0A%09%7D%0A%5D+%7B%0A%0A%09LET+%24created+%3D+%28CREATE+ONLY+person+SET+age+%3D+%24person.age%2C+name+%3D+%24person.name%29%3B%0A%0A%09RELATE+%24created+-%3E+works_at+-%3E+place%3Asurreal_library%3B%0A%0A%7D%3B%0ADEFINE+FIELD+account_created+ON+customer_of+READONLY+VALUE+time%3A%3Anow%28%29%3B%0ADEFINE+FIELD+last_updated+ON+customer_of+VALUE+time%3A%3Anow%28%29%3B%0ADEFINE+FIELD+customer_for+ON+customer_of+VALUE+%3Cfuture%3E+%7B+time%3A%3Anow%28%29+-+account_created+%7D%3B%0A%0AFOR+%24data+IN+%5B%0A%09%7B%0A%09%09age%3A+15%2C%0A%09%09name%3A+%27Archie+Andrews%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+15%2C%0A%09%09name%3A+%27Veronica+Lodge%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+15%2C%0A%09%09name%3A+%27Betty+Cooper%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+15%2C%0A%09%09name%3A+%27Jughead+Jones%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+15%2C%0A%09%09name%3A+%27Reggie+Mantle%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+50%2C%0A%09%09name%3A+%27Hiram+P.+Lodge%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+55%2C%0A%09%09name%3A+%27Geraldine+Grundy%27%0A%09%7D%2C%0A%09%7B%0A%09%09age%3A+65%2C%0A%09%09name%3A+%27Waldo+Weatherbee%27%0A%09%7D%0A%5D+%7B%0A%0A%09LET+%24person+%3D+%28CREATE+ONLY+person+SET+name+%3D+%24data.name%2C+age+%3D+%24data.age%29%3B%0A%0A%09RELATE+%24person+-%3E+customer_of+-%3E+place%3Asurreal_library%3B%0A%0A%7D%3B%0AFOR+%24data+IN+%5B%0A%09%7B%0A%09%09author%3A+%27Charles+Dickens%27%2C%0A%09%09english_title%3A+%27A+Tale+of+Two+Cities%27%2C%0A%09%09language%3A+%27en%27%2C%0A%09%09published%3A+%271859-11-26%27%2C%0A%09%09title%3A+%27A+Tale+of+Two+Cities%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27Antoine+de+Saint-Exup%C3%A9ry%27%2C%0A%09%09english_title%3A+%27The+Little+Prince%27%2C%0A%09%09language%3A+%27fr%27%2C%0A%09%09published%3A+%271943-04-15%27%2C%0A%09%09title%3A+%27Le+Petit+Prince%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27Paulo+Coelho%27%2C%0A%09%09english_title%3A+%27The+Alchemist%27%2C%0A%09%09language%3A+%27pt%27%2C%0A%09%09published%3A+%271988-03-01%27%2C%0A%09%09title%3A+%27O+Alquimista%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27J.K.+Rowling%27%2C%0A%09%09english_title%3A+%22Harry+Potter+and+the+Philosopher%27s+Stone%22%2C%0A%09%09language%3A+%27en%27%2C%0A%09%09published%3A+%271997-06-26%27%2C%0A%09%09title%3A+%22Harry+Potter+and+the+Philosopher%27s+Stone%22%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27Agatha+Christie%27%2C%0A%09%09english_title%3A+%27And+Then+There+Were+None%27%2C%0A%09%09language%3A+%27en%27%2C%0A%09%09published%3A+%271939-11-06%27%2C%0A%09%09title%3A+%27And+Then+There+Were+None%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27Cao+Xueqin%27%2C%0A%09%09english_title%3A+%27Dream+of+the+Red+Chamber%27%2C%0A%09%09language%3A+%27zh%27%2C%0A%09%09published%3A+%271791-03-01%27%2C%0A%09%09title%3A+%27%E7%B4%85%E6%A8%93%E5%A4%A2%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27J.R.R.+Tolkien%27%2C%0A%09%09english_title%3A+%27The+Hobbit%27%2C%0A%09%09language%3A+%27en%27%2C%0A%09%09published%3A+%271937-09-21%27%2C%0A%09%09title%3A+%27The+Hobbit%27%0A%09%7D%2C%0A%09%7B%0A%09%09author%3A+%27Lewis+Carroll%27%2C%0A%09%09english_title%3A+%22Alice%27s+Adventures+in+Wonderland%22%2C%0A%09%09language%3A+%27en%27%2C%0A%09%09published%3A+%271865-07-04%27%2C%0A%09%09title%3A+%22Alice%27s+Adventures+in+Wonderland%22%0A%09%7D%0A%5D+%7B%0A%0A%09LET+%24author+%3D+%28CREATE+ONLY+person+SET+name+%3D+%24data.author%29%3B%0A%09LET+%24book+%3D+%28CREATE+ONLY+book+SET+author+%3D+%24data.author%2C+english_title+%3D+%24data.english_title%2C+language+%3D+%24data.language%2C+published+%3D+%3Cdatetime%3E+%24data.published%2C+title+%3D+%24data.title%29%3B%0A%09RELATE+%24author+-%3E+wrote+-%3E+%24book%3B%0A%09RELATE+place%3Asurreal_library+-%3E+has+-%3E+%24book%3B%0A%0A%7D%3B%0ADEFINE+FIELD+author+ON+book+TYPE+string%3B%0ADEFINE+FIELD+language+ON+book+TYPE+string%3B%0ADEFINE+FIELD+title+ON+book+TYPE+string%3B%0A%0ADEFINE+EVENT+language_check+ON+book+WHEN+%24event+%3D+%27CREATE%27+THEN+%7B%0A%09IF+%24after.language+%3D+%27en%27%0A%09%09%7B%0A%09%09%09UPDATE+%24after+SET+english_title+%3D+%24after.title%3B%0A%09%09%7D%0A%09ELSE+IF+%24after.language+%21%3D+%27en%27+AND+%24after.english_title+%3D+NONE%0A%09%09%7B%0A%09%09%09THROW+%27Please+enter+an+English+title+for+%27+%2B+%3Cstring%3E+%24after.title+%2B+%27+written+in+%27+%2B+%3Cstring%3E+%24after.language%3B%0A%09%09%7D%0A%09%3B%0A%7D%3B%0ACREATE+book+SET+title+%3D+%27The+Shadow+Rising%27%2C+author+%3D+%27Robert+Jordan%27%2C+language+%3D+%27en%27%3B%0ADEFINE+INDEX+unique_address+ON+place+FIELDS+address+UNIQUE%3B&resultmode=combined) into SurrealDB Studio, which should give you the following view.

![The database schema so far which contains a number of new regular and graph tables that have not been defined yet and thus display as unconnected to the others](https://surrealdb.com/assets/static/schema3.0lCTffdo.avif)

At the moment, the relation tables in particular are vulnerable because they have not been defined as relation tables. Without this definition, a regular `works_at` table could be created and the database would accept it.

```surql
CREATE works_at SET name = ...
```

This can be fixed by using `DEFINE TABLE` along with three clauses:

- `TYPE RELATION` to ensure that it can only be used as a graph table,
- `IN record_name OUT record_name` to set the record types that can be used, and
- `OVERWRITE` after `DEFINE TABLE`, because currently the existing tables are defined as schemaless, non-relation tables. Without `OVERWRITE`, the database will return an error saying that the table already exists.

Since we have four graph relations set up, four `DEFINE TABLE` statements will be enough to improve the schema output.

```surql
/**
[test]

[[test.results]]
value = "NONE"

[[test.results]]
value = "NONE"

[[test.results]]
value = "NONE"

[[test.results]]
value = "NONE"

*/

DEFINE TABLE OVERWRITE works_at TYPE RELATION IN person OUT place;
DEFINE TABLE OVERWRITE wrote TYPE RELATION IN person OUT book;
DEFINE TABLE OVERWRITE has TYPE RELATION IN place OUT book;
DEFINE TABLE OVERWRITE customer_of TYPE RELATION IN person OUT place;
```

SurrealDB Studio is now able to say for sure that these tables are used for relations, which allows all the types inside the schema to be joined together.

![The database schema so far now that each table has been defined, allowing SurrealDB Studio to understand the connections between each one](https://surrealdb.com/assets/static/schema4.Fa6BNCmW.avif)

Previous

25: Defining indexes

[Previous](https://surrealdb.com/learn/tour/page-25)

Next lesson

27: Visualising graph paths

[Next lesson](https://surrealdb.com/learn/tour/page-27)

```json
{"@context":"https://schema.org","@type":"Course","name":"Welcome to the Tour of SurrealDB!","description":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour","inLanguage":"en","isAccessibleForFree":true,"provider":{"@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com"},"hasPart":[{"@type":"LearningResource","name":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour"},{"@type":"LearningResource","name":"1: Connecting to a database","url":"https://surrealdb.com/learn/tour/page-01"},{"@type":"LearningResource","name":"2: Creating records","url":"https://surrealdb.com/learn/tour/page-02"},{"@type":"LearningResource","name":"3: Using the ONLY keyword","url":"https://surrealdb.com/learn/tour/page-03"},{"@type":"LearningResource","name":"4: Non-ASCII record IDs","url":"https://surrealdb.com/learn/tour/page-04"},{"@type":"LearningResource","name":"5: Parentheses, indexing, and fields","url":"https://surrealdb.com/learn/tour/page-05"},{"@type":"LearningResource","name":"6: Deleting records","url":"https://surrealdb.com/learn/tour/page-06"},{"@type":"LearningResource","name":"7: Field order in objects","url":"https://surrealdb.com/learn/tour/page-07"},{"@type":"LearningResource","name":"8: Tips when selecting records","url":"https://surrealdb.com/learn/tour/page-08"},{"@type":"LearningResource","name":"9: Casting and aliases","url":"https://surrealdb.com/learn/tour/page-09"},{"@type":"LearningResource","name":"10: Affixes","url":"https://surrealdb.com/learn/tour/page-10"},{"@type":"LearningResource","name":"11: Updating records","url":"https://surrealdb.com/learn/tour/page-11"},{"@type":"LearningResource","name":"12: Linking records","url":"https://surrealdb.com/learn/tour/page-12"},{"@type":"LearningResource","name":"13: Viewing the database as a whole","url":"https://surrealdb.com/learn/tour/page-13"},{"@type":"LearningResource","name":"14: Schemafull and schemaless","url":"https://surrealdb.com/learn/tour/page-14"},{"@type":"LearningResource","name":"15: Viewing the schema","url":"https://surrealdb.com/learn/tour/page-15"},{"@type":"LearningResource","name":"16: Literal types and assertions","url":"https://surrealdb.com/learn/tour/page-16"},{"@type":"LearningResource","name":"17: INSERT and using JSON","url":"https://surrealdb.com/learn/tour/page-17"},{"@type":"LearningResource","name":"18: FOR loops","url":"https://surrealdb.com/learn/tour/page-18"},{"@type":"LearningResource","name":"19: Basic graph relations","url":"https://surrealdb.com/learn/tour/page-19"},{"@type":"LearningResource","name":"20: Creating graph edges","url":"https://surrealdb.com/learn/tour/page-20"},{"@type":"LearningResource","name":"21: Querying graph relations","url":"https://surrealdb.com/learn/tour/page-21"},{"@type":"LearningResource","name":"22: Graph relations at greater depth","url":"https://surrealdb.com/learn/tour/page-22"},{"@type":"LearningResource","name":"23: Adding and improving data","url":"https://surrealdb.com/learn/tour/page-23"},{"@type":"LearningResource","name":"24: Defining events","url":"https://surrealdb.com/learn/tour/page-24"},{"@type":"LearningResource","name":"25: Defining indexes","url":"https://surrealdb.com/learn/tour/page-25"},{"@type":"LearningResource","name":"26: Improving the schema","url":"https://surrealdb.com/learn/tour/page-26"},{"@type":"LearningResource","name":"27: Visualising graph paths","url":"https://surrealdb.com/learn/tour/page-27"},{"@type":"LearningResource","name":"28: Recursive queries: setup","url":"https://surrealdb.com/learn/tour/page-28"},{"@type":"LearningResource","name":"29: Recursive queries","url":"https://surrealdb.com/learn/tour/page-29"},{"@type":"LearningResource","name":"30: More on recursive queries","url":"https://surrealdb.com/learn/tour/page-30"},{"@type":"LearningResource","name":"31: AI vector search","url":"https://surrealdb.com/learn/tour/page-31"},{"@type":"LearningResource","name":"Done the tour!","url":"https://surrealdb.com/learn/tour/page-32"}]}
```

```json
{"@context":"https://schema.org","@type":"LearningResource","name":"26: Improving the schema","description":"Improving the schema","url":"https://surrealdb.com/learn/tour/page-26","learningResourceType":"lesson","isPartOf":{"@type":"Course","name":"Welcome to the Tour of SurrealDB!","url":"https://surrealdb.com/learn/tour"},"position":27}
```

```json
{"@context":"https://schema.org","@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com","logo":"https://surrealdb.com/assets/static/logo.BG7_TG2b.svg","description":"SurrealDB is the unified data layer for AI. A multi-model database for documents, graphs, vectors, and time-series.","foundingDate":"2022","hasCertification":[{"@type":"Certification","name":"SOC 2 Type 2"},{"@type":"Certification","name":"GDPR"},{"@type":"Certification","name":"Cyber Essentials Plus"},{"@type":"Certification","name":"ISO 27001"}],"owns":[{"@type":"SoftwareApplication","name":"SurrealDB","url":"https://surrealdb.com/surrealdb"},{"@type":"SoftwareApplication","name":"Agent Memory","url":"https://surrealdb.com/agent-memory"}],"knowsAbout":["multi-model databases","document databases","graph databases","vector search","time-series databases","SurrealQL","Agent Memory","real-time databases","embedded databases","context layer","graph ontology","distributed database","knowledge graphs","distributed transaction protocols","highly-scalable databases"],"sameAs":["https://www.wikidata.org/wiki/Q124316308","https://github.com/surrealdb/surrealdb","https://twitter.com/surrealdb","https://www.youtube.com/@surrealdb","https://www.linkedin.com/company/surrealdb","https://discord.gg/surrealdb","https://www.reddit.com/r/surrealdb","https://www.instagram.com/surrealdb","https://medium.com/surrealdb","https://dev.to/surrealdb"]}
```

```json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://surrealdb.com"},{"@type":"ListItem","position":2,"name":"Learn","item":"https://surrealdb.com/learn"},{"@type":"ListItem","position":3,"name":"Page 26","item":"https://surrealdb.com/learn/tour/page-26"}]}
```
