Introduction
In this project, you will learn how to create a student information management system using MySQL. This system includes two tables: student_info and student_score, with a foreign key relationship between them.
👀 Preview
MySQL [studentSys]> DESC student_info;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| Id | int | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.002 sec)
MySQL [studentSys]> DESC student_score;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| Id | int | NO | PRI | NULL | |
| score | varchar(4) | YES | | NULL | |
| Sid | int | YES | MUL | NULL | |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
🎯 Tasks
In this project, you will learn:
- How to start the MySQL server and log into the MySQL terminal
- How to create a database and tables using SQL statements
- How to establish a foreign key relationship between two tables
🏆 Achievements
After completing this project, you will be able to:
- Understand the concept of a relational database and how to create and manage tables
- Implement a foreign key constraint to ensure data integrity between related tables
- Apply your knowledge of SQL to build a simple student information management system
Start MySQL and Create the Database
In this step, you will learn how to start the MySQL server and create the studentSys database.
Start the MySQL server:
sudo /etc/init.d/mysql startLog into the MySQL terminal:
mysql -urootCreate a new file named
studentSys.sqlin the/home/labex/projectdirectory.In the
studentSys.sqlfile, create thestudentSysdatabase:DROP DATABASE IF EXISTS studentSys; CREATE DATABASE IF NOT EXISTS studentSys CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;The
DROP DATABASE IF EXISTSstatement checks if thestudentSysdatabase already exists, and if it does, it deletes it. TheCREATE DATABASE IF NOT EXISTSstatement then creates thestudentSysdatabase with the specified character set and collation.
Create the student_info Table
In this step, you will learn how to create the student_info table.
In the
studentSys.sqlfile, use thestudentSysdatabase:USE studentSys;Create the
student_infotable:DROP TABLE IF EXISTS student_info; CREATE TABLE student_info ( Id INT NOT NULL AUTO_INCREMENT, name VARCHAR(10), PRIMARY KEY (Id) );The
DROP TABLE IF EXISTSstatement checks if thestudent_infotable already exists, and if it does, it deletes it. TheCREATE TABLEstatement then creates thestudent_infotable with two columns:Idandname. TheIdcolumn is set as the primary key and is set to auto-increment.
Create the student_score Table and Establish the Foreign Key Relationship
In this step, you will learn how to create the student_score table and establish a foreign key relationship between the student_score table and the student_info table.
In the
studentSys.sqlfile, create thestudent_scoretable:DROP TABLE IF EXISTS student_score; CREATE TABLE student_score ( Id INT NOT NULL, score VARCHAR(4), Sid INT, PRIMARY KEY (Id), FOREIGN KEY (Sid) REFERENCES student_info(Id) );The
DROP TABLE IF EXISTSstatement checks if thestudent_scoretable already exists, and if it does, it deletes it. TheCREATE TABLEstatement then creates thestudent_scoretable with three columns:Id,score, andSid.The
PRIMARY KEY (Id)statement sets theIdcolumn as the primary key for thestudent_scoretable.The
FOREIGN KEY (Sid) REFERENCES student_info(Id)statement establishes a foreign key relationship between theSidcolumn in thestudent_scoretable and theIdcolumn in thestudent_infotable. This means that the values in theSidcolumn of thestudent_scoretable must match the values in theIdcolumn of thestudent_infotable.In the MySQL prompt, run the following command to execute the
studentSys.sqlscript:
SOURCE ~/project/studentSys.sql
After completing these steps, you have created the studentSys database, the student_info table, and the student_score table, and established a foreign key relationship between the two tables.
MySQL [studentSys]> DESC student_info;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| Id | int | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.002 sec)
MySQL [studentSys]> DESC student_score;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| Id | int | NO | PRI | NULL | |
| score | varchar(4) | YES | | NULL | |
| Sid | int | YES | MUL | NULL | |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.
