Foreign Keys and Structure

795619
Mega Contributor

Hi I'm new to ServiceNow and today I'm trying to create tables in the Berlin version. I'd like to use ServiceNow tables to store my structure as well as data. I've used regular MySQL tables in the past, but here (in the Berlin version), I can't find any way to connect tables using foreign keys. For example, there will be a number of posts in the content . So the posts table will need to have a key to the content table to define which posts go to which contents. So my questions are:

  1. How do I set up foreign keys in tables please (in ServiceNow Berlin)?
  2. What would be the best way to achieve my goal, that is, to connect several tables (but keep them separate) for my project (for example, the main table called content has children like form, list, navigation etc.)?

I'll appreciate any help.

10 REPLIES 10

Community Alums
Not applicable

Jakub,



To relate tables to one another, create a Reference field on one of the tables. If you want to share some of what you are trying to do, I can provide some specific examples.



If you need to do create a many to many relationship, check out this wiki article: http://wiki.servicenow.com/index.php?title=Creating_a_Many-to-Many_Relationship



ben


Ok, I created the reference on one of my columns but now I have a problem with my view. I'd like to have a view that will connect two tables:


  • u_character
  • u_item

The u_character_id column in u_item is a reference to u_character_id in u_character. My view should select all items from u_item that have u_item_id that I define in the first select. Here are my where clauses for these two selects:


  • table: u_character, where clause: char_u_character_id = 25
  • table: u_item, where clause: item_u_character_id = char_u_character_id

When I run this view, I get no results. But when ran the same view in the past (when the u_character_id column in the u_item table was not of a reference type; it was of a string type), it worked



So it seems like I can't compare a reference type of column with a string type of column. Which makes sense, but how do I set up this view to make sure it's working again?



Thanks Ben for your help.


Community Alums
Not applicable

When you use a reference field, the real connection is through the sys_id, so what you really want to use for your where clause on u_item should be item_u_character_id = char_sys_id. This should get you the items where your manually defined character id is 25.


Thanks so much Ben! It took me a while to set it up. I noticed that when defining the references in my tables, the popup window sometimes doesn't show the value in the column of the parent table and it displays the sys_id instead. That's a little inconvenient. I have to click on the sys_id and then the popup window disappears and the proper row value shows in the reference column. Any ideas what could be causing this?