sql basic operation in mysql

Database Environment: Windows 10

Database Version: Mysql 5.7

First, please write the following query and manipulate
1, link database
mysql -uroot -p

2, display database version
mysql --version;

3, view all current database
show databases;

4, create a demo database, and set the character set is utf8
the CREATE DATABASE IF the NOT EXISTS demo the DEFAULT CHARACTER the DEFAULT the SET utf8 COLLATE utf8_general_ci;

5, choosing the database demo
use demo;

6, the display time
select now ();


Second, create in the demo database the following data in Table
1, to create student table Student, and enter the corresponding data
Create Table IF Not EXISTS Student (
  SID int unsigned Primary Key AUTO_INCREMENT,
  name VARCHAR (40) Not null,
  Gender VARCHAR (10) Not null ,
  class_id unsigned int Not null
);
INSERT INTO Student values ( '. 1', 'John Doe', 'F', '. 1');
INSERT INTO Student values ( '2', 'John Doe', 'F', ' . 4 ');
INSERT INTO Student values (' 3 ',' Wang Wu ',' M ',' 2 ');
INSERT INTO Student values ('. 4 ',' Zhao six ',' F ',' 3 ');
insert into student values ( '5' , ' pseudo-ginseng', 'F', '. 5');
INSERT INTO Student values ( '. 6', 'northern', 'M', '10');
insert into student values ( '7' , ' Qi eight', 'female', '8');
INSERT INTO Student values ( '8', 'Wei nine', 'female', '9');
INSERT INTO Student values ( '9', 'old stone', 'M', '7');
insert into student values ('10 ', ' money power ',' female ',' 6 ');
INSERT INTO Student values ('11', 'John Doe', 'male', 11 ');

2, creating teacher table Teacher, and enter the corresponding data
Create Table IF Not EXISTS Teacher (
  TID int unsigned Primary Key AUTO_INCREMENT,
  tname VARCHAR (40) Not null
);
INSERT INTO Teacher values ( '. 1', 'Zhao');
INSERT into teacher values ( '2', ' money teacher');
INSERT INTO teacher values ( '. 3', 'Sun teacher');

3, create a curriculum Course, and enter the corresponding data
Create Table IF Not EXISTS Course (
  CID int unsigned Primary Key AUTO_INCREMENT,
  CNAME VARCHAR (40) Not null,
  teacher_id int unsigned Not null
);
INSERT INTO Course values ( '. 1', ' biological ''. 1 ');
INSERT INTO Course values (' 2 ',' Sport ',' 2 ');
INSERT INTO Course values (' 3 ',' physical ',' 3 ');

4、创建成绩表score,并输入对应数据
create table if not exists score(
  student_id int unsigned not null,
  course_id int unsigned not null,
  number int unsigned not null
);
insert into score values('1','1','80');
insert into score values('1','2','90');
insert into score values('1','3','99');
insert into score values('2','1','70');
insert into score values('2','2','60');
insert into score values('2','3','80');
insert into score values('3','1','80');
insert into score values('3','2','80');
insert into score values('3','3','75');
insert into score values('4','1','50');
insert into score values('4','2','30');
insert into score values('4','3','20');
insert into score values('5','1','76');
insert into score values('5','2','87');
insert into score values('6','1','31');
insert into score values('6','3','34');
insert into score values('7','2','89');
insert into score values('7','3','98');

5, to see all the tables in the current database
show tables;

6, to query all the instructor's name
select tname from teacher;

7, each sex query class how many people
the SELECT Gender, COUNT (Gender) AS genderNum from Student
Group by Gender;

8, the query "biological" course higher than the "physical" course grade all students learn numbers
the SELECT A.student_id, Shengwu, wuli from (
  the SELECT student_id, from Score Number The AS Shengwu
  left the Join Course,
  ON score.course_id = course.cid
  the WHERE course.cname = 'biological') AS A
left the Join (
  SELECT the student_id, AS Number Score from wuli
  left the Join Course
  ON score.course_id = course.cid
  WHERE course.cname = 'physical') AS B
ON A.student_id = B. the student_id
WHERE Shengwu> IF (ISNULL (wuli), 0, wuli);

9, the average score of the query is greater than 60 minutes and the number of school students GPA
SELECT the student_id, round (AVG (Number), 2) from AS avgScore Score
Group by the student_id
HAVING avgScore> 60
Order by avgScore desc;

10, all students of inquiry number, name, number of elective total score; statement evolutionary process
Step one: find a table where the target
number, name, exists in the student
number elective total score, exists in the score

Step two: two tables created inline
SELECT * from Student AS ST
Inner the Join Score SC AS
ON st.sid = sc.student_id;

Step three: Filter the display data on demand
SELECT st.sid, st.name, COUNT (sc.course_id), SUM (sc.number) from Student AS ST
Inner the Join Score SC AS
ON st.sid = sc.student_id
Group by ST .sid;

11, the query name "Zhao" teacher number
query teacher table name "Zhao" teacher number
SELECT COUNT (*) from teacher
WHERE tname like 'Zhao%';

Query score table, select the name "Zhao" teacher number
SELECT COUNT (*) AS S score from
Inner Teacher the Join AS T
ON s.course_id = t.tid
WHERE t.tname = 'Zhao';

12, never learned query money teacher class student school number, name
the SELECT sid, Student name from
the WHERE sid not in (
  the SELECT student_id Score from
  the WHERE COURSE_ID in (
    the SELECT c.cid Course, AS c from
    Inner Teacher the Join AS t
    ON c = t.tid .teacher_id
    the WHERE t.tname = 'money teachers'
  )
);

13, learned inquiry "1" and I have learned a number "2" students of the course number, name
the SELECT st.sid, st.name from Student AS ST
Inner Score the Join AS sc
ON st.sid = sc.student_id
the WHERE in sc.course_id (1,2)
Group by st.sid, st.name;

14, all lessons learned inquiry Sun teacher taught the students the school, name
the SELECT sid, Student name from
the WHERE sid in (
  the SELECT student_id Score from
  the WHERE COURSE_ID in (
    the SELECT c.cid Course, AS c from
    Inner Teacher AS the Join t
    t.tid c.teacher_id = ON
    WHERE t.tname = 'Mr. Sun'
  )
);

15, details of the curriculum number "2" results than the course number "1" low course all students learn, name
the SELECT sid, Student name from
the WHERE sid in (
  the SELECT A.student_id from (
    the SELECT * Score from
    the WHERE COURSE_ID = ' 2 ') AS A
  Inner the Join (
    SELECT * from Score
    WHERE COURSE_ID ='. 1 ') AS B
  ON A.student_id = B.student_id
  WHERE A.number <B.number
);

16 inquiries have less than 60 points of the course grade students number, name
the SELECT st.sid, st.name from Student AS ST
Inner Score the Join AS sc
ON st.sid = sc.student_id
the WHERE sc.number <60
Group by ST .sid, st.name;

17, the query did not learn the lesson of all the students of the whole number, name
the SELECT st.sid, st.name from Student AS ST
Inner Score the Join AS sc
ON st.sid = sc.student_id
Group by sc.student_id
the HAVING COUNT (sc. COURSE_ID) <(
  SELECT COUNT (*) from Course
);

18, exactly the same query and "2" of students learning courses other students learn number and name
the SELECT * Student from
the WHERE sid in (
  the SELECT score.student_id Score from
  the Join (
    the SELECT * Score from
    the WHERE student_id = 2) AS sc
  ON = score.course_id sc.course_id
  ! = score.student_id and sc.student_id
  Group by score.student_id
  HAVING COUNT (*) = (
    SELECT COUNT (*) Score from
    WHERE the student_id = 2
  )
);

19, delete the class teacher to learn Sun SC table records
the Delete from Score
the WHERE COURSE_ID in (
  the SELECT c.cid Course, AS c from
  Inner Teacher the Join AS t
  ON c.teacher_id = t.tid
  the WHERE t.tname = 'Teacher Sun'
);

Guess you like

Origin www.cnblogs.com/rui0513/p/11221906.html