mysql take notes

Keep learning, keep updating

1. Add a student record

This topic requires writing an Insert statement and adding a student record in the stu table: Student ID: S012, Name: , Zhou Qiang, Gender: 1, and other attributes are NULL.

Table Structure:

CREATE TABLE `stu` (
  `sno` char(4) NOT NULL,
  `sname` char(8) NOT NULL,
  `sex` tinyint(1) DEFAULT NULL,
  `mno` char(2) DEFAULT NULL,
  `birdate` datetime DEFAULT NULL,
  `memo` text,
  PRIMARY KEY (`sno`)
);

Table sample (stu table):
insert image description hereoutput sample (stu table):
insert image description here
problem solution:

 INSERT INTO stu (sno,sname,sex) VALUES ('S012','周强',1);

2. Query the top two students with the highest grades in the course

This topic requires writing SQL statements to retrieve the number, name and grade of the two students with the highest grades in the C002 course

Table Structure:

CREATE TABLE `stu` (
  `sno` char(4) NOT NULL,
  `sname` char(8) NOT NULL,
  `sex` tinyint(1) DEFAULT NULL,
  `mno` char(2) DEFAULT NULL,
  `birdate` datetime DEFAULT NULL,
  `memo` text,
  PRIMARY KEY (`sno`),
);

CREATE TABLE `sc` (
  `sno` char(4) NOT NULL,
  `cno` char(4) NOT NULL,
  `grade` decimal(6,1) DEFAULT NULL,
  PRIMARY KEY (`sno`,`cno`),
  CONSTRAINT `fk_sc_sno` FOREIGN KEY (`sno`) REFERENCES `stu` (`sno`)
);

Table sample (stu table and sc table):
insert image description hereinsert image description here
output sample:
insert image description here
problem solution:

SELECT stu.sno,stu.sname,sc.grade
FROM stu LEFT JOIN sc 
ON stu.sno=sc.sno
WHERE sc.cno='C002'
ORDER BY sc.grade 
DESC LIMIT 2;

3. Delete all student information

This topic requires writing a DELETE statement to delete all the data information of the student whose name is 'Zhou Qiang'.

Table Structure:

CREATE TABLE `stu` (
  `sno` char(4) NOT NULL,
  `sname` char(8) NOT NULL,
  `sex` tinyint(1) DEFAULT NULL,
  `mno` char(2) DEFAULT NULL,
  `birdate` datetime DEFAULT NULL,
  `memo` text,
  PRIMARY KEY (`sno`)
);
CREATE TABLE `sc` (
  `sno` char(4) NOT NULL,
  `cno` char(4) NOT NULL,
  `grade` decimal(6,1) DEFAULT NULL,
  PRIMARY KEY (`sno`,`cno`),
  CONSTRAINT `fk_sc_sno` FOREIGN KEY (`sno`) REFERENCES `stu` (`sno`)
);

Table example (stu table and sc table):
insert image description hereinsert image description here

Sample output:
insert image description hereinsert image description here
problem solution:

DELETE t1,t2
FROM stu AS t1 LEFT JOIN sc AS t2
ON t1.sno = t2.sno
WHERE t1.sname = '周强';

4. Sort in ascending order

Query the number, name and entry date of all male employees in the employee table, the results are sorted in ascending order by employee number

Table Structure:

create table 员工(员工编号 char(3) primary key,姓名 char(5),性别 char(1),出生日期 date,入职日期 date,电话 char(11))

Table sample example:
insert image description here

Sample output:
insert image description here
problem solution:

SELECT 员工编号,姓名,入职日期
FROM 员工
WHERE 性别='男'
ORDER BY 员工编号 ASC;

5. Conditional query

This topic requires writing a SQL statement to retrieve the records of the student numbers and names of all girls who are less than or equal to 25 years old in the students table.

Table Structure:

CREATE TABLE students (
 sno char(7) ,
  sname char(8) NOT NULL,
  class char(10),
  ssex char(2),
  bday date ,
  bplace char(10) ,
  IDNum char(18) ,
  sdept char(16),
  phone char(11),
  PRIMARY KEY (sno)
) ;

Table sample:
insert image description hereoutput sample:
insert image description here
problem solution:

select sno as "学号",sname as "姓名",ssex as "性别"
from students 
where ROUND(DATEDIFF(CURDATE(), bday)/365.2422) <=25 
and 
ssex="女";

Inquire

This topic requires writing SQL statements to retrieve the course name, prerequisite course number and credit record of the "0000010" course in the course table.
Hint: Please use the SELECT statement to answer.

Table Structure:

CREATE TABLE course (
 cno char(7) ,
 cname varchar(20) NOT NULL,
  cpno char(7),
  ccredit int NOT NULL,
  PRIMARY KEY (cno)
) ;

Table sample:
insert image description here
output sample:
insert image description here
problem solution:

select cname,cpno,ccredit from course where cno = '0000010';

Guess you like

Origin blog.csdn.net/qq_50216270/article/details/117170615