django-orm table Homework


Django-ORM exercises https://www.cnblogs.com/Michael--chen/protected/articles/10926556.html
Django-ORM

A. Table structure
  , create the following table, and create the relevant constraints.

 

 

  models file, the model class code:

复制代码
class Classes(models.Model):
cid = models.AutoField(primary_key=True)
caption = models.CharField(max_length=32)
grade = models.ForeignKey("ClassGrade", on_delete=models.CASCADE)


class ClassGrade(models.Model):
gid = models.AutoField(primary_key=True)
gname = models.CharField(max_length=32)


class Student(models.Model):
sid = models.AutoField(primary_key=True)
sname = models.CharField(max_length=32)
gender_choice = ((0, "女"), (1, "男"))
gender = models.SmallIntegerField(choices=gender_choice, default=1)
classes = models.ForeignKey("Classes", on_delete=models.CASCADE)


class Teacher(models.Model):
tid = models.AutoField(primary_key=True)
tname = models.CharField(max_length=32)
classes = models.ManyToManyField("Classes")


class Course(models.Model):
cid = models.AutoField(primary_key=True)
cname = models.CharField(max_length=32)
teacher = models.ForeignKey("Teacher", on_delete=models.CASCADE)


Score class (models.Model):
SID = models.AutoField (primary_key = True)
Student = models.ForeignKey ( "Student", on_delete = models.CASCADE)
Course models.ForeignKey = ( 'Course,', on_delete = models.CASCADE)
score = models.IntegerField ()
to copy the code
two insert test data
  1. Grade table (ClassGrade)

INSERT INTO app01_classgrade (gid, gname) VALUES (1, ' Grade');
the INSERT the INTO app01_classgrade (GID, gname) the VALUES (2, 'second grade');
the INSERT the INTO app01_classgrade (GID, gname) the VALUES (. 3, 'three Grade ');
the INSERT the INTO app01_classgrade (GID, gname) the VALUES (. 4,' fourth grade ');
  2. class table (the classes)

Copy the code
INSERT INTO app01_classes (cid, caption, grade_id) VALUES (1, '1 year class', 1);
the INSERT the INTO app01_classes (CID, Caption, grade_id) the VALUES (2, '2 year classes', 1);
INSERT INTO app01_classes (cid, caption, grade_id) VALUES (3, '1 class years', 2);
the INSERT the INTO app01_classes (CID, Caption, grade_id) the VALUES (. 4, '2 years classes', 2);
the INSERT the INTO app01_classes (cid, caption, grade_id) VALUES (5, ' three classes 1',. 3);
the INSERT the INTO app01_classes (CID, Caption, grade_id) the VALUES (. 6, 'three classes 2',. 3);
the INSERT the INTO app01_classes ( cid, caption, grade_id) VALUES ( 7, ' class three 3', 3);
the INSERT the INTO app01_classes (CID, Caption, grade_id) the VALUES (. 8, 'four classes 1', 4);
duplicated code
  3. table teacher (Teacher)

INSERT INTO app01_teacher (tid, tname) VALUES (1, ' bin teacher');
the INSERT the INTO app01_teacher (TID, tname) the VALUES (2, 'any graceful');
the INSERT the INTO app01_teacher (TID, tname) the VALUES (. 3, 'any Bank ');
the INSERT the INTO app01_teacher (TID, tname) the VALUES (. 4,' Yue Buqun ');
the INSERT the INTO app01_teacher (TID, tname) the VALUES (. 5,' Tang ');
  4. curriculum (course,)

INSERT INTO app01_course (cid, cname, teacher_id) VALUES (1, '物理', 3);
INSERT INTO app01_course (cid, cname, teacher_id) VALUES (2, '生物', 1);
INSERT INTO app01_course (cid, cname, teacher_id) VALUES (3, '语文', 5);
INSERT INTO app01_course (cid, cname, teacher_id) VALUES (4, '数学', 4);
INSERT INTO app01_course (cid, cname, teacher_id) VALUES (5, '日语', 1);
INSERT INTO app01_course (cid, cname, teacher_id) VALUES (6, '体育', 2);
  5. 学生表(Student)

