sql 作业 1-6

This commit is contained in:
flykhan 2023-08-24 21:54:42 +08:00
parent b520c1de82
commit e11f684219
8 changed files with 533 additions and 0 deletions

View File

@ -0,0 +1,109 @@
-- MySQL dump 10.13 Distrib 5.7.33, for Linux (x86_64)
--
-- Host: localhost Database: bookdb
-- ------------------------------------------------------
-- Server version 5.7.33-0ubuntu0.16.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `images`
--
DROP TABLE IF EXISTS `images`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `images` (
`img_id` int(11) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`width` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL,
PRIMARY KEY (`img_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `images`
--
LOCK TABLES `images` WRITE;
/*!40000 ALTER TABLE `images` DISABLE KEYS */;
INSERT INTO `images` VALUES (1,'Image Set 1',800,600),(2,'Image Set 2',1024,768),(3,'Image Set 3',1280,720),(4,'Image Set 4',800,600),(5,'Image Set 5',1024,768),(6,'Image Set 6',1280,720),(7,'Image Set 7',800,600),(8,'Image Set 8',1024,768),(9,'Image Set 9',1280,720),(10,'Image Set 10',800,600);
/*!40000 ALTER TABLE `images` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `login`
--
DROP TABLE IF EXISTS `login`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `login` (
`login_id` int(11) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`auth_string` varchar(50) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`last_time` datetime DEFAULT NULL,
PRIMARY KEY (`login_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `login`
--
LOCK TABLES `login` WRITE;
/*!40000 ALTER TABLE `login` DISABLE KEYS */;
INSERT INTO `login` VALUES (1001,'张三','zspasswd','2021-08-29 12:30:23','2023-06-05 23:10:23'),(1002,'李四','lspasswd','2022-08-29 12:30:23','2023-07-05 21:10:23'),(1003,'王五','wwpasswd','2023-04-29 12:30:23','2023-06-14 23:23:23');
/*!40000 ALTER TABLE `login` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `profile_info`
--
DROP TABLE IF EXISTS `profile_info`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `profile_info` (
`login_id` int(11) NOT NULL,
`real_name` varchar(50) DEFAULT NULL,
`nick_name` varchar(50) DEFAULT NULL COMMENT '昵称',
`sex` char(1) DEFAULT '',
`head` text COMMENT '用户头像图片文件的路径',
`phone` varchar(11) DEFAULT NULL,
PRIMARY KEY (`login_id`),
CONSTRAINT `profile_info_ibfk_1` FOREIGN KEY (`login_id`) REFERENCES `login` (`login_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `profile_info`
--
LOCK TABLES `profile_info` WRITE;
/*!40000 ALTER TABLE `profile_info` DISABLE KEYS */;
INSERT INTO `profile_info` VALUES (1001,'张三','三三','','/path/to/head1.jpg','12345678901'),(1002,'李四','四四','','/path/to/head2.jpg','12345678902'),(1003,'王五','五五','','/path/to/head3.jpg','12345678903');
/*!40000 ALTER TABLE `profile_info` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2023-08-24 20:52:25

29
day9/homework/h1.sql Normal file
View File

@ -0,0 +1,29 @@
/*
bookdbbookdb数据库中创建login登录表 login(login_id , name , passwd create_time , last_time )
*/
-- 删除原有数据库
-- drop database if exists bookdb;
-- 创建数据库
CREATE DATABASE IF NOT EXISTS bookdb;
-- 进入bookdb数据库
USE bookdb;
-- 创建login表
CREATE TABLE IF NOT EXISTS
login(
-- 主键
login_id INT PRIMARY KEY,
name VARCHAR(50),
passwd VARCHAR(50),
create_time DATETIME,
last_time DATETIME
);
-- 显示数据库bookdb结构
SHOW TABLES;
-- 显示表结构
DESCRIBE login;

33
day9/homework/h2.sql Normal file
View File

@ -0,0 +1,33 @@
/*
bookdb库中创建 person
person(login_id real_name , nick_name , sex性别, head , phone )
1. login_id是引用login表的login_id
2. head是存储用户头像图片文件的路径
3. sex 使"default 默认值"
*/
-- 进入 bookdb 数据库
USE bookdb;
-- 创建 person 个人信息表
CREATE TABLE IF NOT EXISTS
person(
-- 主键
login_id INT,
real_name VARCHAR(50),
nick_name VARCHAR(50) COMMENT '昵称',
sex CHAR(1) DEFAULT '',
head TEXT COMMENT '用户头像图片文件的路径',
phone VARCHAR(11),
-- 外键约束
FOREIGN KEY (login_id) REFERENCES login(login_id)
);
-- 显示数据库 bookdb 结构
SHOW TABLES;
-- 显示 person 表结构
DESCRIBE person;

39
day9/homework/h3.sql Normal file
View File

@ -0,0 +1,39 @@
/*
loginperson表中插入3条记录
*/
-- 进入bookdb数据库
use bookdb;
-- 向login表中插入数据
INSERT INTO login
VALUES
(1001, '张三', 'zspasswd', '2021-08-29 12:30:23', '2023-06-05 23:10:23'),
(1002, '李四', 'lspasswd', '2022-08-29 12:30:23', '2023-07-05 21:10:23'),
(1003, '王五', 'wwpasswd', '2023-04-29 12:30:23', '2023-06-14 23:23:23');
-- 向person表中插入数据
INSERT INTO person
VALUES
(1001, '张三', '三三', '', '/path/to/head1.jpg', '12345678901'),
(1002, '李四', '四四', '', '/path/to/head2.jpg', '12345678902'),
(1003, '王五', '五五', '', '/path/to/head3.jpg', '12345678903');
-- 显示login表中数据
SELECT
login_id AS '登录编号',
name AS '登录名',
passwd AS '密码',
create_time AS '注册时间',
last_time AS '最近登录时间'
FROM login;
-- 显示person表中数据
SELECT
login_id AS '登录编号',
real_name AS '真实姓名',
nick_name AS '昵称',
sex AS '性别',
head AS '头像路径',
phone AS '手机号'
FROM person;

16
day9/homework/h4.sql Normal file
View File

@ -0,0 +1,16 @@
/*
login表的passwd的字段名为auth_stringperson表名为profile_info
*/
-- 修改login表的passwd字段名为auth_string
ALTER TABLE login
CHANGE passwd auth_string VARCHAR(50);
-- 将person表重命名为profile_info
RENAME TABLE person TO profile_info;
-- 显示login表结构查看字段修改结果
DESCRIBE login;
-- 显示bookdb库结构查看表重命名结果
SHOW TABLES;

68
day9/homework/h5.sql Normal file
View File

@ -0,0 +1,68 @@
/*
2.
3. images
img_id, title, width, height
10
*/
-- 进入 bookdb 数据库
USE bookdb;
-- 创建个人图片集表 images
CREATE TABLE IF NOT EXISTS
images(
img_id INT PRIMARY KEY,
title VARCHAR(50),
width INT,
height INT,
user_id INT COMMENT '所属用户id',
FOREIGN KEY (user_id) REFERENCES login(login_id)
);
-- 为不同人员添加10条图片集记录
INSERT INTO images (img_id, title, width, height, user_id) VALUES
-- 1001 用户图集
(1, 'Image Set 1', 800, 600, 1001),
(2, 'Image Set 2', 1024, 768, 1001),
(3, 'Image Set 3', 1280, 720, 1001),
(4, 'Image Set 4', 800, 600, 1001),
(5, 'Image Set 5', 1024, 768, 1001),
(6, 'Image Set 6', 1280, 720, 1001),
(7, 'Image Set 7', 800, 600, 1001),
(8, 'Image Set 8', 1024, 768, 1001),
(9, 'Image Set 9', 1280, 720, 1001),
(10, 'Image Set 10', 800, 600, 1001),
-- 1002 用户图集
(11, 'Image Set 11', 800, 600, 1002),
(12, 'Image Set 12', 1024, 768, 1002),
(13, 'Image Set 13', 1280, 720, 1002),
(14, 'Image Set 14', 800, 600, 1002),
(15, 'Image Set 15', 1024, 768, 1002),
(16, 'Image Set 16', 1280, 720, 1002),
(17, 'Image Set 17', 800, 600, 1002),
(18, 'Image Set 18', 1024, 768, 1002),
(19, 'Image Set 19', 1280, 720, 1002),
(20, 'Image Set 20', 800, 600, 1002),
-- 1003 用户图集
(21, 'Image Set 21', 800, 600, 1003),
(22, 'Image Set 22', 1024, 768, 1003),
(23, 'Image Set 23', 1280, 720, 1003),
(24, 'Image Set 24', 800, 600, 1003),
(25, 'Image Set 25', 1024, 768, 1003),
(26, 'Image Set 26', 1280, 720, 1003),
(27, 'Image Set 27', 800, 600, 1003),
(28, 'Image Set 28', 1024, 768, 1003),
(29, 'Image Set 29', 1280, 720, 1003),
(30, 'Image Set 30', 800, 600, 1003);
-- 显示内容
DESCRIBE images;
SELECT
img_id AS '图片编号',
title AS '标题',
width AS '宽度',
height AS '高度',
user_id AS '所属用户id'
FROM images;

22
day9/homework/h6.sql Normal file
View File

@ -0,0 +1,22 @@
/*
NULL
*/
-- 进入 bookdb 数据库
USE bookdb;
-- 更新关联个人信息表数据为NULL
UPDATE profile_info SET login_id = NULL WHERE login_id = 1002;
-- 更新关联图片集表数据为NULL
UPDATE images SET user_id = NULL WHERE user_id = 1002;
-- 删除 login_id 为 1002 的用户
DELETE
FROM login
WHERE login_id = 1002;
-- 显示效果
SELECT * FROM login;
SELECT * FROM profile_info;
SELECT * FROM images;

217
day9/homework/stu.sql Normal file
View File

@ -0,0 +1,217 @@
-- MySQL dump 10.13 Distrib 5.6.25, for Win64 (x86_64)
--
-- Host: localhost Database: stu
-- ------------------------------------------------------
-- Server version 5.6.25-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `a`
--
DROP TABLE IF EXISTS `a`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `a` (
`year` int(11) DEFAULT NULL,
`month` int(11) DEFAULT NULL,
`amount` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `a`
--
LOCK TABLES `a` WRITE;
/*!40000 ALTER TABLE `a` DISABLE KEYS */;
INSERT INTO `a` VALUES (1991,1,1.1),(1991,2,1.2),(1992,1,2.1),(1992,2,2.2);
/*!40000 ALTER TABLE `a` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `course`
--
DROP TABLE IF EXISTS `course`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `course` (
`cid` varchar(10) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`tid` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `course`
--
LOCK TABLES `course` WRITE;
/*!40000 ALTER TABLE `course` DISABLE KEYS */;
INSERT INTO `course` VALUES ('01','语文','02'),('02','数学','01'),('03','英语','03');
/*!40000 ALTER TABLE `course` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `sc`
--
DROP TABLE IF EXISTS `sc`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `sc` (
`sid` varchar(10) DEFAULT NULL,
`cid` varchar(10) DEFAULT NULL,
`score` decimal(18,1) DEFAULT NULL,
KEY `sid_cid_index` (`sid`,`cid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `sc`
--
LOCK TABLES `sc` WRITE;
/*!40000 ALTER TABLE `sc` DISABLE KEYS */;
INSERT INTO `sc` VALUES ('01','01',80.0),('01','02',90.0),('01','03',99.0),('02','01',70.0),('02','02',60.0),('02','03',80.0),('03','01',80.0),('03','02',80.0),('03','03',80.0),('04','01',50.0),('04','02',30.0),('04','03',20.0),('05','01',76.0),('05','02',87.0),('06','01',31.0),('06','03',34.0),('07','02',89.0),('07','03',98.0);
/*!40000 ALTER TABLE `sc` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger watch_sc_delete before delete on sc for each row
insert into sc_copy values (OLD.sid, OLD.cid, OLD.score) */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`sid` varchar(10) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`age` datetime DEFAULT NULL,
`sex` varchar(10) DEFAULT NULL,
`xid` varchar(10) DEFAULT NULL,
UNIQUE KEY `name_unique` (`name`) USING HASH
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `student`
--
LOCK TABLES `student` WRITE;
/*!40000 ALTER TABLE `student` DISABLE KEYS */;
INSERT INTO `student` VALUES ('01','赵雷','1990-01-01 00:00:00','','01'),('02','钱电','1990-12-21 00:00:00','','01'),('03','孙风','1990-05-20 00:00:00','','02'),('04','李云','1990-08-06 00:00:00','','01'),('05','周梅','1991-12-01 00:00:00','','03'),('06','吴兰','1992-03-01 00:00:00','','02'),('07','郑竹','1989-07-01 00:00:00','','03'),('08','王菊','1990-01-20 00:00:00','','04'),('110','disen','1991-10-10 00:00:00','','01');
/*!40000 ALTER TABLE `student` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `teacher`
--
DROP TABLE IF EXISTS `teacher`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `teacher` (
`tid` varchar(10) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `teacher`
--
LOCK TABLES `teacher` WRITE;
/*!40000 ALTER TABLE `teacher` DISABLE KEYS */;
INSERT INTO `teacher` VALUES ('01','张三'),('02','李四'),('03','王五');
/*!40000 ALTER TABLE `teacher` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `update_pwd_log`
--
DROP TABLE IF EXISTS `update_pwd_log`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `update_pwd_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`username` varchar(20) DEFAULT NULL,
`old_pwd` varchar(50) DEFAULT NULL,
`new_pwd` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `update_pwd_log`
--
LOCK TABLES `update_pwd_log` WRITE;
/*!40000 ALTER TABLE `update_pwd_log` DISABLE KEYS */;
INSERT INTO `update_pwd_log` VALUES (1,'2022-03-29 06:31:49','disen','123','666');
/*!40000 ALTER TABLE `update_pwd_log` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `xb`
--
DROP TABLE IF EXISTS `xb`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `xb` (
`xid` varchar(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `xb`
--
LOCK TABLES `xb` WRITE;
/*!40000 ALTER TABLE `xb` DISABLE KEYS */;
INSERT INTO `xb` VALUES ('01','计算机系'),('02','信息系'),('03','英语系'),('04','数学系');
/*!40000 ALTER TABLE `xb` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-04-28 18:44:23