From 17ef05de17cf0725bb3daa75e264df062f912996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=BD=E6=99=9F=E5=AE=B8?= <3230408416@qq.com> Date: Wed, 22 Feb 2023 12:30:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../student.sql" | 56 +++++ ...1\351\201\223\344\275\234\344\270\232.sql" | 194 +++++++++++++++ ...6\346\235\237\344\275\234\344\270\232.sql" | 223 ++++++++++++++++++ 3 files changed, 473 insertions(+) create mode 100644 "22\346\226\275\346\231\237\345\256\270/student.sql" create mode 100644 "22\346\226\275\346\231\237\345\256\270/\344\270\211\351\201\223\344\275\234\344\270\232.sql" create mode 100644 "22\346\226\275\346\231\237\345\256\270/\347\272\246\346\235\237\344\275\234\344\270\232.sql" diff --git "a/22\346\226\275\346\231\237\345\256\270/student.sql" "b/22\346\226\275\346\231\237\345\256\270/student.sql" new file mode 100644 index 0000000..5e10cbd --- /dev/null +++ "b/22\346\226\275\346\231\237\345\256\270/student.sql" @@ -0,0 +1,56 @@ +-- MySQL dump 10.13 Distrib 5.7.40, for Win64 (x86_64) +-- +-- Host: localhost Database: class3 +-- ------------------------------------------------------ +-- Server version 5.7.40-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 `student` +-- + +DROP TABLE IF EXISTS `student`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `student` ( + `xh` int(11) DEFAULT NULL, + `name` char(20) DEFAULT NULL, + `gender` char(2) DEFAULT NULL, + `hobby` char(20) DEFAULT NULL, + `address` varchar(30) DEFAULT NULL, + `phone` char(15) DEFAULT NULL, + `email` char(30) DEFAULT NULL, + `qq` char(10) DEFAULT NULL +) 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 (102,'林鑫','男','唱歌','北京大学','18506090903','1057759237@qq.com','1057759237'); +/*!40000 ALTER TABLE `student` 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-02-15 23:07:14 diff --git "a/22\346\226\275\346\231\237\345\256\270/\344\270\211\351\201\223\344\275\234\344\270\232.sql" "b/22\346\226\275\346\231\237\345\256\270/\344\270\211\351\201\223\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..5e871c8 --- /dev/null +++ "b/22\346\226\275\346\231\237\345\256\270/\344\270\211\351\201\223\344\275\234\344\270\232.sql" @@ -0,0 +1,194 @@ +-- 第1题 +-- +-- 1、创建数据库test01_market +CREATE DATABASE test01_market; +show DATABASES; +USE test01_market; +-- 2、创建表格customers +CREATE TABLE customers( +c_num int(11), +c_name varchar(50), +c_contact varchar(50) , + c_city varchar(50), + c_birth date ); + DESC customers; +-- | 字段名 | 数据类型 | +-- | --- | --- | +-- | c_num | int(11) | +-- | c_name | varchar(50) | +-- | c_contact | varchar(50) | +-- | c_city | varchar(50) | +-- | c_birth | date | +-- +-- **要求3:**将c_contact字段移动到c_birth字段后面 +ALTER TABLE customers MODIFY c_contact varchar(50) AFTER c_birth; +-- **要求4:**将c_name字段数据类型改为 varchar(70) +ALTER TABLE customers MODIFY c_name varchar(70); +-- **要求5:**将c_contact字段改名为c_phone +ALTER TABLE customers CHANGE c_contact c_phone varchar(50); +-- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers ADD COLUMN c_gender char(1); +ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; +-- **要求7:**将表名改为customers_info +ALTER TABLE customers RENAME TO customers_info; +-- **要求8:**删除字段c_city +ALTER TABLE customers_info DROP COLUMN c_city; + + + + +-- 1、创建数据库test02_library +-- CREATE DATABASE test02_library; +-- USE test02_library; +-- 2、创建表格books +CREATE TABLE books( +b_id int(11) not NULL primary KEY, +b_name VARCHAR(50) not NULL, +b_authors varchar(100) not NULL, +price float(10.2) NOT NULL, +pubdate year NOT NULL, +note varchar(100), +num int(11) NOT NULL +); +DESC books; + +-- | 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +-- | --- | --- | --- | --- | --- | +-- | b_id | 书编号 | int(11) | 否 | 是 | +-- | b_name | 书名 | varchar(50) | 否 | 否 | +-- | authors | 作者 | varchar(100) | 否 | 否 | +-- | price | 价格 | float | 否 | 否 | +-- | pubdate | 出版日期 | year | 否 | 否 | +-- | note | 说明 | varchar(100) | 是 | 否 | +-- | num | 库存 | int(11) | 否 | 否 | +-- +-- 3、向books表中插入记录 +-- +-- 1) 指定所有字段名称插入第一条记录 +INSERT INTO books (b_id,b_name,b_authors,price,pubdate,note,num) VALUES ('1','Tal of AAA','Dickes','23','1995','novel','11'); +DESC books; +SELECT * FROM books; +-- 2)不指定字段名称插入第二记录 +INSERT into books VALUES ('2','EmmaT','Jane lura','35','1993','joke','22'); +-- 3)同时插入多条记录(剩下的所有记录) +INSERT INTO books VALUES ('3','Story of Jane','Jane Tim','40','2001','novel','0'),('4','Lovey Day','George Byron','20','2005','novel','30'),('5','Old land','Honore Blade','30','2010','law','0'),('6','The Battle','Upton Sara','30','1999','medicine','40'),('7','Rose Hood','Richard haggard','28','2008','cartoon','28' ); +-- | b_id | b_name | authors | price | pubdate | note | num | +-- | --- | --- | --- | --- | --- | --- | --- | +-- | 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +-- | 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +-- | 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +-- | 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +-- | 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +-- | 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +-- | 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | +-- +-- 4、将小说类型(novel)的书的价格都增加5。 +-- +-- 5、将名称为EmmaT的书的价格改为40。 +-- +-- 6、删除库存为0的记录 +-- + + +-- DROP TABLES book; +-- DROP DATABASES test03_bookstore; + +-- 1、创建数据库test03_bookstore +CREATE DATABASE test03_bookstore CHARSET utf8; +-- 2、创建book表 +USE test03_bookstore; +CREATE TABLE book( +id int(11) NOT NULL PRIMARY KEY, +title varchar(100) NOT NULL, +author varchar(100) NOT NULL, +price double(11,2) NOT NULL, +sales int(11) NOT NULL, +stock int(11) NOT NULL, +img_path varchar(100) NOT NULL ); +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | title | varchar(100) | NO | | NULL | | +-- | author | varchar(100) | NO | | NULL | | +-- | price | double(11,2) | NO | | NULL | | +-- | sales | int(11) | NO | | NULL | | +-- | stock | int(11) | NO | | NULL | | +-- | img_path | varchar(100) | NO | | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +-- +-- 尝试添加部分模拟数据,参考示例如下: +-- +-- +----+-------------+------------+-------+-------+-------+----------------------------+ +-- | id | title | author | price | sales | stock | img_path | +-- +----+-------------+------------+-------+-------+-------+-----------------------------+ +-- | 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | +-- | 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | +-- +----+---------------+------------+-------+-------+-------+----------------------------+ +INSERT INTO book VALUES('1','解忧杂货店','东野圭吾','27.20','102','98','upload/books/解忧杂货店.jpg'); +SELECT * FROM book; +-- 3、创建用户表users,并插入数据 +create table users(id int(11)not null primary key auto_increment,username varchar(100)not null,password varchar(100)not null,email varchar(100)not null); +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | username | varchar(100) | NO | UNI | NULL | | +-- | password | varchar(100) | NO | | NULL | | +-- | email | varchar(100) | YES | | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +insert into users values(1,'admin',112233,'admin@mxdx.com '); +-- 尝试添加部分模拟数据,参考示例如下: + +-- +----+----------+----------------------------------+--------------------+ +-- | id | username | password | email | +-- +----+----------+----------------------------------+--------------------+ +-- | 1 | admin | 112233 | admin@mxdx.com | +-- +----+----------+----------------------------------+--------------------+ +-- +-- 4、创建订单表orders +create table orders(id varchar(100)not null primary key,order_time datetime not null,total_count int(11)not null,total_amount double(11,2)not null,state int(11)not null,user_id int(11)not null, key(user_id)); +-- +--------------+--------------+------+-----+---------+-------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +--------------+--------------+------+-----+---------+-------+ +-- | id | varchar(100) | NO | PRI | NULL | | +-- | order_time | datetime | NO | | NULL | | +-- | total_count | int(11) | NO | | NULL | | +-- | total_amount | double(11,2) | NO | | NULL | | +-- | state | int(11) | NO | | NULL | | +-- | user_id | int(11) | NO | MUL | NULL | | +-- +--------------+--------------+------+-----+---------+-------+ +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into orders values(15294258455691 ,'2018-06-20 00:30:45',2,50.20,0,1); +select * from orders; +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | id | order_time | total_count | total_amount | state | user_id | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- | 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +-- +----------------+---------------------+-------------+--------------+-------+---------+ +-- +-- 5、创建订单明细表order_items +create table order_itrms(id int(11)not null auto_increment,count int(11)not null,amount double(11,2)not null,title varchar(100)not null,author varchar(100)not null,price double(11,2)not null,img_path varchar(100)not null,order_id varchar(100)not null,primary key(id),key(order_id)); +-- +----------+--------------+------+-----+---------+----------------+ +-- | Field | Type | Null | Key | Default | Extra | +-- +----------+--------------+------+-----+---------+----------------+ +-- | id | int(11) | NO | PRI | NULL | auto_increment | +-- | count | int(11) | NO | | NULL | | +-- | amount | double(11,2) | NO | | NULL | | +-- | title | varchar(100) | NO | | NULL | | +-- | author | varchar(100) | NO | | NULL | | +-- | price | double(11,2) | NO | | NULL | | +-- | img_path | varchar(100) | NO | | NULL | | +-- | order_id | varchar(100) | NO | MUL | NULL | | +-- +----------+--------------+------+-----+---------+----------------+ +-- +-- 尝试添加部分模拟数据,参考示例如下: +insert into order_itrms values(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); +select * from order_itrms; +-- +----+-------+--------+---------+---------+-------+----------------+----------------+ +-- | id |count| amount| title | author | price | img_path | order_id | +-- +----+-------+--------+------------+----------+-------+----------------+----------------+ +-- | 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | +-- | 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | +-- +----+-------+--------+------------+----------+-------+------------+----------------+ \ No newline at end of file diff --git "a/22\346\226\275\346\231\237\345\256\270/\347\272\246\346\235\237\344\275\234\344\270\232.sql" "b/22\346\226\275\346\231\237\345\256\270/\347\272\246\346\235\237\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..eedeee7 --- /dev/null +++ "b/22\346\226\275\346\231\237\345\256\270/\347\272\246\346\235\237\344\275\234\344\270\232.sql" @@ -0,0 +1,223 @@ +-- 1、创建数据库test01_company +create database test01_company charset utf8; +use test01_company; +-- 2、创建表格offices +create table offices(officeCode int,city varchar(30),address varchar(50),country varchar(50),postalCode varchar(25)); +-- | 字段名 | 数据类型 | +-- | ---------- | ----------- |rt +-- | officeCode | int | +-- | city | varchar(30) | +-- | address | varchar(50) | +-- | country | varchar(50) | +-- | postalCode | varchar(25) | +-- +-- 3、创建表格employees +create table employees(empNum int(11),lastName varchar(50),firstName varchar(50),mobile varchar(25),code int,jobTitle varchar(50),birth date,Note varchar(255),Sex varchar(5)); +-- | 字段名 | 数据类型 | +-- | --------- | ------------- | +-- | empNum | int(11) | +-- | lastName | varchar(50) | +-- | firstName | varchar(50) | +-- | mobile | varchar(25) | +-- | code | int | +-- | jobTitle | varchar(50) | +-- | birth | date | +-- | Note | varchar(255) | +-- | Sex | varchar(5) | +-- +-- **要求4:**将表employees的mobile字段修改到code字段后面。 +alter table employees modify column mobile varchar(50) after code; +-- **要求5:**将表employees的birth字段改名为birthday; +alter table employees change birth birthday date; +-- **要求6:**修改sex字段,数据类型为char(1)。 +alter table employees modify column sex char(1); +-- **要求7:**删除字段note; +alter table employees drop note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +-- **要求9:**将表employees的名称修改为 employees_info +alter table employees rename employees_info; + +## 第2题 + +-- 1、创建数据库test02db +create database test02db charset utf8; +-- 2、创建表格pet +create table pet(name varchar(20),owner varchar(20),species varchar(20),sex char(1),birth year,death year); +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +-- +-- 3、添加记录 +insert into pet values('Fluffy','harold','Cat','f',2003,2010),('Claws','gwen','Cat','m',2004,null),('Buffy',null,'Dog','f',2009,null),('Fang','benny','Dog','m',2000,null),('bowser','diane','Dog','m',2003,2009),('Chirpy',null,'Bird','f',2008,null); +-- | name | owner | species | sex | birth | death | +-- | ------ | ------ | ------- | ---- | ----- | ----- | +-- | Fluffy | harold | Cat | f | 2003 | 2010 | +-- | Claws | gwen | Cat | m | 2004 | | +-- | Buffy | | Dog | f | 2009 | | +-- | Fang | benny | Dog | m | 2000 | | +-- | bowser | diane | Dog | m | 2003 | 2009 | +-- | Chirpy | | Bird | f | 2008 | | +-- +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth varchar(50); +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set owner='kevin' where name='Claws'; +-- 6、 将没有死的狗的主人改为duck +update pet set owner='duck' where isnull(death) and species='Dog'; +-- 7、 查询没有主人的宠物的名字; +select * from pet where isnull(owner); +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,owner,death from pet where death !='' and species='Cat'; +-- 9、 删除已经死亡的狗 +delete from pet where death !='' and species='Dog'; +-- 10、查询所有宠物信息 +select * from pet; +-- + + +## 第3题 + +-- 1、创建数据库:test03_company +create database test03_company charset utf8; +-- 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +-- +-- A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +-- +-- ```mysql +use test03_company ; +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +); +desc department; +-- ``` +-- +-- B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +-- +-- * ​ 雇员编号为主键; +-- * ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +-- * ​ 性别默认为男; +-- +-- ```mysql +create table employee ( + empid int primary key auto_increment, + name varchar(10) not null, + sex enum('男','女') not null default '男', + title varchar(10), + birthday date, + depid int , + foreign key (depid) references department(depid) +); +-- ``` +-- +-- C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +-- +-- 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +create table salary( + empid int not null primary key auto_increment, + basesalary varchar(10)not null, + titlesalary varchar(10)not null, + deduction varchar(10), + foreign key (empid) references employee(empid) +); + +-- 4、添加数据如下: +-- +-- 部门表: +insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源管理'); +-- | 部门编号 | 部门名称 | 部门简介 | +-- | -------- | -------- | ------------ | +-- | 111 | 生产部 | Null | +-- | 222 | 销售部 | Null | +-- | 333 | 人事部 | 人力资源管理 | +-- +-- 雇员表: +insert into employee values(1001,'张三','男','高级工程师','1975-1-1',111),(1002,'李四','女','助工','1985-1-1',111),(1003,'王五','男','工程师','1978-11-11',222),(1004,'张六','男','工程师','1999-1-1',222); +-- | 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +-- | -------- | ---- | ---- | ---------- | ---------- | ------------ | +-- | 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +-- | 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +-- | 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +-- | 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +-- +-- 工资表: +insert into salary values(1001,2200,1100,200),(1002,1200,200,null),(1003,2900,700,200),(1004,1950,700,150); +-- | 雇员编号 | 基本工资 | 职务工资 | 扣除 | +-- | -------- | -------- | -------- | ---- | +-- | 1001 | 2200 | 1100 | 200 | +-- | 1002 | 1200 | 200 | NULL | +-- | 1003 | 2900 | 700 | 200 | +-- | 1004 | 1950 | 700 | 150 | + + +## 第4题 + +-- 1、创建一个数据库:test04_school +create database test04_school charset utf8; +use test04_school; +-- 2、创建如下表格 +-- +-- 表1 Department表的定义 +create table Department( + DenNo int(10)primary key unique key, + DepName varchar(20)not null, + DepNote varchar(50) +); +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +-- | DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +-- | DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +-- +-- 表2 Teacher表的定义 +create table teacher( + Number int unique key primary key, + Name varchar(30)not null, + Sex varchar(4), + Birth date, + DepNO int, + Salary float, + Address varchar(100), + foreign key (DepNo) references Department(DenNo) +); +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | Number | 教工号 | int | 是 | 否 | 是 | 是 | +-- | Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +-- | Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +-- | Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +-- | DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +-- | Salary | 工资 | float | 否 | 否 | 否 | 否 | +-- | Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +-- +-- 3、添加记录 +insert into department values(601,'软件技术系','软件技术等专业'),(602,'网络技术系','多媒体技术等专业'),(603,'艺术设计系','广告艺术设计等专业'),(604,'管理工程系','连锁经营管理等专业'); +-- | **DepNo** | **DepName** | **DepNote** | +-- | --------- | ----------- | ------------------ | +-- | 601 | 软件技术系 | 软件技术等专业 | +-- | 602 | 网络技术系 | 多媒体技术等专业 | +-- | 603 | 艺术设计系 | 广告艺术设计等专业 | +-- | 604 | 管理工程系 | 连锁经营管理等专业 | +insert into teacher values(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'),(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'),(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'),(2004,'James','女','1980-10-20',602,3500,'四川省成都市'),(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); +-- | **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | +-- | ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | +-- | 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | +-- | 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | +-- | 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | +-- | 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | +-- | 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | +-- +-- 4、用SELECT语句查询Teacher表的所有记录。 +-- +select * from teacher; + + + + -- Gitee From 86cbf8ae3c851752af60ceea88b5367b12988ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=BD=E6=99=9F=E5=AE=B8?= <3230408416@qq.com> Date: Wed, 22 Feb 2023 23:14:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?20220222=E7=AC=94=E8=AE=B0=E5=92=8C?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2 \346\226\275\346\231\237\345\256\270.md" | 198 ++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 "22\346\226\275\346\231\237\345\256\270/22030222 \346\226\275\346\231\237\345\256\270.md" diff --git "a/22\346\226\275\346\231\237\345\256\270/22030222 \346\226\275\346\231\237\345\256\270.md" "b/22\346\226\275\346\231\237\345\256\270/22030222 \346\226\275\346\231\237\345\256\270.md" new file mode 100644 index 0000000..8e38ee8 --- /dev/null +++ "b/22\346\226\275\346\231\237\345\256\270/22030222 \346\226\275\346\231\237\345\256\270.md" @@ -0,0 +1,198 @@ +# 笔记 + +​ select * from 表名 where (要查询的信息) +mysql中等于的运算符为= +​ between and 在什么什么之间 +如select * from 表名 where salary between 范围 and 范围 ; +​ 查询名字 +%%为任意字符 +_下划线为一个字符 +如:select * from 表名 where 查询名 like ‘ %名%’ +select * from 表名 where 查询名 'like _名' +​ 逻辑语和html一样 !!!除 xor(两者只能满足其中一个,不能同时满足,不能同时不满足) +如 select * from 表名 where (查询名1 < 10000 and 查询名2 > '2016-1-1') or (查询名1 >= 10000 and 查询名2 <'2016-1-1'); +可替换为 +​ select * from t_employee where 查询名1 < 10000 xor 查询名2< '2016-1-1'; +1.5 null 与任何计算返回的都是null +ifnull 例外 + + + +# 作业 + +## 第1题:员工表 + +```mysql +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +``` + + + +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | + +**要求1:**查询出薪资在12000~13000之间的员工信息。 + +```mysql +select * from employee where salary between 12000 and 13000; +``` + +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +```mysql +select id,name,addr from employee where name like '%刘%'; +``` + +**要求3:**将“李四”的家庭住址改为“广东韶关” + +```mysql +update employee set addr = '广东佛山', addr = '广东韶山' where name='李四'; +``` + +**要求4:**查询出名字中带“小”的员工 + +```mysql +select * from employee where name like '%小%'; +``` + +**要求5:**查询出薪资高于11000的男员工信息 + +```mysql +select * from employee where salary >11000 and sex='男'; +``` + +**要求6:**查询没有登记电话号码的员工 + +```mysql +select * from employee where tel is null ; +``` + +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +```mysql +select * from employee where salary >12000 and sex ='男' and addr='广东广州' or addr='广东深圳'; +``` + +**要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +select name '姓名' , salary*12 as '年薪' from employee; +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +``` + +表数据样例: + +```mysql ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | + 亚洲 +| Albania | Europe | 28748 | 2831741 | 12960000000 | + 欧洲 +| Algeria | Africa | 2381741 | 37100000 | 188681000000 | + 非洲 +| Andorra | Europe | 468 | 78115 | 3712000000 | + 欧洲 +| Angola | Africa | 1246700 | 20609294 | 100990000000 | + 非洲 ++-------------+-----------+---------+------------+--------------+ +``` + +**要求1:** 查询大国 的国家名称、人口和面积。 + +```mysql +select continent,area,population from countries_info where area >= 3000000 or population >= 25000000; +``` + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +```mysql +select continent,area,population,gdp from countries_info where continent='Asia'; +``` + +**要求3:**查询国土面积不足1万平方公里且人口不足10万人的国家信息 + +```mysql +select * from countries_info where area<10000 and population <100000; +``` + +**要求4:**查询国家名字中包含“o“字母的国家信息 + +```mysql +select * from countries_info where continent like '%o%'; +``` + +**要求5:**查询GDP值超过10000000000的国家信息 + +```mysql +select * from countries_info where gdp > 10000000000; +``` + +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +```mysql +select continent '国家名',population '人口',gdp'GDP值', gdp / population '人均贡献GDP值' from countries_info; +``` + +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + +```mysql +select * from countries_info where gdp/population<1000; +``` + +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```mysql +select continent '国家名',area '面积',population '人口',area/population '人均国土面积' from countries_info; +``` + -- Gitee From 968105092d6dc61b50a12a94a63c6343a813b0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=BD=E6=99=9F=E5=AE=B8?= <11785147+demon-cat-fruit-orange@user.noreply.gitee.com> Date: Wed, 22 Feb 2023 15:18:24 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?=E6=96=BD=E6=99=9F=E5=AE=B8/student.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../student.sql" | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 "22\346\226\275\346\231\237\345\256\270/student.sql" diff --git "a/22\346\226\275\346\231\237\345\256\270/student.sql" "b/22\346\226\275\346\231\237\345\256\270/student.sql" deleted file mode 100644 index 5e10cbd..0000000 --- "a/22\346\226\275\346\231\237\345\256\270/student.sql" +++ /dev/null @@ -1,56 +0,0 @@ --- MySQL dump 10.13 Distrib 5.7.40, for Win64 (x86_64) --- --- Host: localhost Database: class3 --- ------------------------------------------------------ --- Server version 5.7.40-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 `student` --- - -DROP TABLE IF EXISTS `student`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `student` ( - `xh` int(11) DEFAULT NULL, - `name` char(20) DEFAULT NULL, - `gender` char(2) DEFAULT NULL, - `hobby` char(20) DEFAULT NULL, - `address` varchar(30) DEFAULT NULL, - `phone` char(15) DEFAULT NULL, - `email` char(30) DEFAULT NULL, - `qq` char(10) DEFAULT NULL -) 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 (102,'林鑫','男','唱歌','北京大学','18506090903','1057759237@qq.com','1057759237'); -/*!40000 ALTER TABLE `student` 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-02-15 23:07:14 -- Gitee