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 |
SELECT * FROM class;
| class_id | class_name | teacher_id | |----------|--------------------|------------| | 9 | Math | 101 | | 10 | English | 102 | | 11 | Science | 103 | | 12 | History | 104 |
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.ムニセカール先生の詳細を見つける
SELECT * FROM teacher WHERE teacher_name='U. Munisekhar'
| teacher_id | teacher_name | age | subject | experience | |------------|--------------------|-----|---------------|------------| | 102 | U. Munisekhar | 35 | English | 8 |
8.Skを見つける。ソハナ先生の経験は?
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 中国語 Web サイトの他の関連記事を参照してください。