MySQL "Functions"

drop database if exists StudentManage;
create database StudentManage;

use StudentManage;

create table Student
(Sno int primary key,
Sname nchar(10) ,
Ssex nchar(2),
Sage int,
Sdept nvarchar(30)
)charset utf8;

create table Course
(Cno int primary key,
Cname nvarchar(30),
Cpno int,
Ccredit int
)charset utf8;

create table SC
(Sno int,
Cno int,
Grade int,
primary key(Sno,Cno)
)charset utf8;

insert into Student
values(201215121,'李勇','男',20,'CS'),
(201215122,'刘晨','女',19,'CS'),
(201215123,'王敏','女',18,'MA'),
(201215124,'张立','男',19,'IS');


insert into Course
values(1,'数据库',5,4),(2,'数学',NULL,2),
(3,'信息系统',1,4),(4,'操作系统',6,3),
(5,'数据结构',7,4),(6,'数据处理',NULL,2),
(7,'PASCAL',6,4);

insert into SC 
values(201215121,1,92),(201215121,2,85),
(201215121,3,88),(201215122,2,90),(201215122,3,80);

(1) Find an integer not greater than -1.23. ①Sentence; ②Result.

The first empty:
SELECT FLOOR(-1.23);

Second empty:
-2

(2) Find the ascii code value of the character'd'. ①Sentence; ②Result.

The first empty:
SELECT ASCII('d');

Second empty:
100

(3) Concatenate the three strings "My", "S", and "QL". ①Sentence; ②Result.

The first empty:
SELECT CONCAT('My','S','QL');

Second empty:
MySQL

(4) Find out the length of the string "Text". ①Sentence; ②Result.
The first empty:
SELECT CHAR_LENGTH('Text');

Second empty:
4

(5) Find the first occurrence of the string'bar' in the string'foobarbar'. ①Sentence; ②Result.
The first empty:
SELECT INSTR('foobarbar','bar'); SELECT LOCATE('bar','foobarbar'); SELECT POSITION('bar' IN'foobarbar');

Second empty:
4

(6) Find the rightmost five characters of the string'foobarbar'. ①Sentence; ②Result.
correct answer:

The first empty:
SELECT RIGHT('foobarbar',5); SELECT SUBSTR('foobarbar',-5);

Second empty:
arbar

(7) Replace the'w' in the string'www.mysql.com' with'Ww'. ①Sentence; ②Result.

The first empty:
SELECT REPLACE('www.mysql.com','w','Ww');

Second empty:
WwWwWw.mysql.com

(8) The current date of the acquisition system is the first few days of the year. (The same function as NOW() uses NOW()) ① statement.
correct answer:

The first empty:
SELECT DAYOFYEAR( NOW());

(9) Get the date after 100 days. (The same function as NOW() uses NOW()) ① statement.

select curdate()+interval 100 day;

(10) Get the version number of the database. ① Statement.

select version();

(11) Perform md5 encryption on the string "abcd". ①Sentence; ②Result.
correct answer:

The first empty:
SELECT md5('abcd');

Second empty:
e2fc714c4727ee9395f324cd2e7f331f

Define a function myselect, which returns an integer 666; and call the function. ①Define; ②Call.

第一空:
create function myselect()
return int
BEGIN
return 666;
end

The second empty:
select myselect();

Define a function myselect2, the function of which is to find the student ID of "Liu Chen"; and call this function. ①Define; ②Call.

第一空:
create function myselect2() returns int
begin
return(select sno from student where sname=‘刘晨’);
end

The second empty:
select myselect2();

Define a function myselect3. The function of this function is to find out the student's student ID according to the student's name (the parameter name uses name).

CREATE FUNCTION myselect3 (name VARCHAR(30)) RETURNS INT
BEGIN
RETURN (SELECT sno FROM student WHERE sname = name)
END;

Call the function myselect3 to query the student ID of "Li Yong". ①Invoke the command; ②Result.

The first empty:
SELECT myselect3('Li Yong');

Second empty:
201215121

Define a function stu_count to query the number of elective courses of the student based on the student's name. (Use name for the parameter name)

CREATE FUNCTION stu_count (name VARCHAR(30) charset utf8) RETURNS INT
BEGIN
RETURN ( SELECT COUNT(*) FROM student s
JOIN SC ON s.sno = sc.sno
WHERE s.sname = name )
END;

Call the function stu_count to query the number of courses taken by "Li Yong" respectively. ①Invoke the command; ②Result.
The first empty:
SELECT stu_count('Li Yong');

Second empty:
3

Define a function sdept_avggrade that queries the average score of the students in the course based on the department name and course name.

CREATE FUNCTION sdept_avggrade (p_sdept VARCHAR(30) charset utf8,p_cname VARCHAR(30) charset utf8) RETURNS DECIMAL(6,2)
BEGIN
RETURN ( SELECT AVG(SC.grade) FROM student s
JOIN sc ON s.sno = sc.sno
JOIN course c ON sc.cno = c.cno
WHERE s.sdept = p_sdept
AND c.cname = p_cname )
END;

Call the function sdept_avggrade to query the average grade of the "CS" department and the "mathematics" course. ①Invoke the command; ②Result.
The first empty:
SELECT sdept_avggrade('CS','Mathematics');

Second empty:
87.5

Use the SQL statement to delete the sdept_avggrade function.

DROP FUNCTION IF EXISTS sdept_avggrade;

Guess you like

Origin blog.csdn.net/ziyue13/article/details/112075707