0

My question: is there a way to save data into a SQL database with a key value? I have an example below.

As you see in the example table below, in column Friends I have ""Uno", John". "Uno" would be the key value that is associated with "John".

I would be able to get the data from "Uno" like this:

SELECT * 
FROM Table_Name 
WHERE KEY VALUE = "Uno" 

Is saving data like this possible? If not, I would love some suggestions!

Thank you!

enter image description here

2
  • 3
    I removed the inconsistent database tags. Please tag only with the database you are really using. Commented Jul 25, 2020 at 17:57
  • Very well! Thank you! Commented Jul 25, 2020 at 18:19

1 Answer 1

1

Saving keys is not only possible, but it's also one of the fundamental abilities of relational databases. You don't want to store friends' names though, just store keys of them and make an SQL query to get names from keys.

This is a pseudocode you might use, but it might not compile for every DBMS

CREATE TABLE people (
  id INT NOT NULL PRIMARY KEY,
  name CHAR(30)
);

CREATE TABLE friends (
  person_id INT NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES people(id),
  friend_id INT NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES people(id)
);
Sign up to request clarification or add additional context in comments.

2 Comments

THANK YOU! THANK YOU! This is exactly what I needed! I highly appreciate your response! :)
What you're looking at is called a join table, and it's used for many-to-many relationships in SQL. But if your database has many many-to-many relationships, you're better off looking at a NoSQL solution such as graph database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.