[MySQL database principle] Run SQL code in the MySQL Workbench interface - student management system

In MySQL Workbench 8.0, you can use the following steps to create new content and run MySQL language code:

1. Open MySQL Workbench and connect to your MySQL database server.
Insert image description here

2. In the left navigation bar, expand your connection to view the database. Select the database in which you want to run the SQL code.
Insert image description here

3. In the top menu bar, click the "Query" tab to open the query editor.
Insert image description here

4. In the query editor, paste your SQL code. Please make sure your SQL syntax is correct.

The sample code to run is as follows:

SELECT 'Hello, World!' AS greeting;

Paste this SQL code into the query editor of MySQL Workbench and click "Execute" to run it. The results will be displayed in the results window, which should say "Hello, World!". This is a simple example to show how to execute SQL queries in MySQL Workbench.

The run button is an icon with a thunderbolt symbol:
Insert image description here

operation result:
Insert image description here

The following uses mysql code to implement the student management system database:

-- 创建一个名为 "students_database" 的数据库
CREATE DATABASE students_database;

-- 使用 "students_database" 数据库(替换成你要使用的数据库名)
USE students_database;

-- 创建学生信息表格
CREATE TABLE students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,   -- 学生ID,自动递增,主键
    first_name VARCHAR(50) NOT NULL,             -- 名字,不为空
    last_name VARCHAR(50) NOT NULL,              -- 姓氏,不为空
    email VARCHAR(100) UNIQUE NOT NULL,          -- 邮箱,唯一且不为空
    birthdate DATE,                             -- 出生日期
    registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP  -- 注册日期,默认为当前时间戳
);

-- 创建课程信息表格
CREATE TABLE courses (
    course_id INT AUTO_INCREMENT PRIMARY KEY,     -- 课程ID,自动递增,主键
    course_name VARCHAR(100) NOT NULL,           -- 课程名称,不为空
    instructor VARCHAR(100) NOT NULL             -- 教师姓名,不为空
);

-- 插入学生数据(使用中文名字)
INSERT INTO students (first_name, last_name, email, birthdate)
VALUES
    ('张三', '李四', '[email protected]', '1995-05-15'),
    ('王五', '赵六', '[email protected]', '1996-07-20'),
    ('刘七', '陈八', '[email protected]', '1997-03-10');

-- 插入课程数据(使用中文课程名和教师名)
INSERT INTO courses (course_name, instructor)
VALUES
    ('数学101', '张老师'),
    ('历史101', '王老师'),
    ('科学101', '李老师');

operation result:
Insert image description here

After running the program, remember to refresh it to see the newly created database:
Insert image description here

Check whether the database creation is complete and the student management data content is:

Insert image description here
Insert image description here

So, now that the student management data information has been stored in the database, how to read the required results from the database?

To retrieve data for students and courses, you can use the following SQL queries, which will retrieve student and teacher information from your database:

sql

-- 检索所有学生的信息
SELECT * FROM students;

-- 检索所有课程的信息
SELECT * FROM courses;

These two queries will return information for all students and courses. If you only want to retrieve students or courses under certain criteria, you can add the appropriate WHEREclause to filter the data.

For example, the following query will retrieve information for all students with the last name "Zhang":

SELECT * FROM students
WHERE last_name = '张';

Alternatively, the following query will retrieve all courses taught by "Teacher Zhang":

SELECT * FROM courses
WHERE instructor = '张老师';

These queries can be adapted and expanded based on your needs. Paste them into MySQL Workbench or other MySQL-enabled database tool and execute to retrieve the data.

Combined with the above introduction, create a new query sql file completely. The complete code of the file is as follows:

-- 使用 "students_database" 数据库(替换成你要使用的数据库名)
USE students_database;

-- 检索所有学生的信息
SELECT * FROM students;

-- 检索所有课程的信息
SELECT * FROM courses;

-- 检索姓氏为 "张" 的所有学生的信息
SELECT * FROM students
WHERE last_name = '张';

-- 检索由 "张老师" 教授的所有课程的信息
SELECT * FROM courses
WHERE instructor = '张老师';

The above comments explain the purpose and meaning of each SQL query and help you understand the function of the code. You can paste these queries into MySQL Workbench or other MySQL database tools and execute them to retrieve the data.

Results of the:
Insert image description here

Supongo que te gusta

Origin blog.csdn.net/weixin_41194129/article/details/132807195
Recomendado
Clasificación