基本 sql 练习
This commit is contained in:
parent
8b63cc049a
commit
db9e1dd070
|
@ -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
|
|
@ -0,0 +1,6 @@
|
|||
-- comment 表示字段的说明 (备份)
|
||||
create table t_course(
|
||||
cid int,
|
||||
name varchar(50) comment '课程名',
|
||||
tid int comment '教师编号'
|
||||
);
|
|
@ -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
|
||||
);
|
|
@ -0,0 +1,5 @@
|
|||
create table t_score(
|
||||
sid int,
|
||||
cid int,
|
||||
score decimal(5,2)
|
||||
);
|
|
@ -0,0 +1,7 @@
|
|||
-- sql comment
|
||||
create table t_student(
|
||||
sid int,
|
||||
name varchar(20),
|
||||
sex varchar(2),
|
||||
birthday date
|
||||
);
|
|
@ -0,0 +1,4 @@
|
|||
create table t_teacher(
|
||||
tid integer,
|
||||
name varchar(50)
|
||||
);
|
|
@ -0,0 +1,4 @@
|
|||
use studb;
|
||||
|
||||
delete from t_emp where hire_date > '2022-12-31';
|
||||
select * from t_emp;
|
|
@ -0,0 +1,8 @@
|
|||
-- 删除测试
|
||||
use studb;
|
||||
|
||||
create table if not exists t_stu3
|
||||
select * from t_stu;
|
||||
|
||||
delete from t_stu3
|
||||
where sex='男';
|
|
@ -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;
|
|
@ -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');
|
|
@ -0,0 +1,3 @@
|
|||
use studb;
|
||||
|
||||
select * from t_emp where salary>5000;
|
|
@ -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%';
|
|
@ -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;
|
|
@ -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;
|
||||
*/
|
Loading…
Reference in New Issue