Copy the code
INSERT INTO app01_student (sid, sname, classes_id, gender) VALUES (1, ' zhangwuji', 2, 1);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (2, 'Zhao', 1, 0);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 3, 'Linghu',. 3,. 1);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (4, 'any graceful', 4 , 0);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 5, 'Fang Shiyu',. 5,. 1);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 6, 'Zhang', . 6,. 1);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 7, "Wu",. 8,. 1);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 8, "Lu", 7, 1);
INSERT INTO app01_student (sid, sname, classes_id, gender) VALUES (9, ' lotus',. 6, 0);
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (10, 'Ximen', 8, 1) ;
the INSERT the INTO app01_student (SID, sname, classes_id, Gender) the VALUES (. 11, 'Lin', 7, 1);
duplicated code
  6. class office table (TeacherClasses table)

复制代码
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (1, 1, 8);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (2, 1, 7);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (3, 1, 6);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (4, 2, 5);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (5, 3, 4);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (6, 3, 5);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (7, 4, 4);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (8, 5, 3);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (9, 2, 2);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (10, 3, 1);
INSERT INTO app01_teacher_classes (id, teacher_id, classes_id) VALUES (11, 4, 1);
复制代码
  7. 成绩表(Score)

复制代码
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (1, 80, 1, 2);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (2, 60, 1, 3);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (3, 72, 2, 1);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (4, 58, 3, 1);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (5, 91, 4, 5);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (6, 84, 5, 5);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (7, 78, 6, 8);
INSERT INTO app01_score (sid, score, course_id, student_id) VALUES (8, 82, 4, 7);
The INSERT the INTO app01_score (SID, Score, COURSE_ID, the student_id) the VALUES (. 9, 87,. 4,. 6);
the INSERT the INTO app01_score (SID, Score, COURSE_ID, the student_id) the VALUES (10, 57 is, 2,. 3);
the INSERT the INTO app01_score ( SID, Score, COURSE_ID, the student_id) the vALUES (. 11, 68,. 3, 2);
INSERT INTO app01_score (SID, Score, COURSE_ID, the student_id) values (12 is, 73 is, 2, 2);
duplicated code
Third query operation
duplicated code
# 1, create test data itself;
# 2, the total number of student inquiries;
RES = models.Student.objects.count ()
RES = models.Student.objects.aggregate (c = the Count ( 'sid'))

# 3, the query "biological" course and "physical" course grade student who has failed both id and name;
# ideas: first filter out biological or physical achievement of students is greater than 60, then follow the students into groups, statistically greater than 1, that is all student who has failed
res = models.Score.objects.filter (course__cname__in = [ 'biological', 'physical'], score__gt = 60) .values ( "student__sid"). annotate (c = Count ( 'student__sid')). filter (c__gt = 1) .values ( ' student__sid', 'student__sname')

# 4, the number of queries in each grade class, the number of classes before taken up three grades;
# idea: by grade packet statistics for each class grade counts, and then descending to take the first three values
res = models .Classes.objects.values ( 'grade_id') annotate ( c = Count ( 'cid')) order_by ( '- c').. [0: 3]

# 5, the query highest grade point average of the student id and name and grade point average;
# ideas: by student group, to get each student's grade point average, and according to the results sorted out first to
res = models.Score. objects.values ( 'student_id', 'student__sname ') annotate (a = Avg ( 'score')) order_by.. ( '- a') [0]

# 6, query the number of students in each grade;
# ideas: grouped by grade level, statistics on the number of students across the table
res = models.Classes.objects.values ( 'grade_id') annotate (c = Count ( 'student__sid')).

# 7, the query each student's school number, name, grade point average;
# ideas: by student group, the statistics of the average score
res = models.Score.objects.values ( 'student_id', 'student__sname') annotate (a =. Avg ( 'score'))

