MYSQL Tutorial



RELATIONAL DATABASES


🔗 Relational Databases in MySQL

MySQL is a relational database management system (RDBMS). This means it stores data in a structured format using **tables** and allows relationships between those tables using keys.

💡 What is a Relational Database?
A relational database stores data in tables (like spreadsheets). Each table has rows (records) and columns (fields). The **power** of a relational database comes from linking tables using relationships.

📊 Example Tables

Let's imagine a school database with two related tables:

🧑‍🎓 Students Table

student_id name class
1 Amit 10

📚 Marks Table

mark_id student_id subject score
101 1 Math 88

🔗 How Are They Related?

The student_id in the Marks table is a foreign key that links back to the Students table. This is how relationships are created.

🛠️ Creating Tables with Relationships

-- Create Students table
CREATE TABLE students (
  student_id INT PRIMARY KEY,
  name VARCHAR(50),
  class INT
);

-- Create Marks table with foreign key
CREATE TABLE marks (
  mark_id INT PRIMARY KEY,
  student_id INT,
  subject VARCHAR(50),
  score INT,
  FOREIGN KEY (student_id) REFERENCES students(student_id)
);
  

🔎 Fetching Related Data (JOIN)

-- Join both tables to see student names with marks
SELECT students.name, marks.subject, marks.score
FROM students
JOIN marks ON students.student_id = marks.student_id;
  

✅ Summary

  • Relational databases store data in related tables.
  • Primary key uniquely identifies each row in a table.
  • Foreign key creates a link between two tables.
  • We use JOIN to combine data from multiple tables.
Next Step: Learn about One-to-One, One-to-Many, and Many-to-Many relationships with practical examples!

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review