100 cases of ChatGPT actual combat - (08) Transform database design into SQL and obtain ER diagram

100 cases of ChatGPT actual combat - (08) Transform database design into SQL and obtain ER diagram

1. Requirements and Ideas

Are you still playing SQL? ChatGPT laughs and faints in the toilet In this blog post,
it is designed for the classic 3 tables:
the student table S (SNO, SNAME, AGE, SEX), whose attributes represent the student’s student number, name, age and gender; the class
selection table SC (SNO, CNO, GRADE), whose attributes represent the student’s student number, course number and grade of the course studied;
curriculum table C (CNO, CNAME, TEACHER), whose attributes represent the course number, course name and teacher’s name.
I wrote a set of additions, deletions, checks and changes, which has proved that ChatGPT is by no means in vain.

But this article does not involve the part of the ER diagram, so the basic SQL table creation and ER diagram are listed separately.

2. SQL table creation

question:

作为一名优秀的数据库专家,针对经典3表设计:
学生表 S(SNO,SNAME,AGE,SEX),其属性表示学生的学号、姓名、年龄和性别;
选课表 SC(SNO,CNO,GRADE),其属性表示学生的学号、所学课程的课程号和成绩;
课程表 C(CNO,CNAME,TEACHER),其属性表示课程号、课程名称和任课教师姓名。
写出SQL建表语句,要求使用英文字段中文注释描述

Answer:
Here is the SQL table creation statement of the classic 3-table design, using English field names and Chinese comments:

-- 学生表S 
CREATE TABLE Student(
    SNO VARCHAR(10),      -- 学号
    SNAME VARCHAR(20),    -- 姓名
    AGE INT,              -- 年龄
    SEX VARCHAR(2) CHECK(SEX IN ('M','F')), -- 性别
    CONSTRAINT Student_PK PRIMARY KEY (SNO)   -- 主键约束
);  

-- 选课表SC  
CREATE TABLE SelectCourse(
    SNO VARCHAR(10),       -- 学号 
    CNO VARCHAR(10),      -- 课程号
    GRADE DECIMAL(4,1),   -- 成绩
    CONSTRAINT SC_PK PRIMARY KEY(SNO,CNO),    -- 主键
    CONSTRAINT SC_FK_Student FOREIGN KEY(SNO) REFERENCES Student(SNO), -- 外键,引用学生表 
    CONSTRAINT SC_FK_Course FOREIGN KEY(CNO) REFERENCES Course(CNO)   -- 外键,引用课程表
);   

-- 课程表C  
CREATE TABLE Course(
    CNO VARCHAR(10),     -- 课程号 
    CNAME VARCHAR(40),   -- 课程名称
    TEACHER VARCHAR(20), -- 任课教师
    CONSTRAINT Course_PK PRIMARY KEY(CNO)      -- 主键
);

Table names and attributes are in English, and corresponding Chinese interpretations are given in each line of comments. The design also creates student table, course selection table and curriculum table, and sets primary key constraints and foreign key constraints.
Compared with directly using Chinese table names and fields, this method can maximize compatibility with different databases and configurations, and also provides attribute translation for people who do not understand Chinese, which has better versatility.
Therefore, if the Chinese friendliness of the SQL statement and the compatibility of the database are to be satisfied at the same time, this method of using English fields plus Chinese comments is an ideal design method. It allows more people to understand and use the SQL statement.
Please note that the table names and attributes here are all in English, and the comments are in Chinese, so the actual stored names are still in English, which is also for the sake of the versatility of the database. We use Chinese comments in SQL statements just to make it easier for people to understand the meaning of each field.

This is a bit detailed

3. Drawing

question:

针对以上sql建表语句,使用plantuml语法绘制ER图

Answer:
According to the SQL table creation statement generated earlier, here use Plantuml syntax to draw the corresponding ER diagram:

@startuml

' 设置字体大小和整图的方向
skinparam defaultTextAlignment center 
skinparam monochrome true  
skinparam size 14  
left to right direction  

' 定义实体类型,圆角实体表示弱实体
entity 学生 {
    *学号 
    姓名
    年龄 
    性别
}

entity 选课 {
    *学号 
    *课程号
    成绩 
}   

entity 课程{
  *课程号
  课程名称
  任课教师  
}

' 连接选课与学生实体,并标注多对一关系 
选课 ||--o{ 学生 : 多对一

' 连接选课与课程实体,并标注多对一关系
选课 }o--|| 课程 : 多对一

' 设置实体的主键  
学生 : 学号 
课程 : 课程号
选课 : 学号 + 课程号  

@enduml

This ER diagram clearly describes the structured relationship between 3 entities (student, course selection, course):

  • The many-to-one relationship between the student entity and the course selection entity means that a student can choose multiple courses.
  • The many-to-one relationship between the course selection entity and the course entity indicates that a course can be selected by multiple students.
  • The primary key of each entity is also clearly marked, such as the primary key of the student entity is the student number, the primary key of the course entity is the course number, and the primary key of the course selection entity is the combination of the student number and the course number.
    insert image description here

Therefore, through the simple Plantuml syntax, we can intuitively describe the table structure and key constraint relationship in the SQL statement, and generate a clear and accurate ER diagram. This also reflects the practicability of Plantuml as a text drawing tool in the field of database design.

Four. Summary

To review, the main promptknowledge points in this section:

  • CPU Dafa
作为一名优秀的{高帽子},请{干啥}
  • Build table according to needs
针对{某某某设计},写出SQL建表语句,要求使用英文字段中文注释描述
  • Draw the ER diagram according to the table SQL
针对以上sql建表语句,使用plantuml语法绘制ER图

Guess you like

Origin blog.csdn.net/u010764910/article/details/130256972