mysql
向 students 表添加数据:
INSERT INTO `students` (`S_name`, `S_id`, `Dept_id`, `S_age`, `S_gender`) VALUES
('黎旭瑶', 1114701, 0, 20, '女'),
('张悦', 1113080, 0, 19, '女'),
-- ... 其他数据
('徐林凡', 1114118, 0, 18, '男');
向 books 表添加数据:
INSERT INTO `books` (`isbn`, `bookname`, `press`, `price`, `version`, `author`) VALUES
(9345532332325, '程序设计基础', '科学出版社', 28, '2013-05-12', '何力'),
(9347234498337, '数据库技术', '时代出版社', 28.6, '2013-04-02', '李克'),
-- ... 其他数据
(9847453433422, '普通物理学', '教育出版社', 27.4, '2012-05-12', '张力');
向 score 表添加数据:
INSERT INTO `score` (`S_id`, `C_id`, `score`, `c_name`) VALUES
(1114629, 1, 65, '大学英语'),
(1114629, 2, 70, '计算机基础'),
-- ... 其他数据
(1115102, 4, 77, '普通物理学');
为 score 表中 s_id 设置外键关联 student 表的 s_id :
ALTER TABLE `score`
ADD CONSTRAINT `fk_student_id` FOREIGN KEY (`S_id`) REFERENCES `students` (`S_id`);
显示当前日期和时间:
SELECT NOW();
重命名 students 表中 s_age 字段为 age 并修改类型:
ALTER TABLE `students`
CHANGE COLUMN `S_age` `age` smallint(4) NOT NULL;
为 books 表添加‘备注’列:
ALTER TABLE `books`
ADD COLUMN `备注` varchar(30) DEFAULT NULL;
删除 books 表中‘备注’列:
ALTER TABLE `books`
DROP COLUMN `备注`;
向 books 表中增加记录:
INSERT INTO `books` (`isbn`, `bookname`, `press`, `price`, `version`, `author`) VALUES
(978756859517, '数据库技术_与应用', '高等教育出版社', 46.8, '2023-01-02', '高寒');
更新 books 表中出版社名称:
UPDATE `books` SET `press` = '教育出版社' WHERE `press` = '高等教育出版社';
查询 students 、 books 、 score 中记录的总数:
SELECT COUNT(*) FROM `students`;
SELECT COUNT(*) FROM `books`;
SELECT COUNT(*) FROM `score`;
查询 students 表中所有学生的学号和性别:
SELECT `S_id` AS '学号', `S_gender` AS '性别' FROM `students` ORDER BY `S_id`;
查询 student 表中学号第五位是‘5’的学生人数:
SELECT COUNT(*) FROM `students` WHERE `S_id` LIKE '%5%';
查询学号后三位为“102”的学生的最高成绩:
SELECT MAX(`score`) FROM `score` WHERE `S_id` LIKE '%102';
查询 students 表中学号里不以‘1114’开头的学生信息:
SELECT * FROM `students` WHERE `S_id` NOT LIKE '1114%';
查询 books 表中价格大于20元且出版社不是科学出版社的书籍名称和价格:
SELECT `bookname`, `price` FROM `books` WHERE `price` > 20 AND `press` != '科学出版社' ORDER BY `price` DESC;
查询 books 表中出版社的数量:
SELECT COUNT(DISTINCT `press`) FROM `books`;
统计 books 中不同出版社书籍的数量:
SELECT `press`, COUNT(*) AS `数量` FROM `books` GROUP BY `press`;
删除 books 表中含有下划线的书籍记录:
DELETE FROM `books` WHERE `bookname` LIKE '%_%';
将 score 表重命名为‘成绩登记表’:
RENAME TABLE `score` TO `成绩登记表`;
查询选修‘计算机基础’课程且成绩大于70的学生的学号、姓名、计算机基础的成绩:
SELECT `students`.`S_id`, `students`.`S_name`, `score`.`score`
FROM `students`
JOIN `成绩登记表` ON `students`.`S_id` = `成绩登记表`.`S_id`
WHERE `成绩登记表`.`c_name` = '计算机基础' AND `成绩登记表`.`score` > 70;
为 books 表的‘ISBN’字段创建唯一索引:
CREATE UNIQUE INDEX `index_n` ON `books` (`isbn`);
为 students 表的 s_id 和 s_name 字段创建联合索引:
CREATE INDEX `index_x` ON `students` (`s_id`, `s_name`);
创建视图:
CREATE VIEW `view_books` AS
SELECT `bookname` AS '书名', `price` AS '价格' FROM `books`;
CREATE VIEW `view_students_scores` AS
SELECT `students`.`S_name` AS '姓名', `score`.`score` AS '成绩', `score`.`c_name` AS '课程'
FROM `students`
JOIN `成绩登记表` ON `students`.`S_id` = `成绩登记表`.`S_id`;
创建存储过程:
DELIMITER //
CREATE PROCEDURE `count_press`()
BEGIN
SELECT COUNT(DISTINCT `press`) FROM `books`;
END //
DELIMITER ;
创建存储函数:
DELIMITER //
CREATE FUNCTION `count_stu`()
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM `students` WHERE `age` > 18);
END //
DELIMITER ;
调用存储过程和函数:
CALL `count_press`();
SELECT `count_stu`();
删除所有索引和视图:
DROP INDEX `index_n` ON `books`;
DROP INDEX `index_x` ON `students`;
DROP VIEW `view_books`;
DROP VIEW `view_students_scores`;