diff --git a/day9/backup.sql b/day9/backup.sql new file mode 100644 index 0000000..9f33021 --- /dev/null +++ b/day9/backup.sql @@ -0,0 +1,171 @@ +-- MySQL dump 10.13 Distrib 5.7.33, for Linux (x86_64) +-- +-- Host: localhost Database: studb +-- ------------------------------------------------------ +-- 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 `t_course` +-- + +DROP TABLE IF EXISTS `t_course`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_course` ( + `cid` int(11) DEFAULT NULL, + `name` varchar(50) DEFAULT NULL COMMENT '课程名', + `tid` int(11) DEFAULT NULL COMMENT '教师编号' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_course` +-- + +LOCK TABLES `t_course` WRITE; +/*!40000 ALTER TABLE `t_course` DISABLE KEYS */; +/*!40000 ALTER TABLE `t_course` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `t_emp` +-- + +DROP TABLE IF EXISTS `t_emp`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_emp` ( + `emp_id` int(11) DEFAULT NULL, + `name` varchar(50) DEFAULT NULL, + `tel` char(11) DEFAULT NULL, + `salary` decimal(10,2) DEFAULT NULL, + `hire_date` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_emp` +-- + +LOCK TABLES `t_emp` WRITE; +/*!40000 ALTER TABLE `t_emp` DISABLE KEYS */; +INSERT INTO `t_emp` VALUES (1,'jack','17189792205',13000.00,'2021-10-12'),(2,'lucy','17178982206',3200.00,'2022-11-15'),(3,'disen','17178979385',23000.00,'2022-10-12'); +/*!40000 ALTER TABLE `t_emp` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `t_score` +-- + +DROP TABLE IF EXISTS `t_score`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_score` ( + `sid` int(11) DEFAULT NULL, + `cid` int(11) DEFAULT NULL, + `score` decimal(5,2) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_score` +-- + +LOCK TABLES `t_score` WRITE; +/*!40000 ALTER TABLE `t_score` DISABLE KEYS */; +/*!40000 ALTER TABLE `t_score` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `t_stu` +-- + +DROP TABLE IF EXISTS `t_stu`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_stu` ( + `sid` int(11) DEFAULT NULL, + `name` varchar(20) DEFAULT NULL, + `sex` varchar(2) DEFAULT NULL, + `birthday` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_stu` +-- + +LOCK TABLES `t_stu` WRITE; +/*!40000 ALTER TABLE `t_stu` DISABLE KEYS */; +INSERT INTO `t_stu` VALUES (1,'张三','女','1990-12-28'),(2,'王龙','男','1991-01-07'),(3,'刘冬冬','女','1992-08-21'),(4,'刘红','男','1998-02-15'),(5,'王玉玉','女',NULL); +/*!40000 ALTER TABLE `t_stu` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `t_stu3` +-- + +DROP TABLE IF EXISTS `t_stu3`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_stu3` ( + `sid` int(11) DEFAULT NULL, + `name` varchar(20) DEFAULT NULL, + `sex` varchar(2) DEFAULT NULL, + `birthday` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_stu3` +-- + +LOCK TABLES `t_stu3` WRITE; +/*!40000 ALTER TABLE `t_stu3` DISABLE KEYS */; +INSERT INTO `t_stu3` VALUES (1,'张三','女','1990-12-28'),(3,'刘冬冬','女','1992-08-21'),(5,'王玉玉','女',NULL); +/*!40000 ALTER TABLE `t_stu3` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `t_tch` +-- + +DROP TABLE IF EXISTS `t_tch`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `t_tch` ( + `tid` int(11) DEFAULT NULL, + `name` varchar(50) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `t_tch` +-- + +LOCK TABLES `t_tch` WRITE; +/*!40000 ALTER TABLE `t_tch` DISABLE KEYS */; +/*!40000 ALTER TABLE `t_tch` 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 17:01:50 diff --git a/day9/create_course.sql b/day9/create_course.sql new file mode 100644 index 0000000..062c453 --- /dev/null +++ b/day9/create_course.sql @@ -0,0 +1,6 @@ +-- comment 表示字段的说明 (备份) +create table t_course( + cid int, + name varchar(50) comment '课程名', + tid int comment '教师编号' +); diff --git a/day9/create_employee.sql b/day9/create_employee.sql new file mode 100644 index 0000000..63f92cb --- /dev/null +++ b/day9/create_employee.sql @@ -0,0 +1,10 @@ +use studb; + +-- 员工表 +create table t_emp( + emp_id int, + name varchar(50), + tel char(11), + salary decimal(10,2), + hire_date date +); diff --git a/day9/create_score.sql b/day9/create_score.sql new file mode 100644 index 0000000..ee6a32a --- /dev/null +++ b/day9/create_score.sql @@ -0,0 +1,5 @@ +create table t_score( + sid int, + cid int, + score decimal(5,2) +); diff --git a/day9/create_stu.sql b/day9/create_stu.sql new file mode 100644 index 0000000..1f89058 --- /dev/null +++ b/day9/create_stu.sql @@ -0,0 +1,7 @@ +-- sql comment +create table t_student( + sid int, + name varchar(20), + sex varchar(2), + birthday date +); diff --git a/day9/create_teacher.sql b/day9/create_teacher.sql new file mode 100644 index 0000000..dadbe88 --- /dev/null +++ b/day9/create_teacher.sql @@ -0,0 +1,4 @@ +create table t_teacher( + tid integer, + name varchar(50) +); diff --git a/day9/delete_employee.sql b/day9/delete_employee.sql new file mode 100644 index 0000000..594957e --- /dev/null +++ b/day9/delete_employee.sql @@ -0,0 +1,4 @@ +use studb; + +delete from t_emp where hire_date > '2022-12-31'; +select * from t_emp; diff --git a/day9/delete_students.sql b/day9/delete_students.sql new file mode 100644 index 0000000..14b23f8 --- /dev/null +++ b/day9/delete_students.sql @@ -0,0 +1,8 @@ +-- 删除测试 +use studb; + +create table if not exists t_stu3 +select * from t_stu; + +delete from t_stu3 +where sex='男'; diff --git a/day9/insert_employee.sql b/day9/insert_employee.sql new file mode 100644 index 0000000..a6305f6 --- /dev/null +++ b/day9/insert_employee.sql @@ -0,0 +1,10 @@ +use studb; + +insert into t_emp values +(1,'jack','17189792205',12000,'2021-10-12'), +(2,'lucy','17178982206',2200,'2022-11-15'), +(3,'disen','17178979385',22000,'2022-10-12'), +(4,'mack','17178982209',5000,'2023-08-15'), +(5,'judy','17178982105',9000,'2023-08-20'); + +select * from t_emp; diff --git a/day9/insert_students.sql b/day9/insert_students.sql new file mode 100644 index 0000000..3d5678f --- /dev/null +++ b/day9/insert_students.sql @@ -0,0 +1,12 @@ +-- 单条数据插入 +insert into t_stu values(1,'张三','男','1991-01-15'); + +-- 按照指定格式插入 +insert into t_stu(sid,name,birthday,sex) + values(2,'王龙','1990-12-18','男'); + +-- 元组插入 +insert into t_stu values +(3,'刘冬冬','女','1992-08-21'), +(4,'刘红','女','1992-05-19'), +(5,'王玉玉','女','1991-08-12'); diff --git a/day9/search_employee.sql b/day9/search_employee.sql new file mode 100644 index 0000000..f8dbfa4 --- /dev/null +++ b/day9/search_employee.sql @@ -0,0 +1,3 @@ +use studb; + +select * from t_emp where salary>5000; diff --git a/day9/search_students.sql b/day9/search_students.sql new file mode 100644 index 0000000..810053e --- /dev/null +++ b/day9/search_students.sql @@ -0,0 +1,5 @@ +-- 查询测试 +-- select * from t_stu where name like '刘'; +select * from t_stu where name like '刘%'; + +select * from t_stu where birthday like '1991%'; diff --git a/day9/update_employee.sql b/day9/update_employee.sql new file mode 100644 index 0000000..2eb4332 --- /dev/null +++ b/day9/update_employee.sql @@ -0,0 +1,10 @@ +use studb; + +update t_emp +set salary = salary + 1000; + +update t_emp +set salary = salary * 1.15 +where tel like '%8979$'; + +select * from t_emp; diff --git a/day9/update_students.sql b/day9/update_students.sql new file mode 100644 index 0000000..7d89eb3 --- /dev/null +++ b/day9/update_students.sql @@ -0,0 +1,20 @@ +-- 修改测试 +update t_stu +set sex='男',birthday='1998-02-15' +where name = '刘红'; + +update t_stu +set sex='女',birthday='1990-12-28' +where sid = 1; + +update t_stu +-- set birthday=CURDATE() +set birthday=null +where sid = 5; + +/* +update t_stu +-- 使用函数加 5 天 +set birthday = DATE_ADD(birthday,INTERVAL 5 DAY) +where sid = 2; +*/