# 8, the query student number "2" of the student's name, the name of student achievement and curriculum highest score;
# ideas: first by pk value filter out the students, then, subject to the maximum value out of results sorted by
res models.Score.objects.filter = (student__sid = 2) .values ( 'student__sname', 'course__cname', 'Score') ORDER_BY. ( '- Score') [0]
RES = models.Score.objects.filter (student__sid = 2) .values ( 'student__sname' , 'course__cname'). annotate (m = Max ( 'score')) [0]

# 9, the query name "any" of the teacher brought the number of classes;
# ideas: first filtered out teacher surnamed 'any', according to the teacher tenure table, the statistics of the number of classes
res = models.Teacher.objects.filter (tname__startswith = " any ") .values ( 'tname') . annotate (c = Count ( 'classes'))

# 10, the number of classes is smaller than the query id and Grade 3 Grade name;
# ideas: to their grades group, to obtain the number of classes for each grade, then the filter conditions to
res = models.Classes.objects.values ( ' grade_id ',' grade__gname '). annotate (c = Count (' cid ')). filter (c__lt = 5)

# 11, the query taught courses over two teachers of id and name;
# ideas: According to the teacher group, the statistics of the number of courses taught, filtering can
res = models.Course.objects.values ( 'teacher_id', ' teacher__tname '). annotate (c = Count ( "cname")). filter (c__gte = 2)

# 12, the query learned number "1" course and the number "2" students of the course number, name;
# ideas: first filtering out students learned 1 or 2 courses, and then follow the students into groups to give both greater than 1 students have learned.
res = models.Score.objects.filter (course_id__in = [1 , 2]). values ( 'student_id', 'student__sname'). annotate (c = Count ( 'student_id')). filter (c__gt = 1)

# 13, the number of queries brought up to the teacher's class and id name;
# ideas: first by the teachers group, the statistics of the number carried by each class teacher, take the first sorted according to the number brought to class.
. res = models.Teacher.objects.annotate (c = Count ( 'classes')) values ( 'tid', 'tname') order_by. ( '- c') [0]

# 14, there are queries course grade students of less than 60 points number, name;
# ideas: in the results table to filter out students score less than 60, then you can go heavy
res = models.Score.objects.filter (score__lt = 60) .values ( 'student_id' , 'student__sname'). distinct ()

# 15, the number of inquiries into male and female, are arranged in reverse order;
# ideas: Gender press packet, counted, and finally descending order to
res = models.Student.objects.values ( 'gender') annotate (c = Count. ( 'sid')) order_by ( . '- c')

# 16 inquiries each elective courses and the corresponding number;
# ideas: According to the results table, grouped according to the course, you can count the number of electives.
res = models.Score.objects.values ( 'course__cname') . annotate (c = Count ( 'student_id'))

# 17, the query while enrolled in physics and biology classes the student id and name;
# ideas: with 12
RES = models.Score.objects.filter (course__cname__in = [ 'physical', 'bio']) values ( 'student_id. ',' student__sname '). annotate (c = Count (' student_id ')). filter (c__gt = 1)

# 18, the search "3" course fraction is less than 60, No. students learn by score in descending order;
# ideas: students press condition was filtered off, and by score in descending order to
res = models.Score.objects.filter (student_id = 3 , score__lt = 60) .order_by ( ' - score') values ( 'student_id').

# 19, the query grade point average in each course, results in ascending order according to grade point average, grade point average is the same, according to the course number in descending order;
# ideas: according to the course packet, find the average score, you can then sort.
res = models.Score.objects.values ( 'course__cname') . annotate (a = Avg ( 'score')). order_by ( 'a', '-course_id')

# 20, the highest score query subjects and lowest points: Displayed in the following form: Course ID, highest score, lowest score;
# ideas: In the transcript grouped according to the course to calculate the score highest and lowest scores, you can display complete .
res = models.Score.objects.values ( 'course__cname') . annotate (max_num = Max ( 'score'), Min_num = Min ( 'score'))
copying the code

 

Guess you like

Origin www.cnblogs.com/zengxiaowen/p/11838746.html