首页  >  文章  >  数据库  >  SQL || MySQL ||作者:穆尼塞卡·乌达瓦拉帕蒂

SQL || MySQL ||作者:穆尼塞卡·乌达瓦拉帕蒂

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-10-13 06:15:30893浏览

SQL || MySQL || By Munisekhar Udavalapati

1.SQL第2部分

1.创建类表

CREATE TABLE class(
    class_id INT PRIMARY KEY,
    class_name VARCHAR(50),
    FOREIGN KEY (teacher_id) REFERENCES teacher(teacher_id)
);

2.创建教师表

CREATE TABLE teacher (
    teacher_id INT PRIMARY KEY,
    teacher_name VARCHAR(100),
    age INT,
    subject VARCHAR(50),
    experience INT
);

3.将教师数据插入表

INSERT INTO teacher(teacher_id,teacher_name,age,subject,experience)
VALUES
(101, 'Sk. Sohana', 30, 'Mathematics', 5),
(102, 'U. Munisekhar', 35, 'English', 8),
(103, 'SK. Nellu', 40, 'Science', 10),
(104, 'A. Venu', 28, 'History', 3);

4.将类数据插入表

INSERT INTO class(class_id,class_name,teacher_id)
(9, 'Math', 101),
(10, 'English', 102),
(11, 'Science', 103),
(12, 'History', 104);

老师桌

teacher_id teacher_name age subject experience
101 Sk. Sohana 30 Mathematics 5
102 U. Munisekhar 35 English 8
103 SK. Nellu 40 Science 10
104 A. Venu 28 History 3
105 S. Jagadeesh 28 Telugu 3

类表

class_id class_name teacher_id
9 Math 101
10 English 102
11 Science 103
12 History 104
  1. 从Class表中获取数据
SELECT * FROM class;
| class_id | class_name         | teacher_id |
|----------|--------------------|------------|
| 9        | Math               | 101        |
| 10       | English            | 102        |
| 11       | Science            | 103        |
| 12       | History            | 104        |

  1. 从教师表中获取数据 5年经验教师
SELECT * FROM teacher WHARE experience >5
| teacher_id | teacher_name       | age | subject       | experience |
|------------|--------------------|-----|---------------|------------|
| 102        | U. Munisekhar      | 35  | English       | 8          |
| 103        | SK. Nellu          | 40  | Science       | 10         |

7.查找Munisekhar老师详细信息

SELECT * FROM teacher WHERE teacher_name='U. Munisekhar'
| teacher_id | teacher_name       | age | subject       | experience |
|------------|--------------------|-----|---------------|------------|
| 102        | U. Munisekhar      | 35  | English       | 8          |

8.找到 Sk. Sohana老师的经验?

SELECT experience FROM teacher WHERE teacher_name='Sk. Sohana';
| experience |
|------------|
|     8      |

9.查找老师的姓名和年龄,其中年龄为 29 至 39

SELECT name,age FROM teacher WHERE age BETWEEN 29 AND 39;
| teacher_name       | age |
|--------------------|-----|
| Sk. Sohana         | 30  | 
| U. Munisekhar      | 35  | 

10.查找班级名称和老师姓名以使用左连接

SELECT class.class_name, teacher.teacher_name
FROM class
RIGHT JOIN teacher ON class.teacher_id=teacher.teacher_id;
| class_name | teacher_name       |
|------------|--------------------|
| Math       | Sk. Sohana         |
| English    | U. Munisekhar      |
| Science    | SK. Nellu          |
| History    | A. Venu            |

11.查找班级名称和所有教师姓名以使用右连接

SELECT class.class_name, teacher.teacher_name
FROM class
RIGHT JOIN teacher ON class.teacher_id=teacher.teacher_id;
| class_name | teacher_name       |
|------------|--------------------|
| Math       | Sk. Sohana         |
| English    | U. Munisekhar      |
| Science    | SK. Nellu          |
| History    | A. Venu            |
| NULL       | S. Jagadeesh       |

12.查找班级名称和教师姓名以使用内连接

SELECT class.class_name, teacher.teacher_name
FROM class
INNER JOIN teacher ON class.teacher_id=teacher.teacher_id;
| class_name | teacher_name       |
|------------|--------------------|
| Math       | Sk. Sohana         |
| English    | U. Munisekhar      |
| Science    | SK. Nellu          |
| History    | A. Venu            |

13.查找munisekhar班级显示他的姓名和班级

SELECT teacher.teacher.name, class.class_name
FROM teacher 
RIGHT JOIN class ON teacher.teacher_id=class.teacher_id
WHERE teacher.teacher_name = 'U. Munisekhar';
| teacher_name       | class_name |
|--------------------|------------|
| U. Munisekhar      | English    |

以上是SQL || MySQL ||作者:穆尼塞卡·乌达瓦拉帕蒂的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn